diff --git a/app/routers/content_apis.py b/app/routers/content_apis.py index ae4a4542..9a69ecf1 100644 --- a/app/routers/content_apis.py +++ b/app/routers/content_apis.py @@ -1354,3 +1354,18 @@ async def delete_deleteditems(request: Request,user_details =Depends(get_user_or log.info('In delete_deleteditems') deleted_item_count = structurals_crud.cleanup_database(db_=db_) return {'message': "Database cleanup done!!",'deletedItemCount':deleted_item_count} + +@router.get('/v2/jobs', response_model=schemas_nlp.JobStatusResponse, + response_model_exclude_none=True, status_code=200, + responses={502: {"model": schemas.ErrorResponse}, + 422: {"model": schemas.ErrorResponse},404:{"model": schemas.ErrorResponse}}, + tags=['Jobs']) +@get_auth_access_check_decorator +async def check_job_status(request: Request, + job_id:int=Query(...,examples="100000"),user_details =Depends(get_user_or_none), + db_:Session=Depends(get_db)): + '''Checking the status of a job''' + log.info('In check_job_status') + log.debug('job_id:%s', job_id) + result = nlp_sw_crud.check_job_status(db_, job_id) + return result diff --git a/app/routers/translation_apis.py b/app/routers/translation_apis.py deleted file mode 100644 index f51bd2ff..00000000 --- a/app/routers/translation_apis.py +++ /dev/null @@ -1,778 +0,0 @@ -'''API endpoints for AgMT app''' - -from typing import List -from fastapi import APIRouter, Query, Body, Depends, Request, Path, BackgroundTasks -from sqlalchemy.orm import Session -from dependencies import get_db, log, AddHiddenInput -from schema import schemas, schemas_nlp, schema_auth, schema_content -from crud import nlp_crud, projects_crud, nlp_sw_crud, structurals_crud, utils -from crud.projects_crud import check_app_compatibility_decorator -from custom_exceptions import GenericException -from routers import content_apis -from auth.authentication import get_user_or_none,get_auth_access_check_decorator - -router = APIRouter() -#pylint: disable=too-many-arguments,unused-argument -############## Translation Projects ########################## -@router.get('/v2/text/translate/token-based/projects', - response_model=List[schemas_nlp.TranslationProject], - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 404: {"model": schemas.ErrorResponse}}, - status_code=200, tags=['Translation-Project management']) -@get_auth_access_check_decorator -async def get_projects(request: Request, - project_name:str=Query(None,examples="Hindi-Bilaspuri Gospels"), - source_language:schemas.LangCodePattern=Query(None,examples='en'), - target_language:schemas.LangCodePattern=Query(None,examples='ml'), - active:bool=True, user_id:str=Query(None), - compatible_with: List[schema_auth.App] = Query(None,examples=["Autographa","SanketMAST"]), - skip: int=Query(0, ge=0), limit: int=Query(100, ge=0), - user_details =Depends(get_user_or_none), db_:Session=Depends(get_db), - filtering_required=Depends(AddHiddenInput(value=True))): - '''Fetches the list of proejct and their details''' - log.info('In get_projects') - log.debug('project_name: %s, source_language:%s, target_language:%s,\ - active:%s, user_id:%s',project_name, source_language, target_language, active, user_id) - if compatible_with is None: - app = request.headers['app'] - compatible_with = [app] - return projects_crud.get_translation_projects(db_, project_name, source_language, - target_language, active=active,compatible_with=compatible_with, user_id=user_id, - skip=skip, limit=limit) - -@router.post('/v2/text/translate/token-based/projects', status_code=201, - response_model=schemas_nlp.TranslationProjectUpdateResponse, - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}}, - tags=['Translation-Project management']) -@get_auth_access_check_decorator -async def create_project(request: Request, - project_obj:schemas_nlp.TranslationProjectCreate, - user_details =Depends(get_user_or_none), db_:Session=Depends(get_db)): - '''Creates a new translation project''' - log.info('In create_project') - log.debug('project_obj: %s',project_obj) - app = request.headers['app'] - return {'message': "Project created successfully", - "data": projects_crud.create_translation_project(db_=db_, project=project_obj, - user_id=user_details['user_id'],app = app)} - -@router.put('/v2/text/translate/token-based/projects', status_code=201, - response_model=schemas_nlp.TranslationProjectUpdateResponse, - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 500: {"model": schemas.ErrorResponse},404: {"model": schemas.ErrorResponse}}, - tags=['Translation-Project management']) -@get_auth_access_check_decorator -@check_app_compatibility_decorator -async def update_project(request: Request, project_obj:schemas_nlp.TranslationProjectEdit, - project_id:int=Query(...,examples="1022004"), user_details =Depends(get_user_or_none), - db_:Session=Depends(get_db), - operates_on=Depends(AddHiddenInput(value=schema_auth.ResourceType.PROJECT.value))): - # operates_on=schema_auth.ResourceType.PROJECT.value): - '''Adds more books to a Translation project's source. Delete or activate project.''' - log.info('In update_project') - log.debug('project_obj: %s',project_obj) - if project_obj.selectedBooks: - sentences = [] - books_param_list = "" - for buk in project_obj.selectedBooks.books: - books_param_list += f"&books={buk}"#pylint: disable=R1713 - - # request.scope['method'] = 'GET' - # request._url = URL('/v2/resources')#pylint: disable=protected-access - response = await content_apis.extract_text_contents( - request=request, - resource_name=project_obj.selectedBooks.bible, - #typeerror - books=project_obj.selectedBooks.books, - language_code=None, - resource_type='bible', - skip=0, limit=100000, - user_details = user_details, - db_=db_, - operates_on=schema_auth.ResourceType.CONTENT.value) - if "error" in response: - raise GenericException(response['error']) - for item in response: - sentences.append(schemas_nlp.SentenceInput( - sentenceId=item[0], surrogateId=item[1], sentence=item[2])) - if project_obj.sentenceList is not None: - project_obj.sentenceList += sentences - else: - project_obj.sentenceList = sentences - return {'message': "Project updated successfully", - "data": projects_crud.update_translation_project(db_, project_obj, - project_id=project_id, user_id=user_details['user_id'])} - -@router.delete('/v2/text/translate/token-based/projects', status_code=201, - response_model=schemas.DeleteResponse, - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 404: {"model": schemas.ErrorResponse}, 403:{"model": schemas.ErrorResponse}}, - tags=['Translation-Project management']) -@get_auth_access_check_decorator -@check_app_compatibility_decorator -async def remove_project(request: Request, - project_id:int = Query(..., examples=100001), - user_details =Depends(get_user_or_none), db_: Session = Depends(get_db)): - '''Removes a project.''' - log.info('In remove_project') - log.debug('project_id:%s',project_id) - deleted_content = projects_crud.remove_translation_project(db_, project_id) - delcont = structurals_crud.add_deleted_data(db_, del_content= deleted_content, - table_name = "translation_projects", deleting_user=user_details['user_id']) - return {'message': f"Project with identity {project_id} deleted successfully", - "data": delcont} - -@router.post('/v2/text/translate/token-based/project/user', status_code=201, - response_model=schemas_nlp.UserUpdateResponse, - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 404: {"model": schemas.ErrorResponse}}, - tags=['Translation-Project management']) -@get_auth_access_check_decorator -@check_app_compatibility_decorator -async def add_user(request: Request,project_id:int, user_id:str, - user_details =Depends(get_user_or_none), db_:Session=Depends(get_db)): - '''Adds new user to a project.''' - log.info('In add_user') - log.debug('project_id: %s, user_id:%s',project_id, user_id) - return {'message': "User added to project successfully", - "data": projects_crud.add_project_user(db_, project_id, user_id, - current_user=user_details['user_id'])} - -@router.put('/v2/text/translate/token-based/project/user', status_code=201, - response_model=schemas_nlp.UserUpdateResponse, - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 404: {"model": schemas.ErrorResponse}},tags=['Translation-Project management']) -@get_auth_access_check_decorator -@check_app_compatibility_decorator -async def update_user(request: Request,user_obj:schemas_nlp.ProjectUser, - project_id:int=Query(...,examples="1022004"), - user_details =Depends(get_user_or_none),db_:Session=Depends(get_db)): - '''Changes role, metadata or active status of user of a project.''' - log.info('In update_user') - log.debug('user_obj:%s',user_obj) - return {'message': "User updated in project successfully", - "data": projects_crud.update_project_user(db_, user_obj,project_id=project_id, - current_user=user_details['user_id'])} - -@router.delete('/v2/text/translate/token-based/project/user', status_code=201, - response_model=schemas.DeleteResponse, - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 404: {"model": schemas.ErrorResponse}, 403:{"model": schemas.ErrorResponse}}, - tags=['Translation-Project management']) -@get_auth_access_check_decorator -@check_app_compatibility_decorator -async def remove_user(request: Request,project_id:int, user_id:str, - user_details =Depends(get_user_or_none), db_: Session = Depends(get_db)): - '''Removes a user from a project.''' - log.info('In remove_user') - log.debug('project_id:%s, user_id:%s',project_id, user_id) - deleted_content = projects_crud.remove_project_user(db_, project_id, user_id, - current_user=user_details['user_id']) - delcont = structurals_crud.add_deleted_data(db_, del_content= deleted_content['db_content'], - table_name = "translation_project_users", deleting_user=user_details['user_id']) - return {'message': "User removed from project successfully", - "data": delcont} - -############## Translation APIs ########################## -@router.get('/v2/text/translate/token-based/project/tokens', response_model=List[schemas_nlp.Token], - response_model_exclude_unset=True, - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 404: {"model": schemas.ErrorResponse}}, - status_code=200, tags=['Project-Based-Translation']) -@get_auth_access_check_decorator -@check_app_compatibility_decorator -async def get_tokens(request: Request, project_id:int=Query(...,examples="1022004"), - books:List[schemas.BookCodePattern]=Query(None,examples=["mat", "mrk"]), - sentence_id_range:List[int]=Query(None,max_items=2,min_items=2,examples=(410010001, 41001999)), - sentence_id_list:List[int]=Query(None, examples=[41001001,41001002,41001003]), - use_translation_memory:bool=True, include_phrases:bool=True, include_stopwords:bool=False, - user_details =Depends(get_user_or_none), db_:Session=Depends(get_db)): - '''Tokenize the source texts. Optional params books, - sentence_id_range or sentence_id_list can be used to specify the source verses. - If more than one of these filters are given, only one would be used - in the following order of priority: books, range, list. - Flags use_translation_memory, include_phrases and include_stopwords can be - used to alter the tokens output as per user need''' - log.info('In get_tokens') - log.debug('project_id: %s, books:%s, sentence_id_range:%s, sentence_id_list:%s, \ - use_translation_memory:%s, include_phrases:%s, include_stopwords:%s', project_id, - books, sentence_id_range, sentence_id_range, use_translation_memory, - include_phrases, include_stopwords) - return nlp_crud.get_project_tokens(db_, project_id, books, sentence_id_range, sentence_id_list, - use_translation_memory=use_translation_memory, - include_phrases = include_phrases, include_stopwords=include_stopwords) - -@router.put('/v2/text/translate/token-based/project/tokens', - response_model=schemas_nlp.TranslateResponse, - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 500: {"model": schemas.ErrorResponse},404: {"model": schemas.ErrorResponse}}, - status_code=201, tags=['Project-Based-Translation']) -@get_auth_access_check_decorator -@check_app_compatibility_decorator -async def apply_token_translations(request: Request,project_id:int=Query(...,examples="1022004"), - token_translations:List[schemas_nlp.TokenUpdate]=Body(...), return_drafts:bool=True, - user_details =Depends(get_user_or_none), db_:Session=Depends(get_db)): - '''Updates drafts using the provided token translations and returns updated verses''' - log.info('In apply_token_translations') - log.debug('project_id: %s, token_translations:%s, ',project_id, token_translations) - drafts = nlp_crud.save_project_translations(db_, project_id, token_translations, return_drafts, - user_id=user_details['user_id']) - return {"message": "Token translations saved", "data":drafts} - -@router.get('/v2/text/translate/token-based/project/token-translations', status_code=200, - response_model= schemas_nlp.Translation, - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 404: {"model": schemas.ErrorResponse}}, - tags=['Project-Based-Translation']) -@get_auth_access_check_decorator -@check_app_compatibility_decorator -async def get_token_translation(request: Request,project_id:int=Query(...,examples="1022004"), - token:str=Query(...,examples="duck"), - sentence_id:int=Query(..., examples="41001001"), - offset:List[int]=Query(..., max_items=2,min_items=2,examples=[0,4]), - user_details =Depends(get_user_or_none), db_:Session=Depends(get_db)): - '''Get the current translation for specific tokens providing their occurence in source''' - log.info('In get_token_translation') - occurrences = [{"sentenceId":sentence_id, "offset":offset}] - log.debug('project_id: %s, token:%s, occurrences:%s',project_id, token, occurrences) - return projects_crud.obtain_project_token_translation(db_, project_id, token, occurrences) - -@router.put('/v2/text/translate/token-based/project/token-sentences', status_code=200, - response_model = List[schemas_nlp.Sentence], - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 404: {"model": schemas.ErrorResponse}}, - tags=['Project-Based-Translation']) -@get_auth_access_check_decorator -@check_app_compatibility_decorator -async def get_token_sentences(request: Request,project_id:int=Query(...,examples="1022004"), - token:str=Query(...,examples="duck"), - occurrences:List[schemas_nlp.TokenOccurence]=Body(..., examples=[ - {"sentenceId":41001001, "offset":[0,4]}]), - user_details =Depends(get_user_or_none), db_:Session=Depends(get_db)): - '''Pass in the occurence list of a token and get all sentences it is present in with draftMeta - that allows easy highlight of token and translation''' - log.info('In get_token_sentences') - log.debug('project_id: %s, token:%s, occurrences:%s',project_id, token, occurrences) - return projects_crud.get_project_source_per_token(db_, project_id, token, occurrences) - -@router.get('/v2/text/translate/token-based/project/draft', status_code=200, - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 415: {"model": schemas.ErrorResponse},404: {"model": schemas.ErrorResponse}}, - tags=['Project-Based-Translation']) -@get_auth_access_check_decorator -@check_app_compatibility_decorator -async def get_draft(request: Request,project_id:int=Query(...,examples="1022004"), - books:List[schemas.BookCodePattern]=Query(None,examples=["mat", "mrk"]), - sentence_id_list:List[int]=Query(None,examples=[41001001,41001002,41001003]), - sentence_id_range:List[int]=Query(None,max_items=2,min_items=2,examples=[41001001,41001999]), - output_format:schemas_nlp.DraftFormats=Query(schemas_nlp.DraftFormats.USFM), - user_details =Depends(get_user_or_none), db_:Session=Depends(get_db)): - '''Obtains draft, as per current project status, in any of the formats: - text for UI display, usfm for downloading, or alignment-json for project export''' - log.info('In get_draft') - log.debug('project_id: %s, books:%s, sentence_id_list:%s, sentence_id_range:%s,\ - output_format:%s',project_id, books, sentence_id_list, sentence_id_range, - output_format) - return projects_crud.obtain_project_draft(db_, project_id, books, - sentence_id_list, sentence_id_range, output_format=output_format) - -@router.put('/v2/text/translate/token-based/project/draft', status_code=201, - response_model = List[schemas_nlp.Sentence], - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 415: {"model": schemas.ErrorResponse},404: {"model": schemas.ErrorResponse}}, - tags=['Project-Based-Translation']) -@get_auth_access_check_decorator -@check_app_compatibility_decorator -async def update_draft(request: Request,project_id:int=Query(...,examples="1022004"), - sentence_list:List[schemas_nlp.ProjectDraftInput]=Body(...), - user_details =Depends(get_user_or_none),db_:Session=Depends(get_db)): - '''Obtains draft, as per current project status, in any of the formats: - text for UI display, usfm for downloading, or alignment-json for project export''' - log.info('In update_draft') - log.debug('project_id: %s, sentence_list:%s, user_details:%s', - project_id, sentence_list, user_details) - return projects_crud.update_project_draft(db_, project_id, sentence_list, - user_details['user_id']) - -@router.get('/v2/text/translate/token-based/project/sentences', status_code=200, - response_model_exclude_unset=True, response_model=List[schemas_nlp.Sentence], - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 404: {"model": schemas.ErrorResponse}}, - tags=['Project-Based-Translation']) -@get_auth_access_check_decorator -@check_app_compatibility_decorator -async def get_project_source(request: Request,project_id:int=Query(...,examples="1022004"), - books:List[schemas.BookCodePattern]=Query(None,examples=["mat", "mrk"]), - sentence_id_list:List[int]=Query(None,examples=[41001001,41001002,41001003]), - sentence_id_range:List[int]=Query(None,max_items=2,min_items=2,examples=[41001001,41001999]), - with_draft:bool=False, only_ids:bool=False, user_details =Depends(get_user_or_none), - db_:Session=Depends(get_db)): - '''Obtains source sentences or verses, as per the filters''' - log.info('In get_source') - log.debug('project_id: %s, books:%s, sentence_id_list:%s, sentence_id_range:%s, \ - with_draft:%s, only_ids:%s',project_id, books, sentence_id_list, sentence_id_range, - with_draft, only_ids) - return projects_crud.obtain_project_source(db_, project_id, books, sentence_id_range, - sentence_id_list, with_draft=with_draft, only_ids=only_ids) - -@router.delete('/v2/text/translate/token-based/project/sentences', status_code=201, - response_model=schemas.DeleteResponse, - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 404: {"model": schemas.ErrorResponse}, 403:{"model": schemas.ErrorResponse}}, - tags=['Project-Based-Translation']) -@get_auth_access_check_decorator -@check_app_compatibility_decorator -async def remove_sentence(request: Request,project_id:int=Query(...,examples="1022004"), - sentence_id:int=Query(...,examples="41001001"), - user_details =Depends(get_user_or_none), db_: Session = Depends(get_db)): - '''Remove sentence.''' - log.info('In remove_sentence') - log.debug('project_id:%s, sentence_id:%s',project_id, sentence_id) - deleted_content = projects_crud.remove_project_sentence(db_, project_id,sentence_id, - user_id=user_details['user_id']) - delcont = structurals_crud.add_deleted_data(db_, del_content= deleted_content['db_content'], - table_name = "translation_sentences", deleting_user=user_details['user_id']) - return {'message': f"Sentence with identity {sentence_id} deleted successfully", - "data": delcont} - -@router.get('/v2/text/translate/token-based/project/progress', status_code=200, - response_model= schemas_nlp.Progress, - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 404: {"model": schemas.ErrorResponse}}, - tags=['Project-Based-Translation']) -@get_auth_access_check_decorator -@check_app_compatibility_decorator -async def get_progress(request: Request,project_id:int=Query(...,examples="1022004"), - books:List[schemas.BookCodePattern]=Query(None,examples=["mat", "mrk"]), - sentence_id_list:List[int]=Query(None,examples=[41001001,41001002,41001003]), - sentence_id_range:List[int]=Query(None,max_items=2,min_items=2,examples=[41001001,41001999]), - user_details =Depends(get_user_or_none), db_:Session=Depends(get_db)): - '''Obtains source sentences or verses, as per the filters''' - log.info('In get_progress') - log.debug('project_id: %s, books:%s, sentence_id_list:%s, sentence_id_range:%s', - project_id, books, sentence_id_list, sentence_id_range) - return projects_crud.obtain_project_progress(db_, project_id, books, - sentence_id_list, sentence_id_range) - -@router.get('/v2/text/translate/token-based/project/versification', status_code=200, - response_model= schema_content.Versification, - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 404: {"model": schemas.ErrorResponse}}, - tags=['Project-Based-Translation']) -@get_auth_access_check_decorator -@check_app_compatibility_decorator -async def get_project_versification(request: Request,project_id:int=Query(...,examples="1022004"), - user_details =Depends(get_user_or_none), db_:Session=Depends(get_db)): - '''Obtains versification structure for source sentences or verses''' - log.info('In get_project_versification') - log.debug('project_id: %s', project_id) - return projects_crud.get_project_source_versification(db_, project_id) - -@router.put('/v2/text/translate/token-based/project/suggestions', status_code=201, - response_model=List[schemas_nlp.Sentence], - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 404: {"model": schemas.ErrorResponse}}, - tags=["Translation Suggestion"]) -@get_auth_access_check_decorator -@check_app_compatibility_decorator -async def suggest_auto_translation(request: Request,project_id:int=Query(...,examples="1022004"), - books:List[schemas.BookCodePattern]=Query(None,examples=["mat", "mrk"]), - sentence_id_list:List[int]=Query(None,examples=[41001001,41001002,41001003]), - sentence_id_range:List[int]=Query(None,max_items=2,min_items=2,examples=[41001001,41001999]), - confirm_all:bool=False,user_details =Depends(get_user_or_none), db_:Session=Depends(get_db)): - '''Try to fill draft with suggestions. If confirm_all is set, will only change status of all - "suggestion" to "confirmed" in the selected sentences and will not fill in new suggestion''' - log.info('In suggest_translation') - log.debug('project_id: %s, books:%s, sentence_id_list:%s, sentence_id_range:%s', - project_id, books, sentence_id_list, sentence_id_range) - return nlp_crud.project_suggest_translations(db_, project_id, books, - sentence_id_list = sentence_id_list, user_id = user_details['user_id'], - sentence_id_range = sentence_id_range,confirm_all = confirm_all) - -########### Generic Translation ################## -@router.put('/v2/text/translate/token-based/tokens', response_model=List[schemas_nlp.Token], - response_model_exclude_unset=True, status_code=200, - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 404: {"model": schemas.ErrorResponse}}, - tags=['Generic Translation']) -@get_auth_access_check_decorator -async def tokenize(request: Request, - source_language:schemas.LangCodePattern=Query(...,examples="hi"), - sentence_list:List[schemas_nlp.SentenceInput]=Body(...), - target_language:schemas.LangCodePattern=Query(None,examples="ml"), - use_translation_memory:bool=True, include_phrases:bool=True, include_stopwords:bool=False, - punctuations:List[str]=Body(None), stopwords:schemas_nlp.Stopwords=Body(None), - user_details =Depends(get_user_or_none),db_:Session=Depends(get_db)): - '''Tokenize any set of input sentences. - Makes use of translation memory and stopwords for forming better phrase tokens. - Flags use_translation_memory, include_phrases and include_stopwords can be - used to alter the tokens output as per user need''' - log.info('In tokenize') - log.debug('source_language: %s, sentence_list:%s, target_language:%s, punctuations:%s,\ - stopwords:%s, use_translation_memory:%s, include_phrases:%s, include_stopwords:%s', - source_language, sentence_list, target_language, punctuations, stopwords, - use_translation_memory, include_phrases, include_stopwords) - return nlp_crud.get_generic_tokens(db_, source_language, sentence_list, target_language, - punctuations =punctuations, stopwords = stopwords, - use_translation_memory = use_translation_memory, include_phrases = include_phrases, - include_stopwords = include_stopwords) - -@router.put('/v2/text/translate/token-based/token-translate', - response_model=schemas_nlp.TranslateResponse, - status_code=200, - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 500: {"model": schemas.ErrorResponse},404: {"model": schemas.ErrorResponse}}, - tags=['Generic Translation']) -@get_auth_access_check_decorator -async def token_replace(request: Request,sentence_list:List[schemas_nlp.DraftInput]=Body(...), - token_translations:List[schemas_nlp.TokenUpdate]=Body(...), - source_language:schemas.LangCodePattern=Query(...,examples='hi'), - target_language:schemas.LangCodePattern=Query(...,examples='ml'), - use_data_for_learning:bool=True, - user_details =Depends(get_user_or_none), db_:Session=Depends(get_db)): - '''Perform token replacement on provided sentences and - returns obtained drafts and draft_meta''' - log.info('In token_replace') - log.debug('sentence_list:%s, token_translations:%s,\ - source_lanuage:%s, target_language:%s, use_data_for_learning:%s', - sentence_list, token_translations, source_language, - target_language, use_data_for_learning) - result = nlp_crud.replace_bulk_tokens(db_, sentence_list, token_translations, source_language, - target_language, use_data_for_learning=use_data_for_learning) - return {"message": "Tokens replaced with translations", "data": result} - -@router.put('/v2/text/translate/token-based/draft', status_code=200, - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 415: {"model": schemas.ErrorResponse},404: {"model": schemas.ErrorResponse}}, - tags=['Generic Translation']) -@get_auth_access_check_decorator -async def generate_draft(request: Request,sentence_list:List[schemas_nlp.DraftInput]=Body(...), - doc_type:schemas_nlp.TranslationDocumentType=Query(schemas_nlp.TranslationDocumentType.USFM), - user_details =Depends(get_user_or_none)): - '''Converts the drafts in input sentences to following output formats: - usfm, text, csv or alignment-json''' - log.info('In generate_draft') - log.debug('sentence_list:%s, doc_type:%s',sentence_list, doc_type) - for sent in sentence_list: - if sent.draftMeta is not None and sent.draftMeta != []: - utils.validate_draft_meta(sent.sentence, sent.draft, sent.draftMeta) - return nlp_crud.obtain_draft(sentence_list, doc_type) - -@router.put('/v2/text/translate/token-based/suggestions', response_model=List[schemas_nlp.Sentence], - status_code=200,responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}}, - tags=["Translation Suggestion"]) -@get_auth_access_check_decorator -async def suggest_translation(request: Request, - source_language:schemas.LangCodePattern=Query(...,examples="hi"), - target_language:schemas.LangCodePattern=Query(...,examples="ml"), - sentence_list:List[schemas_nlp.DraftInput]=Body(...), - punctuations:List[str]=Body(None), stopwords:schemas_nlp.Stopwords=Body(None), - user_details =Depends(get_user_or_none),db_:Session=Depends(get_db)): - '''Attempts to tokenize sentences and prepare draft with autogenerated suggestions - If draft and draft_meta are provided indicating some portion of sentence is user translated, - then it is left untouched.''' - log.info("In suggest_translation") - log.debug('source_language:%s, target_language:%s, sentence_list:%s,punctuations:%s\ - stopwords:%s', source_language, target_language, sentence_list, punctuations, stopwords) - for sent in sentence_list: - if sent.draftMeta is not None and sent.draftMeta != []: - utils.validate_draft_meta(sent.sentence, sent.draft, sent.draftMeta) - return nlp_crud.auto_translate(db_, sentence_list, source_language, target_language, - punctuations=punctuations, stopwords=stopwords) - -@router.get('/v2/nlp/gloss', response_model=schemas_nlp.GlossOutput, - status_code=200,responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}}, - tags=["Nlp"]) -@get_auth_access_check_decorator -async def get_glossary(request: Request, - source_language:schemas.LangCodePattern=Query(...,examples="en"), - target_language:schemas.LangCodePattern=Query(...,examples="hi"), - token:str=Query(...,examples="duck"), - context:str=Query(None,examples="The duck swam in the lake"), - token_offset:List[int]=Query(None,max_items=2,min_items=2,examples=(4,8)), - user_details =Depends(get_user_or_none), db_:Session=Depends(get_db)): - '''Finds translation suggestions or gloss for one token in the given context''' - log.info('In get_glossary') - log.debug('source_language:%s, target_language:%s, token:%s, context:%s,\ - token_offset:%s',source_language, target_language, token, - context, token_offset) - return nlp_crud.glossary(db_, source_language, target_language, token, - context=context, token_offset=token_offset) - -@router.get('/v2/nlp/gloss-entries', response_model=List[schemas_nlp.TranslationMemoryOut], - status_code=200,responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}}, - tags=["Nlp"]) -@get_auth_access_check_decorator -async def get_glossary_entries(request: Request, - source_language:schemas.LangCodePattern=Query(...,examples="en"), - target_language:schemas.LangCodePattern=Query(...,examples="hi"), - token:str=Query(None,examples="duck"), - skip: int=Query(None, ge=0), limit: int=Query(None, ge=0), - user_details =Depends(get_user_or_none), db_:Session=Depends(get_db)): - '''Searches the translation memory for matching entries. Not context aware''' - log.info('In get_glossary_entries') - log.debug('source_language:%s, target_language:%s, token:%s', - source_language, target_language, token) - response = nlp_crud.get_glossary_list(db_, source_language, target_language, token, - skip=skip, limit=limit) - return response['token_translation_count'] - -@router.get('/v2/nlp/gloss-entries/count', - response_model= schemas_nlp.GlossCount, - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},415:{"model": schemas.ErrorResponse}, - 404:{"model": schemas.ErrorResponse},}, status_code=200, tags=["Nlp"]) -@get_auth_access_check_decorator -async def get_gloss_count(request: Request, - source_language:schemas.LangCodePattern=Query(...,examples="en"), - target_language:schemas.LangCodePattern=Query(...,examples="hi"), - token:str=Query(None,examples="love"), - user_details =Depends(get_user_or_none), db_:Session=Depends(get_db)): - '''Counts all glossary entries in translation memory between two languages. - * Also counts unique tokens in translation memory - ` * Can filter with or without search word/token - * Source and target language should be specified - * "tokenTranslationCount" in response counts different - translations of same word as different. - * "tokenCount" in response doesn't consider the multiple translations - but give the count of unique source tokens.''' - log.info('In get_glossary_count') - log.debug('source_language:%s, target_language:%s, token:%s', - source_language, target_language, token) - response = nlp_crud.get_glossary_list(db_, source_language, target_language, token) - response['tokenTranslationCount'] = len(response['token_translation_count']) - response['tokenCount'] = len(response['token_count']) - return response - -@router.post('/v2/nlp/gloss', response_model=schemas_nlp.GlossUpdateResponse, - status_code=201,responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 404: {"model": schemas.ErrorResponse}}, - tags=["Nlp"]) -@get_auth_access_check_decorator -async def add_gloss(request: Request, - source_language:schemas.LangCodePattern=Query(...,examples='en'), - target_language:schemas.LangCodePattern=Query(..., examples="hi"), - token_translations:List[schemas_nlp.GlossInput]=Body(...), - user_details =Depends(get_user_or_none), db_:Session=Depends(get_db)): - '''Load a list of predefined tokens and translations to improve tokenization and suggestion''' - log.info('In add_gloss') - log.debug('source_language:%s, target_language:%s, token_translations:%s', - source_language, target_language, token_translations) - tw_data = nlp_crud.add_to_translation_memory(db_,source_language, target_language, - token_translations) - return { "message": "Added to glossary", "data":tw_data } - -@router.put('/v2/nlp/gloss', response_model=schemas_nlp.TranslationMemoryUpdateResponse, - status_code=200,responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}}, - tags=["Nlp"]) -@get_auth_access_check_decorator -async def update_glossary(request: Request, - token_info:schemas_nlp.TranslationMemoryUpdate=Body(...), - user_details =Depends(get_user_or_none),db_:Session=Depends(get_db)): - '''Updates translation and/or metadata of a token''' - log.info("In update-glossary") - token_data = nlp_crud.edit_glossary(db_,token_info=token_info) - return { "message": "Glossary Updated", "data":token_data } - -@router.delete('/v2/nlp/gloss', status_code=201, - response_model=schemas.DeleteResponse, - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 404: {"model": schemas.ErrorResponse}, 403:{"model": schemas.ErrorResponse}}, - tags=['Nlp']) -@get_auth_access_check_decorator -async def remove_glossary(request: Request, - source_lang:schemas.LangCodePattern=Query(...,examples="en"), - target_lang:schemas.LangCodePattern=Query(...,examples="hi"), - token:str=Query(...,examples="duck"), - translation:str=Query(None,examples="बत्तख"), - user_details =Depends(get_user_or_none), db_: Session = Depends(get_db)): - '''Remove glossary.''' - log.info('In remove_gloss') - log.debug('source_language:%s,target_language:%s,token:%s,translation:%s', - source_lang,target_lang,token,translation) - deleted_content = nlp_crud.remove_glossary(db_, source_lang,target_lang,token,translation) - delcont = structurals_crud.add_deleted_data(db_, del_content= deleted_content['db_content'], - table_name = "translation_memory", deleting_user=user_details['user_id']) - return {'message': f"Token-Translation pair {token} -> {translation} deleted successfully", - "data": delcont} - -@router.post('/v2/nlp/alignment', response_model=schemas_nlp.GlossUpdateResponse, - status_code=201,responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 415: {"model": schemas.ErrorResponse}},tags=["Nlp"]) -@get_auth_access_check_decorator -async def add_alignments(request: Request, - source_language:schemas.LangCodePattern, target_language:schemas.LangCodePattern, - alignments:List[schemas_nlp.Alignment], - user_details =Depends(get_user_or_none), db_:Session=Depends(get_db)): - '''Prepares training data with alignments and update translation memory & suggestion models''' - log.info('In add_alignments') - log.debug('source_language:%s, target_language:%s, alignments:%s', - source_language, target_language, alignments) - tw_data = nlp_crud.alignments_to_trainingdata(db_,src_lang=source_language, - trg_lang=target_language, alignment_list=alignments, user_id=user_details['user_id']) - return { "message": "Alignments used for learning", "data":tw_data } - -@router.get('/v2/nlp/stopwords/{language_code}', response_model=List[schemas_nlp.StopWords], - response_model_exclude_none=True, status_code=200, - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 404: {"model": schemas.ErrorResponse}}, - tags=["Lookups"]) -@get_auth_access_check_decorator -async def get_stop_words(request: Request, - language_code:schemas.LangCodePattern=Path(...,examples="hi"), - include_system_defined:bool=True, include_user_defined:bool=True, - include_auto_generated :bool=True, only_active:bool=True, skip: int=Query(0, ge=0), - limit: int=Query(100, ge=0), - user_details =Depends(get_user_or_none), db_:Session=Depends(get_db)): - '''Api to retreive stopwords from lookup table''' - log.info('In get_stop_words') - log.debug('language_code:%s, include_system_defined:%s, include_user_defined:%s, \ - include_auto_generated:%s ,only_active:%s',language_code, include_system_defined, - include_user_defined, include_auto_generated, only_active) - return nlp_sw_crud.retrieve_stopwords(db_, language_code, - include_system_defined=include_system_defined, include_user_defined=include_user_defined, - include_auto_generated=include_auto_generated, only_active=only_active, skip=skip, - limit=limit) - -@router.put('/v2/nlp/stopwords/{language_code}', - response_model=schemas_nlp.StopWordUpdateResponse, response_model_exclude_none=True, - status_code=201,responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 404: {"model": schemas.ErrorResponse}}, - tags=['Lookups']) -@get_auth_access_check_decorator -async def update_stop_words(request: Request, - language_code:schemas.LangCodePattern=Path(...,examples="hi"), - sw_info:schemas_nlp.StopWordUpdate=Body(...), - user_details =Depends(get_user_or_none), db_:Session=Depends(get_db)): - '''Api to update fields of a stopword in lookup table''' - log.info('In update_stop_words') - log.debug('language_code:%s, sw_info:%s',language_code, sw_info) - sw_data = nlp_sw_crud.update_stopword_info(db_, language_code, sw_info,user_details['user_id']) - return { "message": "Stopword info updated successfully", "data":sw_data } - -@router.post('/v2/nlp/stopwords/{language_code}', - response_model=schemas_nlp.StopWordsAddResponse, response_model_exclude_none=True, - status_code=201,responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 404: {"model": schemas.ErrorResponse}}, - tags=['Lookups']) -@get_auth_access_check_decorator -async def add_stopwords(request: Request, - language_code:schemas.LangCodePattern=Path(...,examples="hi"), - stopwords_list:List[str]=Body(..., examples=["और", "के", "उसका"]), - user_details =Depends(get_user_or_none), db_:Session=Depends(get_db)): - '''Insert provided stopwords into db and returns added data''' - log.info('In add_stopwords') - log.debug('language_code:%s, stopwords_list:%s',language_code, stopwords_list) - result = nlp_sw_crud.add_stopwords(db_, language_code, stopwords_list, - user_id=user_details['user_id']) - msg = f"{len(result)} stopwords added successfully" - return {"message": msg, "data": result} - -@router.delete('/v2/nlp/stopwords/{language_code}', status_code=201, - response_model=schemas.DeleteResponse, - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}, - 404: {"model": schemas.ErrorResponse}, 403:{"model": schemas.ErrorResponse}}, - tags=['Lookups']) -@get_auth_access_check_decorator -async def remove_stopword(request: Request, - lang:schemas.LangCodePattern=Query(...,examples="en"), - stopword:str=Query(...,examples="as"), - user_details =Depends(get_user_or_none), db_: Session = Depends(get_db)): - '''Api to remove stopwords from lookup table''' - log.info('In remove_stopword') - log.debug('language:%s,stopword:%s',lang,stopword) - deleted_content = nlp_sw_crud.remove_stopword(db_, lang, stopword) - delcont = structurals_crud.add_deleted_data(db_, del_content= deleted_content['db_content'], - table_name = "stopwords_look_up", deleting_user=user_details['user_id']) - return {'message': "Stopword removed successfully", - "data": delcont} - -@router.post('/v2/nlp/stopwords-generate', - response_model=schemas_nlp.StopWordsGenerateResponse, response_model_exclude_none=True, - status_code=201,responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},401: {"model": schemas.ErrorResponse}}, - tags=['Nlp']) -@get_auth_access_check_decorator -async def generate_stopwords(request: Request, background_tasks: BackgroundTasks, - language_code:schemas.LangCodePattern=Query(...,examples="bi"), - use_server_data:bool=True, - source_name: schemas.TableNamePattern=Query(None,examples="en_TW_1_vocabulary"), - user_details =Depends(get_user_or_none), - sentence_list:List[schemas_nlp.SentenceInput]=Body(None), db_:Session=Depends(get_db), - operates_on=Depends(AddHiddenInput(value=schema_auth.ResourceType.LOOKUP.value))):#pylint: disable=unused-argument - '''Auto generate stop words for a given language''' - log.info('In generate_stopwords') - log.debug('language_code:%s, use_server_data:%s, source_name:%s, sentence_list:%s', - language_code, use_server_data, source_name, sentence_list) - - # job_info = create_job( - # request=request, #pylint: disable=W0613 - # db_=db_, user_id=user_details['user_id']) - job_info = nlp_sw_crud.create_job(db_=db_, user_id=user_details['user_id']) - job_id = job_info.jobId - background_tasks.add_task(nlp_sw_crud.generate_stopwords, db_, request, language_code, - source_name, sentence_list, job_id, use_server_data=use_server_data, - user_details=user_details) - msg = "Generating stop words in background" - # data = {"jobId": job_info['data']['jobId'], "status": job_info['data']['status']} - data = {"jobId": job_info.jobId, "status": job_info.status} - return {"message": msg, "data": data} - -#################### Jobs #################### - -# @router.post('/v2/jobs', response_model=schemas_nlp.JobCreateResponse, status_code=201, -# tags=['Jobs']) -# def create_job(request:Request, #pylint: disable=W0613 -# db_:Session=Depends(get_db), user_id="10101"): -# '''Creates a new job''' -# log.info('In create_job') -# job_info = nlp_sw_crud.create_job(db_=db_, user_id=user_id) -# return {'message': "Job created successfully", -# "data": {"jobId": job_info.jobId, "status": job_info.status}} - -@router.get('/v2/jobs', response_model=schemas_nlp.JobStatusResponse, - response_model_exclude_none=True, status_code=200, - responses={502: {"model": schemas.ErrorResponse}, - 422: {"model": schemas.ErrorResponse},404:{"model": schemas.ErrorResponse}}, - tags=['Jobs']) -@get_auth_access_check_decorator -async def check_job_status(request: Request, - job_id:int=Query(...,examples="100000"),user_details =Depends(get_user_or_none), - db_:Session=Depends(get_db)): - '''Checking the status of a job''' - log.info('In check_job_status') - log.debug('job_id:%s', job_id) - result = nlp_sw_crud.check_job_status(db_, job_id) - return result diff --git a/app/test/test_agmt_projects.py b/app/test/test_agmt_projects.py deleted file mode 100644 index 3854bfa2..00000000 --- a/app/test/test_agmt_projects.py +++ /dev/null @@ -1,1538 +0,0 @@ -'''Test cases for Agmt projects related APIs''' -import time -from . import client -from . import assert_input_validation_error, assert_not_available_content -from . import check_default_get -from schema import schema_auth -from .test_bibles import check_post as add_bible, gospel_books_data -from .test_resources import check_post as add_resource -from .test_versions import check_post as add_version -from .conftest import initial_test_users -from . test_auth_basic import login,SUPER_PASSWORD,SUPER_USER,logout_user - -UNIT_URL = '/v2/text/translate/token-based/projects' -USER_URL = '/v2/text/translate/token-based/project/user' -SENTENCE_URL = '/v2/text/translate/token-based/project/sentences' -RESTORE_URL = '/v2/admin/restore' -headers = {"contentType": "application/json", "accept": "application/json", "app":"Autographa"} -headers_auth = {"contentType": "application/json", - "accept": "application/json", - "app":"Autographa" - } - -bible_books = { - "mat": "\\id MAT\n\\c 1\n\\p\n\\v 1 इब्राहीम के वंशज दाऊद के पुत्र यीशु मसीह की वंशावली इस "+ - "प्रकार है:\n\\v 2 इब्राहीम का पुत्र था इसहाक और इसहाक का पुत्र हुआ याकूब। फिर याकूब "+ - "से यहूदा और उसके भाई उत्पन्न हुए।\n\\v 3 यहूदा के बेटे थे फिरिस और जोरह। (उनकी माँ "+ - "का नाम तामार था।) फिरिस, हिस्रोन का पिता था। हिस्रोन राम का पिता था।", - "mrk": "\\id MRK\n\\c 1\n\\p\n\\v 1 यह परमेश्वर के पुत्र यीशु मसीह के शुभ संदेश का प्रारम्भ"+ - " है।\n\\v 2 भविष्यवक्ता यशायाह की पुस्तक में लिखा है कि: “सुन! मैं अपने दूत को तुझसे"+ - " पहले भेज रहा हूँ। वह तेरे लिये मार्ग तैयार करेगा।”\n\\v 3 “जंगल में किसी पुकारने "+ - "वाले का शब्द सुनाई दे रहा है: ‘प्रभु के लिये मार्ग तैयार करो। और उसके लिये राहें "+ - "सीधी बनाओ।’”\n\\v 4 यूहन्ना लोगों को जंगल में बपतिस्मा देते आया था। उसने लोगों से"+ - " पापों की क्षमा के लिए मन फिराव का बपतिस्मा लेने को कहा।\n\\v 5 फिर समूचे यहूदिया"+ - " देश के और यरूशलेम के लोग उसके पास गये और उस ने यर्दन नदी में उन्हें बपतिस्मा दिया"+ - "। क्योंकि उन्होंने अपने पाप मान लिये थे।" -} - -def assert_positive_get(item): - '''Check for the properties in the normal return object''' - assert "projectId" in item - assert isinstance(item['projectId'], int) - assert "projectName" in item - assert "sourceLanguage" in item - assert "code" in item['sourceLanguage'] - assert "targetLanguage" in item - assert "code" in item['targetLanguage'] - assert 'metaData' in item - assert 'books' in item['metaData'] - assert 'useDataForLearning' in item['metaData'] - assert "active" in item - assert "users" in item - assert isinstance(item['users'], list) - assert "createTime" in item - assert "updateTime" in item - - -def check_post(data, auth_token=None): - '''creates a projects''' - headers_auth = {"contentType": "application/json", - "accept": "application/json", - "app":"Autographa"} - if not auth_token: - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - else: - headers_auth['Authorization'] = "Bearer"+" "+auth_token - response = client.post(UNIT_URL, headers=headers_auth, json=data) - return response - -def test_default_post_put_get(): - '''Positive test to create a project''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - resp = client.get(UNIT_URL+'?project_name=Test project 1',headers=headers_auth) - assert_not_available_content(resp) - - # create with minimum data - post_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - new_project = response.json()['data'] - assert_positive_get(new_project) - - # check if all defaults are coming - assert new_project['metaData']["useDataForLearning"] - assert isinstance(new_project['metaData']['books'], list) - assert len(new_project['metaData']['books']) == 0 - assert new_project['active'] - - # upload books - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - response2 = client.put(UNIT_URL+'?project_id='+str(new_project['projectId']),\ - headers=headers_auth, json=put_data) - assert response2.status_code == 201 - assert response2.json()['message'] == "Project updated successfully" - updated_project = response2.json()['data'] - assert_positive_get(updated_project) - - assert new_project['projectId'] == updated_project['projectId'] - assert new_project['projectName'] == updated_project['projectName'] - assert updated_project['metaData']['books'] == ['mat', 'mrk'] - - #add bible , create resource with open-access - - version_data = { - "versionAbbreviation": "TTT", - "versionName": "test version for bibles", - } - add_version(version_data) - resource_data = { - "resourceType": "bible", - "language": "gu", - "version": "TTT", - "year": 3030, - "versionTag": 1, - "accessPermissions": [ - "content","open-access" - ], - } - resource = add_resource(resource_data) - table_name = resource.json()['data']['resourceName'] - - #add books with vachan admin - headers_auth_content = {"contentType": "application/json", - "accept": "application/json" - } - headers_auth_content['Authorization'] = "Bearer"+" "+initial_test_users['VachanAdmin']['token'] - resp = client.post('/v2/resources/bibles/'+table_name+'/books', headers=headers_auth_content, json=gospel_books_data) - - # resp, resource_name = add_bible(gospel_books_data) - assert resp.status_code == 201 - - put_data = { - "selectedBooks": { - "bible": table_name, - "books": ["luk", "jhn"] - } - } - response2b = client.put(UNIT_URL+'?project_id='+str(new_project['projectId']), \ - headers=headers_auth, json=put_data) - assert response2b.status_code == 201 - assert response2b.json()['message'] == "Project updated successfully" - updated_project = response2b.json()['data'] - assert_positive_get(updated_project) - assert updated_project['metaData']['books'] == ['mat', 'mrk', 'luk', 'jhn'] - - # fetch projects - response3 = client.get(UNIT_URL,headers=headers_auth) - assert len(response3.json()) >= 1 - found_project = False - for proj in response3.json(): - if proj['projectName'] == post_data['projectName']: - found_project = True - fetched_project = proj - assert found_project - assert_positive_get(fetched_project) - - assert fetched_project['projectName'] == post_data['projectName'] - assert fetched_project['sourceLanguage']['code'] == post_data['sourceLanguageCode'] - assert fetched_project['targetLanguage']['code'] == post_data['targetLanguageCode'] - assert fetched_project['metaData']['books'] == ['mat', 'mrk', 'luk', 'jhn'] - - # change name - put_data = { - "projectName":"New name for old project"} - response4 = client.put(UNIT_URL+'?project_id='+str(new_project['projectId']),\ - headers=headers_auth, json=put_data) - assert response4.status_code == 201 - assert response4.json()['message'] == "Project updated successfully" - updated_project = response4.json()['data'] - assert_positive_get(updated_project) - assert updated_project['projectName']== "New name for old project" - - - # create with all possible options - post_data = { - "projectName": "Test Project 2", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml", - "useDataForLearning": True, - "stopwords": { - "prepositions": [ "कोई", "यह", "इस", "इसे", "उस", "कई", "इसी", "अभी", "जैसे" ], - "postpositions": [ "के", "का", "में", "की", "है", "और", "से", "हैं", "को", "पर"] - }, - "punctuations": [",","\"","!",".",":",";","\n","\\","“","”","“","*","।","?",";","'", - "’","(",")","‘","—" ], - "active": True - } - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - new_project = response.json()['data'] - assert_positive_get(new_project) - - # add a few more projects - post_data = { - "projectName": "Test Project 3", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - resp = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert resp.status_code == 201 - post_data = { - "projectName": "Test Project 4", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - resp = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert resp.status_code == 201 - - check_default_get(UNIT_URL,headers_auth, assert_positive_get) - - -def test_post_invalid(): - '''test input validation for project create''' - # Missing mandatory content - data1 = { - "projectName": "Test project 1", - "targetLanguageCode": "ml" - } - res1 = client.post(UNIT_URL, headers=headers_auth, json=data1) - assert_input_validation_error(res1) - - data2 = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - } - res2 = client.post(UNIT_URL, headers=headers_auth, json=data2) - assert_input_validation_error(res2) - - data3 = { - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - res3 = client.post(UNIT_URL, headers=headers_auth, json=data3) - assert_input_validation_error(res3) - - # incorrect data in fields - data1 = { - "projectName": "Test project 1", - "sourceLanguageCode": "2hindi", - "targetLanguageCode": "ml" - } - res1 = client.post(UNIT_URL, headers=headers_auth, json=data1) - assert_input_validation_error(res1) - - data2 = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml", - "useDataForLearning": "use" - } - res2 = client.post(UNIT_URL, headers=headers_auth, json=data2) - assert_input_validation_error(res2) - - data3 = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml", - "stopwords": ["a", "mromal", "list"] - } - res3 = client.post(UNIT_URL, headers=headers_auth, json=data3) - assert_input_validation_error(res3) - - data4 = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml", - "punctuations": "+_*())^%$#<>?:'" - } - res4 = client.post(UNIT_URL, headers=headers_auth, json=data4) - assert_input_validation_error(res4) - -def test_put_invalid(): - '''Give incorrect data for project update''' - post_data = {"projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml"} - resp = check_post(post_data) - assert resp.json()['message'] == "Project created successfully" - new_project = resp.json()['data'] - - # missing projectId - data = {"active": False} - resp = client.put(UNIT_URL, headers=headers_auth, json=data) - assert_input_validation_error(resp) - - # incorrect values in fields - data = {"active": "delete"} - resp = client.put(UNIT_URL+'?project_id='+str(new_project['projectId']),\ - headers=headers_auth, json=data) - assert_input_validation_error(resp) - - data = {"uploadedUSFMs": "mat"} - resp = client.put(UNIT_URL+'?project_id='+str(new_project['projectId']),\ - headers=headers_auth, json=data) - assert_input_validation_error(resp) - - data = {"uploadedUSFMs": ["The contents of matthew in text"]} - resp = client.put(UNIT_URL+'?project_id='+str(new_project['projectId']),\ - headers=headers_auth, json=data) - assert resp.status_code == 415 - assert resp.json()['error'] == "Not the Required Type" - -def check_project_user(project_name, user_id, role=None, status=None, metadata = None): - '''Make sure the user is in project and if specified, check for other values''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - response = client.get(UNIT_URL+'?project_name='+project_name,headers=headers_auth) - found_user = False - found_owner = False - - for user in response.json()[0]['users']: - if user['userId'] == user_id: - found_user = True - if role: - assert user['userRole'] == role - if status is not None: - assert user['active'] == status - if metadata: - assert user['metaData'] == metadata - if user['userRole'] == "projectOwner" : - found_owner = True - assert found_owner - assert found_user - -def test_add_user(): - '''Positive test to add a user to a project''' - project_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - resp = check_post(project_data) - assert resp.status_code == 201 - new_project = resp.json()['data'] - - #not exising user id - new_user_id = str(5555) - response = client.post(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert response.status_code == 404 - assert response.json()['error'] == 'Requested Content Not Available' - #exising user - new_user_id = initial_test_users['AgUser']['test_user_id'] - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - response = client.post(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert response.status_code == 201 - assert response.json()['message'] == "User added to project successfully" - data = response.json()['data'] - assert data["project_id"] == new_project['projectId'] - assert data["userId"] == new_user_id - assert data["userRole"] == "projectMember" - assert data['active'] - - # fetch this project and check for new user - check_project_user(project_data['projectName'], new_user_id, role='projectMember') - - -def test_add_user_invalid(): - '''Negative tests to add a user to a project''' - project_data = { - "projectName": "Test project 10", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - resp = check_post(project_data) - assert resp.status_code == 201 - new_project = resp.json()['data'] - - # No projectId - resp = client.post(USER_URL+'?user_id=11111',headers=headers_auth) - assert_input_validation_error(resp) - - # No User - resp = client.post(USER_URL+'?project_id='+str(new_project['projectId']),headers=headers_auth) - assert_input_validation_error(resp) - - # Invalid project - resp = client.post(USER_URL+'?project_id='+str(new_project['projectName'])+ - '&user_id=111111',headers=headers_auth) - assert_input_validation_error(resp) - - # Invalid user - resp = client.post(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id=some_name',headers=headers_auth) - assert resp.status_code == 404 - assert resp.json()['error'] == 'Requested Content Not Available' - - -def test_update_user(): - '''Positive tests to change role, status & metadata of a user''' - project_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - resp = check_post(project_data) - assert resp.json()['message'] == "Project created successfully" - new_project = resp.json()['data'] - new_user_id = initial_test_users['AgUser']['test_user_id'] - - resp = client.post(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert resp.json()['message'] == "User added to project successfully" - - update_data = { - "userId": new_user_id - } - - # change role - update1 = update_data - update1['userRole'] = 'projectOwner' - response1 = client.put(USER_URL+'?project_id='+str(new_project['projectId']),\ - headers=headers_auth, json=update1) - assert response1.status_code == 201 - assert response1.json()['message'] == "User updated in project successfully" - check_project_user(project_data['projectName'], new_user_id, role="projectOwner") - - # change status - update2 = update_data - update2['active'] = False - response2 = client.put(USER_URL+'?project_id='+str(new_project['projectId']),\ - headers=headers_auth, json=update2) - assert response2.status_code == 201 - assert response2.json()['message'] == "User updated in project successfully" - check_project_user(project_data['projectName'], new_user_id, status=False) - - # add metadata - meta = {"last_filter": "mat"} - update3 = update_data - update3['metaData'] = meta - response3 = client.put(USER_URL+'?project_id='+str(new_project['projectId']),\ - headers=headers_auth, json=update3) - assert response3.status_code == 201 - assert response3.json()['message'] == "User updated in project successfully" - check_project_user(project_data['projectName'], new_user_id, metadata=meta) - - -def test_update_user_invlaid(): - '''Negative test for update user''' - project_data = { - "projectName": "Test project 101", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - resp = check_post(project_data) - assert resp.json()['message'] == "Project created successfully" - new_project = resp.json()['data'] - new_user_id = initial_test_users['AgUser']['test_user_id'] - - resp = client.post(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert resp.json()['message'] == "User added to project successfully" - - - # not the added user - update_data = { - "userId": "not-a-valid-user-11233", - "active": False - } - response = client.put(USER_URL+'?project_id='+str(new_project['projectId']),\ - headers=headers_auth, json=update_data) - assert response.status_code == 404 - assert response.json()['details'] == "User-project pair not found" - - #non existant project - update_data = { - "userId": new_user_id, - "active": False - } - response = client.put(USER_URL+'?project_id='+str(new_project['projectId']+1),\ - headers=headers_auth, json=update_data) - assert response.status_code == 404 - assert response.json()['details'] == f"Project with id, {new_project['projectId']+1}, not present" - - # invalid status - update_data = { - "userId": new_user_id, - "active": "Delete" - } - response = client.put(USER_URL+'?project_id='+str(new_project['projectId']+1),\ - headers=headers_auth, json=update_data) - assert_input_validation_error(response) - - # invalid metadata - update_data = { - "userId": new_user_id, - "metaData": "A normal string intead of json" - } - response = client.put(USER_URL+'?project_id='+str(new_project['projectId']+1),\ - headers=headers_auth, json=update_data) - assert_input_validation_error(response) - - -def test_soft_delete(): - '''Check if unsetting active status works the desired way''' - data = [ - { - "projectName": "Test Project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - }, - { - "projectName": "Test Project 2", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - }, - { - "projectName": "Test Project 3", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - }, - { - "projectName": "Test Project 4", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - }, - { - "projectName": "Test Project 5", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - ] - - delete_data = [ - {"project_name": "Test Project 4"}, - {"project_name": "Test Project 5"} - ] - for item in data: - response = check_post(item) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - - get_response1 = client.get(UNIT_URL,headers=headers_auth) - assert len(get_response1.json()) >= len(data) - - - # positive PUT - for item in delete_data: - # find the project id - resp = client.get(UNIT_URL+"?project_name="+item['project_name'],headers=headers_auth) - assert resp.status_code == 200 - project_id = resp.json()[0]['projectId'] - - put_obj = {"active": False} - put_obj['projectId'] = project_id - response = client.put(UNIT_URL+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_obj) - assert response.status_code == 201 - assert response.json()['message'] == 'Project updated successfully' - assert not response.json()['data']['active'] - - get_response2 = client.get(UNIT_URL,headers=headers_auth) - assert len(get_response2.json()) == len(get_response1.json()) - len(delete_data) - - get_response3 = client.get(UNIT_URL+'?active=false',headers=headers_auth) - assert len(get_response3.json()) >= len(delete_data) - - -#Access Rules and related Test - -#Project Access Rules based tests -def test_agmt_projects_access_rule(): - """create related access rule and auth""" - #with and without auth - post_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - response = client.post(UNIT_URL, headers=headers, json=post_data) - assert response.status_code == 401 - assert 'error' in response.json() - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - project1_id = response.json()['data']['projectId'] - - #create from app other than Autographa - post_data["projectName"] = "Test project 2" - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - headers_auth["app"] = "API-user" - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - post_data["projectName"] = "Test project 3" - headers_auth["app"] = "Vachan-online or vachan-app" - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - post_data["projectName"] = "Test project 4" - headers_auth["app"] = "VachanAdmin" - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - - #create project by not allowed users - post_data["projectName"] = "Test project 5" - headers_auth["app"] = "Autographa" - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['BcsDev']['token'] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanAdmin']['token'] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['APIUser']['token'] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanUser']['token'] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanContentAdmin']['token'] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanContentViewer']['token'] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - - #Super Admin - SA_user_data = { - "user_email": SUPER_USER, - "password": SUPER_PASSWORD - } - response = login(SA_user_data) - assert response.json()['message'] == "Login Succesfull" - test_SA_token = response.json()["token"] - - #create with AGUser and SA - post_data["projectName"] = "Test project 6" - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - project6_id = response.json()['data']['projectId'] - post_data["projectName"] = "Test project 7" - headers_auth['Authorization'] = "Bearer"+" "+test_SA_token - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - project7_id = response.json()['data']['projectId'] - - #update Project - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - #update with Owner of project - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - response2 = client.put(UNIT_URL+'?project_id='+str(project6_id),\ - headers=headers_auth, json=put_data) - assert response2.status_code == 201 - assert response2.json()['message'] == "Project updated successfully" - updated_project = response2.json()['data'] - assert updated_project['metaData']['books'] == ['mat', 'mrk'] - - #aguser project can be updated by super admin and Ag admin - put_data = { - "projectName":"New name for old project6"} - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - response2 = client.put(UNIT_URL+'?project_id='+str(project6_id),\ - headers=headers_auth, json=put_data) - assert response2.status_code == 201 - assert response2.json()['message'] == "Project updated successfully" - assert response2.json()['data']['projectName'] == put_data["projectName"] - put_data["projectName"] = "New name for project7 by SA" - headers_auth['Authorization'] = "Bearer"+" "+test_SA_token - response2 = client.put(UNIT_URL+'?project_id='+str(project6_id),\ - headers=headers_auth, json=put_data) - assert response2.status_code == 201 - assert response2.json()['message'] == "Project updated successfully" - assert response2.json()['data']['projectName'] == put_data["projectName"] - - #update project with not Owner - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - response2 = client.put(UNIT_URL+'?project_id='+str(project7_id),\ - headers=headers_auth, json=put_data) - assert response2.status_code == 403 - assert response2.json()['error'] == 'Permission Denied' - - #project user create - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - new_user_id = initial_test_users['AgUser']['test_user_id'] - #add user from another app than Autographa - headers_auth["app"] = "VachanAdmin" - response = client.post(USER_URL+'?project_id='+str(project1_id)+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - headers_auth["app"] = "Vachan-online or vachan-app" - response = client.post(USER_URL+'?project_id='+str(project1_id)+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - headers_auth["app"] = "API-user" - response = client.post(USER_URL+'?project_id='+str(project1_id)+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - #add user by not owner - headers_auth["app"] = "Autographa" - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - response = client.post(USER_URL+'?project_id='+str(project1_id)+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - #add from Autograpaha by allowed user - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - post_data["projectName"] = "Test project 8" - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - project8_id = response.json()['data']['projectId'] - - response = client.post(USER_URL+'?project_id='+str(project8_id)+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert response.status_code == 201 - assert response.json()['message'] == "User added to project successfully" - data = response.json()['data'] - assert data["project_id"] == project8_id - assert data["userId"] == new_user_id - assert data["userRole"] == "projectMember" - assert data['active'] - - #update user with not owner - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - update_data = { - "userId": new_user_id - } - # add metadata - meta = {"last_filter": "luk"} - update_data['metaData'] = meta - response3 = client.put(USER_URL+'?project_id='+str(project8_id),\ - headers=headers_auth, json=update_data) - assert response3.status_code == 403 - assert response3.json()['error'] == 'Permission Denied' - #update with owner - post_data['projectName'] = 'Test project 1' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - response3 = client.put(USER_URL+'?project_id='+str(project8_id),\ - headers=headers_auth, json=update_data) - assert response3.status_code == 201 - assert response3.json()['message'] == "User updated in project successfully" - -def test_get_project_access_rules(): - """test for get project access rules""" - #create projects - post_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - project1_id = response.json()['data']['projectId'] - - post_data["projectName"] = "Test project 2" - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - project2_id = response.json()['data']['projectId'] - - post_data["projectName"] = "Test project 3" - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - project3_id = response.json()['data']['projectId'] - - #Super Admin - SA_user_data = { - "user_email": SUPER_USER, - "password": SUPER_PASSWORD - } - response = login(SA_user_data) - assert response.json()['message'] == "Login Succesfull" - test_SA_token = response.json()["token"] - - #get project from apps other than autographa - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - headers_auth["app"] = "API-user" - response = client.get(UNIT_URL,headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - headers_auth["app"] = "Vachan-online or vachan-app" - response = client.get(UNIT_URL,headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - headers_auth["app"] = "VachanAdmin" - response = client.get(UNIT_URL,headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - headers_auth["app"] = "VachanContentDashboard" - response = client.get(UNIT_URL,headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - - #get project from Autographa with not allowed users get empty result - headers_auth["app"] = "Autographa" - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanAdmin']['token'] - response = client.get(UNIT_URL,headers=headers_auth) - assert len(response.json()) == 0 - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['APIUser']['token'] - response = client.get(UNIT_URL,headers=headers_auth) - assert len(response.json()) == 0 - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanUser']['token'] - response = client.get(UNIT_URL,headers=headers_auth) - assert len(response.json()) == 0 - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanContentAdmin']['token'] - response = client.get(UNIT_URL,headers=headers_auth) - assert len(response.json()) == 0 - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanContentViewer']['token'] - response = client.get(UNIT_URL,headers=headers_auth) - assert len(response.json()) == 0 - - #get all project by SA , AgAdmin, Bcs internal dev - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - response = client.get(UNIT_URL,headers=headers_auth) - assert len(response.json()) >= 3 - - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['BcsDev']['token'] - response = client.get(UNIT_URL,headers=headers_auth) - assert len(response.json()) >= 3 - - headers_auth['Authorization'] = "Bearer"+" "+ test_SA_token - response = client.get(UNIT_URL,headers=headers_auth) - assert len(response.json()) >= 3 - - #get project by project owner Aguser only get owner project - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - response = client.get(UNIT_URL,headers=headers_auth) - assert len(response.json()) >= 2 - - #create project by SA and add Aguser as member to the project - post_data["projectName"] = 'Test project 4' - headers_auth['Authorization'] = "Bearer"+" "+ test_SA_token - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - project4_id = response.json()['data']['projectId'] - - new_user_id = initial_test_users['AgUser']['test_user_id'] - response = client.post(USER_URL+'?project_id='+str(project4_id)+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert response.status_code == 201 - assert response.json()['message'] == "User added to project successfully" - data = response.json()['data'] - assert data["project_id"] == project4_id - assert data["userId"] == new_user_id - assert data["userRole"] == "projectMember" - assert data['active'] - - #get after add as member - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - response = client.get(UNIT_URL+"?user_id="+new_user_id,headers=headers_auth) - assert len(response.json()) >= 3 - for proj in response.json(): - assert proj['projectName'] in ["Test project 4","Test project 3","Test project 2"] - - #A new Aguser requesting for all projecrts - # test_ag_user_data = { - # "email": "testaguser@test.com", - # "password": "passwordag@1" - # } - # response = register(test_ag_user_data, apptype='Autographa') - # ag_user_id = [response.json()["registered_details"]["id"]] - # ag_user_token = response.json()["token"] - - #get projects where user have no projects result is [] - headers_auth["app"] = "Autographa" - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser2']['token'] - # headers_auth['Authorization'] = "Bearer"+" "+ag_user_token - response = client.get(UNIT_URL,headers=headers_auth) - assert len(response.json()) == 0 - # delete_user_identity(ag_user_id) - - -def test_create_n_update_times(): - '''Test to ensure created time and last updated time are included in project GET response''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - resp = client.get(UNIT_URL+'?project_name=Test project 1',headers=headers_auth) - assert_not_available_content(resp) - - # create with minimum data - post_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - new_project = response.json()['data'] - assert_positive_get(new_project) - project_id = response.json()['data']['projectId'] - - response = client.get(f"{UNIT_URL}?project_name={post_data['projectName']}",headers=headers_auth) - assert response.json()[0]["createTime"] is not None - assert response.json()[0]['updateTime'] is not None - assert response.json()[0]["createTime"] == response.json()[0]["updateTime"] - - time.sleep(10) - # create project 2 - post_data = { - "projectName": "Test project 2", - "sourceLanguageCode": "hi", - "targetLanguageCode": "en" - } - response1 = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response1.status_code == 201 - assert response1.json()['message'] == "Project created successfully" - new_project2 = response1.json()['data'] - assert_positive_get(new_project2) - print("///post resp1:",new_project2) - create_time_2 = new_project2['createTime'] - update_time_2 = new_project2['updateTime'] - assert create_time_2 == update_time_2 - - assert not new_project['createTime'] == create_time_2 - time.sleep(10) - - # Make an update to project - update_data = { - "metaData": {"last_filter": "luk"} - } - response2 = client.put(UNIT_URL+'?project_id='+str(project_id),\ - headers=headers_auth, json=update_data) - assert response2.status_code == 201 - assert response2.json()['message'] == "Project updated successfully" - updated_project = response2.json()['data'] - print("response.json()[0][updateTime]:",response.json()[0]["updateTime"]) - print("response2.json()[0][updateTime]:",updated_project["updateTime"]) - assert response.json()[0]["updateTime"] != updated_project["updateTime"] - assert not response.json()[0]["updateTime"] == updated_project['updateTime'] - assert updated_project['updateTime'] > response.json()[0]["updateTime"] - - -def test_delete_project(): - '''Test the removal a project''' - - #Create Project - project_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - resp = check_post(project_data, auth_token=initial_test_users['AgAdmin']['token']) - assert resp.status_code == 201 - assert resp.json()['message'] == "Project created successfully" - new_project = resp.json()['data'] - project_id = new_project['projectId'] - assert_positive_get(new_project) - - # fetch project - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - get_response = client.get(UNIT_URL,headers=headers_auth) - assert len(get_response.json()) >= 1 - found_project = False - for proj in get_response.json(): - if proj['projectName'] == project_data['projectName']: - found_project = True - fetched_project = proj - assert found_project - assert_positive_get(fetched_project) - - assert fetched_project['projectName'] == project_data['projectName'] - assert fetched_project['sourceLanguage']['code'] == project_data['sourceLanguageCode'] - assert fetched_project['targetLanguage']['code'] == project_data['targetLanguageCode'] - - #Delete Project with no auth - resp = client.delete(UNIT_URL+'?project_id='+str(new_project['projectId']),headers=headers) - assert resp.status_code == 401 - assert resp.json()['details'] == "Access token not provided or user not recognized." - - # deleting non existing project - invalid_project_id = 9999 - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - resp = client.delete(UNIT_URL+'?project_id='+str(invalid_project_id),headers=headers_auth) - assert resp.status_code == 404 - assert resp.json()['details'] == f"Project with id, {invalid_project_id}, not present" - - - # Delete as unauthorized users - for user in ['APIUser','VachanAdmin','VachanUser','BcsDev','VachanContentAdmin','VachanContentViewer']: - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users[user]['token'] - response = client.delete(UNIT_URL+'?project_id='+str(project_id),headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - - # Delete as AgAdmin- Positive test - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - resp = client.delete(UNIT_URL+'?project_id='+str(project_id),headers=headers_auth) - assert resp.status_code == 201 - assert "successfull" in resp.json()['message'] - - #Ensure deleted project is not present - resp = client.get(UNIT_URL+'?project_name=Test project 1',headers=headers_auth) - assert_not_available_content(resp) - - #Create and Delete Project with AgUser - Positive Test - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - post_resp_aguser = check_post(project_data, auth_token=initial_test_users['AgUser']['token']) - assert post_resp_aguser.status_code == 201 - #Ensure presence of created project - get_response = client.get(UNIT_URL,headers=headers_auth) - assert len(get_response.json()) >= 1 - found_project = False - for proj in get_response.json(): - if proj['projectName'] == project_data['projectName']: - found_project = True - fetched_project = proj - assert found_project - assert_positive_get(fetched_project) - #Get project Id - new_project = post_resp_aguser.json()['data'] - project_id = new_project['projectId'] - #Delete - delete_resp_aguser = client.delete(UNIT_URL+'?project_id='+str(project_id),headers=headers_auth) - assert delete_resp_aguser.status_code == 201 - #Ensure deleted project is not present - resp = client.get(UNIT_URL+'?project_name=Test project 1',headers=headers_auth) - assert_not_available_content(resp) - - #Create and Delete Project with SuperAdmin - Positive Test - #Login as Super Admin - sa_data = { - "user_email": SUPER_USER, - "password": SUPER_PASSWORD - } - response = login(sa_data) - assert response.json()['message'] == "Login Succesfull" - test_user_token = response.json()["token"] - headers_auth['Authorization'] = "Bearer"+" "+test_user_token - post_resp_sa = check_post(project_data, auth_token=test_user_token) - assert post_resp_sa.status_code == 201 - #Ensure presence of created project - get_response = client.get(UNIT_URL,headers=headers_auth) - assert len(get_response.json()) >= 1 - found_project = False - for proj in get_response.json(): - if proj['projectName'] == project_data['projectName']: - found_project = True - fetched_project = proj - assert found_project - assert_positive_get(fetched_project) - #Get project Id - new_project = post_resp_sa.json()['data'] - project_id = new_project['projectId'] - #Delete - delete_resp_sa= client.delete(UNIT_URL+'?project_id='+str(project_id),headers=headers_auth) - assert delete_resp_sa.status_code == 201 - #Ensure deleted project is not present - resp = client.get(UNIT_URL+'?project_name=Test project 1',headers=headers_auth) - assert_not_available_content(resp) - logout_user(test_user_token) - -def test_restore_project(): - '''positive test case, checking for correct return object''' - #only Super Admin can restore deleted data - #Creating and Deleting project - project_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - post_resp= check_post(project_data, auth_token=initial_test_users['AgAdmin']['token']) - assert post_resp.status_code == 201 - - #Get project Id - project_id = post_resp.json()['data']['projectId'] - - #Delete - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - delete_resp= client.delete(UNIT_URL+'?project_id='+str(project_id),headers=headers_auth) - assert delete_resp.status_code == 201 - #Ensure deleted project is not present - resp = client.get(UNIT_URL+'?project_name=Test project 1',headers=headers_auth) - assert_not_available_content(resp) - - deleteditem_id = delete_resp.json()['data']['itemId'] - data = {"itemId": deleteditem_id} - - #Restoring data - #Restore without authentication - Negative Test - response = client.put(RESTORE_URL, headers=headers, json=data) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - - #Restore project with other API user,VachanAdmin,AgAdmin,AgUser,VachanUser,BcsDev and APIUSer2 - Negative Test - for user in ['APIUser','VachanAdmin','AgAdmin','AgUser','VachanUser','BcsDev','APIUser2','VachanContentAdmin','VachanContentViewer']: - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users[user]['token'] - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - - #Restore Project with Super Admin - Positive Test - # Login as Super Admin - sa_data = { - "user_email": SUPER_USER, - "password": SUPER_PASSWORD - } - response = login(sa_data) - assert response.json()['message'] == "Login Succesfull" - test_user_token = response.json()["token"] - headers_auth['Authorization'] = "Bearer"+" "+test_user_token - - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 201 - assert response.json()['message'] == \ - f"Deleted Item with identity {deleteditem_id} restored successfully" - - #Ensure presence of restored project - get_response = client.get(UNIT_URL,headers=headers_auth) - assert len(get_response.json()) >= 1 - found_project = False - for proj in get_response.json(): - if proj['projectName'] == project_data['projectName']: - found_project = True - fetched_project = proj - assert found_project - assert_positive_get(fetched_project) - - #restore with missing data - Negative Test - data = {} - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert_input_validation_error(response) - - #Restore with invalid item id - Negative Test - data = {"itemId":9999} - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 404 - assert response.json()['error'] == "Requested Content Not Available" - logout_user(test_user_token) - -def test_delete_user(): - '''Test the removal of a user from a project''' - project_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - resp = check_post(project_data, auth_token=initial_test_users['AgAdmin']['token']) - assert resp.json()['message'] == "Project created successfully" - new_project = resp.json()['data'] - new_user_id = initial_test_users['AgUser']['test_user_id'] - - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - resp = client.post(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert resp.json()['message'] == "User added to project successfully" - - # fetch this project and check for new user - check_project_user(project_data['projectName'], new_user_id, role='projectMember') - - #no auth - resp = client.delete(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(new_user_id),headers=headers) - assert resp.status_code == 401 - assert resp.json()['details'] == "Access token not provided or user not recognized." - check_project_user(project_data['projectName'], new_user_id, role='projectMember') - - # deleting non existing user - second_user_id = initial_test_users['AgUser2']['test_user_id'] - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - resp = client.delete(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(second_user_id),headers=headers_auth) - assert resp.status_code == 404 - assert resp.json()['details'] == "User-project pair not found" - - resp = client.post(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(second_user_id),headers=headers_auth) - assert resp.json()['message'] == "User added to project successfully" - check_project_user(project_data['projectName'], second_user_id, role='projectMember') - - # as non-owner user - user_id = initial_test_users['AgUser2']['test_user_id'] - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - resp =client.delete(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(user_id),headers=headers_auth) - assert resp.status_code == 403 - assert resp.json()['error'] == "Permission Denied" - check_project_user(project_data['projectName'], user_id, role='projectMember') - - # as same user - user_id = initial_test_users['AgAdmin']['test_user_id'] - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - resp =client.delete(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(user_id),headers=headers_auth) - assert resp.status_code == 403 - assert resp.json()['details'] == "A user cannot remove oneself from a project." - check_project_user(project_data['projectName'], user_id, role='projectOwner') - - # as project owner - Positive test - user_id = initial_test_users['AgUser2']['test_user_id'] - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - resp =client.delete(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(user_id),headers=headers_auth) - assert resp.status_code == 201 - assert "successfull" in resp.json()['message'] - - # Check get project to ensure deleted user is not present - get_project_response = client.get(UNIT_URL+'?project_id='+str(new_project['projectId']),headers=headers_auth) - found_user = False - - for user in get_project_response.json()[0]['users']: - if user['userId'] == user_id: - found_user = True - assert user['userRole'] is 'projectMember' - assert user['active'] is True - assert user['metaData'] is None - assert not found_user - -def test_restore_user(): - '''positive test case, checking for correct return object''' - #only Super Admin can restore deleted data - #Creating and Deleting project user - project_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - resp = check_post(project_data, auth_token=initial_test_users['AgAdmin']['token']) - assert resp.json()['message'] == "Project created successfully" - # Get project Id - project_id = resp.json()['data']['projectId'] - new_project = resp.json()['data'] - user_id = initial_test_users['AgUser']['test_user_id'] - - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - resp = client.post(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(user_id),headers=headers_auth) - assert resp.json()['message'] == "User added to project successfully" - - # fetch this project and check for new user - check_project_user(project_data['projectName'], user_id, role='projectMember') - - #Delete - # user_id = initial_test_users['AgUser']['test_user_id'] - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - delete_resp = client.delete(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(user_id),headers=headers_auth) - assert delete_resp.status_code == 201 - assert "successfull" in delete_resp.json()['message'] - - #Ensure deleted user is not present - get_project_response = client.get(UNIT_URL+'?project_id='+str(new_project['projectId']),headers=headers_auth) - found_user = False - - for user in get_project_response.json()[0]['users']: - if user['userId'] == user_id: - found_user = True - assert user['userRole'] is 'projectMember' - assert user['active'] is True - assert user['metaData'] is None - assert not found_user - - - - deleteditem_id = delete_resp.json()['data']['itemId'] - data = {"itemId": deleteditem_id} - - #Restoring data - #Restore user without authentication - Negative Test - response = client.put(RESTORE_URL, headers=headers, json=data) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - - #Restore project user with other API user,VachanAdmin,AgAdmin,AgUser,VachanUser,BcsDev and APIUSer2 - Negative Test - for user in ['APIUser','VachanAdmin','AgAdmin','AgUser','VachanUser','BcsDev','APIUser2','VachanContentAdmin','VachanContentViewer']: - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users[user]['token'] - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - - #Restore Project User with Super Admin - Positive Test - # Login as Super Admin - sa_data = { - "user_email": SUPER_USER, - "password": SUPER_PASSWORD - } - response = login(sa_data) - assert response.json()['message'] == "Login Succesfull" - test_user_token = response.json()["token"] - headers_auth['Authorization'] = "Bearer"+" "+test_user_token - - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 201 - assert response.json()['message'] == \ - f"Deleted Item with identity {deleteditem_id} restored successfully" - - #Ensure presence of restored user - check_project_user(project_data['projectName'], user_id, role='projectMember') - - #restore with missing data - Negative Test - data = {} - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert_input_validation_error(response) - - #Restore with invalid item id - Negative Test - data = {"itemId":9999} - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 404 - assert response.json()['error'] == "Requested Content Not Available" - logout_user(test_user_token) - -def test_bugfix_split_n_merged_verse(): - '''BUg fix for https://github.com/Bridgeconn/vachan-api/issues/543''' - post_data = { - "projectName": "Test project usfm upload", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - project_id = response.json()['data']['projectId'] - - prj_book_data = { - "uploadedUSFMs": [ - "\\id REV\n\\c 1\n\\p\n\\v 1 some text\n\\v 2-10 merged text\n\\v 11a split text\n\\v 11b rest" - ] - } - prj_update_resp = client.put(UNIT_URL+'?project_id='+str(project_id),\ - json=prj_book_data, headers=headers_auth) - assert prj_update_resp.status_code == 201 - resp_obj = prj_update_resp.json() - assert resp_obj['message'] == 'Project updated successfully' - - sentences_resp = client.get(f"{SENTENCE_URL}?project_id={project_id}", headers=headers_auth) - assert sentences_resp.status_code == 200 - sentences = sentences_resp.json() - assert len(sentences) == 3 - assert sentences[0]['sentenceId'] == 67001001 - assert sentences[0]['surrogateId'] == 'rev 1:1' - assert sentences[0]["sentence"] == 'some text' - assert sentences[1]['sentenceId'] == 67001002 - assert sentences[1]['surrogateId'] == 'rev 1:2-10' - assert sentences[1]["sentence"] == 'merged text' - assert sentences[2]['sentenceId'] == 67001011 - assert sentences[2]['surrogateId'] == 'rev 1:11a-b' - assert sentences[2]["sentence"] == 'split text rest' - -def test_post_put_app_compatibility(): - '''Positive test to check app compatibility - * Only Autographa app can access translation APIs''' - headers_auth = {"contentType": "application/json", - "accept": "application/json", - "app":"Autographa" - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - - # create with minimum data - post_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - - #Creating new project with out adding compatibleWith field - Positive Test - #app value in the header will be auto assigned as compatibleWith field - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - response.json()['data']['compatibleWith'] = ["Autographa"] - assert_positive_get( response.json()['data']) - - # Creating new project with compatible app which is not passed as a list - Negative Test - post_data['compatibleWith'] = "Autographa" - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert_input_validation_error(response) - - # Creating new project with compatible app passed as a list - Positive Test - post_data["projectName"] = "Test Project 2" - post_data["compatibleWith"] = ["SanketMAST","Autographa"] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - new_project = response.json()['data'] - assert_positive_get(new_project) - - # check if all defaults are coming - assert new_project['metaData']["useDataForLearning"] - assert isinstance(new_project['metaData']['books'], list) - assert len(new_project['metaData']['books']) == 0 - assert new_project['active'] - - # update data with compatible app not passed as a list - negative test - put_data = { - "uploadedUSFMs":[bible_books['mat']] - } - put_data["compatibleWith"] = "Autographa" - response2 = client.put(UNIT_URL+'?project_id='+str(new_project['projectId']),\ - headers=headers_auth, json=put_data) - assert_input_validation_error(response2) - - # update data with compatible app passed as a list - positive test - put_data = { - "uploadedUSFMs":[bible_books['mat']] - } - put_data["compatibleWith"] = ["SanketMAST","VachanAdmin"] - response2 = client.put(UNIT_URL+'?project_id='+str(new_project['projectId']), \ - headers=headers_auth, json=put_data) - assert response2.status_code == 201 - assert response2.json()['message'] == "Project updated successfully" - updated_project = response2.json()['data'] - assert_positive_get(updated_project) - - assert new_project['projectId'] == updated_project['projectId'] - assert new_project['projectName'] == updated_project['projectName'] - assert updated_project['metaData']['books'] == ['mat'] - - # update project with incompatible app - Negative Test - put_data = { - "uploadedUSFMs":[bible_books['mrk']], - "compatibleWith": ["VachanAdmin"] - } - response2 = client.put(UNIT_URL+'?project_id='+str(new_project['projectId']), headers=headers_auth, json=put_data) - assert response2.status_code == 403 - assert response2.json()['details'] == "Incompatible app" - -def test_delete_with_compatible_app(): - '''Test to delete project based on app compatibiblty''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - data_1 = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml", - "compatibleWith": ["Autographa"] - } - data_2 = { - "projectName": "Test project 2", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml", - "compatibleWith": ["SanketMAST","Autographa"] - } - # creating two projects - response1 = client.post(UNIT_URL, headers=headers_auth, json=data_1) - project1 = response1.json()['data'] - project_id_1 = project1['projectId'] - response2 = client.post(UNIT_URL, headers=headers_auth, json=data_2) - project2 = response2.json()['data'] - project_id_2 = project2['projectId'] - - # delete project1 with compatible app - Positive Test - resp = client.delete(UNIT_URL+'?project_id='+str(project_id_1),headers=headers_auth) - assert resp.status_code == 201 - assert resp.json()['message'] == f"Project with identity {project_id_1} deleted successfully" - # update compatibility of project 2 - put_data = { - "compatibleWith": ["VachanAdmin"] - } - response2 = client.put(UNIT_URL+'?project_id='+str(project_id_2), headers=headers_auth, json=put_data) - - # delete project with incompatible app - Negative Test - resp = client.delete(UNIT_URL+'?project_id='+str(project_id_2),headers=headers_auth) - assert resp.status_code == 403 - assert resp.json()['details'] == "Incompatible app" - -def test_get_filter_with_app_compatibility(): - '''Test to filter with app compatibility''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - data_1 = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml", - "compatibleWith": ["Autographa"] - } - data_2 = { - "projectName": "Test project 2", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - data_3 = { - "projectName": "Test project 3", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml", - "compatibleWith": ["SanketMAST","Autographa"] - } - # Creating new project with compatible app - Positive Test - response1 = client.post(UNIT_URL, headers=headers_auth, json=data_1) - # Creating new project without mentioning compatible_with field - Positive test - response2 = client.post(UNIT_URL, headers=headers_auth, json=data_2) - # Creating new project with multiple items in compatible_with field - Positive test - response3 = client.post(UNIT_URL, headers=headers_auth, json=data_3) - - #filtering with single app - passing app not as a list - Negative test - app = "Autographa" - params = [] - for item in app: - params.append(("compatible_with", item)) - get_resp = client.get(UNIT_URL ,params=params,headers=headers_auth) - assert_input_validation_error(get_resp) - - # filtering with single app - positive test - app = ["Autographa"] - params = [] - for item in app: - params.append(("compatible_with", item)) - get_resp = client.get(UNIT_URL ,params=params,headers=headers_auth) - for item in get_resp.json(): - assert_positive_get(item) - assert len(get_resp.json()) == 3 - assert "Autographa" in get_resp.json()[0]['compatibleWith'] - assert "Autographa" in get_resp.json()[1]['compatibleWith'] - assert "Autographa" in get_resp.json()[2]['compatibleWith'] - - #filtering with multiple apps - positive test - app_list = [schema_auth.App.SMAST.value, schema_auth.App.AG.value] - params = [] - for app in app_list: - params.append(("compatible_with", app)) - get_resp2 = client.get(UNIT_URL, params=params, headers=headers_auth) - for item in get_resp2.json(): - assert_positive_get(item) - assert len(get_resp2.json()) == 1 - for item in get_resp2.json(): - assert_positive_get(item) - assert "SanketMAST" in item['compatibleWith'] - assert "Autographa" in item['compatibleWith'] - - #filtering with app not in the list - get_resp = client.get(UNIT_URL + "?compatible_with=VachanAdmin",headers=headers_auth) - assert_not_available_content(get_resp) \ No newline at end of file diff --git a/app/test/test_agmt_translation.py b/app/test/test_agmt_translation.py deleted file mode 100644 index a07efc7b..00000000 --- a/app/test/test_agmt_translation.py +++ /dev/null @@ -1,1106 +0,0 @@ -'''tests for the translation workflow within AgMT projects''' -import json -import re -from math import ceil, floor -from . import client -from . import assert_input_validation_error, assert_not_available_content -from .test_agmt_projects import bible_books, check_post as add_project -from .conftest import initial_test_users -from . test_auth_basic import login,SUPER_PASSWORD,SUPER_USER - - -UNIT_URL = '/v2/text/translate/token-based/project' -headers = {"contentType": "application/json", "accept": "application/json","app":"Autographa"} -headers_auth = {"contentType": "application/json", - "accept": "application/json", - "app":"Autographa" - } - -project_data = { - "projectName": "Test agmt workflow", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" -} - -sample_stopwords = ["के", "की"] - -def assert_positive_get_tokens(item): - '''common tests for a token response object''' - assert "token" in item - assert "occurrences" in item - assert len(item['occurrences']) > 0 - assert "translations" in item - for trans in item['translations']: - assert isinstance(item['translations'][trans], (int, float)) - if "metaData" in item and item['metaData'] is not None: - assert isinstance(item['metaData'], dict) - -def assert_positive_get_sentence(item): - '''common test for senstence object''' - assert "sentenceId" in item - assert "sentence" in item - assert isinstance(item['sentence'], str) - if "draft" in item: - assert isinstance(item['draft'], str) - assert "draftMeta" in item - assert isinstance(item['draftMeta'], list) - -def test_get_tokens(): - '''Positive tests for tokenization process''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - resp = add_project(project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - # before adding books - get_response1 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id),headers=headers_auth) - assert_not_available_content(get_response1) - - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - # after adding books - #without auth - get_response2 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id), headers=headers) - assert get_response2.json()['error'] == 'Authentication Error' - get_response2 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id),headers=headers) - assert get_response2.status_code == 401 - assert get_response2.json()['error'] == 'Authentication Error' - #with auth - get_response2 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id),headers=headers_auth) - assert get_response2.status_code == 200 - assert isinstance(get_response2.json(), list) - for item in get_response2.json(): - assert_positive_get_tokens(item) - - # with book filter - get_response3 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+"&books=mat" - ,headers=headers_auth) - assert get_response3.status_code == 200 - assert len(get_response3.json()) < len(get_response2.json()) - - get_response4 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+"&books=mrk" - ,headers=headers_auth) - assert get_response4.status_code == 200 - all_tokens = [item['token'] for item in get_response3.json() + get_response4.json()] - assert len(get_response2.json()) == len(set(all_tokens)) - - # with range filter - get_response5 = client.get(UNIT_URL+'/tokens?project_id='+str(project_id)+ - "&sentence_id_range=0&sentence_id_range=10",headers=headers_auth) - # print(get_response5.json()) - assert_not_available_content(get_response5) - - get_response6 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&sentence_id_range=41000000&sentence_id_range=41999999",headers=headers_auth) - assert get_response6.status_code ==200 - assert get_response6.json() == get_response3.json() - - # with list filter - get_response7 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&sentence_id_list=41000000",headers=headers_auth) - assert_not_available_content(get_response7) - - get_response7 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&sentence_id_list=41001001",headers=headers_auth) - assert get_response7.status_code == 200 - assert 0 < len(get_response7.json()) < 25 - - # translation_memory flag - get_response8 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&use_translation_memory=true",headers=headers_auth) - assert get_response8.json() == get_response2.json() - - get_response9 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&use_translation_memory=false",headers=headers_auth) - assert get_response9.status_code == 200 - assert len(get_response9.json()) > 0 - - # include_phrases flag - get_response10 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&include_phrases=true",headers=headers_auth) - assert get_response10.json() == get_response2.json() - - get_response11 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&include_phrases=false",headers=headers_auth) - assert get_response11.status_code == 200 - assert len(get_response11.json()) <= len(get_response10.json()) - for item in get_response11.json(): - assert " " not in item['token'] - - # include_stopwords flag - get_response12 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&include_stopwords=false",headers=headers_auth) - assert get_response12.json() == get_response2.json() - for item in get_response12.json(): - assert item['token'] not in sample_stopwords - - get_response13 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&include_stopwords=True&include_phrases=false",headers=headers_auth) - all_tokens = [item['token'] for item in get_response13.json()] - assert sample_stopwords[0] in all_tokens - - -def test_tokenization_invalid(): - '''Negative tests for tokenization''' - resp = add_project(project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - # non existant project - response = client.get(UNIT_URL+"/tokens?project_id="+str(project_id+1),headers=headers_auth) - assert response.status_code == 404 - assert response.json()['details'] == "Project with id, %s, not present"%(project_id+1) - - #invalid book - response = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+"&books=mmm" - ,headers=headers_auth) - assert response.status_code == 404 - assert response.json()['details'] == 'Book, mmm, not in database' - - # only one value for range - response = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&sentence_id_range=41000000",headers=headers_auth) - assert_input_validation_error(response) - - # incorrect value for range - response = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&sentence_id_range=gen&sentence_id_range=num",headers=headers_auth) - assert_input_validation_error(response) - - # incorrect value for id - response = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&sentence_id_list=first",headers=headers_auth) - assert_input_validation_error(response) - - # incorrect value for flags - response = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&include_stopwords=Few",headers=headers_auth) - assert_input_validation_error(response) - - response = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&include_phrases=10",headers=headers_auth) - assert_input_validation_error(response) - - response = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&use_translation_memory=always",headers=headers_auth) - assert_input_validation_error(response) - -def test_save_translation(): - '''Positive tests for PUT tokens method''' - resp = add_project(project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - resp = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&include_stopwords=true",headers=headers_auth) - assert resp.status_code == 200 - all_tokens = resp.json() - - # one occurence - post_obj_list = [ - { - "token": all_tokens[0]['token'], - "occurrences": [all_tokens[0]["occurrences"][0]], - "translation": "test translation" - } - ] - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert response.status_code == 201 - assert response.json()['message'] == 'Token translations saved' - assert response.json()['data'][0]['draft'].startswith("test translation") - assert response.json()['data'][0]['draftMeta'][0][2] == "confirmed" - for segment in response.json()['data'][0]['draftMeta'][1:]: - assert segment[2] != "confirmed" - - # multiple occurances - post_obj_list = [ - { - "token": all_tokens[0]['token'], - "occurrences": all_tokens[0]["occurrences"], - "translation": "test translation" - } - ] - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert response.status_code == 201 - assert response.json()['message'] == 'Token translations saved' - for sent in response.json()['data']: - assert "test translation" in sent['draft'] - - #Without auth - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - json=post_obj_list, headers=headers) - assert response.json()['error'] == 'Authentication Error' - - # all tokens at once - post_obj_list = [] - for item in all_tokens: - obj = { - "token": item['token'], - "occurrences": item["occurrences"], - "translation": "test" - } - post_obj_list.append(obj) - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert response.status_code == 201 - for sent in response.json()['data']: - words = re.findall(r'\w+', sent['draft']) - for wrd in words: - assert wrd == 'test' - -def test_save_translation_invalid(): - '''Negative tests for PUT tokens method''' - resp = add_project(project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - resp = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&include_stopwords=true",headers=headers_auth) - assert resp.status_code == 200 - all_tokens = resp.json() - - # incorrect project id - obj = { - "token": all_tokens[0]['token'], - "occurrences": all_tokens[0]["occurrences"], - "translation": "sample translation" - } - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id+1), - headers=headers_auth, json=[obj]) - assert response.status_code == 404 - assert response.json()['details'] == 'Project with id, %s, not present'%(project_id+1) - - # without token - obj = { - "occurrences": all_tokens[0]["occurrences"], - "translation": "sample translation" - } - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id+1), - headers=headers_auth, json=[obj]) - assert_input_validation_error(response) - - # without occurences - obj = { - "token": all_tokens[0]['token'], - "translation": "sample translation" - } - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id+1), - headers=headers_auth, json=[obj]) - assert_input_validation_error(response) - - # without translation - obj = { - "token": all_tokens[0]['token'], - "occurrences": all_tokens[0]["occurrences"] - } - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id+1), - headers=headers_auth, json=[obj]) - assert_input_validation_error(response) - - # incorrect occurences - wrong_occur = all_tokens[0]["occurrences"][0] - wrong_occur['sentenceId'] = 0 - obj = { - "token": all_tokens[0]['token'], - "occurrences": [wrong_occur], - "translation": "sample translation" - } - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=[obj]) - assert response.status_code ==404 - assert response.json()['details'] == "Sentence id, 0, not found for the selected project" - - # wrong token-occurence pair - obj = { - "token": all_tokens[0]['token'], - "occurrences": all_tokens[1]['occurrences'], - "translation": "sample translation" - } - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=[obj]) - assert response.status_code == 500 - assert response.json()['details'] ==\ - 'Token, %s, and its occurence, not matching'%(all_tokens[0]['token']) - -def test_drafts(): - '''End to end test from tokenization to draft generation''' - resp = add_project(project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - resp = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&include_stopwords=true",headers=headers_auth) - assert resp.status_code == 200 - all_tokens = resp.json() - - # translate all tokens at once - post_obj_list = [] - for item in all_tokens: - obj = { - "token": item['token'], - "occurrences": item["occurrences"], - "translation": "test" - } - post_obj_list.append(obj) - resp = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert resp.status_code == 201 - assert resp.json()['message'] == "Token translations saved" - - response = client.get(UNIT_URL+'/draft?project_id='+str(project_id),headers=headers_auth) - assert response.status_code ==200 - assert isinstance(response.json(), list) - assert "\\v 1 test test test" in response.json()[0] - - response = client.get(UNIT_URL+'/draft?project_id='+str(project_id)+ - "&books=mat",headers=headers_auth) - assert len(response.json()) == 1 - assert "\\id MAT" in response.json()[0] - - #Without Auth - response = client.get(UNIT_URL+'/draft?project_id='+str(project_id)+ - "&books=mat", headers=headers) - assert response.json()['error'] == 'Authentication Error' - response = client.get(UNIT_URL+'/draft?project_id='+str(project_id)+ - "&books=mat",headers=headers) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - - # To be added: proper tests for alignment json drafts - response = client.get(UNIT_URL+'/draft?project_id='+str(project_id)+ - "&output_format=alignment-json",headers=headers_auth) - assert response.status_code == 200 - assert isinstance(response.json(), dict) - -def test_get_token_sentences(): - '''Check if draft-meta is properly segemneted according to specifed token occurence''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - resp = add_project(project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - resp = client.get("/v2/text/translate/token-based/project/tokens?project_id="+str(project_id) - ,headers=headers_auth) - tokens = resp.json() - our_token = tokens[0]['token'] - occurrences = tokens[0]['occurrences'] - - #before translating - response = client.put('/v2/text/translate/token-based/project/token-sentences?project_id='+ - str(project_id)+'&token='+our_token, - json=occurrences, headers=headers_auth) - assert response.status_code == 200 - for sent, occur in zip(response.json(), occurrences): - assert_positive_get_sentence(sent) - found_slice = False - if sent['sentenceId'] == occur["sentenceId"]: - for meta in sent['draftMeta']: - if meta[0] == occur['offset']: - found_slice = True - assert found_slice - - post_obj_list = [ - { - "token": our_token, - "occurrences": occurrences, - "translation": "sample" - } - ] - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert response.status_code == 201 - assert response.json()['message'] == 'Token translations saved' - - # after translation - response2 = client.put('/v2/text/translate/token-based/project/token-sentences?project_id='+ - str(project_id)+'&token='+our_token, headers=headers_auth, - json=occurrences) - assert response2.status_code == 200 - for sent, occur in zip(response2.json(), occurrences): - found_slice = False - if sent['sentenceId'] == occur["sentenceId"]: - for meta in sent['draftMeta']: - if meta[0] == occur['offset']: - found_slice = True - assert meta[2] == "confirmed" - assert found_slice - - #Without auth - response2 = client.put('/v2/text/translate/token-based/project/token-sentences?project_id='+ - str(project_id)+'&token='+our_token, json=occurrences) - assert response2.json()['error'] == 'Permission Denied' - -def test_get_sentence(): - '''Positive test for agmt sentence/draft fetch API''' - resp = add_project(project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - # before adding books - response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id) - ,headers=headers_auth) - assert_not_available_content(response) - - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - # before translation - response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&with_draft=True",headers=headers_auth) - assert response.status_code ==200 - assert len(response.json()) > 1 - for item in response.json(): - assert_positive_get_sentence(item) - assert item['sentence'] != "" - assert item['draft'] == "" - - - # translate all tokens at once - resp = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&include_stopwords=true",headers=headers_auth) - assert resp.status_code == 200 - all_tokens = resp.json() - post_obj_list = [] - for item in all_tokens: - obj = { - "token": item['token'], - "occurrences": item["occurrences"], - "translation": "test" - } - post_obj_list.append(obj) - resp = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert resp.status_code == 201 - assert resp.json()['message'] == "Token translations saved" - - # after token translation - response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&with_draft=True",headers=headers_auth) - assert response.status_code ==200 - for item in response.json(): - assert_positive_get_sentence(item) - words = re.findall(r'\w+',item['draft']) - for wrd in words: - assert wrd == "test" - - #without auth - response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&with_draft=True", headers=headers) - assert response.json()['error'] == 'Authentication Error' - response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&with_draft=True",headers=headers) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - - # With only_ids - response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&only_ids=True",headers=headers_auth) - assert response.status_code ==200 - for item in response.json(): - assert "sentenceId" in item - assert "surrogateId" in item - assert "sentence" not in item - assert "draft" not in item - assert "draftMeta" not in item - -def test_progress_n_suggestion(): - '''tests for project progress API of AgMT''' - resp = add_project(project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - # before adding books - response = client.get(UNIT_URL+"/progress?project_id="+str(project_id) - ,headers=headers_auth) - assert response.status_code ==200 - assert response.json()['confirmed'] == 0 - assert response.json()['untranslated'] == 0 - assert response.json()['suggestion'] == 0 - - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - # before translation - response = client.get(UNIT_URL+"/progress?project_id="+str(project_id) - ,headers=headers_auth) - assert response.status_code ==200 - assert response.json()['confirmed'] == 0 - assert response.json()['untranslated'] == 1 - assert response.json()['suggestion'] == 0 - - # # Apply suggestions - # resp = client.put(UNIT_URL+"/suggestions?project_id="+str(project_id)) - # assert resp.status_code ==201 - # found_suggestion = False - # [print(sent['draft']) for sent in resp.json()] - # for sent in resp.json(): - # for meta in sent['draftMeta']: - # if meta[2] == 'suggestion': - # found_suggestion = True - # break - # assert found_suggestion - - # # after suggestions - # response = client.get(UNIT_URL+"/progress?project_id="+str(project_id)) - # assert response.status_code ==200 - # assert response.json()['suggestion'] > 0 - - # translate all tokens at once - resp = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&include_stopwords=true",headers=headers_auth) - assert resp.status_code == 200 - all_tokens = resp.json() - post_obj_list = [] - for item in all_tokens: - obj = { - "token": item['token'], - "occurrences": item["occurrences"], - "translation": "test" - } - post_obj_list.append(obj) - resp = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert resp.status_code == 201 - assert resp.json()['message'] == "Token translations saved" - - # after token translation - response = client.get(UNIT_URL+"/progress?project_id="+str(project_id) - ,headers=headers_auth) - assert response.status_code ==200 - assert ceil(response.json()['confirmed']) == 1 - assert floor(response.json()['untranslated']) == 0 - - #Without Auth - response = client.get(UNIT_URL+"/progress?project_id="+str(project_id), headers=headers) - assert response.json()['error'] == 'Authentication Error' - response = client.get(UNIT_URL+"/progress?project_id="+str(project_id) - ,headers=headers) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - -def test_get_versification(): - '''Positive test for agmt sentence/draft fetch API''' - resp = add_project(project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - # before adding books - response = client.get(UNIT_URL+"/versification?project_id="+str(project_id) - ,headers=headers_auth) - for key in response.json(): - assert len(response.json()[key]) == 0 - - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - response = client.get(UNIT_URL+"/versification?project_id="+str(project_id) - ,headers=headers_auth) - found_mat = False - found_mrk = False - for book in response.json()['maxVerses']: - if book == "mat": - found_mat = True - if book == "mrk": - found_mrk = True - assert found_mat and found_mrk - - #without auth - response = client.get(UNIT_URL+"/versification?project_id="+str(project_id), headers=headers) - assert response.json()['error'] == 'Authentication Error' - #without auth but from Autographa - response = client.get(UNIT_URL+"/versification?project_id="+str(project_id) - ,headers=headers) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - -#Project translation Access Rules based tests -def test_agmt_translation_access_rule_app(): - """project translation related access rule and auth""" - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - #create a project - resp = add_project(project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - #get tokens - #without auth not from Autographa - resp = client.get("/v2/text/translate/token-based/project/tokens?project_id="+str(project_id)) - assert resp.json()['error'] == "Permission Denied" - #without auth and from Autographa - resp = client.get("/v2/text/translate/token-based/project/tokens?project_id="+str(project_id) - ,headers=headers) - assert resp.status_code == 401 - assert resp.json()['error'] == 'Authentication Error' - #With Auth and From Autographa - resp = client.get("/v2/text/translate/token-based/project/tokens?project_id="+str(project_id) - ,headers=headers_auth) - assert resp.status_code == 200 - assert isinstance(resp.json(), list) - for item in resp.json(): - assert_positive_get_tokens(item) - - #Apply token translations PUT - all_tokens = resp.json() - our_token = all_tokens[0]['token'] - occurrences = all_tokens[0]['occurrences'] - - post_obj_list = [ - { - "token": all_tokens[0]['token'], - "occurrences": [all_tokens[0]["occurrences"][0]], - "translation": "test translation" - } - ] - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert response.status_code == 201 - assert response.json()['message'] == 'Token translations saved' - #Wihout Auth from Autographa - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers, json=post_obj_list) - assert response.json()['error'] == "Authentication Error" - #Outside Autographa wihtou Auth - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - json=post_obj_list) - assert response.json()['error'] == "Permission Denied" - - #get token translation - ################################################################################## - ######################################## - data_str = json.dumps(post_obj_list) - params={ - "project_id": str(project_id), - "token": all_tokens[0]['token'], - "sentence_id": "41001001", - "offset": ["0", "4"], - "data": data_str - } - - response = client.get(UNIT_URL + "/token-translations", - params=params, - headers=headers_auth) - assert response.status_code == 200 - assert len(response.json()) > 0 - #Without Auth from Autographa - - response = client.get(UNIT_URL + "/token-translations", - params=params, - headers=headers - ) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - #Without Auth and not from Autographa - - response = client.get( - UNIT_URL + "/token-translations", - params=params) - assert response.json()['error'] == "Permission Denied" - - #Get Token Sentences PUT - response = client.put('/v2/text/translate/token-based/project/token-sentences?project_id='+ - str(project_id)+'&token='+our_token, - json=occurrences, headers=headers_auth) - assert response.status_code == 200 - #Without Auth and from Autographa - response = client.put('/v2/text/translate/token-based/project/token-sentences?project_id='+ - str(project_id)+'&token='+our_token, - json=occurrences, headers=headers) - assert response.status_code == 401 - assert response.json()['error'] == "Authentication Error" - #Without Auth and not from Autographa - response = client.put('/v2/text/translate/token-based/project/token-sentences?project_id='+ - str(project_id)+'&token='+our_token, - json=occurrences) - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" - - #Get Draft - response = client.get(UNIT_URL+'/draft?project_id='+str(project_id)+ - "&output_format=alignment-json",headers=headers_auth) - assert response.status_code == 200 - #Without Auth and From Autographa - response = client.get(UNIT_URL+'/draft?project_id='+str(project_id)+ - "&output_format=alignment-json",headers=headers) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - #Without Auth and not From Autographa - response = client.get(UNIT_URL+'/draft?project_id='+str(project_id)+ - "&output_format=alignment-json") - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" - - #Project Resource Get - response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&with_draft=True",headers=headers_auth) - assert response.status_code ==200 - #Without Auth and From Autographa - response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&with_draft=True",headers=headers) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - #Without Auth and not From Autographa - response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&with_draft=True") - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" - - #Project Progress - response = client.get(UNIT_URL+"/progress?project_id="+str(project_id), - headers=headers_auth) - assert response.status_code ==200 - #Without Auth and From Autographa - response = client.get(UNIT_URL+"/progress?project_id="+str(project_id), - headers=headers) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - #Without Auth and not From Autographa - response = client.get(UNIT_URL+"/progress?project_id="+str(project_id)) - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" - - #Project Versification - response = client.get(UNIT_URL+"/versification?project_id="+str(project_id) - ,headers=headers_auth) - assert response.status_code ==200 - #Without Auth and From Autographa - response = client.get(UNIT_URL+"/versification?project_id="+str(project_id) - ,headers=headers) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - #Without Auth and not From Autographa - response = client.get(UNIT_URL+"/versification?project_id="+str(project_id)) - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" - -def test_data_updated_time(): - '''Bugfix of issue #563:https://github.com/Bridgeconn/vachan-api/issues/563 ''' - - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - - #create an empty project - create_project_resp = add_project(project_data) - assert create_project_resp.json()['message'] == "Project created successfully" - project_id = create_project_resp.json()['data']['projectId'] - project_name =create_project_resp.json()['data']['projectName'] - project_create_time = create_project_resp.json()['data']['createTime'] - - # Add book into empty project - put_data = { - "uploadedUSFMs":[bible_books['mat']] - } - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - # Get tokens - resp = client.get("/v2/text/translate/token-based/project/tokens?project_id="+str(project_id) - ,headers=headers_auth) - - # Case 1:PUT Tokens - #Apply token translations - all_tokens = resp.json() - post_obj_list = [ - { - "token": all_tokens[0]['token'], - "occurrences": [all_tokens[0]["occurrences"][0]], - "translation": "test translation" - } - ] - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - - # Get project details - project_url = f"/v2/text/translate/token-based/projects?project_name={project_name}" - project_resp = client.get(project_url, headers=headers_auth) - project_update_time = project_resp.json()[0]['updateTime'] - assert not project_create_time == project_update_time - - # Check sentences are added - resp = client.get(f"{UNIT_URL}/sentences?project_id={project_id}", headers=headers_auth) - sentence_id = resp.json()[0]['sentenceId'] - - # Case 2 : PUT Draft - put_data = [ - {"sentenceId":sentence_id, - "draft": "കാട്", - "draftMeta":[ - [ [5,11], [0,4], "confirmed"] - ] - } - ] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data) - # Get project details - project_resp = client.get(project_url, headers=headers_auth) - project_update_time = project_resp.json()[0]['updateTime'] - assert not project_create_time == project_update_time - - #Case 3: Update suggestions - resp = client.put(f"/v2/text/translate/token-based/project/suggestions?project_id={project_id}&sentenceIdList={sentence_id}", - headers=headers_auth) - assert resp.status_code == 201 - # Get project details - project_resp = client.get(project_url, headers=headers_auth) - project_update_time = project_resp.json()[0]['updateTime'] - assert not project_create_time == project_update_time - - # Case 4 : Delete Sentence - response = client.delete(f"{UNIT_URL}/sentences?project_id={project_id}&sentence_id={sentence_id}", - headers=headers_auth) - assert response.status_code == 201 - assert "successfull" in response.json()['message'] - # Get project details - project_resp = client.get(project_url, headers=headers_auth) - project_update_time = project_resp.json()[0]['updateTime'] - assert not project_create_time == project_update_time - - -#Project translation Access rules based permission -def test_agmt_translation_access_permissions(): - """test for access permission to project translation""" - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - #create a project - resp = add_project(project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - resp = client.get("/v2/text/translate/token-based/project/tokens?project_id="+str(project_id) - ,headers=headers_auth) - assert resp.status_code == 200 - assert isinstance(resp.json(), list) - for item in resp.json(): - assert_positive_get_tokens(item) - - #Apply token translations PUT - all_tokens = resp.json() - our_token = all_tokens[0]['token'] - occurrences = all_tokens[0]['occurrences'] - - post_obj_list = [ - { - "token": all_tokens[0]['token'], - "occurrences": [all_tokens[0]["occurrences"][0]], - "translation": "test translation" - } - ] - - #create a AgUser and add as memeber to a project and SA - #Super Admin - SA_user_data = { - "user_email": SUPER_USER, - "password": SUPER_PASSWORD - } - response = login(SA_user_data) - assert response.json()['message'] == "Login Succesfull" - test_SA_token = response.json()["token"] - - #PUT Permission in agmt translations - #"SuperAdmin", "AgAdmin", "projectOwner", "projectMember" - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert response.status_code == 201 - assert response.json()['message'] == 'Token translations saved' - - headers_auth['Authorization'] = "Bearer"+" "+test_SA_token - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert response.status_code == 201 - assert response.json()['message'] == 'Token translations saved' - - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser2']['token'] - #PUT with Aguser not a member - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" - - response = client.put('/v2/text/translate/token-based/project/token-sentences?project_id='+ - str(project_id)+'&token='+our_token, - json=occurrences, headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" - - #Add AgUser as memeber to projects - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - response = client.post('/v2/text/translate/token-based/project/user'+'?project_id='+str(project_id)+ - '&user_id='+str(initial_test_users['AgUser2']["test_user_id"]),headers=headers_auth) - assert response.status_code == 201 - assert response.json()['message'] == "User added to project successfully" - - #After adding as member PUT - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser2']['token'] - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert response.status_code == 201 - print("resp after put token:",resp.json()) - - response = client.put('/v2/text/translate/token-based/project/token-sentences?project_id='+ - str(project_id)+'&token='+our_token, - json=occurrences, headers=headers_auth) - assert response.status_code == 200 - - #GET Permission in agmt translations - #"SuperAdmin", "AgAdmin", "projectOwner", "projectMember", "BcsDeveloper" - token_list = [] - token_list.append(test_SA_token) - token_list.append(initial_test_users['AgUser2']['token']) - token_list.append(initial_test_users['AgAdmin']['token']) - token_list.append(initial_test_users['BcsDev']['token']) - token_list.append(initial_test_users['AgAdmin']['token']) - - for user_token in token_list: - headers_auth['Authorization'] = "Bearer"+" "+user_token - resp = client.get("/v2/text/translate/token-based/project/tokens?project_id="+str(project_id) - ,headers=headers_auth) - assert resp.status_code == 200 - assert isinstance(resp.json(), list) - for item in resp.json(): - assert_positive_get_tokens(item) - - data_str = json.dumps(post_obj_list) - response = client.get( - UNIT_URL + "/token-translations", - params={ - "project_id": str(project_id), - "token": all_tokens[0]['token'], - "sentence_id": "41001001", - "offset": ["0", "4"], - "data": data_str - }, - headers=headers_auth) - assert response.status_code == 200 - assert len(response.json()) > 0 - - response = client.get(UNIT_URL+'/draft?project_id='+str(project_id)+ - "&output_format=alignment-json",headers=headers_auth) - assert response.status_code == 200 - assert len(response.json()) > 0 - - response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&with_draft=True",headers=headers_auth) - assert response.status_code ==200 - assert len(response.json()) > 0 - - response = client.get(UNIT_URL+"/progress?project_id="+str(project_id), - headers=headers_auth) - assert response.status_code ==200 - assert len(response.json()) > 0 - - response = client.get(UNIT_URL+"/versification?project_id="+str(project_id) - ,headers=headers_auth) - assert response.status_code ==200 - assert len(response.json()) > 0 - - #Not getting Content - #Aguser is not member of project - token_list = [] - token_list.append(initial_test_users['VachanUser']['token']) - token_list.append(initial_test_users['AgUser']['token']) - token_list.append(initial_test_users['APIUser']['token']) - token_list.append(initial_test_users['VachanAdmin']['token']) - token_list.append(initial_test_users['VachanContentAdmin']['token']) - token_list.append(initial_test_users['VachanContentViewer']['token']) - - for user_token in token_list: - headers_auth['Authorization'] = "Bearer"+" "+user_token - resp = client.get("/v2/text/translate/token-based/project/tokens?project_id="+str(project_id) - ,headers=headers_auth) - assert resp.status_code == 403 - assert resp.json()['error'] == "Permission Denied" - - data_str = json.dumps(post_obj_list) - response = client.get( - UNIT_URL + "/token-translations", - params={ - "project_id": str(project_id), - "token": all_tokens[0]['token'], - "sentence_id": "41001001", - "offset": ["0", "4"], - "data": data_str - }, - headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" - - response = client.get(UNIT_URL+'/draft?project_id='+str(project_id)+ - "&output_format=alignment-json",headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" - - response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&with_draft=True",headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" - - response = client.get(UNIT_URL+"/progress?project_id="+str(project_id), - headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" - - response = client.get(UNIT_URL+"/versification?project_id="+str(project_id) - ,headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" \ No newline at end of file diff --git a/app/test/test_agmt_translation2.py b/app/test/test_agmt_translation2.py deleted file mode 100644 index 44166390..00000000 --- a/app/test/test_agmt_translation2.py +++ /dev/null @@ -1,690 +0,0 @@ -'''tests for the translation workflow within AgMT projects continued''' -import json -from . import client -from .test_agmt_projects import check_post as add_project -from .conftest import initial_test_users -from . import assert_input_validation_error, assert_not_available_content -from . test_agmt_translation import UNIT_URL, assert_positive_get_sentence -from . test_auth_basic import login,SUPER_PASSWORD,SUPER_USER,logout_user - -RESTORE_URL = '/v2/admin/restore' -headers_auth = {"contentType": "application/json", - "accept": "application/json", - "app":"Autographa" - } -headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - -project_data = { - "projectName": "Test agmt draft", - "sourceLanguageCode": "en", - "targetLanguageCode": "ml" -} - -source_sentences = [ - {"sentenceId": 100, - "sentence": "In a jungle far away there lived a fox"}, - {"sentenceId": 101, - "sentence": "The fox was friends with a tiger."}, - {"sentenceId": 102, - "sentence": "They used to play together."}, - {"sentenceId": 103, - "sentence": "One day the fox wished he had tiger's striped coat."}, - {"sentenceId": 104, - "sentence": "The good friend tiger lent it to him."}, - {"sentenceId": 105, - "sentence": "Tharjima illatha thettu vaakkukal mathram."}, - {"sentenceId": 106, - "sentence": "Oru jungle allathe mattellam tharjima illatha thettu vaakkukal mathram."} -] - -def test_draft_update_positive(): - '''Positive test for updating draft and draftMeta(Alignment) in a project''' - resp = add_project(project_data, auth_token=initial_test_users['AgUser']['token']) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "sentenceList":source_sentences - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - resp = client.get(f"{UNIT_URL}/sentences?project_id={project_id}", headers=headers_auth) - for sent in resp.json(): - assert_positive_get_sentence(sent) - - # translate just one word - put_data = [ - {"sentenceId":100, - "draft": "കാട്", - "draftMeta":[ - [ [5,11], [0,4], "confirmed"] - ] - } - ] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data) - sents = resp.json() - assert len(sents) == 1 - assert_positive_get_sentence(sents[0]) - assert sents[0]["draft"] == "കാട്" - assert sents[0]["draftMeta"] == put_data[0]['draftMeta'] - - #fetch sentences to make sure - data_str = json.dumps(put_data) - resp = client.get( - "/v2/text/translate/token-based/project/sentences", - params={ - "project_id": project_id, - "sentence_id_list": 100, - "with_draft": True, - "data": data_str - }, - headers=headers_auth, - ) - sents = resp.json() - print(sents) - assert sents[0]["draft"] == "കാട്" - assert sents[0]["draftMeta"] == put_data[0]['draftMeta'] - - # edit the existing draft - put_data2 = [ - {"sentenceId":100, - "draft": "ഒരു കാട്ടില്‍ ഒരു കുറുക്കന്‍ ജീവിച്ചിരുന്നു", - "draftMeta":[ - [ [3,4], [0,3], "confirmed"], - [ [5,5], [3,4], "confirmed"], - [ [5,11], [4,11], "confirmed"], - [ [11,11], [11,13], "confirmed"], - [ [32,33], [13,16], "confirmed"], - [ [33,33], [16,18], "confirmed"], - [ [35,38], [18,26], "confirmed"], - [ [38,38], [26,43], "untranslated"], - ] - } - ] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data2) - sents = resp.json() - print(sents) - assert_positive_get_sentence(sents[0]) - assert sents[0]["draft"] == "ഒരു കാട്ടില്‍ ഒരു കുറുക്കന്‍ ജീവിച്ചിരുന്നു" - assert sents[0]["draftMeta"] == put_data2[0]['draftMeta'] - - #fetch sentences again - data_str2 = json.dumps(put_data2) # Convert put_data to JSON - - resp = client.get( - "/v2/text/translate/token-based/project/sentences", - params={ - "project_id": project_id, - "sentence_id_list": 100, - "with_draft": True, - "data2":data_str2 - }, - headers=headers_auth - ) - sents = resp.json() - assert sents[0]["draft"] == put_data2[0]['draft'] - assert sents[0]["draftMeta"] == put_data2[0]['draftMeta'] - -def test_draft_update_negative(): - '''Checking effective validations and error messages''' - resp = add_project(project_data, auth_token=initial_test_users['AgUser']['token']) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - # incorrect project id - put_data = [ - {"sentenceId":100, - "draft": "കാട്", - "draftMeta":[ - [ [5,11], [0,4], "confirmed"] - ] - } - ] - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id+1}", - headers=headers_auth, json=put_data) - assert resp.json()['details'] == f"Project with id, {project_id+1}, not present" - - # non existing sentence - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data) - assert resp.json()['error'] == "Requested Content Not Available" - - # upload source and repeat last action - put_data_source = { - "sentenceList":source_sentences - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data_source) - assert resp.json()['message'] == "Project updated successfully" - - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data) - assert resp.json()[0]["draft"] == put_data[0]['draft'] - - # incorrect user - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser2']['token'] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data) - assert resp.json()['error'] == "Permission Denied" - - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - - # incorrect payloads - put_data2 = [ - { - # "sentenceId":100, no sentenceId - "draft": "കാട്", - "draftMeta":[ - [ [5,11], [0,4], "confirmed"] - ] - } - ] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data2) - assert resp.status_code == 422 - - put_data2 = [ - { - "sentenceId":100, - # "draft": "കാട്", no draft - "draftMeta":[ - [ [5,11], [0,4], "confirmed"] - ] - } - ] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data2) - assert resp.status_code == 422 - - put_data2 = [ - { - "sentenceId":100, - "draft": "കാട്", - # "draftMeta":[ no draft - # [ [5,11], [0,4], "confirmed"] - # ] - } - ] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data2) - assert resp.status_code == 422 - - # incorrect index of target segment - put_data2 = [ - { - "sentenceId":100, - "draft": "കാട്", - "draftMeta":[ - [ [5,11], [0,10], "confirmed"] - ] - } - ] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data2) - assert resp.status_code == 422 - assert resp.json()['details'] == "Incorrect metadata:Target segment [0, 10], is improper!" - - -def test_empty_draft_initalization(): - '''Bugfix test for #452 after the changes in #448''' - resp = add_project(project_data, auth_token=initial_test_users['AgUser']['token']) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "sentenceList":source_sentences - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - # Ensure draft is set to "" - resp = client.get(f"{UNIT_URL}/sentences?project_id={project_id}&with_draft=True", - headers=headers_auth) - for sent in resp.json(): - assert_positive_get_sentence(sent) - assert sent["draft"] == "" - - #Ensure draft if still empty when there is no suggestion - resp = client.put(f"{UNIT_URL}/suggestions?project_id={project_id}&sentence_id_list={105}", - headers=headers_auth) - assert resp.status_code == 201 - assert resp.json()[0]['draft'] == "" - - # Add a gloss to make sure there will be suggestions - tokens_trans = [ - {"token":"jungle", "translations":["കാട്"]} - ] - response = client.post('/v2/nlp/gloss?source_language=en&target_language=ml', - headers=headers_auth, json=tokens_trans) - assert response.status_code == 201 - assert response.json()['message'] == "Added to glossary" - - resp = client.put(f"{UNIT_URL}/suggestions?project_id={project_id}&sentence_id_list=100"+\ - "&sentence_id_list=106", - headers=headers_auth) - assert resp.status_code == 201 - assert resp.json()[1]['draft'] == "കാട്" # only one suggestion - assert "കാട്" in resp.json()[0]['draft'] # at least one suggestion - found_jungle_meta = False - found_untranslated = False - for meta in resp.json()[0]['draftMeta']: - if meta[0] == [5,11] and meta[2] == "suggestion": - found_jungle_meta = True - print("draft:", resp.json()[0]['draft']) - print("draft.index('കാട്'')",resp.json()[0]['draft'].index("കാട്")) - trans_index = resp.json()[0]['draft'].index("കാട്") - assert meta[1][0] == trans_index - elif meta[2] == "untranslated": - found_untranslated = True - assert found_jungle_meta - assert found_untranslated - -def test_draft_meta_validation(): - '''Bugfix test for #479 after the changes in PR #481''' - resp = add_project(project_data, auth_token=initial_test_users['AgUser']['token']) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "sentenceList":source_sentences - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - #Get suggestions - resp = client.put(f"/v2/text/translate/token-based/project/suggestions?project_id={project_id}&sentenceIdList=100", - headers=headers_auth) - assert resp.status_code == 201 - resp_draft_meta = resp.json()[0]['draftMeta'] - print("draftMeta:", resp_draft_meta) - empty_seg = False - for seg in resp_draft_meta: - if seg[0][0] == seg[0][1] or seg[1][0] == seg[1][1]: - empty_seg = True - break - # assert empty_seg - - # PUT the draft meta back to server - json_data = [{ - "sentenceId":100, - "draft":resp.json()[0]['draft'], - "draftMeta":resp_draft_meta - }] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=json_data) - assert resp.status_code == 201 - -def test_space_in_suggested_draft(): - '''BUgfix text for #485, after changes in PR #486''' - resp = add_project(project_data, auth_token=initial_test_users['AgUser']['token']) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "sentenceList":source_sentences - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - # Add a gloss to ensure some suggestion in output - tokens_trans = [ - {"token":"jungle", "translations":["കാട്"]}, - {"token":"far", "translations":["ദൂരെ"]}, - {"token":"fox", "translations":["കുറുക്കന്‍"]} - ] - response = client.post('/v2/nlp/gloss?source_language=en&target_language=ml', - headers=headers_auth, json=tokens_trans) - assert response.status_code == 201 - assert response.json()['message'] == "Added to glossary" - - - #Get suggestions - resp = client.put(f"/v2/text/translate/token-based/project/suggestions?project_id={project_id}&sentenceIdList=100", - headers=headers_auth) - assert resp.status_code == 201 - resp_obj = resp.json() - assert resp_obj[0]['draft'] != "" - assert not resp_obj[0]['draft'].startswith(" ") - - #Get suggestions again - resp = client.put(f"/v2/text/translate/token-based/project/suggestions?project_id={project_id}&sentenceIdList=100", - headers=headers_auth) - assert resp.status_code == 201 - resp_obj = resp.json() - assert resp_obj[0]['draft'] != "" - assert not resp_obj[0]['draft'].startswith(" ") - -def test_delete_sentence(): - '''Test the removal of a sentence from project''' - - #Adding Project and sentences into it - resp = add_project(project_data, auth_token=initial_test_users['AgUser']['token']) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "sentenceList":source_sentences - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - # Check sentences are added - resp = client.get(f"{UNIT_URL}/sentences?project_id={project_id}", headers=headers_auth) - sentence_id1 = resp.json()[0]['sentenceId'] - sentence_id2 = resp.json()[1]['sentenceId'] - for sent in resp.json(): - assert_positive_get_sentence(sent) - - - #deleting sentence with no auth - headers = {"contentType": "application/json", - "accept": "application/json", - "app":"Autographa" - } - resp = client.delete(f"{UNIT_URL}/sentences?project_id={project_id}&sentence_id={sentence_id1}", - headers=headers) - assert resp.status_code == 401 - assert resp.json()['details'] == "Access token not provided or user not recognized." - - #Deleting Sentence with unauthorized users - Negative Test - for user in ['APIUser','VachanAdmin','VachanUser','BcsDev','AgUser','VachanContentAdmin','VachanContentViewer']: - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users[user]['token'] - response =client.delete(f"{UNIT_URL}/sentences?project_id={project_id}&sentence_id={sentence_id1}", - headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - - # Delete as AgAdmin - Positive test - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - response = client.delete(f"{UNIT_URL}/sentences?project_id={project_id}&sentence_id={sentence_id1}", - headers=headers_auth) - assert response.status_code == 201 - assert "successfull" in response.json()['message'] - - # Check get sentence to ensure deleted sentence is not present - get_response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&sentence_id_list="+str(sentence_id1),headers=headers_auth) - assert_not_available_content(get_response) - - #Create and Delete sentence with superadmin - Positive test - # Login as Super Admin - sa_data = { - "user_email": SUPER_USER, - "password": SUPER_PASSWORD - } - response = login(sa_data) - assert response.json()['message'] == "Login Succesfull" - test_user_token = response.json()["token"] - headers_auth['Authorization'] = "Bearer"+" "+test_user_token - - response = client.delete(f"{UNIT_URL}/sentences?project_id={project_id}&sentence_id={sentence_id2}", - headers=headers_auth) - assert response.status_code == 201 - assert "successfull" in response.json()['message'] - - # Check get sentence to ensure deleted sentence is not present - get_response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&sentence_id_list="+str(sentence_id2),headers=headers_auth) - assert_not_available_content(get_response) - - #Delete not available sentence - response =client.delete(f"{UNIT_URL}/sentences?project_id={project_id}&sentence_id=9999", - headers=headers_auth) - assert response.status_code == 404 - assert "Requested Content Not Available" in response.json()['error'] - - -def test_restore_sentence(): - '''positive test case, checking for correct return object''' - #only Super Admin can restore deleted data - #Creating and Deleting project sentence - #Adding Project and sentences into it - resp = add_project(project_data, auth_token=initial_test_users['AgAdmin']['token']) - project_id = resp.json()['data']['projectId'] - - put_data = { - "sentenceList":source_sentences - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - delete_resp =client.delete(f"{UNIT_URL}/sentences?project_id={project_id}&sentence_id=100", - headers=headers_auth) - - #Ensure deleted sentence is not present - get_response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&sentence_id_list=100",headers=headers_auth) - assert_not_available_content(get_response) - - deleteditem_id = delete_resp.json()['data']['itemId'] - data = {"itemId": deleteditem_id} - - #Restoring data - #Restore user without authentication - Negative Test - headers = {"contentType": "application/json", - "accept": "application/json", - "app":"Autographa" - } - response = client.put(RESTORE_URL, headers=headers, json=data) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - - #Restore project user with other API user,VachanAdmin,AgAdmin,AgUser,VachanUser,BcsDev,,'VachanContentAdmin','VachanContentViewer' - Negative Test - for user in ['APIUser','VachanAdmin','AgAdmin','AgUser','VachanUser','BcsDev','VachanContentAdmin','VachanContentViewer']: - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users[user]['token'] - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - - #Restore Project User with Super Admin - Positive Test - # Login as Super Admin - sa_data = { - "user_email": SUPER_USER, - "password": SUPER_PASSWORD - } - response = login(sa_data) - assert response.json()['message'] == "Login Succesfull" - test_user_token = response.json()["token"] - headers_auth['Authorization'] = "Bearer"+" "+test_user_token - - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 201 - assert response.json()['message'] == \ - f"Deleted Item with identity {deleteditem_id} restored successfully" - - #Ensure deleted sentence is not present - get_response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&sentence_id_list=100",headers=headers_auth) - assert get_response.status_code ==200 - assert len(get_response.json())== 1 - for item in get_response.json(): - assert_positive_get_sentence(item) - - #restore with missing data - Negative Test - data = {} - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert_input_validation_error(response) - - #Restore with invalid item id - Negative Test - data = {"itemId":9999} - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 404 - assert response.json()['error'] == "Requested Content Not Available" - logout_user(test_user_token) - -def test_suggestion_when_token_overlaps_confirmed_segment(): - # Testing bug fix https://github.com/Bridgeconn/vachan-api/issues/542 - resp = add_project(project_data, auth_token=initial_test_users['AgAdmin']['token']) - project_id = resp.json()['data']['projectId'] - - put_data = { - "sentenceList": [ - { - "sentenceId": 57001002, - "surrogateId": "tit 1:2", - "sentence":"This faith and knowledge make us sure that we have eternal life. God promised that life to us before time began—and God does not lie." - } - ] - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - # add a translation for just "not" when it occurs as "doesnot" - update_draft_url = f"/v2/text/translate/token-based/project/draft?project_id={project_id}" - data = [{ "draft": "they this", - "draftMeta": [ [ [ 0, 4 ], [ 9, 9 ], "untranslated" ], - [ [ 4, 5 ], [ 9, 9 ], "untranslated" ], - [ [ 5, 10 ], [ 9, 9 ], "untranslated" ], - [ [ 10, 11 ], [ 9, 9 ], "untranslated" ], - [ [ 11, 24 ], [ 9, 9 ], "untranslated" ], - [ [ 24, 25 ], [ 9, 9 ], "untranslated" ], - [ [ 25, 29 ], [ 9, 9 ], "untranslated" ], - [ [ 29, 30 ], [ 9, 9 ], "untranslated" ], - [ [ 30, 32 ], [ 9, 9 ], "untranslated" ], - [ [ 32, 33 ], [ 9, 9 ], "untranslated" ], - [ [ 33, 37 ], [ 9, 9 ], "untranslated" ], - [ [ 37, 38 ], [ 9, 9 ], "untranslated" ], - [ [ 38, 42 ], [ 9, 9 ], "untranslated" ], - [ [ 42, 43 ], [ 9, 9 ], "untranslated" ], - [ [ 43, 45 ], [ 9, 9 ], "untranslated" ], - [ [ 45, 46 ], [ 9, 9 ], "untranslated" ], - [ [ 46, 58 ], [ 9, 9 ], "untranslated" ], - [ [ 58, 59 ], [ 9, 9 ], "untranslated" ], - [ [ 59, 63 ], [ 9, 9 ], "untranslated" ], - [ [ 63, 65 ], [ 9, 9 ], "untranslated" ], - [ [ 65, 68 ], [ 9, 9 ], "untranslated" ], - [ [ 68, 69 ], [ 9, 9 ], "untranslated" ], - [ [ 69, 77 ], [ 9, 9 ], "untranslated" ], - [ [ 77, 78 ], [ 9, 9 ], "untranslated" ], - [ [ 78, 82 ], [ 9, 9 ], "untranslated" ], - [ [ 82, 83 ], [ 9, 9 ], "untranslated" ], - [ [ 83, 87 ], [ 9, 9 ], "untranslated" ], - [ [ 87, 88 ], [ 9, 9 ], "untranslated" ], - [ [ 88, 93 ], [ 9, 9 ], "untranslated" ], - [ [ 93, 94 ], [ 9, 9 ], "untranslated" ], - [ [ 94, 105 ], [ 9, 9 ], "untranslated" ], - [ [ 105, 106 ], [ 9, 9 ], "untranslated" ], - [ [ 106, 111 ], [ 9, 9 ], "untranslated" ], - [ [ 111, 112 ], [ 9, 9 ], "untranslated" ], - [ [ 112, 119 ], [ 9, 9 ], "untranslated" ], - [ [ 119, 120 ], [ 9, 9 ], "untranslated" ], - [ [ 132, 133 ], [ 9, 9 ], "untranslated" ], - [ [ 125, 128 ], [ 5, 9 ], "confirmed" ], - [ [ 129, 132 ], [ 0, 4 ], "confirmed" ], - [ [ 125, 125 ], [ 4, 5 ], "confirmed" ] ], - "sentenceId": 57001002 }] - update_resp = client.put(update_draft_url, - json=data, - headers={'Authorization': f'bearer {initial_test_users["AgAdmin"]["token"]}', - 'app':"Autographa"}) - assert update_resp.status_code == 201 - - - # Suggestion call 1 - suggest_url = f"/v2/text/translate/token-based/project/suggestions?project_id={project_id}&sentence_id_list=57001002" - suggest_resp = client.put(suggest_url, - headers={'Authorization': f"bearer {initial_test_users['AgAdmin']['token']}", - 'app':"Autographa"}) - assert suggest_resp.status_code == 201 - - # Suggestion call 2 - suggest_url = f"/v2/text/translate/token-based/project/suggestions?project_id={project_id}&sentence_id_list=57001002" - suggest_resp = client.put(suggest_url, - headers={'Authorization': f"bearer {initial_test_users['AgAdmin']['token']}", - 'app':"Autographa"}) - assert suggest_resp.status_code == 201 - -def test_glossupdate_upon_draft_update(): - '''If a draft update contain confirmed translations, - it should also be fed into translation memory - issue #592''' - resp = add_project(project_data, auth_token=initial_test_users['AgUser']['token']) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "sentenceList":source_sentences - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - resp = client.get(f"{UNIT_URL}/sentences?project_id={project_id}", headers=headers_auth) - for sent in resp.json(): - assert_positive_get_sentence(sent) - - # translate just one word - put_data = [ - {"sentenceId":100, - "draft": "കാട്", - "draftMeta":[ - [ [5,11], [0,4], "confirmed"] - ] - } - ] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data) - sents = resp.json() - assert len(sents) == 1 - assert_positive_get_sentence(sents[0]) - assert sents[0]["draft"] == "കാട്" - assert sents[0]["draftMeta"] == put_data[0]['draftMeta'] - - resp = client.get(f"/v2/nlp/gloss?source_language={project_data['sourceLanguageCode']}"+\ - f"&target_language={project_data['targetLanguageCode']}&token=jungle", - headers=headers_auth) - assert resp.status_code == 200 - assert "കാട്" in resp.json()['translations'] - -def test_draftmeta_validation(): - '''All protions of the draft should have a draftmeta segment - issue #602''' - resp = add_project(project_data, auth_token=initial_test_users['AgUser']['token']) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "sentenceList":source_sentences - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - ## Spaces not accounted for - put_data2 = [ - {"sentenceId":100, - "draft": "ഒരു കാട്ടില്‍ ഒരു കുറുക്കന്‍ ജീവിച്ചിരുന്നു", - "draftMeta":[ - [ [3,4], [0,3], "confirmed"], - [ [5,11], [4,11], "confirmed"], - [ [32,33], [13,16], "confirmed"], - [ [35,38], [18,26], "confirmed"] - ] - } - ] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data2) - assert resp.status_code == 422 - assert "error" in resp.json() - assert resp.json()['error'] == "Unprocessable Data" \ No newline at end of file diff --git a/app/test/test_commentaries.py b/app/test/test_commentaries.py index 0d4771c2..12005772 100644 --- a/app/test/test_commentaries.py +++ b/app/test/test_commentaries.py @@ -8,7 +8,6 @@ from .test_resources import check_post as add_resource from . test_auth_basic import SUPER_PASSWORD,SUPER_USER, login, logout_user from .conftest import initial_test_users -from .test_stop_words_generation import get_job_status UNIT_URL = '/v2/resources/commentaries/' @@ -17,6 +16,7 @@ headers_auth = {"contentType": "application/json", "accept": "application/json"} RESTORE_URL = '/v2/admin/restore' +JOBS_URL = '/v2/jobs' def assert_positive_get(item): '''Check for the properties in the normal return object''' @@ -70,6 +70,27 @@ def check_commentary_job_finished(response): time.sleep(60) return job_response +def assert_positive_job_response(out): + '''Check the properties in the update response''' + assert "message" in out + assert "data" in out + +def get_job_status(job_id): + '''Retrieve status of a job''' + # registered user can get job status + response = client.get(JOBS_URL+'/?job_id='+str(job_id),headers=headers) + assert response.status_code == 401 + assert response.json()['error'] == 'Authentication Error' + + headers_auth['Authorization'] = "Bearer"+" "+ initial_test_users['APIUser']['token'] + response = client.get(JOBS_URL+'/?job_id='+str(job_id),headers=headers_auth) + assert response.status_code == 200 + assert_positive_job_response(response.json()) + assert "jobId" in response.json()['data'] + assert "status" in response.json()['data'] + return response + + def test_post_default(): '''Positive test to upload commentries, with various kins of ref ranges supported''' data = [ @@ -405,12 +426,14 @@ def test_put_after_upload(): # not available PUT new_data[0]['commentaryId'] = 9999 + headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanAdmin']['token'] response = client.put(UNIT_URL+resource_name,headers=headers_auth, json=new_data) job_response = get_job_status(response.json()['data']['jobId']) assert job_response.json()['data']['status'] == 'job error' assert job_response.json()["message"] == "Job is terminated with an error" assert "message" in job_response.json()["data"]["output"] + headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanAdmin']['token'] resource_name = resource_name.replace('1', '2') response = client.put(UNIT_URL+resource_name,headers=headers_auth, json=[]) assert response.status_code == 404 @@ -519,6 +542,7 @@ def test_soft_delete(): for i,item in enumerate(job_response.json()['data']['output']['data']): #pylint: disable=unused-variable assert not item['active'] + headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanAdmin']['token'] get_response2 = client.get(UNIT_URL+resource_name, headers=headers_auth) assert len(get_response2.json()) == len(data) - len(delete_data) @@ -569,6 +593,7 @@ def test_created_user_can_only_edit(): assert job_response.json()["message"] == "Commentaries added successfully" #update commentary with created SA user + headers_auth['Authorization'] = "Bearer"+" "+test_user_token get_response = client.get(UNIT_URL+resource_name,headers=headers_auth) commentary_id1 = get_response.json()[0]['commentaryId'] commentary_id2 = get_response.json()[1]['commentaryId'] diff --git a/app/test/test_generic_translation.py b/app/test/test_generic_translation.py deleted file mode 100644 index 0c2ac93d..00000000 --- a/app/test/test_generic_translation.py +++ /dev/null @@ -1,362 +0,0 @@ -'''Tests the translation APIs that do need projects available in DB''' -import re -import csv - -from . import client -from . import assert_input_validation_error, assert_not_available_content -from .test_agmt_translation import assert_positive_get_tokens, assert_positive_get_sentence -from .conftest import initial_test_users - -UNIT_URL = '/v2/text/translate/token-based' -headers = {"contentType": "application/json", "accept": "application/json"} -headers_auth = {"contentType": "application/json", - "accept": "application/json" - } - -sentences = [ - "Once upon a time, there was a test case.", - "The test case tested for all the anticipated usages, that the developer could think of.", - "But there were a few senarios which escaped his thoughts and were left out.", - "Thus, even though the test was always run and ensured to pass every time,", - "the few cases that were forgotten made it not effective.", - "This is the sad story of a poor test which worked so hard and yet couldn't be good enough.☹️." -] -sentence_list = [{"sentenceId":i, "sentence":s} for i,s in enumerate(sentences)] - -sample_sent = "इब्राहीम के वंशज दाऊद के पुत्र यीशु मसीह की वंशावली इस प्रकार है" -post_obj = {"sentence_list":[{ - "sentenceId":1, - "sentence":sample_sent}], - "stopwords":{ - "prepositions":["इस"], - "postpositions":["के", "की", "है"] - }} -def test_tokenize(): - '''Positve tests for generic tokenization API''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanUser']['token'] - default_response = client.put(UNIT_URL+'/tokens?source_language=en', headers=headers_auth, - json={"sentence_list":sentence_list}) - assert default_response.status_code == 200 - assert len(default_response.json()) >10 - for item in default_response.json(): - assert_positive_get_tokens(item) - - #without auth - response = client.put(UNIT_URL+'/tokens?source_language=en&include_phrases=True', - headers=headers, json={"sentence_list":sentence_list}) - assert response.json()['error'] == "Authentication Error" - assert response.status_code == 401 - #with auth - response = client.put(UNIT_URL+'/tokens?source_language=en&include_phrases=True', - headers=headers_auth, json={"sentence_list":sentence_list}) - assert response.status_code == 200 - assert default_response.json() == response.json() - - response = client.put(UNIT_URL+'/tokens?source_language=en', - headers=headers, json={"sentence_list":sentence_list[:3]}) - #without auth - assert response.json()['error'] == "Authentication Error" - assert response.status_code == 401 - #with auth - response = client.put(UNIT_URL+'/tokens?source_language=en', - headers=headers_auth, json={"sentence_list":sentence_list[:3]}) - assert response.status_code == 200 - for item in response.json(): - assert_positive_get_tokens(item) - assert len(default_response.json()) > len(response.json()) - - # include_phrases flag - #without auth - response = client.put(UNIT_URL+"/tokens?source_language=en"+ - "&include_phrases=true", headers=headers, - json={"sentence_list":sentence_list}) - assert response.json()['error'] == "Authentication Error" - assert response.status_code == 401 - #with auth - response = client.put(UNIT_URL+"/tokens?source_language=en"+ - "&include_phrases=true", headers=headers_auth, - json={"sentence_list":sentence_list}) - assert response.json() == default_response.json() - - response = client.put(UNIT_URL+"/tokens?source_language=en"+ - "&include_phrases=false", headers=headers_auth, - json={"sentence_list":sentence_list}) - assert response.status_code == 200 - assert len(response.json()) <= len(default_response.json()) - for item in response.json(): - assert_positive_get_tokens(item) - assert " " not in item['token'] - - # include stopwords flag - sample_stopwords = ["the", "is", "a"] - #without auth - response = client.put(UNIT_URL+"/tokens?source_language=en"+ - "&include_stopwords=false", headers=headers, - json={"sentence_list":sentence_list}) - assert response.json()['error'] == "Authentication Error" - assert response.status_code == 401 - #with auth - response = client.put(UNIT_URL+"/tokens?source_language=en"+ - "&include_stopwords=false", headers=headers_auth, - json={"sentence_list":sentence_list}) - assert response.json() == default_response.json() - for item in response.json(): - assert_positive_get_tokens(item) - assert item['token'] not in sample_stopwords - - response = client.put(UNIT_URL+"/tokens?source_language=en"+ - "&include_stopwords=true", headers=headers_auth, - json={"sentence_list":sentence_list}) - assert response.status_code == 200 - for item in response.json(): - assert_positive_get_tokens(item) - - response = client.put(UNIT_URL+"/tokens?source_language=en"+ - "&include_stopwords=true&include_phrases=false", headers=headers_auth, - json={"sentence_list":sentence_list}) - assert response.status_code == 200 - all_words = [item['token'] for item in response.json()] - for swd in sample_stopwords: - assert swd in all_words - - - -def test_tokenize_with_diff_flags(): - '''Postive tests for tokenizing a single input sentence with varying parameters''' - response = client.put(UNIT_URL+"/tokens?source_language=hi"+ - "&include_stopwords=true&include_phrases=false&use_translation_memory=false", - headers=headers_auth, json=post_obj) - assert response.status_code == 200 - all_words = [item['token'] for item in response.json()] - for word in sample_sent.split(): # covers all words in resource - assert word in all_words - assert len(all_words) == len(set(all_words)) #unique - for word in all_words: # no phrases - assert " " not in word - - response = client.put(UNIT_URL+"/tokens?source_language=hi"+ - "&include_stopwords=true&include_phrases=true&use_translation_memory=false", - headers=headers_auth, json=post_obj) - assert response.status_code == 200 - all_tokens = [item['token'] for item in response.json()] - assert "इस प्रकार है" in all_tokens - assert "इब्राहीम के" in all_tokens - - response = client.put(UNIT_URL+"/tokens?source_language=hi"+ - "&include_stopwords=false&include_phrases=true&use_translation_memory=false", - headers=headers_auth, json=post_obj) - assert response.status_code == 200 - all_tokens = [item['token'] for item in response.json()] - # no independant stopwords are present - for swd in post_obj['stopwords']['prepositions'] + post_obj['stopwords']['postpositions']: - assert swd not in all_tokens - assert "इस प्रकार है" in all_tokens # stopwords coming within phrases are included - assert "इब्राहीम के" in all_tokens - - # make a translation - trans_obj = post_obj - trans_obj['token_translations'] = [ - { "token": "यीशु मसीह", - "occurrences": [ - { "sentenceId": 1, - "offset": [31,40]} - ], - "translation": "Jesus Christ"} - ] - - response = client.put(UNIT_URL+"/token-translate?source_language=hi"+ - "&target_language=en&use_data_for_learning=true", - headers=headers_auth, json=trans_obj) - assert response.status_code ==200 - - # after a translation testing for use_translation_memory flag - response = client.put(UNIT_URL+"/tokens?source_language=hi"+ - "&include_stopwords=true&include_phrases=true&use_translation_memory=true", - headers=headers_auth, json=post_obj) - assert response.status_code == 200 - all_tokens = [item['token'] for item in response.json()] - assert "यीशु मसीह" in all_tokens - assert "की" in all_tokens - - response = client.put(UNIT_URL+"/tokens?source_language=hi"+ - "&use_translation_memory=true", - headers=headers_auth, json=post_obj) - assert response.status_code == 200 - all_tokens = [item['token'] for item in response.json()] - assert "यीशु मसीह" in all_tokens - assert "की" not in all_tokens - - response = client.put(UNIT_URL+"/tokens?source_language=hi"+ - "&use_translation_memory=false", - headers=headers_auth, json=post_obj) - assert response.status_code == 200 - all_tokens = [item['token'] for item in response.json()] - assert "यीशु मसीह" not in all_tokens - assert "यीशु" in all_tokens - -def test_token_translate(): - '''Positive tests to apply token translationa nd obtain drafts''' - - #tokenize - resp = client.put(UNIT_URL+"/tokens?source_language=hi&use_translation_memory=false"+ - "&include_stopwords=true", - headers=headers_auth, json=post_obj) - assert resp.status_code ==200 - all_tokens = resp.json() - for tok in all_tokens: - assert_positive_get_tokens(tok) - assert tok != "यीशु मसीह" - - # translate one token - trans_obj = post_obj - trans_obj['token_translations'] = [ - { "token": all_tokens[0]['token'], - "occurrences": [all_tokens[0]['occurrences'][0]], - "translation": "test"} - ] - - response = client.put(UNIT_URL+"/token-translate?source_language=hi"+ - "&target_language=en&use_data_for_learning=false", - headers=headers_auth, json=trans_obj) - assert response.status_code ==200 - return_sent = response.json()['data'][0] - assert_positive_get_sentence(return_sent) - assert return_sent['draft'].startswith("test") - assert return_sent['draftMeta'][0][2] == "confirmed" - assert return_sent['draftMeta'][1][2] == "untranslated" - assert len(return_sent['draftMeta']) == 2 - - # translate all tokens - trans_obj = {"sentence_list": [return_sent], 'token_translations':[]} - for tok in all_tokens: - obj = {"token": tok['token'], "occurrences":tok["occurrences"], "translation":"test"} - trans_obj['token_translations'].append(obj) - response = client.put(UNIT_URL+"/token-translate?source_language=hi"+ - "&target_language=en&use_data_for_learning=false", - headers=headers_auth, json=trans_obj) - assert response.status_code ==200 - return_sent = response.json()['data'][0] - assert_positive_get_sentence(return_sent) - words_in_draft = re.findall(r'\w+',return_sent['draft']) - for word in words_in_draft: - assert word == 'test' - for meta in return_sent['draftMeta']: - if meta[0][1] - meta[0][0] > 1: # all non space and non-punct parts of input - assert meta[2] == "confirmed" - - # make a translation for a token not in token list - # it combines & re-translates 2 already translated tokens - # and use data for learning - trans_obj = {"sentence_list": [return_sent]} - trans_obj['token_translations'] = [ - { "token": "यीशु मसीह", - "occurrences": [ - { "sentenceId": 1, - "offset": [31,40]} - ], - "translation": "Jesus Christ"} - ] - response = client.put(UNIT_URL+"/token-translate?source_language=hi"+ - "&target_language=en", - headers=headers_auth, json=trans_obj) - assert response.status_code ==200 - new_return_sent = response.json()['data'][0] - assert "Jesus Christ" in new_return_sent['draft'] - # combined two segments to one - found_combined = False - found_seperate = False - for meta in new_return_sent['draftMeta']: - match meta[0]: - case [31, 35]: - found_seperate = True - case [36,40]: - found_seperate = True - case [31,40]: - found_combined = True - assert found_combined - assert not found_seperate - -def test_draft_generation(): - '''tests conversion of sentence list to differnt draft formats''' - verse_start = 41001001 - for i,sentence in enumerate(sentence_list): - sentence['sentenceId'] = verse_start+i - #without auth - response = client.put(UNIT_URL+'/draft?doc_type=usfm', headers=headers, json=sentence_list) - assert response.json()['error'] == "Authentication Error" - assert response.status_code == 401 - #with auth - response = client.put(UNIT_URL+'/draft?doc_type=usfm', headers=headers_auth, json=sentence_list) - assert response.status_code == 200 - assert "\\id MAT" in response.json()[0] - assert sentence_list[0]['sentence'] in response.json()[0] - - response = client.put(UNIT_URL+'/draft?doc_type=csv', headers=headers_auth, json=sentence_list) - assert response.status_code == 200 - assert sentence_list[0]['sentence'] in response.json() - lines = response.json().split('\n') - parse_csv = True - try: - csv.reader(lines) - except csv.Error: - parse_csv = False - assert parse_csv - - response = client.put(UNIT_URL+'/draft?doc_type=text', headers=headers_auth, json=sentence_list) - assert response.status_code == 200 - input_text = " ".join([sent['sentence'] for sent in sentence_list]) - assert input_text.strip() == response.json().strip() - - # to be implemented: alignment-json - -def test_space_handling_in_token_replacement(): - '''In connection with issue #628''' - # yesu and mashi together and then separatetly, to check draft meta segment for space too - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanUser']['token'] - - trans_obj = post_obj - trans_obj['token_translations'] = [ - { "token": "यीशु मसीह", - "occurrences": [ - { "sentenceId": 1, - "offset": [31,40]} - ], - "translation": "Jesus Christ"} - ] - - response = client.put("/v2/text/translate/token-based/token-translate?source_language=hi"+ - "&target_language=en&use_data_for_learning=true", - headers=headers_auth, json=trans_obj) - assert response.status_code ==200 - resp_sent = response.json()['data'][0] - assert resp_sent['draft'] == "Jesus Christ", response.json() - - post_obj['sentence_list'][0]['draftMeta'] = resp_sent['draftMeta'] - post_obj['sentence_list'][0]['draft'] = resp_sent['draft'] - trans_obj = post_obj - trans_obj['token_translations'] = [ - { "token": "यीशु", - "occurrences": [ - { "sentenceId": 1, - "offset": [31,35]} - ], - "translation": "Jesus"}, - { "token": "मसीह", - "occurrences": [ - { "sentenceId": 1, - "offset": [36,40]} - ], - "translation": "Christ"} - ] - response = client.put("/v2/text/translate/token-based/token-translate?source_language=hi"+ - "&target_language=en&use_data_for_learning=true", - headers=headers_auth, json=trans_obj) - assert response.status_code == 200 - resp_sent = response.json()['data'][0] - assert resp_sent['draft'] == "Jesus Christ", response.json() - - found_space = False - for seg in resp_sent['draftMeta']: - if seg[1] == [5, 6]: - found_space = True - assert found_space, resp_sent['draftMeta'] diff --git a/app/test/test_nlp_glossary.py b/app/test/test_nlp_glossary.py deleted file mode 100644 index 48874347..00000000 --- a/app/test/test_nlp_glossary.py +++ /dev/null @@ -1,131 +0,0 @@ -'''Test cases for Glossary related APIs''' -from . import client -from . import assert_input_validation_error, assert_not_available_content -from . import check_default_get -from .test_auth_basic import login,SUPER_USER,SUPER_PASSWORD,logout_user -from .conftest import initial_test_users - -src_lang = "en" -trg_lang = "aa" - -POST_URL = '/v2/nlp/gloss' -GET_LIST_URL = "/v2/nlp/gloss-entries" -GET_COUNT_URL = '/v2/nlp/gloss-entries/count' - -gloss_data = [ - {"token":"love", "translations":["love"]}, - {"token":"happy", "translations":["happy"]}, - {"token":"joy", "translations":["happy"]}, - {"token":"smile", "translations":["happy", "face"]}, - {"token":"love", "translations":["love-2"]}, -] - -def assert_positive_get(item): - '''Check for the properties in the normal return object''' - assert "token" in item - assert "translation" in item - for key in item: - assert key in ['tmID', 'token', 'translation', 'frequency', 'metaData'] - -def check_post(src, trg, data, headers): - '''common steps for positive post test cases''' - response = client.post(POST_URL+\ - f"?source_language={src}&target_language={trg}", headers=headers, json=data) - assert response.status_code == 201 - assert "added" in response.json()['message'].lower() - -def test_get_gloss_entries(): - '''Happy path testing for glossary list/translation meory list''' - headers_auth = {"contentType": "application/json", - "accept": "application/json", - 'Authorization': "Bearer"+" "+initial_test_users['APIUser']['token'] - } - # upload data before GET call - check_post(src_lang, trg_lang, gloss_data, headers_auth) - - GET_URL = GET_LIST_URL+f"?source_language={src_lang}&target_language={trg_lang}" - - check_default_get(GET_URL, headers_auth, assert_positive_get) - - # with all registered users - for user in ['APIUser', 'APIUser2', "BcsDev", "AgAdmin", "AgUser", "VachanUser", "VachanAdmin",'VachanContentAdmin','VachanContentViewer']: - headers_auth = {"contentType": "application/json", - "accept": "application/json", - 'Authorization': "Bearer"+" "+initial_test_users[user]['token'] - } - - resp = client.get(GET_URL, headers=headers_auth) - assert resp.status_code == 200 - for item in resp.json(): - assert_positive_get(item) - assert len(resp.json()) >= 5 - - # with filter for token - headers_auth = {"contentType": "application/json", - "accept": "application/json", - 'Authorization': "Bearer"+" "+initial_test_users['APIUser']['token'] - } - resp = client.get(GET_URL+"&token=love", headers=headers_auth) - assert resp.status_code == 200 - assert len(resp.json()) == 2 - for item in resp.json(): - assert item['token'] == "love" - -def test_get_gloss_count(): - '''Test cases to count all glossary records and count unique tokens in translation memory''' - headers_auth = {"contentType": "application/json", - "accept": "application/json", - 'Authorization': "Bearer"+" "+initial_test_users['APIUser']['token'] - } - # upload data before GET call - check_post(src_lang, trg_lang, gloss_data, headers_auth) - - GET_URL = GET_COUNT_URL+f"?source_language={src_lang}&target_language={trg_lang}" - - # Without authentication - Negative Test - headers = {"contentType": "application/json", "accept": "application/json"} - resp = client.get(GET_URL, headers=headers) - assert resp.status_code == 401 - assert resp.json()['error'] == 'Authentication Error' - - # With all registered users - Positive Test - for user in ['APIUser', 'APIUser2', "BcsDev", "AgAdmin", "AgUser", "VachanUser", "VachanAdmin",'VachanContentAdmin','VachanContentViewer']: - headers_auth = {"contentType": "application/json", - "accept": "application/json", - 'Authorization': "Bearer"+" "+initial_test_users[user]['token'] - } - - # Without filter of token - Positive Test - response = client.get(GET_URL,headers=headers_auth) - assert response.status_code == 200 - assert isinstance(response.json(), dict) - assert len(response.json()) == 2 - assert "tokenTranslationCount" in response.json() - assert "tokenCount" in response.json() - assert response.json()['tokenTranslationCount'] == 6 - assert response.json()['tokenCount'] == 4 - - # With filter for token - Positive Test - resp = client.get(GET_URL+"&token=love", headers=headers_auth) - assert resp.status_code == 200 - assert resp.json()['tokenTranslationCount'] == 2 - # Validate tokenCount - assert resp.json()['tokenCount'] == 1 - - # With notavailable token - Negative Test - resp = client.get(GET_URL+"&token=ttt", headers=headers_auth) - assert resp.status_code == 200 - assert resp.json()['tokenTranslationCount'] == 0 - assert resp.json()['tokenCount'] == 0 - - # With notavailable resource language - Negative Test - GET_URL = GET_COUNT_URL+f"?source_language=x-ttt&target_language={trg_lang}" - resp = client.get(GET_URL,headers=headers_auth) - assert resp.status_code == 404 - assert resp.json()['error'] == 'Requested Content Not Available' - - # With notavailable target language - Negative Test - GET_URL = GET_COUNT_URL+f"?source_language={src_lang}&target_language=x-ttt" - resp = client.get(GET_URL,headers=headers_auth) - assert resp.status_code == 404 - assert resp.json()['error'] == 'Requested Content Not Available' diff --git a/app/test/test_smast_projects.py b/app/test/test_smast_projects.py deleted file mode 100644 index 9ff05293..00000000 --- a/app/test/test_smast_projects.py +++ /dev/null @@ -1,1594 +0,0 @@ -'''Test cases for SanketMAST projects related APIs''' -import time -from schema import schema_auth -from . import client -from . import assert_input_validation_error, assert_not_available_content -from . import check_default_get -from .test_bibles import check_post as add_bible, gospel_books_data -from .test_resources import check_post as add_resource -from .test_versions import check_post as add_version -from .conftest import initial_test_users -from . test_auth_basic import login,SUPER_PASSWORD,SUPER_USER,logout_user - -UNIT_URL = '/v2/text/translate/token-based/projects' -USER_URL = '/v2/text/translate/token-based/project/user' -SENTENCE_URL = '/v2/text/translate/token-based/project/sentences' -RESTORE_URL = '/v2/admin/restore' -headers = {"contentType": "application/json", "accept": "application/json", "app":"SanketMAST"} -headers_auth = {"contentType": "application/json", - "accept": "application/json", - "app":"SanketMAST" - } - -bible_books = { - "mat": "\\id MAT\n\\c 1\n\\p\n\\v 1 इब्राहीम के वंशज दाऊद के पुत्र यीशु मसीह की वंशावली इस "+ - "प्रकार है:\n\\v 2 इब्राहीम का पुत्र था इसहाक और इसहाक का पुत्र हुआ याकूब। फिर याकूब "+ - "से यहूदा और उसके भाई उत्पन्न हुए।\n\\v 3 यहूदा के बेटे थे फिरिस और जोरह। (उनकी माँ "+ - "का नाम तामार था।) फिरिस, हिस्रोन का पिता था। हिस्रोन राम का पिता था।", - "mrk": "\\id MRK\n\\c 1\n\\p\n\\v 1 यह परमेश्वर के पुत्र यीशु मसीह के शुभ संदेश का प्रारम्भ"+ - " है।\n\\v 2 भविष्यवक्ता यशायाह की पुस्तक में लिखा है कि: “सुन! मैं अपने दूत को तुझसे"+ - " पहले भेज रहा हूँ। वह तेरे लिये मार्ग तैयार करेगा।”\n\\v 3 “जंगल में किसी पुकारने "+ - "वाले का शब्द सुनाई दे रहा है: ‘प्रभु के लिये मार्ग तैयार करो। और उसके लिये राहें "+ - "सीधी बनाओ।’”\n\\v 4 यूहन्ना लोगों को जंगल में बपतिस्मा देते आया था। उसने लोगों से"+ - " पापों की क्षमा के लिए मन फिराव का बपतिस्मा लेने को कहा।\n\\v 5 फिर समूचे यहूदिया"+ - " देश के और यरूशलेम के लोग उसके पास गये और उस ने यर्दन नदी में उन्हें बपतिस्मा दिया"+ - "। क्योंकि उन्होंने अपने पाप मान लिये थे।" -} - -def assert_positive_get(item): - '''Check for the properties in the normal return object''' - assert "projectId" in item - assert isinstance(item['projectId'], int) - assert "projectName" in item - assert "sourceLanguage" in item - assert "code" in item['sourceLanguage'] - assert "targetLanguage" in item - assert "code" in item['targetLanguage'] - assert 'metaData' in item - assert 'books' in item['metaData'] - assert 'useDataForLearning' in item['metaData'] - assert "active" in item - assert "users" in item - assert isinstance(item['users'], list) - assert "createTime" in item - assert "updateTime" in item - - -def check_post(data, auth_token=None): - '''creates a projects''' - headers_auth = {"contentType": "application/json", - "accept": "application/json", - "app":"SanketMAST"} - if not auth_token: - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - else: - headers_auth['Authorization'] = "Bearer"+" "+auth_token - response = client.post(UNIT_URL, headers=headers_auth, json=data) - return response - -def test_default_post_put_get(): - '''Positive test to create a project''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - resp = client.get(UNIT_URL+'?project_name=Test project 1',headers=headers_auth) - assert_not_available_content(resp) - - # create with minimum data - post_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - new_project = response.json()['data'] - assert_positive_get(new_project) - - # check if all defaults are coming - assert new_project['metaData']["useDataForLearning"] - assert isinstance(new_project['metaData']['books'], list) - assert len(new_project['metaData']['books']) == 0 - assert new_project['active'] - - # upload books - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - response2 = client.put(UNIT_URL+'?project_id='+str(new_project['projectId']),\ - headers=headers_auth, json=put_data) - assert response2.status_code == 201 - assert response2.json()['message'] == "Project updated successfully" - updated_project = response2.json()['data'] - assert_positive_get(updated_project) - - assert new_project['projectId'] == updated_project['projectId'] - assert new_project['projectName'] == updated_project['projectName'] - assert updated_project['metaData']['books'] == ['mat', 'mrk'] - - #add bible , create resource with open-access - - version_data = { - "versionAbbreviation": "TTT", - "versionName": "test version for bibles", - } - add_version(version_data) - resource_data = { - "resourceType": "bible", - "language": "gu", - "version": "TTT", - "year": 3030, - "versionTag": 1, - "accessPermissions": [ - "content","open-access" - ], - } - resource = add_resource(resource_data) - table_name = resource.json()['data']['resourceName'] - - #add books with vachan admin - headers_auth_content = {"contentType": "application/json", - "accept": "application/json" - } - headers_auth_content['Authorization'] = "Bearer"+" "+initial_test_users['VachanAdmin']['token'] - resp = client.post('/v2/resources/bibles/'+table_name+'/books', headers=headers_auth_content, json=gospel_books_data) - - # resp, resource_name = add_bible(gospel_books_data) - assert resp.status_code == 201 - - put_data = { - "selectedBooks": { - "bible": table_name, - "books": ["luk", "jhn"] - } - } - response2b = client.put(UNIT_URL+'?project_id='+str(new_project['projectId']),\ - headers=headers_auth, json=put_data) - assert response2b.status_code == 201 - assert response2b.json()['message'] == "Project updated successfully" - updated_project = response2b.json()['data'] - assert_positive_get(updated_project) - assert updated_project['metaData']['books'] == ['mat', 'mrk', 'luk', 'jhn'] - - # fetch projects - response3 = client.get(UNIT_URL,headers=headers_auth) - assert len(response3.json()) >= 1 - found_project = False - for proj in response3.json(): - if proj['projectName'] == post_data['projectName']: - found_project = True - fetched_project = proj - assert found_project - assert_positive_get(fetched_project) - - assert fetched_project['projectName'] == post_data['projectName'] - assert fetched_project['sourceLanguage']['code'] == post_data['sourceLanguageCode'] - assert fetched_project['targetLanguage']['code'] == post_data['targetLanguageCode'] - assert fetched_project['metaData']['books'] == ['mat', 'mrk', 'luk', 'jhn'] - - # change name - put_data = { - "projectName":"New name for old project"} - response4 = client.put(UNIT_URL+'?project_id='+str(new_project['projectId']),\ - headers=headers_auth, json=put_data) - assert response4.status_code == 201 - assert response4.json()['message'] == "Project updated successfully" - updated_project = response4.json()['data'] - assert_positive_get(updated_project) - assert updated_project['projectName']== "New name for old project" - - - # create with all possible options - post_data = { - "projectName": "Test Project 2", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml", - "useDataForLearning": True, - "stopwords": { - "prepositions": [ "कोई", "यह", "इस", "इसे", "उस", "कई", "इसी", "अभी", "जैसे" ], - "postpositions": [ "के", "का", "में", "की", "है", "और", "से", "हैं", "को", "पर"] - }, - "punctuations": [",","\"","!",".",":",";","\n","\\","“","”","“","*","।","?",";","'", - "’","(",")","‘","—" ], - "active": True - } - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - new_project = response.json()['data'] - assert_positive_get(new_project) - - # add a few more projects - post_data = { - "projectName": "Test Project 3", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - resp = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert resp.status_code == 201 - post_data = { - "projectName": "Test Project 4", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - resp = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert resp.status_code == 201 - - check_default_get(UNIT_URL,headers_auth, assert_positive_get) - - -def test_post_invalid(): - '''test input validation for project create''' - # Missing mandatory content - data1 = { - "projectName": "Test project 1", - "targetLanguageCode": "ml" - } - res1 = client.post(UNIT_URL, headers=headers_auth, json=data1) - assert_input_validation_error(res1) - - data2 = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - } - res2 = client.post(UNIT_URL, headers=headers_auth, json=data2) - assert_input_validation_error(res2) - - data3 = { - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - res3 = client.post(UNIT_URL, headers=headers_auth, json=data3) - assert_input_validation_error(res3) - - # incorrect data in fields - data1 = { - "projectName": "Test project 1", - "sourceLanguageCode": "2hindi", - "targetLanguageCode": "ml" - } - res1 = client.post(UNIT_URL, headers=headers_auth, json=data1) - assert_input_validation_error(res1) - - data2 = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml", - "useDataForLearning": "use" - } - res2 = client.post(UNIT_URL, headers=headers_auth, json=data2) - assert_input_validation_error(res2) - - data3 = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml", - "stopwords": ["a", "mromal", "list"] - } - res3 = client.post(UNIT_URL, headers=headers_auth, json=data3) - assert_input_validation_error(res3) - - data4 = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml", - "punctuations": "+_*())^%$#<>?:'" - } - res4 = client.post(UNIT_URL, headers=headers_auth, json=data4) - assert_input_validation_error(res4) - -def test_put_invalid(): - '''Give incorrect data for project update''' - post_data = {"projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml"} - resp = check_post(post_data) - assert resp.json()['message'] == "Project created successfully" - new_project = resp.json()['data'] - - # missing projectId - data = {"active": False} - resp = client.put(UNIT_URL, headers=headers_auth, json=data) - assert_input_validation_error(resp) - - # incorrect values in fields - data = {"active": "delete"} - resp = client.put(UNIT_URL+'?project_id='+str(new_project['projectId']), \ - headers=headers_auth, json=data) - assert_input_validation_error(resp) - - data = {"uploadedUSFMs": "mat"} - resp = client.put(UNIT_URL+'?project_id='+str(new_project['projectId']), \ - headers=headers_auth, json=data) - assert_input_validation_error(resp) - - data = {"uploadedUSFMs": ["The contents of matthew in text"]} - resp = client.put(UNIT_URL+'?project_id='+str(new_project['projectId']), \ - headers=headers_auth, json=data) - assert resp.status_code == 415 - assert resp.json()['error'] == "Not the Required Type" - -def check_project_user(project_name, user_id, role=None, status=None, metadata = None): - '''Make sure the user is in project and if specified, check for other values''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - response = client.get(UNIT_URL+'?project_name='+project_name,headers=headers_auth) - found_user = False - found_owner = False - - for user in response.json()[0]['users']: - if user['userId'] == user_id: - found_user = True - if role: - assert user['userRole'] == role - if status is not None: - assert user['active'] == status - if metadata: - assert user['metaData'] == metadata - if user['userRole'] == "projectOwner" : - found_owner = True - assert found_owner - assert found_user - -def test_add_user(): - '''Positive test to add a user to a project''' - project_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - resp = check_post(project_data) - assert resp.status_code == 201 - new_project = resp.json()['data'] - - #not exising user id - new_user_id = str(5555) - response = client.post(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert response.status_code == 404 - assert response.json()['error'] == 'Requested Content Not Available' - #exising user - new_user_id = initial_test_users['SanketMASTUser']['test_user_id'] - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - response = client.post(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert response.status_code == 201 - assert response.json()['message'] == "User added to project successfully" - data = response.json()['data'] - assert data["project_id"] == new_project['projectId'] - assert data["userId"] == new_user_id - assert data["userRole"] == "projectMember" - assert data['active'] - - # fetch this project and check for new user - check_project_user(project_data['projectName'], new_user_id, role='projectMember') - - -def test_add_user_invalid(): - '''Negative tests to add a user to a project''' - project_data = { - "projectName": "Test project 10", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - resp = check_post(project_data) - assert resp.status_code == 201 - new_project = resp.json()['data'] - - # No projectId - resp = client.post(USER_URL+'?user_id=11111',headers=headers_auth) - assert_input_validation_error(resp) - - # No User - resp = client.post(USER_URL+'?project_id='+str(new_project['projectId']),headers=headers_auth) - assert_input_validation_error(resp) - - # Invalid project - resp = client.post(USER_URL+'?project_id='+str(new_project['projectName'])+ - '&user_id=111111',headers=headers_auth) - assert_input_validation_error(resp) - - # Invalid user - resp = client.post(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id=some_name',headers=headers_auth) - assert resp.status_code == 404 - assert resp.json()['error'] == 'Requested Content Not Available' - - -def test_update_user(): - '''Positive tests to change role, status & metadata of a user''' - project_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - resp = check_post(project_data) - assert resp.json()['message'] == "Project created successfully" - new_project = resp.json()['data'] - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - new_user_id = initial_test_users['SanketMASTUser']['test_user_id'] - - resp = client.post(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert resp.json()['message'] == "User added to project successfully" - - update_data = { - # "project_id": new_project['projectId'], - "userId": new_user_id - } - - # change role - update1 = update_data - update1['userRole'] = 'projectOwner' - # response1 = client.post(USER_URL+'?project_id='+str(new_project['projectId'])+ - # '&user_id='+str(new_user_id),headers=headers_auth) - response1 = client.put(USER_URL+'?project_id='+str(new_project['projectId']), headers=headers_auth, json=update1) - assert response1.status_code == 201 - assert response1.json()['message'] == "User updated in project successfully" - check_project_user(project_data['projectName'], new_user_id, role="projectOwner") - - # change status - update2 = update_data - update2['active'] = False - response2 = client.put(USER_URL+'?project_id='+str(new_project['projectId']), headers=headers_auth, json=update2) - assert response2.status_code == 201 - assert response2.json()['message'] == "User updated in project successfully" - check_project_user(project_data['projectName'], new_user_id, status=False) - - # add metadata - meta = {"last_filter": "mat"} - update3 = update_data - update3['metaData'] = meta - response3 = client.put(USER_URL+'?project_id='+str(new_project['projectId']), headers=headers_auth, json=update3) - assert response3.status_code == 201 - assert response3.json()['message'] == "User updated in project successfully" - check_project_user(project_data['projectName'], new_user_id, metadata=meta) - - -def test_update_user_invlaid(): - '''Negative test for update user''' - project_data = { - "projectName": "Test project 101", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - resp = check_post(project_data) - assert resp.json()['message'] == "Project created successfully" - new_project = resp.json()['data'] - new_user_id = initial_test_users['SanketMASTUser']['test_user_id'] - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - resp = client.post(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert resp.json()['message'] == "User added to project successfully" - - - # not the added user - update_data = { - "userId": "not-a-valid-user-11233", - "active": False - } - response = client.put(USER_URL+'?project_id='+str(new_project['projectId']), headers=headers_auth, json=update_data) - assert response.status_code == 404 - assert response.json()['details'] == "User-project pair not found" - - #non existant project - update_data = { - "userId": new_user_id, - "active": False - } - response = client.put(USER_URL+'?project_id='+str(new_project['projectId']+1), headers=headers_auth, json=update_data) - assert response.status_code == 404 - assert response.json()['details'] == f"Project with id, {new_project['projectId']+1}, not present" - - - # invalid status - update_data = { - "userId": new_user_id, - "active": "Delete" - } - response = client.put(USER_URL+'?project_id='+str(new_project['projectId']+1), headers=headers_auth, json=update_data) - assert_input_validation_error(response) - - # invalid metadata - update_data = { - "userId": new_user_id, - "metaData": "A normal string intead of json" - } - response = client.put(USER_URL+'?project_id='+str(new_project['projectId']+1), headers=headers_auth, json=update_data) - assert_input_validation_error(response) - - -def test_soft_delete(): - '''Check if unsetting active status works the desired way''' - data = [ - { - "projectName": "Test Project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - }, - { - "projectName": "Test Project 2", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - }, - { - "projectName": "Test Project 3", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - }, - { - "projectName": "Test Project 4", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - }, - { - "projectName": "Test Project 5", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - ] - - delete_data = [ - {"project_name": "Test Project 4"}, - {"project_name": "Test Project 5"} - ] - for item in data: - response = check_post(item) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - - get_response1 = client.get(UNIT_URL,headers=headers_auth) - assert len(get_response1.json()) >= len(data) - - - # positive PUT - for item in delete_data: - # find the project id - resp = client.get(UNIT_URL+"?project_name="+item['project_name'],headers=headers_auth) - assert resp.status_code == 200 - project_id = resp.json()[0]['projectId'] - - put_obj = {"active": False} - response = client.put(UNIT_URL+'?project_id='+str(project_id), headers=headers_auth, json=put_obj) - assert response.status_code == 201 - assert response.json()['message'] == 'Project updated successfully' - assert not response.json()['data']['active'] - - get_response2 = client.get(UNIT_URL,headers=headers_auth) - assert len(get_response2.json()) == len(get_response1.json()) - len(delete_data) - - get_response3 = client.get(UNIT_URL+'?active=false',headers=headers_auth) - assert len(get_response3.json()) >= len(delete_data) - - -#Access Rules and related Test - -#Project Access Rules based tests -def test_agmt_projects_access_rule(): - """create related access rule and auth""" - #with and without auth - post_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - response = client.post(UNIT_URL, headers=headers, json=post_data) - assert response.status_code == 401 - assert 'error' in response.json() - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - project1_id = response.json()['data']['projectId'] - - #create from app other than SanketMAST - post_data["projectName"] = "Test project 2" - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - headers_auth["app"] = "API-user" - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - post_data["projectName"] = "Test project 3" - headers_auth["app"] = "Vachan-online or vachan-app" - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - post_data["projectName"] = "Test project 4" - headers_auth["app"] = "VachanAdmin" - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - - #create project by not allowed users - post_data["projectName"] = "Test project 5" - headers_auth["app"] = "SanketMAST" - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['BcsDev']['token'] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanAdmin']['token'] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['APIUser']['token'] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanUser']['token'] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - - #Super Admin - SA_user_data = { - "user_email": SUPER_USER, - "password": SUPER_PASSWORD - } - response = login(SA_user_data) - assert response.json()['message'] == "Login Succesfull" - test_SA_token = response.json()["token"] - - #create with AGUser and SA - post_data["projectName"] = "Test project 6" - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - project6_id = response.json()['data']['projectId'] - post_data["projectName"] = "Test project 7" - headers_auth['Authorization'] = "Bearer"+" "+test_SA_token - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - project7_id = response.json()['data']['projectId'] - - #update Project - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - #update with Owner of project - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - response2 = client.put(UNIT_URL+'?project_id='+str(project6_id), headers=headers_auth, json=put_data) - assert response2.status_code == 201 - assert response2.json()['message'] == "Project updated successfully" - updated_project = response2.json()['data'] - assert updated_project['metaData']['books'] == ['mat', 'mrk'] - - #aguser project can be updated by super admin and SanketMAST admin - put_data = { - "projectName":"New name for old project6"} - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - response2 = client.put(UNIT_URL+'?project_id='+str(project6_id), headers=headers_auth, json=put_data) - assert response2.status_code == 201 - assert response2.json()['message'] == "Project updated successfully" - assert response2.json()['data']['projectName'] == put_data["projectName"] - put_data["projectName"] = "New name for project7 by SA" - headers_auth['Authorization'] = "Bearer"+" "+test_SA_token - response2 = client.put(UNIT_URL+'?project_id='+str(project6_id), headers=headers_auth, json=put_data) - assert response2.status_code == 201 - assert response2.json()['message'] == "Project updated successfully" - assert response2.json()['data']['projectName'] == put_data["projectName"] - - #update project with not Owner - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - response2 = client.put(UNIT_URL+'?project_id='+str(project7_id), headers=headers_auth, json=put_data) - assert response2.status_code == 403 - assert response2.json()['error'] == 'Permission Denied' - - #project user create - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - new_user_id = initial_test_users['SanketMASTUser']['test_user_id'] - #add user from another app than SanketMAST - headers_auth["app"] = "VachanAdmin" - response = client.post(USER_URL+'?project_id='+str(project1_id)+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - headers_auth["app"] = "Vachan-online or vachan-app" - response = client.post(USER_URL+'?project_id='+str(project1_id)+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - headers_auth["app"] = "API-user" - response = client.post(USER_URL+'?project_id='+str(project1_id)+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - #add user by not owner - headers_auth["app"] = "SanketMAST" - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - response = client.post(USER_URL+'?project_id='+str(project1_id)+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - #add from Autograpaha by allowed user - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - post_data["projectName"] = "Test project 8" - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - project8_id = response.json()['data']['projectId'] - - response = client.post(USER_URL+'?project_id='+str(project8_id)+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert response.status_code == 201 - assert response.json()['message'] == "User added to project successfully" - data = response.json()['data'] - assert data["project_id"] == project8_id - assert data["userId"] == new_user_id - assert data["userRole"] == "projectMember" - assert data['active'] - - #update user with not owner - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - update_data = { - "userId": new_user_id - } - # add metadata - meta = {"last_filter": "luk"} - update_data['metaData'] = meta - response3 = client.put(USER_URL+'?project_id='+str(project8_id), headers=headers_auth, json=update_data) - assert response3.status_code == 403 - assert response3.json()['error'] == 'Permission Denied' - #update with owner - post_data['projectName'] = 'Test project 1' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - response3 = client.put(USER_URL+'?project_id='+str(project8_id), headers=headers_auth, json=update_data) - assert response3.status_code == 201 - assert response3.json()['message'] == "User updated in project successfully" - -def test_get_project_access_rules(): - """test for get project access rules""" - #create projects - post_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - project1_id = response.json()['data']['projectId'] - - post_data["projectName"] = "Test project 2" - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - project2_id = response.json()['data']['projectId'] - - post_data["projectName"] = "Test project 3" - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - project3_id = response.json()['data']['projectId'] - - #Super Admin - SA_user_data = { - "user_email": SUPER_USER, - "password": SUPER_PASSWORD - } - response = login(SA_user_data) - assert response.json()['message'] == "Login Succesfull" - test_SA_token = response.json()["token"] - - #get project from apps other than autographa - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - headers_auth["app"] = "API-user" - response = client.get(UNIT_URL,headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - headers_auth["app"] = "Vachan-online or vachan-app" - response = client.get(UNIT_URL,headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - headers_auth["app"] = "VachanAdmin" - response = client.get(UNIT_URL,headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - - #get project from SanketMAST with not allowed users get empty result - headers_auth["app"] = "SanketMAST" - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanAdmin']['token'] - response = client.get(UNIT_URL,headers=headers_auth) - assert len(response.json()) == 0 - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['APIUser']['token'] - response = client.get(UNIT_URL,headers=headers_auth) - assert len(response.json()) == 0 - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanUser']['token'] - response = client.get(UNIT_URL,headers=headers_auth) - assert len(response.json()) == 0 - - #get all project by SA , SanketMASTAdmin, Bcs internal dev - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - response = client.get(UNIT_URL,headers=headers_auth) - assert len(response.json()) >= 3 - - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['BcsDev']['token'] - response = client.get(UNIT_URL,headers=headers_auth) - assert len(response.json()) >= 3 - - headers_auth['Authorization'] = "Bearer"+" "+ test_SA_token - response = client.get(UNIT_URL,headers=headers_auth) - assert len(response.json()) >= 3 - - #get project by project owner SanketMASTuser only get owner project - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - response = client.get(UNIT_URL,headers=headers_auth) - assert len(response.json()) >= 2 - - #create project by SA and add SanketMASTuser as member to the project - post_data["projectName"] = 'Test project 4' - headers_auth['Authorization'] = "Bearer"+" "+ test_SA_token - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - project4_id = response.json()['data']['projectId'] - - new_user_id = initial_test_users['SanketMASTUser']['test_user_id'] - response = client.post(USER_URL+'?project_id='+str(project4_id)+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert response.status_code == 201 - assert response.json()['message'] == "User added to project successfully" - data = response.json()['data'] - assert data["project_id"] == project4_id - assert data["userId"] == new_user_id - assert data["userRole"] == "projectMember" - assert data['active'] - - #get after add as member - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - response = client.get(UNIT_URL+"?user_id="+new_user_id,headers=headers_auth) - assert len(response.json()) >= 3 - for proj in response.json(): - assert proj['projectName'] in ["Test project 4","Test project 3","Test project 2"] - - #A new SanketMASTuser requesting for all projecrts - # test_ag_user_data = { - # "email": "testaguser@test.com", - # "password": "passwordag@1" - # } - # response = register(test_ag_user_data, apptype='SanketMAST') - # ag_user_id = [response.json()["registered_details"]["id"]] - # ag_user_token = response.json()["token"] - - #get projects where user have no projects result is [] - headers_auth["app"] = "SanketMAST" - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser2']['token'] - # headers_auth['Authorization'] = "Bearer"+" "+ag_user_token - response = client.get(UNIT_URL,headers=headers_auth) - assert len(response.json()) == 0 - # delete_user_identity(ag_user_id) - - -def test_create_n_update_times(): - '''Test to ensure created time and last updated time are included in project GET response''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - resp = client.get(UNIT_URL+'?project_name=Test project 1',headers=headers_auth) - assert_not_available_content(resp) - - # create with minimum data - post_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - new_project = response.json()['data'] - assert_positive_get(new_project) - project_id = response.json()['data']['projectId'] - - response = client.get(f"{UNIT_URL}?project_name={post_data['projectName']}",headers=headers_auth) - assert response.json()[0]["createTime"] is not None - assert response.json()[0]['updateTime'] is not None - assert response.json()[0]["createTime"] == response.json()[0]["updateTime"] - - time.sleep(10) - # create project 2 - post_data = { - "projectName": "Test project 2", - "sourceLanguageCode": "hi", - "targetLanguageCode": "en" - } - response1 = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response1.status_code == 201 - assert response1.json()['message'] == "Project created successfully" - new_project2 = response1.json()['data'] - assert_positive_get(new_project2) - print("///post resp1:",new_project2) - create_time_2 = new_project2['createTime'] - update_time_2 = new_project2['updateTime'] - assert create_time_2 == update_time_2 - - assert not new_project['createTime'] == create_time_2 - time.sleep(10) - - # Make an update to project - update_data = { - "metaData": {"last_filter": "luk"} - } - response2 = client.put(UNIT_URL+'?project_id='+str(project_id), headers=headers_auth, json=update_data) - assert response2.status_code == 201 - assert response2.json()['message'] == "Project updated successfully" - updated_project = response2.json()['data'] - print("response.json()[0][updateTime]:",response.json()[0]["updateTime"]) - print("response2.json()[0][updateTime]:",updated_project["updateTime"]) - assert response.json()[0]["updateTime"] != updated_project["updateTime"] - assert not response.json()[0]["updateTime"] == updated_project['updateTime'] - assert updated_project['updateTime'] > response.json()[0]["updateTime"] - - -def test_delete_project(): - '''Test the removal a project''' - - #Create Project - project_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - resp = check_post(project_data, auth_token=initial_test_users['SanketMASTAdmin']['token']) - assert resp.status_code == 201 - assert resp.json()['message'] == "Project created successfully" - new_project = resp.json()['data'] - project_id = new_project['projectId'] - assert_positive_get(new_project) - - # fetch project - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - get_response = client.get(UNIT_URL,headers=headers_auth) - assert len(get_response.json()) >= 1 - found_project = False - for proj in get_response.json(): - if proj['projectName'] == project_data['projectName']: - found_project = True - fetched_project = proj - assert found_project - assert_positive_get(fetched_project) - - assert fetched_project['projectName'] == project_data['projectName'] - assert fetched_project['sourceLanguage']['code'] == project_data['sourceLanguageCode'] - assert fetched_project['targetLanguage']['code'] == project_data['targetLanguageCode'] - - #Delete Project with no auth - resp = client.delete(UNIT_URL+'?project_id='+str(new_project['projectId']),headers=headers) - assert resp.status_code == 401 - assert resp.json()['details'] == "Access token not provided or user not recognized." - - # deleting non existing project - invalid_project_id = 9999 - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - resp = client.delete(UNIT_URL+'?project_id='+str(invalid_project_id),headers=headers_auth) - assert resp.status_code == 404 - assert resp.json()['details'] == f"Project with id, {invalid_project_id}, not present" - - # Delete as unauthorized users - for user in ['APIUser','VachanAdmin','VachanUser','BcsDev','VachanContentAdmin','VachanContentViewer']: - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users[user]['token'] - response =client.delete(UNIT_URL+'?project_id='+str(project_id),headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - - # Delete as SanketMASTAdmin- Positive test - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - resp = client.delete(UNIT_URL+'?project_id='+str(project_id),headers=headers_auth) - assert resp.status_code == 201 - assert "successfull" in resp.json()['message'] - - #Ensure deleted project is not present - resp = client.get(UNIT_URL+'?project_name=Test project 1',headers=headers_auth) - assert_not_available_content(resp) - - #Create and Delete Project with SanketMASTUser - Positive Test - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - post_resp_aguser = check_post(project_data, auth_token=initial_test_users['SanketMASTUser']['token']) - assert post_resp_aguser.status_code == 201 - #Ensure presence of created project - get_response = client.get(UNIT_URL,headers=headers_auth) - assert len(get_response.json()) >= 1 - found_project = False - for proj in get_response.json(): - if proj['projectName'] == project_data['projectName']: - found_project = True - fetched_project = proj - assert found_project - assert_positive_get(fetched_project) - #Get project Id - new_project = post_resp_aguser.json()['data'] - project_id = new_project['projectId'] - #Delete - delete_resp_aguser = client.delete(UNIT_URL+'?project_id='+str(project_id),headers=headers_auth) - assert delete_resp_aguser.status_code == 201 - #Ensure deleted project is not present - resp = client.get(UNIT_URL+'?project_name=Test project 1',headers=headers_auth) - assert_not_available_content(resp) - - #Create and Delete Project with SuperAdmin - Positive Test - #Login as Super Admin - sa_data = { - "user_email": SUPER_USER, - "password": SUPER_PASSWORD - } - response = login(sa_data) - assert response.json()['message'] == "Login Succesfull" - test_user_token = response.json()["token"] - headers_auth['Authorization'] = "Bearer"+" "+test_user_token - post_resp_sa = check_post(project_data, auth_token=test_user_token) - assert post_resp_sa.status_code == 201 - #Ensure presence of created project - get_response = client.get(UNIT_URL,headers=headers_auth) - assert len(get_response.json()) >= 1 - found_project = False - for proj in get_response.json(): - if proj['projectName'] == project_data['projectName']: - found_project = True - fetched_project = proj - assert found_project - assert_positive_get(fetched_project) - #Get project Id - new_project = post_resp_sa.json()['data'] - project_id = new_project['projectId'] - #Delete - delete_resp_sa= client.delete(UNIT_URL+'?project_id='+str(project_id),headers=headers_auth) - assert delete_resp_sa.status_code == 201 - #Ensure deleted project is not present - resp = client.get(UNIT_URL+'?project_name=Test project 1',headers=headers_auth) - assert_not_available_content(resp) - logout_user(test_user_token) - -def test_restore_project(): - '''positive test case, checking for correct return object''' - #only Super Admin can restore deleted data - #Creating and Deleting project - project_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - post_resp= check_post(project_data, auth_token=initial_test_users['SanketMASTAdmin']['token']) - assert post_resp.status_code == 201 - - #Get project Id - project_id = post_resp.json()['data']['projectId'] - - #Delete - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - delete_resp= client.delete(UNIT_URL+'?project_id='+str(project_id),headers=headers_auth) - assert delete_resp.status_code == 201 - #Ensure deleted project is not present - resp = client.get(UNIT_URL+'?project_name=Test project 1',headers=headers_auth) - assert_not_available_content(resp) - - deleteditem_id = delete_resp.json()['data']['itemId'] - data = {"itemId": deleteditem_id} - - #Restoring data - #Restore without authentication - Negative Test - response = client.put(RESTORE_URL, headers=headers, json=data) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - - #Restore project with other API user,VachanAdmin,SanketMASTAdmin,SanketMASTUser,VachanUser,BcsDev and APIUSer2 - Negative Test - for user in ['APIUser','VachanAdmin','SanketMASTAdmin','SanketMASTUser','VachanUser','BcsDev','APIUser2','VachanContentAdmin','VachanContentViewer']: - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users[user]['token'] - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - - #Restore Project with Super Admin - Positive Test - # Login as Super Admin - sa_data = { - "user_email": SUPER_USER, - "password": SUPER_PASSWORD - } - response = login(sa_data) - assert response.json()['message'] == "Login Succesfull" - test_user_token = response.json()["token"] - headers_auth['Authorization'] = "Bearer"+" "+test_user_token - - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 201 - assert response.json()['message'] == \ - f"Deleted Item with identity {deleteditem_id} restored successfully" - - #Ensure presence of restored project - get_response = client.get(UNIT_URL,headers=headers_auth) - assert len(get_response.json()) >= 1 - found_project = False - for proj in get_response.json(): - if proj['projectName'] == project_data['projectName']: - found_project = True - fetched_project = proj - assert found_project - assert_positive_get(fetched_project) - - #restore with missing data - Negative Test - data = {} - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert_input_validation_error(response) - - #Restore with invalid item id - Negative Test - data = {"itemId":9999} - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 404 - assert response.json()['error'] == "Requested Content Not Available" - logout_user(test_user_token) - -def test_delete_user(): - '''Test the removal of a user from a project''' - project_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - resp = check_post(project_data, auth_token=initial_test_users['SanketMASTAdmin']['token']) - assert resp.json()['message'] == "Project created successfully" - new_project = resp.json()['data'] - new_user_id = initial_test_users['SanketMASTUser']['test_user_id'] - - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - resp = client.post(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert resp.json()['message'] == "User added to project successfully" - - # fetch this project and check for new user - check_project_user(project_data['projectName'], new_user_id, role='projectMember') - - #no auth - resp = client.delete(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(new_user_id),headers=headers) - assert resp.status_code == 401 - assert resp.json()['details'] == "Access token not provided or user not recognized." - check_project_user(project_data['projectName'], new_user_id, role='projectMember') - - # deleting non existing user - second_user_id = initial_test_users['SanketMASTUser2']['test_user_id'] - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - resp = client.delete(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(second_user_id),headers=headers_auth) - assert resp.status_code == 404 - assert resp.json()['details'] == "User-project pair not found" - - resp = client.post(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(second_user_id),headers=headers_auth) - assert resp.json()['message'] == "User added to project successfully" - check_project_user(project_data['projectName'], second_user_id, role='projectMember') - - # as non-owner user - user_id = initial_test_users['SanketMASTUser2']['test_user_id'] - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - resp = client.delete(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(user_id),headers=headers_auth) - assert resp.status_code == 403 - assert resp.json()['error'] == "Permission Denied" - check_project_user(project_data['projectName'], user_id, role='projectMember') - - # as same user - user_id = initial_test_users['SanketMASTAdmin']['test_user_id'] - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - resp = client.delete(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(user_id),headers=headers_auth) - assert resp.status_code == 403 - assert resp.json()['details'] == "A user cannot remove oneself from a project." - check_project_user(project_data['projectName'], user_id, role='projectOwner') - - # as project owner - Positive test - user_id = initial_test_users['SanketMASTUser2']['test_user_id'] - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - resp =client.delete(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(user_id),headers=headers_auth) - assert resp.status_code == 201 - assert "successfull" in resp.json()['message'] - - # Check get project to ensure deleted user is not present - get_project_response = client.get(UNIT_URL+'?project_id='+str(new_project['projectId']),headers=headers_auth) - found_user = False - - for user in get_project_response.json()[0]['users']: - if user['userId'] == user_id: - found_user = True - assert user['userRole'] is 'projectMember' - assert user['active'] is True - assert user['metaData'] is None - assert not found_user -def test_delete_user_with_projectowner(): - ''' Bugfix for https://github.com/Bridgeconn/vachan-api/issues/683''' - - # creating project with SanketMASTUser - project_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - resp = check_post(project_data, auth_token=initial_test_users['SanketMASTUser']['token']) - assert resp.json()['message'] == "Project created successfully" - new_project = resp.json()['data'] - - #Adding a new user SanketMASTUser2 as projectMember - new_user_id = initial_test_users['SanketMASTUser2']['test_user_id'] - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - resp = client.post(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert resp.json()['message'] == "User added to project successfully" - - # fetch this project and check for new user - check_project_user(project_data['projectName'], new_user_id, role='projectMember') - - # Deleting SanketMASTUser2 with projectOwner - user_id = initial_test_users['SanketMASTUser2']['test_user_id'] - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - resp = client.delete(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(user_id),headers=headers_auth) - assert resp.status_code == 201 - assert "successfull" in resp.json()['message'] - - # Check get project to ensure deleted user is not present - get_project_response = client.get(UNIT_URL+'?project_id='+str(new_project['projectId']),headers=headers_auth) - found_user = False - - for user in get_project_response.json()[0]['users']: - if user['userId'] == user_id: - found_user = True - assert user['userRole'] is 'projectMember' - assert user['active'] is True - assert user['metaData'] is None - assert not found_user - -def test_delete_user_with_projectowner(): - ''' Bugfix for https://github.com/Bridgeconn/vachan-api/issues/683''' - - # creating project with SanketMASTUser - project_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - resp = check_post(project_data, auth_token=initial_test_users['SanketMASTUser']['token']) - assert resp.json()['message'] == "Project created successfully" - new_project = resp.json()['data'] - - #Adding a new user SanketMASTUser2 as projectMember - new_user_id = initial_test_users['SanketMASTUser2']['test_user_id'] - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - resp = client.post(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(new_user_id),headers=headers_auth) - assert resp.json()['message'] == "User added to project successfully" - - # fetch this project and check for new user - check_project_user(project_data['projectName'], new_user_id, role='projectMember') - - # Deleting SanketMASTUser2 with projectOwner - user_id = initial_test_users['SanketMASTUser2']['test_user_id'] - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - resp = client.delete(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(user_id),headers=headers_auth) - assert resp.status_code == 201 - assert "successfull" in resp.json()['message'] - - # Check get project to ensure deleted user is not present - get_project_response = client.get(UNIT_URL+'?project_id='+str(new_project['projectId']),headers=headers_auth) - found_user = False - - for user in get_project_response.json()[0]['users']: - if user['userId'] == user_id: - found_user = True - assert user['userRole'] is 'projectMember' - assert user['active'] is True - assert user['metaData'] is None - assert not found_user - -def test_restore_user(): - '''positive test case, checking for correct return object''' - #only Super Admin can restore deleted data - #Creating and Deleting project user - project_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - resp = check_post(project_data, auth_token=initial_test_users['SanketMASTAdmin']['token']) - assert resp.json()['message'] == "Project created successfully" - # Get project Id - project_id = resp.json()['data']['projectId'] - new_project = resp.json()['data'] - user_id = initial_test_users['SanketMASTUser']['test_user_id'] - - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - resp = client.post(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(user_id),headers=headers_auth) - assert resp.json()['message'] == "User added to project successfully" - - # fetch this project and check for new user - check_project_user(project_data['projectName'], user_id, role='projectMember') - - #Delete - # user_id = initial_test_users['SanketMASTUser']['test_user_id'] - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - delete_resp = client.delete(USER_URL+'?project_id='+str(new_project['projectId'])+ - '&user_id='+str(user_id),headers=headers_auth) - assert delete_resp.status_code == 201 - assert "successfull" in delete_resp.json()['message'] - - #Ensure deleted user is not present - get_project_response = client.get(UNIT_URL+'?project_id='+str(new_project['projectId']),headers=headers_auth) - found_user = False - - for user in get_project_response.json()[0]['users']: - if user['userId'] == user_id: - found_user = True - assert user['userRole'] is 'projectMember' - assert user['active'] is True - assert user['metaData'] is None - assert not found_user - - - - deleteditem_id = delete_resp.json()['data']['itemId'] - data = {"itemId": deleteditem_id} - - #Restoring data - #Restore user without authentication - Negative Test - response = client.put(RESTORE_URL, headers=headers, json=data) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - - #Restore project user with other API user,VachanAdmin,SanketMASTAdmin,SanketMASTUser,VachanUser,BcsDev and APIUSer2 - Negative Test - for user in ['APIUser','VachanAdmin','SanketMASTAdmin','SanketMASTUser','VachanUser','BcsDev','APIUser2','VachanContentAdmin','VachanContentViewer']: - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users[user]['token'] - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - - #Restore Project User with Super Admin - Positive Test - # Login as Super Admin - sa_data = { - "user_email": SUPER_USER, - "password": SUPER_PASSWORD - } - response = login(sa_data) - assert response.json()['message'] == "Login Succesfull" - test_user_token = response.json()["token"] - headers_auth['Authorization'] = "Bearer"+" "+test_user_token - - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 201 - assert response.json()['message'] == \ - f"Deleted Item with identity {deleteditem_id} restored successfully" - - #Ensure presence of restored user - check_project_user(project_data['projectName'], user_id, role='projectMember') - - #restore with missing data - Negative Test - data = {} - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert_input_validation_error(response) - - #Restore with invalid item id - Negative Test - data = {"itemId":9999} - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 404 - assert response.json()['error'] == "Requested Content Not Available" - logout_user(test_user_token) - -def test_bugfix_split_n_merged_verse(): - '''BUg fix for https://github.com/Bridgeconn/vachan-api/issues/543''' - post_data = { - "projectName": "Test project usfm upload", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - project_id = response.json()['data']['projectId'] - - prj_book_data = { - "uploadedUSFMs": [ - "\\id REV\n\\c 1\n\\p\n\\v 1 some text\n\\v 2-10 merged text\n\\v 11a split text\n\\v 11b rest" - ] - } - prj_update_resp = client.put(UNIT_URL+'?project_id='+str(project_id), json=prj_book_data, headers=headers_auth) - assert prj_update_resp.status_code == 201 - resp_obj = prj_update_resp.json() - assert resp_obj['message'] == 'Project updated successfully' - - sentences_resp = client.get(f"{SENTENCE_URL}?project_id={project_id}", headers=headers_auth) - assert sentences_resp.status_code == 200 - sentences = sentences_resp.json() - assert len(sentences) == 3 - assert sentences[0]['sentenceId'] == 67001001 - assert sentences[0]['surrogateId'] == 'rev 1:1' - assert sentences[0]["sentence"] == 'some text' - assert sentences[1]['sentenceId'] == 67001002 - assert sentences[1]['surrogateId'] == 'rev 1:2-10' - assert sentences[1]["sentence"] == 'merged text' - assert sentences[2]['sentenceId'] == 67001011 - assert sentences[2]['surrogateId'] == 'rev 1:11a-b' - assert sentences[2]["sentence"] == 'split text rest' - -def test_post_put_app_compatibility(): - '''Positive test to check app compatibility - * Only SanketMAST app can access translation APIs''' - headers_auth = {"contentType": "application/json", - "accept": "application/json", - "app":"SanketMAST" - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - - # create with minimum data - post_data = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - - #Creating new project with out adding compatibleWith field - Positive Test - #app value in the header will be auto assigned as compatibleWith field - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - response.json()['data']['compatibleWith'] = ["SanketMAST"] - assert_positive_get( response.json()['data']) - - # Creating new project with compatible app which is not passed as a list - Negative Test - post_data['compatibleWith'] = "SanketMAST" - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert_input_validation_error(response) - - # Creating new project with compatible app passed as a list - Positive Test - post_data["projectName"] = "Test Project 2" - post_data["compatibleWith"] = ["SanketMAST","Autographa"] - response = client.post(UNIT_URL, headers=headers_auth, json=post_data) - assert response.status_code == 201 - assert response.json()['message'] == "Project created successfully" - new_project = response.json()['data'] - assert_positive_get(new_project) - - # check if all defaults are coming - assert new_project['metaData']["useDataForLearning"] - assert isinstance(new_project['metaData']['books'], list) - assert len(new_project['metaData']['books']) == 0 - assert new_project['active'] - - # update data with compatible app not passed as a list - negative test - put_data = { - "uploadedUSFMs":[bible_books['mat']] - } - put_data["compatibleWith"] = "SanketMAST" - response2 = client.put(UNIT_URL+'?project_id='+str(new_project['projectId']),\ - headers=headers_auth, json=put_data) - assert_input_validation_error(response2) - - # update data with compatible app passed as a list - positive test - put_data = { - "uploadedUSFMs":[bible_books['mat']] - } - put_data["compatibleWith"] = ["Autographa","VachanAdmin"] - response2 = client.put(UNIT_URL+'?project_id='+str(new_project['projectId']), \ - headers=headers_auth, json=put_data) - assert response2.status_code == 201 - assert response2.json()['message'] == "Project updated successfully" - updated_project = response2.json()['data'] - assert_positive_get(updated_project) - - assert new_project['projectId'] == updated_project['projectId'] - assert new_project['projectName'] == updated_project['projectName'] - assert updated_project['metaData']['books'] == ['mat'] - - # update project with incompatible app - Negative Test - put_data = { - "uploadedUSFMs":[bible_books['mrk']], - "compatibleWith": ["VachanAdmin"] - } - response2 = client.put(UNIT_URL+'?project_id='+str(new_project['projectId']), \ - headers=headers_auth, json=put_data) - assert response2.status_code == 403 - assert response2.json()['details'] == "Incompatible app" - -def test_delete_with_compatible_app(): - '''Test to delete project based on app compatibiblty''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - data_1 = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml", - "compatibleWith": ["SanketMAST"] - } - data_2 = { - "projectName": "Test project 2", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml", - "compatibleWith": ["SanketMAST","Autographa"] - } - # creating two projects - response1 = client.post(UNIT_URL, headers=headers_auth, json=data_1) - project1 = response1.json()['data'] - project_id_1 = project1['projectId'] - response2 = client.post(UNIT_URL, headers=headers_auth, json=data_2) - project2 = response2.json()['data'] - project_id_2 = project2['projectId'] - - # delete project1 with compatible app - Positive Test - resp = client.delete(UNIT_URL+'?project_id='+str(project_id_1),headers=headers_auth) - assert resp.status_code == 201 - assert resp.json()['message'] == f"Project with identity {project_id_1} deleted successfully" - # update compatibility of project 2 - put_data = { - "compatibleWith": ["VachanAdmin"] - } - response2 = client.put(UNIT_URL+'?project_id='+str(project_id_2), \ - headers=headers_auth, json=put_data) - - # delete project with incompatible app - Negative Test - resp = client.delete(UNIT_URL+'?project_id='+str(project_id_2),headers=headers_auth) - assert resp.status_code == 403 - assert resp.json()['details'] == "Incompatible app" - -def test_get_filter_with_app_compatibility(): - '''Test to filter with app compatibility''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - data_1 = { - "projectName": "Test project 1", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml", - "compatibleWith": ["SanketMAST"] - } - data_2 = { - "projectName": "Test project 2", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" - } - data_3 = { - "projectName": "Test project 3", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml", - "compatibleWith": ["SanketMAST","Autographa"] - } - # Creating new project with compatible app - Positive Test - response1 = client.post(UNIT_URL, headers=headers_auth, json=data_1) - # Creating new project without mentioning compatible_with field - Positive test - response2 = client.post(UNIT_URL, headers=headers_auth, json=data_2) - # Creating new project with multiple items in compatible_with field - Positive test - response3 = client.post(UNIT_URL, headers=headers_auth, json=data_3) - - #filtering with single app - passing app not as a list - Negative test - app = "SanketMAST" - params = [] - for item in app: - params.append(("compatible_with", item)) - get_resp = client.get(UNIT_URL ,params=params,headers=headers_auth) - assert_input_validation_error(get_resp) - - # filtering with single app - positive test - app = ["SanketMAST"] - params = [] - for item in app: - params.append(("compatible_with", item)) - get_resp = client.get(UNIT_URL ,params=params,headers=headers_auth) - for item in get_resp.json(): - assert_positive_get(item) - assert len(get_resp.json()) == 3 - assert"SanketMAST" in get_resp.json()[0]['compatibleWith'] - assert"SanketMAST" in get_resp.json()[1]['compatibleWith'] - assert"SanketMAST" in get_resp.json()[2]['compatibleWith'] - - #filtering with multiple apps - positive test - app_list = [schema_auth.App.SMAST.value, schema_auth.App.AG.value] - params = [] - for app in app_list: - params.append(("compatible_with", app)) - get_resp2 = client.get(UNIT_URL, params=params, headers=headers_auth) - for item in get_resp2.json(): - assert_positive_get(item) - assert len(get_resp2.json()) == 1 - for item in get_resp2.json(): - assert_positive_get(item) - assert "SanketMAST" in item['compatibleWith'] - assert "Autographa" in item['compatibleWith'] - - #filtering with app not in the list - get_resp = client.get(UNIT_URL + "?compatible_with=VachanAdmin",headers=headers_auth) - assert_not_available_content(get_resp) \ No newline at end of file diff --git a/app/test/test_smast_translation.py b/app/test/test_smast_translation.py deleted file mode 100644 index 89c088bc..00000000 --- a/app/test/test_smast_translation.py +++ /dev/null @@ -1,1108 +0,0 @@ -'''tests for the translation workflow within SanketMAST projects''' -import json -import re -from math import ceil, floor -from . import client -from . import assert_input_validation_error, assert_not_available_content -from .test_agmt_projects import bible_books -from .conftest import initial_test_users -from . test_auth_basic import login,SUPER_PASSWORD,SUPER_USER - - -UNIT_URL = '/v2/text/translate/token-based/project' -PROJECT_URL = '/v2/text/translate/token-based/projects' -headers = {"contentType": "application/json", "accept": "application/json","app":"SanketMAST"} -headers_auth = {"contentType": "application/json", - "accept": "application/json", - "app":"SanketMAST" - } - -project_data = { - "projectName": "Test SanketMAST workflow", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" -} - -sample_stopwords = ["के", "की"] - -def assert_positive_get_tokens(item): - '''common tests for a token response object''' - assert "token" in item - assert "occurrences" in item - assert len(item['occurrences']) > 0 - assert "translations" in item - for trans in item['translations']: - assert isinstance(item['translations'][trans], (int, float)) - if "metaData" in item and item['metaData'] is not None: - assert isinstance(item['metaData'], dict) - -def assert_positive_get_sentence(item): - '''common test for senstence object''' - assert "sentenceId" in item - assert "sentence" in item - assert isinstance(item['sentence'], str) - if "draft" in item: - assert isinstance(item['draft'], str) - assert "draftMeta" in item - assert isinstance(item['draftMeta'], list) - -def test_get_tokens(): - '''Positive tests for tokenization process''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - resp = client.post(PROJECT_URL, headers=headers_auth, json=project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - # before adding books - get_response1 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id),headers=headers_auth) - assert_not_available_content(get_response1) - - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id), headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - # after adding books - #without auth - get_response2 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id), headers=headers) - assert get_response2.json()['error'] == 'Authentication Error' - get_response2 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id),headers=headers) - assert get_response2.status_code == 401 - assert get_response2.json()['error'] == 'Authentication Error' - #with auth - get_response2 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id),headers=headers_auth) - assert get_response2.status_code == 200 - assert isinstance(get_response2.json(), list) - for item in get_response2.json(): - assert_positive_get_tokens(item) - - # with book filter - get_response3 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+"&books=mat" - ,headers=headers_auth) - assert get_response3.status_code == 200 - assert len(get_response3.json()) < len(get_response2.json()) - - get_response4 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+"&books=mrk" - ,headers=headers_auth) - assert get_response4.status_code == 200 - all_tokens = [item['token'] for item in get_response3.json() + get_response4.json()] - assert len(get_response2.json()) == len(set(all_tokens)) - - # with range filter - get_response5 = client.get(UNIT_URL+'/tokens?project_id='+str(project_id)+ - "&sentence_id_range=0&sentence_id_range=10",headers=headers_auth) - # print(get_response5.json()) - assert_not_available_content(get_response5) - - get_response6 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&sentence_id_range=41000000&sentence_id_range=41999999",headers=headers_auth) - assert get_response6.status_code ==200 - assert get_response6.json() == get_response3.json() - - # with list filter - get_response7 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&sentence_id_list=41000000",headers=headers_auth) - assert_not_available_content(get_response7) - - get_response7 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&sentence_id_list=41001001",headers=headers_auth) - assert get_response7.status_code == 200 - assert 0 < len(get_response7.json()) < 25 - - # translation_memory flag - get_response8 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&use_translation_memory=true",headers=headers_auth) - assert get_response8.json() == get_response2.json() - - get_response9 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&use_translation_memory=false",headers=headers_auth) - assert get_response9.status_code == 200 - assert len(get_response9.json()) > 0 - - # include_phrases flag - get_response10 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&include_phrases=true",headers=headers_auth) - assert get_response10.json() == get_response2.json() - - get_response11 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&include_phrases=false",headers=headers_auth) - assert get_response11.status_code == 200 - assert len(get_response11.json()) <= len(get_response10.json()) - for item in get_response11.json(): - assert " " not in item['token'] - - # include_stopwords flag - get_response12 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&include_stopwords=false",headers=headers_auth) - assert get_response12.json() == get_response2.json() - for item in get_response12.json(): - assert item['token'] not in sample_stopwords - - get_response13 = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&include_stopwords=True&include_phrases=false",headers=headers_auth) - all_tokens = [item['token'] for item in get_response13.json()] - assert sample_stopwords[0] in all_tokens - - -def test_tokenization_invalid(): - '''Negative tests for tokenization''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - resp = client.post(PROJECT_URL, headers=headers_auth, json=project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - # non existant project - response = client.get(UNIT_URL+"/tokens?project_id="+str(project_id+1),headers=headers_auth) - assert response.status_code == 404 - assert response.json()['details'] == "Project with id, %s, not present"%(project_id+1) - - #invalid book - response = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+"&books=mmm" - ,headers=headers_auth) - assert response.status_code == 404 - assert response.json()['details'] == 'Book, mmm, not in database' - - # only one value for range - response = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&sentence_id_range=41000000",headers=headers_auth) - assert_input_validation_error(response) - - # incorrect value for range - response = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&sentence_id_range=gen&sentence_id_range=num",headers=headers_auth) - assert_input_validation_error(response) - - # incorrect value for id - response = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&sentence_id_list=first",headers=headers_auth) - assert_input_validation_error(response) - - # incorrect value for flags - response = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&include_stopwords=Few",headers=headers_auth) - assert_input_validation_error(response) - - response = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&include_phrases=10",headers=headers_auth) - assert_input_validation_error(response) - - response = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&use_translation_memory=always",headers=headers_auth) - assert_input_validation_error(response) - -def test_save_translation(): - '''Positive tests for PUT tokens method''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - resp = client.post(PROJECT_URL, headers=headers_auth, json=project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - resp = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&include_stopwords=true",headers=headers_auth) - assert resp.status_code == 200 - all_tokens = resp.json() - - # one occurence - post_obj_list = [ - { - "token": all_tokens[0]['token'], - "occurrences": [all_tokens[0]["occurrences"][0]], - "translation": "test translation" - } - ] - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert response.status_code == 201 - assert response.json()['message'] == 'Token translations saved' - assert response.json()['data'][0]['draft'].startswith("test translation") - assert response.json()['data'][0]['draftMeta'][0][2] == "confirmed" - for segment in response.json()['data'][0]['draftMeta'][1:]: - assert segment[2] != "confirmed" - - # multiple occurances - post_obj_list = [ - { - "token": all_tokens[0]['token'], - "occurrences": all_tokens[0]["occurrences"], - "translation": "test translation" - } - ] - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert response.status_code == 201 - assert response.json()['message'] == 'Token translations saved' - for sent in response.json()['data']: - assert "test translation" in sent['draft'] - - #Without auth - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - json=post_obj_list, headers=headers) - assert response.json()['error'] == 'Authentication Error' - - # all tokens at once - post_obj_list = [] - for item in all_tokens: - obj = { - "token": item['token'], - "occurrences": item["occurrences"], - "translation": "test" - } - post_obj_list.append(obj) - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert response.status_code == 201 - for sent in response.json()['data']: - words = re.findall(r'\w+', sent['draft']) - for wrd in words: - assert wrd == 'test' - -def test_save_translation_invalid(): - '''Negative tests for PUT tokens method''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - resp = client.post(PROJECT_URL, headers=headers_auth, json=project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - resp = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&include_stopwords=true",headers=headers_auth) - assert resp.status_code == 200 - all_tokens = resp.json() - - # incorrect project id - obj = { - "token": all_tokens[0]['token'], - "occurrences": all_tokens[0]["occurrences"], - "translation": "sample translation" - } - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id+1), - headers=headers_auth, json=[obj]) - assert response.status_code == 404 - assert response.json()['details'] == 'Project with id, %s, not present'%(project_id+1) - - # without token - obj = { - "occurrences": all_tokens[0]["occurrences"], - "translation": "sample translation" - } - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id+1), - headers=headers_auth, json=[obj]) - assert_input_validation_error(response) - - # without occurences - obj = { - "token": all_tokens[0]['token'], - "translation": "sample translation" - } - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id+1), - headers=headers_auth, json=[obj]) - assert_input_validation_error(response) - - # without translation - obj = { - "token": all_tokens[0]['token'], - "occurrences": all_tokens[0]["occurrences"] - } - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id+1), - headers=headers_auth, json=[obj]) - assert_input_validation_error(response) - - # incorrect occurences - wrong_occur = all_tokens[0]["occurrences"][0] - wrong_occur['sentenceId'] = 0 - obj = { - "token": all_tokens[0]['token'], - "occurrences": [wrong_occur], - "translation": "sample translation" - } - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=[obj]) - assert response.status_code ==404 - assert response.json()['details'] == "Sentence id, 0, not found for the selected project" - - # wrong token-occurence pair - obj = { - "token": all_tokens[0]['token'], - "occurrences": all_tokens[1]['occurrences'], - "translation": "sample translation" - } - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=[obj]) - assert response.status_code == 500 - assert response.json()['details'] ==\ - 'Token, %s, and its occurence, not matching'%(all_tokens[0]['token']) - -def test_drafts(): - '''End to end test from tokenization to draft generation''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - resp = client.post(PROJECT_URL, headers=headers_auth, json=project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - resp = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&include_stopwords=true",headers=headers_auth) - assert resp.status_code == 200 - all_tokens = resp.json() - - # translate all tokens at once - post_obj_list = [] - for item in all_tokens: - obj = { - "token": item['token'], - "occurrences": item["occurrences"], - "translation": "test" - } - post_obj_list.append(obj) - resp = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert resp.status_code == 201 - assert resp.json()['message'] == "Token translations saved" - - response = client.get(UNIT_URL+'/draft?project_id='+str(project_id),headers=headers_auth) - assert response.status_code ==200 - assert isinstance(response.json(), list) - assert "\\v 1 test test test" in response.json()[0] - - response = client.get(UNIT_URL+'/draft?project_id='+str(project_id)+ - "&books=mat",headers=headers_auth) - assert len(response.json()) == 1 - assert "\\id MAT" in response.json()[0] - - #Without Auth - response = client.get(UNIT_URL+'/draft?project_id='+str(project_id)+ - "&books=mat", headers=headers) - assert response.json()['error'] == 'Authentication Error' - response = client.get(UNIT_URL+'/draft?project_id='+str(project_id)+ - "&books=mat",headers=headers) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - - # To be added: proper tests for alignment json drafts - response = client.get(UNIT_URL+'/draft?project_id='+str(project_id)+ - "&output_format=alignment-json",headers=headers_auth) - assert response.status_code == 200 - assert isinstance(response.json(), dict) - -def test_get_token_sentences(): - '''Check if draft-meta is properly segemneted according to specifed token occurence''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - resp = client.post(PROJECT_URL, headers=headers_auth, json=project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - resp = client.get("/v2/text/translate/token-based/project/tokens?project_id="+str(project_id) - ,headers=headers_auth) - tokens = resp.json() - our_token = tokens[0]['token'] - occurrences = tokens[0]['occurrences'] - - #before translating - response = client.put('/v2/text/translate/token-based/project/token-sentences?project_id='+ - str(project_id)+'&token='+our_token, - json=occurrences, headers=headers_auth) - assert response.status_code == 200 - for sent, occur in zip(response.json(), occurrences): - assert_positive_get_sentence(sent) - found_slice = False - if sent['sentenceId'] == occur["sentenceId"]: - for meta in sent['draftMeta']: - if meta[0] == occur['offset']: - found_slice = True - assert found_slice - - post_obj_list = [ - { - "token": our_token, - "occurrences": occurrences, - "translation": "sample" - } - ] - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert response.status_code == 201 - assert response.json()['message'] == 'Token translations saved' - - # after translation - response2 = client.put('/v2/text/translate/token-based/project/token-sentences?project_id='+ - str(project_id)+'&token='+our_token, headers=headers_auth, - json=occurrences) - assert response2.status_code == 200 - for sent, occur in zip(response2.json(), occurrences): - found_slice = False - if sent['sentenceId'] == occur["sentenceId"]: - for meta in sent['draftMeta']: - if meta[0] == occur['offset']: - found_slice = True - assert meta[2] == "confirmed" - assert found_slice - - #Without auth - response2 = client.put('/v2/text/translate/token-based/project/token-sentences?project_id='+ - str(project_id)+'&token='+our_token, json=occurrences) - assert response2.json()['error'] == 'Permission Denied' - -def test_get_sentence(): - '''Positive test for agmt sentence/draft fetch API''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - resp = client.post(PROJECT_URL, headers=headers_auth, json=project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - # before adding books - response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id) - ,headers=headers_auth) - assert_not_available_content(response) - - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - # before translation - response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&with_draft=True",headers=headers_auth) - assert response.status_code ==200 - assert len(response.json()) > 1 - for item in response.json(): - assert_positive_get_sentence(item) - assert item['sentence'] != "" - assert item['draft'] == "" - - - # translate all tokens at once - resp = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&include_stopwords=true",headers=headers_auth) - assert resp.status_code == 200 - all_tokens = resp.json() - post_obj_list = [] - for item in all_tokens: - obj = { - "token": item['token'], - "occurrences": item["occurrences"], - "translation": "test" - } - post_obj_list.append(obj) - resp = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert resp.status_code == 201 - assert resp.json()['message'] == "Token translations saved" - - # after token translation - response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&with_draft=True",headers=headers_auth) - assert response.status_code ==200 - for item in response.json(): - assert_positive_get_sentence(item) - words = re.findall(r'\w+',item['draft']) - for wrd in words: - assert wrd == "test" - - #without auth - response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&with_draft=True", headers=headers) - assert response.json()['error'] == 'Authentication Error' - response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&with_draft=True",headers=headers) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - - # With only_ids - response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&only_ids=True",headers=headers_auth) - assert response.status_code ==200 - for item in response.json(): - assert "sentenceId" in item - assert "surrogateId" in item - assert "sentence" not in item - assert "draft" not in item - assert "draftMeta" not in item - -def test_progress_n_suggestion(): - '''tests for project progress API of SanketMASTMT''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - resp = client.post(PROJECT_URL, headers=headers_auth, json=project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - # before adding books - response = client.get(UNIT_URL+"/progress?project_id="+str(project_id) - ,headers=headers_auth) - assert response.status_code ==200 - assert response.json()['confirmed'] == 0 - assert response.json()['untranslated'] == 0 - assert response.json()['suggestion'] == 0 - - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - # before translation - response = client.get(UNIT_URL+"/progress?project_id="+str(project_id) - ,headers=headers_auth) - assert response.status_code ==200 - assert response.json()['confirmed'] == 0 - assert response.json()['untranslated'] == 1 - assert response.json()['suggestion'] == 0 - - # # Apply suggestions - # resp = client.put(UNIT_URL+"/suggestions?project_id="+str(project_id)) - # assert resp.status_code ==201 - # found_suggestion = False - # [print(sent['draft']) for sent in resp.json()] - # for sent in resp.json(): - # for meta in sent['draftMeta']: - # if meta[2] == 'suggestion': - # found_suggestion = True - # break - # assert found_suggestion - - # # after suggestions - # response = client.get(UNIT_URL+"/progress?project_id="+str(project_id)) - # assert response.status_code ==200 - # assert response.json()['suggestion'] > 0 - - # translate all tokens at once - resp = client.get(UNIT_URL+"/tokens?project_id="+str(project_id)+ - "&include_stopwords=true",headers=headers_auth) - assert resp.status_code == 200 - all_tokens = resp.json() - post_obj_list = [] - for item in all_tokens: - obj = { - "token": item['token'], - "occurrences": item["occurrences"], - "translation": "test" - } - post_obj_list.append(obj) - resp = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert resp.status_code == 201 - assert resp.json()['message'] == "Token translations saved" - - # after token translation - response = client.get(UNIT_URL+"/progress?project_id="+str(project_id) - ,headers=headers_auth) - assert response.status_code ==200 - assert ceil(response.json()['confirmed']) == 1 - assert floor(response.json()['untranslated']) == 0 - - #Without Auth - response = client.get(UNIT_URL+"/progress?project_id="+str(project_id), headers=headers) - assert response.json()['error'] == 'Authentication Error' - response = client.get(UNIT_URL+"/progress?project_id="+str(project_id) - ,headers=headers) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - -def test_get_versification(): - '''Positive test for agmt sentence/draft fetch API''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - resp = client.post(PROJECT_URL, headers=headers_auth, json=project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - # before adding books - response = client.get(UNIT_URL+"/versification?project_id="+str(project_id) - ,headers=headers_auth) - for key in response.json(): - assert len(response.json()[key]) == 0 - - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - response = client.get(UNIT_URL+"/versification?project_id="+str(project_id) - ,headers=headers_auth) - found_mat = False - found_mrk = False - for book in response.json()['maxVerses']: - if book == "mat": - found_mat = True - if book == "mrk": - found_mrk = True - assert found_mat and found_mrk - - #without auth - response = client.get(UNIT_URL+"/versification?project_id="+str(project_id), headers=headers) - assert response.json()['error'] == 'Authentication Error' - #without auth but from SanketMAST - response = client.get(UNIT_URL+"/versification?project_id="+str(project_id) - ,headers=headers) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - -#Project translation Access Rules based tests -def test_agmt_translation_access_rule_app(): - """project translation related access rule and auth""" - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - #create a project - resp = client.post(PROJECT_URL, headers=headers_auth, json=project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - #get tokens - #without auth not from SanketMAST - resp = client.get("/v2/text/translate/token-based/project/tokens?project_id="+str(project_id)) - assert resp.json()['error'] == "Permission Denied" - #without auth and from SanketMAST - resp = client.get("/v2/text/translate/token-based/project/tokens?project_id="+str(project_id) - ,headers=headers) - assert resp.status_code == 401 - assert resp.json()['error'] == 'Authentication Error' - #With Auth and From SanketMAST - resp = client.get("/v2/text/translate/token-based/project/tokens?project_id="+str(project_id) - ,headers=headers_auth) - assert resp.status_code == 200 - assert isinstance(resp.json(), list) - for item in resp.json(): - assert_positive_get_tokens(item) - - #Apply token translations PUT - all_tokens = resp.json() - our_token = all_tokens[0]['token'] - occurrences = all_tokens[0]['occurrences'] - - post_obj_list = [ - { - "token": all_tokens[0]['token'], - "occurrences": [all_tokens[0]["occurrences"][0]], - "translation": "test translation" - } - ] - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert response.status_code == 201 - assert response.json()['message'] == 'Token translations saved' - #Wihout Auth from SanketMAST - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers, json=post_obj_list) - assert response.json()['error'] == "Authentication Error" - #Outside SanketMAST wihtou Auth - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - json=post_obj_list) - assert response.json()['error'] == "Permission Denied" - - #get token translation - - data_str = json.dumps(post_obj_list) - params={ - "project_id": str(project_id), - "token": all_tokens[0]['token'], - "sentence_id": "41001001", - "offset": ["0", "4"], - "data": data_str - } - response = client.get(UNIT_URL + "/token-translations", - params=params, - headers=headers_auth) - - assert response.status_code == 200 - assert len(response.json()) > 0 - #Without Auth from SanketMAST - - response = client.get(UNIT_URL + "/token-translations", - params=params, - headers=headers) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - #Without Auth and not from SanketMAST - - response = client.get(UNIT_URL + "/token-translations", - params=params) - assert response.json()['error'] == "Permission Denied" - - #Get Token Sentences PUT - response = client.put('/v2/text/translate/token-based/project/token-sentences?project_id='+ - str(project_id)+'&token='+our_token, - json=occurrences, headers=headers_auth) - assert response.status_code == 200 - #Without Auth and from SanketMAST - response = client.put('/v2/text/translate/token-based/project/token-sentences?project_id='+ - str(project_id)+'&token='+our_token, - json=occurrences, headers=headers) - assert response.status_code == 401 - assert response.json()['error'] == "Authentication Error" - #Without Auth and not from SanketMAST - response = client.put('/v2/text/translate/token-based/project/token-sentences?project_id='+ - str(project_id)+'&token='+our_token, - json=occurrences) - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" - - #Get Draft - response = client.get(UNIT_URL+'/draft?project_id='+str(project_id)+ - "&output_format=alignment-json",headers=headers_auth) - assert response.status_code == 200 - #Without Auth and From SanketMAST - response = client.get(UNIT_URL+'/draft?project_id='+str(project_id)+ - "&output_format=alignment-json",headers=headers) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - #Without Auth and not From SanketMAST - response = client.get(UNIT_URL+'/draft?project_id='+str(project_id)+ - "&output_format=alignment-json") - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" - - #Project Source Get - response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&with_draft=True",headers=headers_auth) - assert response.status_code ==200 - #Without Auth and From SanketMAST - response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&with_draft=True",headers=headers) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - #Without Auth and not From SanketMAST - response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&with_draft=True") - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" - - #Project Progress - response = client.get(UNIT_URL+"/progress?project_id="+str(project_id), - headers=headers_auth) - assert response.status_code ==200 - #Without Auth and From SanketMAST - response = client.get(UNIT_URL+"/progress?project_id="+str(project_id), - headers=headers) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - #Without Auth and not From SanketMAST - response = client.get(UNIT_URL+"/progress?project_id="+str(project_id)) - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" - - #Project Versification - response = client.get(UNIT_URL+"/versification?project_id="+str(project_id) - ,headers=headers_auth) - assert response.status_code ==200 - #Without Auth and From SanketMAST - response = client.get(UNIT_URL+"/versification?project_id="+str(project_id) - ,headers=headers) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - #Without Auth and not From SanketMAST - response = client.get(UNIT_URL+"/versification?project_id="+str(project_id)) - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" - -def test_data_updated_time(): - '''Bugfix of issue #563:https://github.com/Bridgeconn/vachan-api/issues/563 ''' - - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - - #create an empty project - create_project_resp = client.post(PROJECT_URL, headers=headers_auth, json=project_data) - assert create_project_resp.json()['message'] == "Project created successfully" - project_id = create_project_resp.json()['data']['projectId'] - project_name =create_project_resp.json()['data']['projectName'] - project_create_time = create_project_resp.json()['data']['createTime'] - - # Add book into empty project - put_data = { - "uploadedUSFMs":[bible_books['mat']] - } - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - # Get tokens - resp = client.get("/v2/text/translate/token-based/project/tokens?project_id="+str(project_id) - ,headers=headers_auth) - - # Case 1:PUT Tokens - #Apply token translations - all_tokens = resp.json() - post_obj_list = [ - { - "token": all_tokens[0]['token'], - "occurrences": [all_tokens[0]["occurrences"][0]], - "translation": "test translation" - } - ] - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - - # Get project details - project_url = f"/v2/text/translate/token-based/projects?project_name={project_name}" - project_resp = client.get(project_url, headers=headers_auth) - project_update_time = project_resp.json()[0]['updateTime'] - assert not project_create_time == project_update_time - - # Check sentences are added - resp = client.get(f"{UNIT_URL}/sentences?project_id={project_id}", headers=headers_auth) - sentence_id = resp.json()[0]['sentenceId'] - - # Case 2 : PUT Draft - put_data = [ - {"sentenceId":sentence_id, - "draft": "കാട്", - "draftMeta":[ - [ [5,11], [0,4], "confirmed"] - ] - } - ] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data) - # Get project details - project_resp = client.get(project_url, headers=headers_auth) - project_update_time = project_resp.json()[0]['updateTime'] - assert not project_create_time == project_update_time - - #Case 3: Update suggestions - resp = client.put(f"/v2/text/translate/token-based/project/suggestions?project_id={project_id}&sentenceIdList={sentence_id}", - headers=headers_auth) - assert resp.status_code == 201 - # Get project details - project_resp = client.get(project_url, headers=headers_auth) - project_update_time = project_resp.json()[0]['updateTime'] - assert not project_create_time == project_update_time - - # Case 4 : Delete Sentence - response = client.delete(f"{UNIT_URL}/sentences?project_id={project_id}&sentence_id={sentence_id}", - headers=headers_auth) - assert response.status_code == 201 - assert "successfull" in response.json()['message'] - # Get project details - project_resp = client.get(project_url, headers=headers_auth) - project_update_time = project_resp.json()[0]['updateTime'] - assert not project_create_time == project_update_time - - -#Project translation Access rules based permission -def test_agmt_translation_access_permissions(): - """test for access permission to project translation""" - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - #create a project - resp = client.post(PROJECT_URL, headers=headers_auth, json=project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']] - } - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - resp = client.get("/v2/text/translate/token-based/project/tokens?project_id="+str(project_id) - ,headers=headers_auth) - assert resp.status_code == 200 - assert isinstance(resp.json(), list) - for item in resp.json(): - assert_positive_get_tokens(item) - - #Apply token translations PUT - all_tokens = resp.json() - our_token = all_tokens[0]['token'] - occurrences = all_tokens[0]['occurrences'] - - post_obj_list = [ - { - "token": all_tokens[0]['token'], - "occurrences": [all_tokens[0]["occurrences"][0]], - "translation": "test translation" - } - ] - - #create a SanketMASTUser and add as memeber to a project and SA - #Super Admin - SA_user_data = { - "user_email": SUPER_USER, - "password": SUPER_PASSWORD - } - response = login(SA_user_data) - assert response.json()['message'] == "Login Succesfull" - test_SA_token = response.json()["token"] - - #PUT Permission in agmt translations - #"SuperAdmin", "SanketMASTAdmin", "projectOwner", "projectMember" - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert response.status_code == 201 - assert response.json()['message'] == 'Token translations saved' - - headers_auth['Authorization'] = "Bearer"+" "+test_SA_token - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert response.status_code == 201 - assert response.json()['message'] == 'Token translations saved' - - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser2']['token'] - #PUT with SanketMASTuser not a member - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" - - response = client.put('/v2/text/translate/token-based/project/token-sentences?project_id='+ - str(project_id)+'&token='+our_token, - json=occurrences, headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" - - #Add SanketMASTUser as memeber to projects - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - response = client.post('/v2/text/translate/token-based/project/user'+'?project_id='+str(project_id)+ - '&user_id='+str(initial_test_users['SanketMASTUser2']["test_user_id"]),headers=headers_auth) - assert response.status_code == 201 - assert response.json()['message'] == "User added to project successfully" - - #After adding as member PUT - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser2']['token'] - response = client.put(UNIT_URL+"/tokens?project_id="+str(project_id), - headers=headers_auth, json=post_obj_list) - assert response.status_code == 201 - print("resp after put token:",resp.json()) - - response = client.put('/v2/text/translate/token-based/project/token-sentences?project_id='+ - str(project_id)+'&token='+our_token, - json=occurrences, headers=headers_auth) - assert response.status_code == 200 - - #GET Permission in agmt translations - #"SuperAdmin", "SanketMASTAdmin", "projectOwner", "projectMember", "BcsDeveloper" - token_list = [] - token_list.append(test_SA_token) - token_list.append(initial_test_users['SanketMASTUser2']['token']) - token_list.append(initial_test_users['SanketMASTAdmin']['token']) - token_list.append(initial_test_users['BcsDev']['token']) - token_list.append(initial_test_users['SanketMASTAdmin']['token']) - - for user_token in token_list: - headers_auth['Authorization'] = "Bearer"+" "+user_token - resp = client.get("/v2/text/translate/token-based/project/tokens?project_id="+str(project_id) - ,headers=headers_auth) - assert resp.status_code == 200 - assert isinstance(resp.json(), list) - for item in resp.json(): - assert_positive_get_tokens(item) - - data_str = json.dumps(post_obj_list) - response = client.get(UNIT_URL + "/token-translations", - params={ - "project_id": str(project_id), - "token": all_tokens[0]['token'], - "sentence_id": "41001001", - "offset": ["0", "4"], - "data": data_str - } , - headers=headers_auth) - assert response.status_code == 200 - assert len(response.json()) > 0 - - response = client.get(UNIT_URL+'/draft?project_id='+str(project_id)+ - "&output_format=alignment-json",headers=headers_auth) - assert response.status_code == 200 - assert len(response.json()) > 0 - - response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&with_draft=True",headers=headers_auth) - assert response.status_code ==200 - assert len(response.json()) > 0 - - response = client.get(UNIT_URL+"/progress?project_id="+str(project_id), - headers=headers_auth) - assert response.status_code ==200 - assert len(response.json()) > 0 - - response = client.get(UNIT_URL+"/versification?project_id="+str(project_id) - ,headers=headers_auth) - assert response.status_code ==200 - assert len(response.json()) > 0 - - #Not getting Content - #SanketMASTuser is not member of project - token_list = [] - token_list.append(initial_test_users['VachanUser']['token']) - token_list.append(initial_test_users['SanketMASTUser']['token']) - token_list.append(initial_test_users['APIUser']['token']) - token_list.append(initial_test_users['VachanAdmin']['token']) - token_list.append(initial_test_users['VachanContentAdmin']['token']) - token_list.append(initial_test_users['VachanContentViewer']['token']) - - for user_token in token_list: - headers_auth['Authorization'] = "Bearer"+" "+user_token - resp = client.get("/v2/text/translate/token-based/project/tokens?project_id="+str(project_id) - ,headers=headers_auth) - assert resp.status_code == 403 - assert resp.json()['error'] == "Permission Denied" - - data_str = json.dumps(post_obj_list) - response = client.get(UNIT_URL + "/token-translations", - params={ - "project_id": str(project_id), - "token": all_tokens[0]['token'], - "sentence_id": "41001001", - "offset": ["0", "4"], - "data": data_str - } , - headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" - - response = client.get(UNIT_URL+'/draft?project_id='+str(project_id)+ - "&output_format=alignment-json",headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" - - response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&with_draft=True",headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" - - response = client.get(UNIT_URL+"/progress?project_id="+str(project_id), - headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" - - response = client.get(UNIT_URL+"/versification?project_id="+str(project_id) - ,headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == "Permission Denied" \ No newline at end of file diff --git a/app/test/test_smast_translation2.py b/app/test/test_smast_translation2.py deleted file mode 100644 index ab5c3315..00000000 --- a/app/test/test_smast_translation2.py +++ /dev/null @@ -1,651 +0,0 @@ -'''tests for the translation workflow within SMAST projects continued''' -import json -from . import client -from .conftest import initial_test_users -from . import assert_input_validation_error, assert_not_available_content -from . test_agmt_translation import UNIT_URL, assert_positive_get_sentence -from . test_auth_basic import login,SUPER_PASSWORD,SUPER_USER,logout_user - -RESTORE_URL = '/v2/admin/restore' -PROJECT_URL = '/v2/text/translate/token-based/projects' -headers_auth = {"contentType": "application/json", - "accept": "application/json", - "app":"SanketMAST" - } -headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - -project_data = { - "projectName": "Test agmt draft", - "sourceLanguageCode": "en", - "targetLanguageCode": "ml" -} - -source_sentences = [ - {"sentenceId": 100, - "sentence": "In a jungle far away there lived a fox"}, - {"sentenceId": 101, - "sentence": "The fox was friends with a tiger."}, - {"sentenceId": 102, - "sentence": "They used to play together."}, - {"sentenceId": 103, - "sentence": "One day the fox wished he had tiger's striped coat."}, - {"sentenceId": 104, - "sentence": "The good friend tiger lent it to him."}, - {"sentenceId": 105, - "sentence": "Tharjima illatha thettu vaakkukal mathram."}, - {"sentenceId": 106, - "sentence": "Oru jungle allathe mattellam tharjima illatha thettu vaakkukal mathram."} -] - -def test_draft_update_positive(): - '''Positive test for updating draft and draftMeta(Alignment) in a project''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - resp = client.post(PROJECT_URL, headers=headers_auth, json=project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "sentenceList":source_sentences - } - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - resp = client.get(f"{UNIT_URL}/sentences?project_id={project_id}", headers=headers_auth) - for sent in resp.json(): - assert_positive_get_sentence(sent) - - # translate just one word - put_data = [ - {"sentenceId":100, - "draft": "കാട്", - "draftMeta":[ - [ [5,11], [0,4], "confirmed"] - ] - } - ] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data) - sents = resp.json() - assert len(sents) == 1 - assert_positive_get_sentence(sents[0]) - assert sents[0]["draft"] == "കാട്" - assert sents[0]["draftMeta"] == put_data[0]['draftMeta'] - - #fetch sentences to make sure - data_str = json.dumps(put_data) - resp = client.get( - "/v2/text/translate/token-based/project/sentences", - params={ - "project_id": project_id, - "sentence_id_list": "100", - "with_draft": "true", - "data": data_str - }, - headers=headers_auth - ) - sents = resp.json() - print(sents) - assert sents[0]["draft"] == "കാട്" - assert sents[0]["draftMeta"] == put_data[0]['draftMeta'] - # edit the existing draft - put_data2 = [ - {"sentenceId":100, - "draft": "ഒരു കാട്ടില്‍ ഒരു കുറുക്കന്‍ ജീവിച്ചിരുന്നു", - "draftMeta":[ - [ [3,4], [0,3], "confirmed"], - [ [5,5], [3,4], "confirmed"], - [ [5,11], [4,11], "confirmed"], - [ [11,11], [11,13], "confirmed"], - [ [32,33], [13,16], "confirmed"], - [ [33,33], [16,18], "confirmed"], - [ [35,38], [18,26], "confirmed"], - [ [38,38], [26,43], "untranslated"], - ] - } - ] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data2) - sents = resp.json() - assert_positive_get_sentence(sents[0]) - assert sents[0]["draft"] == "ഒരു കാട്ടില്‍ ഒരു കുറുക്കന്‍ ജീവിച്ചിരുന്നു" - assert sents[0]["draftMeta"] == put_data2[0]['draftMeta'] - - #fetch sentences again - data_str2 = json.dumps(put_data2) - resp = client.get( - "/v2/text/translate/token-based/project/sentences", - params={ - "project_id": project_id, - "sentence_id_list": "100", - "with_draft": "true", - "data2": data_str2 - }, - headers=headers_auth) - sents = resp.json() - assert sents[0]["draft"] == put_data2[0]['draft'] - assert sents[0]["draftMeta"] == put_data2[0]['draftMeta'] - -def test_draft_update_negative(): - '''Checking effective validations and error messages''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - resp = client.post(PROJECT_URL, headers=headers_auth, json=project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - # incorrect project id - put_data = [ - {"sentenceId":100, - "draft": "കാട്", - "draftMeta":[ - [ [5,11], [0,4], "confirmed"] - ] - } - ] - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id+1}", - headers=headers_auth, json=put_data) - assert resp.json()['details'] == f"Project with id, {project_id+1}, not present" - - # non existing sentence - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data) - assert resp.json()['error'] == "Requested Content Not Available" - - # upload source and repeat last action - put_data_source = { - "sentenceList":source_sentences - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data_source) - assert resp.json()['message'] == "Project updated successfully" - - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data) - assert resp.json()[0]["draft"] == put_data[0]['draft'] - - # incorrect user - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser2']['token'] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data) - assert resp.json()['error'] == "Permission Denied" - - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - - # incorrect payloads - put_data2 = [ - { - # "sentenceId":100, no sentenceId - "draft": "കാട്", - "draftMeta":[ - [ [5,11], [0,4], "confirmed"] - ] - } - ] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data2) - assert resp.status_code == 422 - - put_data2 = [ - { - "sentenceId":100, - # "draft": "കാട്", no draft - "draftMeta":[ - [ [5,11], [0,4], "confirmed"] - ] - } - ] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data2) - assert resp.status_code == 422 - - put_data2 = [ - { - "sentenceId":100, - "draft": "കാട്", - # "draftMeta":[ no draft - # [ [5,11], [0,4], "confirmed"] - # ] - } - ] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data2) - assert resp.status_code == 422 - - # incorrect index of target segment - put_data2 = [ - { - "sentenceId":100, - "draft": "കാട്", - "draftMeta":[ - [ [5,11], [0,10], "confirmed"] - ] - } - ] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data2) - assert resp.status_code == 422 - assert resp.json()['details'] == "Incorrect metadata:Target segment [0, 10], is improper!" - - -def test_empty_draft_initalization(): - '''Bugfix test for #452 after the changes in #448''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - resp = client.post(PROJECT_URL, headers=headers_auth, json=project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "sentenceList":source_sentences - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - # Ensure draft is set to "" - resp = client.get(f"{UNIT_URL}/sentences?project_id={project_id}&with_draft=True", - headers=headers_auth) - for sent in resp.json(): - assert_positive_get_sentence(sent) - assert sent["draft"] == "" - - #Ensure draft if still empty when there is no suggestion - resp = client.put(f"{UNIT_URL}/suggestions?project_id={project_id}&sentence_id_list={105}", - headers=headers_auth) - assert resp.status_code == 201 - assert resp.json()[0]['draft'] == "" - - # Add a gloss to make sure there will be suggestions - tokens_trans = [ - {"token":"jungle", "translations":["കാട്"]} - ] - response = client.post('/v2/nlp/gloss?source_language=en&target_language=ml', - headers=headers_auth, json=tokens_trans) - assert response.status_code == 201 - assert response.json()['message'] == "Added to glossary" - - resp = client.put(f"{UNIT_URL}/suggestions?project_id={project_id}&sentence_id_list=100"+\ - "&sentence_id_list=106", - headers=headers_auth) - assert resp.status_code == 201 - assert resp.json()[1]['draft'] == "കാട്" # only one suggestion - assert "കാട്" in resp.json()[0]['draft'] # at least one suggestion - found_jungle_meta = False - found_untranslated = False - for meta in resp.json()[0]['draftMeta']: - if meta[0] == [5,11] and meta[2] == "suggestion": - found_jungle_meta = True - print("draft:", resp.json()[0]['draft']) - print("draft.index('കാട്'')",resp.json()[0]['draft'].index("കാട്")) - trans_index = resp.json()[0]['draft'].index("കാട്") - assert meta[1][0] == trans_index - elif meta[2] == "untranslated": - found_untranslated = True - assert found_jungle_meta - assert found_untranslated - -def test_draft_meta_validation(): - '''Bugfix test for #479 after the changes in PR #481''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - resp = client.post(PROJECT_URL, headers=headers_auth, json=project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "sentenceList":source_sentences - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - #Get suggestions - resp = client.put(f"/v2/text/translate/token-based/project/suggestions?project_id={project_id}&sentenceIdList=100", - headers=headers_auth) - assert resp.status_code == 201 - resp_draft_meta = resp.json()[0]['draftMeta'] - print("draftMeta:", resp_draft_meta) - empty_seg = False - for seg in resp_draft_meta: - if seg[0][0] == seg[0][1] or seg[1][0] == seg[1][1]: - empty_seg = True - break - # assert empty_seg - - # PUT the draft meta back to server - json_data = [{ - "sentenceId":100, - "draft":resp.json()[0]['draft'], - "draftMeta":resp_draft_meta - }] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=json_data) - assert resp.status_code == 201 - -def test_space_in_suggested_draft(): - '''BUgfix text for #485, after changes in PR #486''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - resp = client.post(PROJECT_URL, headers=headers_auth, json=project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "sentenceList":source_sentences - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - # Add a gloss to ensure some suggestion in output - tokens_trans = [ - {"token":"jungle", "translations":["കാട്"]}, - {"token":"far", "translations":["ദൂരെ"]}, - {"token":"fox", "translations":["കുറുക്കന്‍"]} - ] - response = client.post('/v2/nlp/gloss?source_language=en&target_language=ml', - headers=headers_auth, json=tokens_trans) - assert response.status_code == 201 - assert response.json()['message'] == "Added to glossary" - - - #Get suggestions - resp = client.put(f"/v2/text/translate/token-based/project/suggestions?project_id={project_id}&sentenceIdList=100", - headers=headers_auth) - assert resp.status_code == 201 - resp_obj = resp.json() - assert resp_obj[0]['draft'] != "" - assert not resp_obj[0]['draft'].startswith(" ") - - #Get suggestions again - resp = client.put(f"/v2/text/translate/token-based/project/suggestions?project_id={project_id}&sentenceIdList=100", - headers=headers_auth) - assert resp.status_code == 201 - resp_obj = resp.json() - assert resp_obj[0]['draft'] != "" - assert not resp_obj[0]['draft'].startswith(" ") - -def test_delete_sentence(): - '''Test the removal of a sentence from project''' - - #Adding Project and sentences into it - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - resp = client.post(PROJECT_URL, headers=headers_auth, json=project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "sentenceList":source_sentences - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - # Check sentences are added - resp = client.get(f"{UNIT_URL}/sentences?project_id={project_id}", headers=headers_auth) - sentence_id1 = resp.json()[0]['sentenceId'] - sentence_id2 = resp.json()[1]['sentenceId'] - for sent in resp.json(): - assert_positive_get_sentence(sent) - - - #deleting sentence with no auth - headers = {"contentType": "application/json", - "accept": "application/json", - "app":"SanketMAST" - } - resp = client.delete(f"{UNIT_URL}/sentences?project_id={project_id}&sentence_id={sentence_id1}", - headers=headers) - assert resp.status_code == 401 - assert resp.json()['details'] == "Access token not provided or user not recognized." - - #Deleting Sentence with unauthorized users - Negative Test - for user in ['APIUser','VachanAdmin','VachanUser','BcsDev','SanketMASTUser','VachanContentAdmin','VachanContentViewer']: - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users[user]['token'] - response = client.delete(f"{UNIT_URL}/sentences?project_id={project_id}&sentence_id={sentence_id1}", - headers=headers_auth) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - - # Delete as SanketMASTAdmin - Positive test - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - response = client.delete(f"{UNIT_URL}/sentences?project_id={project_id}&sentence_id={sentence_id1}", - headers=headers_auth) - assert response.status_code == 201 - assert "successfull" in response.json()['message'] - - # Check get sentence to ensure deleted sentence is not present - get_response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&sentence_id_list="+str(sentence_id1),headers=headers_auth) - assert_not_available_content(get_response) - - #Create and Delete sentence with superadmin - Positive test - # Login as Super Admin - sa_data = { - "user_email": SUPER_USER, - "password": SUPER_PASSWORD - } - response = login(sa_data) - assert response.json()['message'] == "Login Succesfull" - test_user_token = response.json()["token"] - headers_auth['Authorization'] = "Bearer"+" "+test_user_token - - response = client.delete(f"{UNIT_URL}/sentences?project_id={project_id}&sentence_id={sentence_id2}", - headers=headers_auth) - assert response.status_code == 201 - assert "successfull" in response.json()['message'] - - # Check get sentence to ensure deleted sentence is not present - get_response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&sentence_id_list="+str(sentence_id2),headers=headers_auth) - assert_not_available_content(get_response) - - #Delete not available sentence - response = client.delete(f"{UNIT_URL}/sentences?project_id={project_id}&sentence_id=9999", - headers=headers_auth) - assert response.status_code == 404 - assert "Requested Content Not Available" in response.json()['error'] - - -def test_restore_sentence(): - '''positive test case, checking for correct return object''' - #only Super Admin can restore deleted data - #Creating and Deleting project sentence - #Adding Project and sentences into it - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - resp = client.post(PROJECT_URL, headers=headers_auth, json=project_data) - project_id = resp.json()['data']['projectId'] - - put_data = { - "sentenceList":source_sentences - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - delete_resp = client.delete(f"{UNIT_URL}/sentences?project_id={project_id}&sentence_id=100", - headers=headers_auth) - - #Ensure deleted sentence is not present - get_response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&sentence_id_list=100",headers=headers_auth) - assert_not_available_content(get_response) - - deleteditem_id = delete_resp.json()['data']['itemId'] - data = {"itemId": deleteditem_id} - - #Restoring data - #Restore user without authentication - Negative Test - headers = {"contentType": "application/json", - "accept": "application/json", - "app":"SanketMAST" - } - response = client.put(RESTORE_URL, headers=headers, json=data) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - - #Restore project user with other API user,VachanAdmin,SanketMASTAdmin,SanketMASTUser,VachanUser,BcsDev - Negative Test - for user in ['APIUser','VachanAdmin','SanketMASTAdmin','SanketMASTUser','VachanUser','BcsDev','VachanContentAdmin','VachanContentViewer']: - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users[user]['token'] - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - - #Restore Project User with Super Admin - Positive Test - # Login as Super Admin - sa_data = { - "user_email": SUPER_USER, - "password": SUPER_PASSWORD - } - response = login(sa_data) - assert response.json()['message'] == "Login Succesfull" - test_user_token = response.json()["token"] - headers_auth['Authorization'] = "Bearer"+" "+test_user_token - - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 201 - assert response.json()['message'] == \ - f"Deleted Item with identity {deleteditem_id} restored successfully" - - #Ensure deleted sentence is not present - get_response = client.get(UNIT_URL+"/sentences?project_id="+str(project_id)+ - "&sentence_id_list=100",headers=headers_auth) - assert get_response.status_code ==200 - assert len(get_response.json())== 1 - for item in get_response.json(): - assert_positive_get_sentence(item) - - #restore with missing data - Negative Test - data = {} - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert_input_validation_error(response) - - #Restore with invalid item id - Negative Test - data = {"itemId":9999} - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 404 - assert response.json()['error'] == "Requested Content Not Available" - logout_user(test_user_token) - -def test_suggestion_when_token_overlaps_confirmed_segment(): - # Testing bug fix https://github.com/Bridgeconn/vachan-api/issues/542 - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTUser']['token'] - resp = client.post(PROJECT_URL, headers=headers_auth, json=project_data) - project_id = resp.json()['data']['projectId'] - - put_data = { - "sentenceList": [ - { - "sentenceId": 57001002, - "surrogateId": "tit 1:2", - "sentence":"This faith and knowledge make us sure that we have eternal life. God promised that life to us before time began—and God does not lie." - } - ] - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - # add a translation for just "not" when it occurs as "doesnot" - update_draft_url = f"/v2/text/translate/token-based/project/draft?project_id={project_id}" - data = [{ "draft": "they this", - "draftMeta": [ [ [ 0, 4 ], [ 9, 9 ], "untranslated" ], - [ [ 4, 5 ], [ 9, 9 ], "untranslated" ], - [ [ 5, 10 ], [ 9, 9 ], "untranslated" ], - [ [ 10, 11 ], [ 9, 9 ], "untranslated" ], - [ [ 11, 24 ], [ 9, 9 ], "untranslated" ], - [ [ 24, 25 ], [ 9, 9 ], "untranslated" ], - [ [ 25, 29 ], [ 9, 9 ], "untranslated" ], - [ [ 29, 30 ], [ 9, 9 ], "untranslated" ], - [ [ 30, 32 ], [ 9, 9 ], "untranslated" ], - [ [ 32, 33 ], [ 9, 9 ], "untranslated" ], - [ [ 33, 37 ], [ 9, 9 ], "untranslated" ], - [ [ 37, 38 ], [ 9, 9 ], "untranslated" ], - [ [ 38, 42 ], [ 9, 9 ], "untranslated" ], - [ [ 42, 43 ], [ 9, 9 ], "untranslated" ], - [ [ 43, 45 ], [ 9, 9 ], "untranslated" ], - [ [ 45, 46 ], [ 9, 9 ], "untranslated" ], - [ [ 46, 58 ], [ 9, 9 ], "untranslated" ], - [ [ 58, 59 ], [ 9, 9 ], "untranslated" ], - [ [ 59, 63 ], [ 9, 9 ], "untranslated" ], - [ [ 63, 65 ], [ 9, 9 ], "untranslated" ], - [ [ 65, 68 ], [ 9, 9 ], "untranslated" ], - [ [ 68, 69 ], [ 9, 9 ], "untranslated" ], - [ [ 69, 77 ], [ 9, 9 ], "untranslated" ], - [ [ 77, 78 ], [ 9, 9 ], "untranslated" ], - [ [ 78, 82 ], [ 9, 9 ], "untranslated" ], - [ [ 82, 83 ], [ 9, 9 ], "untranslated" ], - [ [ 83, 87 ], [ 9, 9 ], "untranslated" ], - [ [ 87, 88 ], [ 9, 9 ], "untranslated" ], - [ [ 88, 93 ], [ 9, 9 ], "untranslated" ], - [ [ 93, 94 ], [ 9, 9 ], "untranslated" ], - [ [ 94, 105 ], [ 9, 9 ], "untranslated" ], - [ [ 105, 106 ], [ 9, 9 ], "untranslated" ], - [ [ 106, 111 ], [ 9, 9 ], "untranslated" ], - [ [ 111, 112 ], [ 9, 9 ], "untranslated" ], - [ [ 112, 119 ], [ 9, 9 ], "untranslated" ], - [ [ 119, 120 ], [ 9, 9 ], "untranslated" ], - [ [ 132, 133 ], [ 9, 9 ], "untranslated" ], - [ [ 125, 128 ], [ 5, 9 ], "confirmed" ], - [ [ 129, 132 ], [ 0, 4 ], "confirmed" ], - [ [ 125, 125 ], [ 4, 5 ], "confirmed" ] ], - "sentenceId": 57001002 }] - update_resp = client.put(update_draft_url, - json=data, - headers={'Authorization': f'bearer {initial_test_users["SanketMASTAdmin"]["token"]}', - 'app':"SanketMAST"}) - assert update_resp.status_code == 201 - - - # Suggestion call 1 - suggest_url = f"/v2/text/translate/token-based/project/suggestions?project_id={project_id}&sentence_id_list=57001002" - suggest_resp = client.put(suggest_url, - headers={'Authorization': f"bearer {initial_test_users['SanketMASTAdmin']['token']}", - 'app':"SanketMAST"}) - assert suggest_resp.status_code == 201 - - # Suggestion call 2 - suggest_url = f"/v2/text/translate/token-based/project/suggestions?project_id={project_id}&sentence_id_list=57001002" - suggest_resp = client.put(suggest_url, - headers={'Authorization': f"bearer {initial_test_users['SanketMASTAdmin']['token']}", - 'app':"SanketMAST"}) - assert suggest_resp.status_code == 201 - -def test_draftmeta_validation(): - '''All protions of the draft should have a draftmeta segment - issue #602''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - resp = client.post(PROJECT_URL, headers=headers_auth, json=project_data) - assert resp.json()['message'] == "Project created successfully" - project_id = resp.json()['data']['projectId'] - - put_data = { - "sentenceList":source_sentences - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgUser']['token'] - resp = client.put("/v2/text/translate/token-based/projects"+'?project_id='+str(project_id),\ - headers=headers_auth, json=put_data) - assert resp.json()['message'] == "Project updated successfully" - - ## Spaces not accounted for - put_data2 = [ - {"sentenceId":100, - "draft": "ഒരു കാട്ടില്‍ ഒരു കുറുക്കന്‍ ജീവിച്ചിരുന്നു", - "draftMeta":[ - [ [3,4], [0,3], "confirmed"], - [ [5,11], [4,11], "confirmed"], - [ [32,33], [13,16], "confirmed"], - [ [35,38], [18,26], "confirmed"] - ] - } - ] - resp = client.put(f"/v2/text/translate/token-based/project/draft?project_id={project_id}", - headers=headers_auth, json=put_data2) - assert resp.status_code == 422 - assert "error" in resp.json() - assert resp.json()['error'] == "Unprocessable Data" \ No newline at end of file diff --git a/app/test/test_stop_words_generation.py b/app/test/test_stop_words_generation.py deleted file mode 100644 index a895b63e..00000000 --- a/app/test/test_stop_words_generation.py +++ /dev/null @@ -1,498 +0,0 @@ -'''Tests the translation APIs that do need projects available in DB''' -import json -import time -from app.main import log -from app.schema import schema_auth, schemas -from . import client -from . import check_default_get -from . import assert_not_available_content,assert_input_validation_error -from .conftest import initial_test_users -from . test_auth_basic import login,SUPER_PASSWORD,SUPER_USER,logout_user - - -UNIT_URL = '/v2/nlp/stopwords' -GER_URL = '/v2/nlp/stopwords-generate' -JOBS_URL = '/v2/jobs' -RESTORE_URL = '/v2/admin/restore' - -headers = {"contentType": "application/json", "accept": "application/json"} -headers_auth = {"contentType": "application/json", "accept": "application/json"} - -update_obj1 = { - "stopWord": "उसका", - "active": False, - "metaData": { - "type": "verb" - } - } -update_obj2 = { - "stopWord": "हम", - "active": False, - } -update_wrong_obj = { - "stopWord": "prayed", - "active": False, - "metaData": { - "type": "verb" - } - } -add_obj = [ - "asd", - "ert", - "okl" -] - -sentences = [ - "इसलिए हे भाइयों, हमने अपनी सारी सकेती और क्लेश में तुम्हारे विश्वास से तुम्हारे विषय में शान्ति पाई।", - "क्योंकि अब यदि तुम प्रभु में स्थिर रहो तो हम जीवित हैं।", - "और जैसा आनन्द हमें तुम्हारे कारण अपने परमेश्वर के सामने है, उसके बदले तुम्हारे विषय में हम किस रीति से परमेश्वर का धन्यवाद करें?", - "हम रात दिन बहुत ही प्रार्थना करते रहते हैं, कि तुम्हारा मुँह देखें, और तुम्हारे विश्वास की घटी पूरी करें।", - "अब हमारा परमेश्वर और पिता आप ही और हमारा प्रभु यीशु, तुम्हारे यहाँ आने के लिये हमारी अगुआई करे।", - "और प्रभु ऐसा करे, कि जैसा हम तुम से प्रेम रखते हैं; वैसा ही तुम्हारा प्रेम भी आपस में, और सब मनुष्यों के साथ बढ़े, और उन्नति करता जाए" -] -sentence_list = [{"sentenceId":i, "sentence":s} for i,s in enumerate(sentences)] - -def assert_positive_get_stopwords(item): - '''Check for the properties in the normal return object''' - assert "stopWord" in item - assert "stopwordType" in item - if item["stopwordType"] == "auto generated": - assert "confidence" in item - assert isinstance(item["confidence"], float) - assert "active" in item - -def test_get_default(): - '''positive test case, without optional params''' - check_default_get(UNIT_URL+'/hi', headers=headers,assert_positive_get= - assert_positive_get_stopwords) - -def assert_positive_response(out): - '''Check the properties in the update response''' - assert "message" in out - assert "data" in out - -def test_get_stop_words(): - '''Positve tests for get stopwords API''' - default_response = client.get(UNIT_URL+'/hi?', headers=headers) - assert default_response.status_code == 200 - assert isinstance(default_response.json(), list) - for item in default_response.json(): - assert_positive_get_stopwords(item) - - response = client.get(UNIT_URL+'/hi?include_system_defined=False', headers=headers) - assert response.status_code == 200 - sw_types = {sw_dic['stopwordType'] for sw_dic in response.json()} - assert "system defined" not in sw_types - - response = client.get(UNIT_URL+'/hi?include_user_defined=False', headers=headers) - assert response.status_code == 200 - sw_types = {sw_dic['stopwordType'] for sw_dic in response.json()} - assert "user defined" not in sw_types - - response = client.get(UNIT_URL+'/hi?include_auto_generated=False', headers=headers) - assert response.status_code == 200 - sw_types = {sw_dic['stopwordType'] for sw_dic in response.json()} - assert "auto generated" not in sw_types - - response = client.get(UNIT_URL+'/hi?only_active=True', headers=headers) - assert response.status_code == 200 - out = {sw_dic['active'] for sw_dic in response.json()} - assert False not in out - -def test_get_notavailable_code(): - ''' request a not available language_code''' - response = client.get(UNIT_URL+"/abc") - assert_not_available_content(response) - -def test_update_stopword(): - '''Positve tests for update stopwords API''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['APIUser']['token'] - - response = client.put(UNIT_URL+'/hi?',headers=headers_auth, json=update_obj1) - assert response.status_code == 201 - assert_positive_response(response.json()) - assert_positive_get_stopwords(response.json()['data']) - assert response.json()['message'] == "Stopword info updated successfully" - - response = client.put(UNIT_URL+'/hi?',headers=headers_auth, json=update_obj2) - assert response.status_code == 201 - assert_positive_response(response.json()) - assert_positive_get_stopwords(response.json()['data']) - assert response.json()['message'] == "Stopword info updated successfully" - - response = client.put(UNIT_URL+'/hi?',headers=headers_auth, json=update_wrong_obj) - assert response.status_code == 404 - -def test_add_stopword(): - '''Positve tests for add stopwords API''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['APIUser']['token'] - - response = client.post(UNIT_URL+'/aa?',headers=headers_auth, json=add_obj) - assert response.status_code == 201 - assert_positive_response(response.json()) - for item in response.json()['data']: - assert_positive_get_stopwords(item) - assert item['stopwordType'] == "user defined" - assert item['active'] is True - assert item['stopWord'] in add_obj - assert len(response.json()['data']) == len(add_obj) - - response = client.post(UNIT_URL+'/aa?',headers=headers_auth, json=["asd"]) - assert response.status_code == 201 - assert_positive_response(response.json()) - assert not response.json()['data'] - assert response.json()['message'] == "0 stopwords added successfully" - - response = client.post(UNIT_URL+'/aa?',headers=headers_auth, json=["hty"]) - assert response.status_code == 201 - assert_positive_response(response.json()) - assert response.json()['data'] - assert response.json()['data'][0]['stopWord'] == "hty" - assert response.json()['data'][0]['stopwordType'] == "user defined" - assert response.json()['data'][0]['active'] is True - assert len(response.json()['data']) == 1 - - - response = client.post(UNIT_URL+'/hi?',headers=headers_auth, json=["की"]) - assert response.status_code == 201 - assert_positive_response(response.json()) - assert not response.json()['data'] - assert response.json()['message'] == "0 stopwords added successfully" - - response = client.post(UNIT_URL+'/hi?',headers=headers_auth, json=["चुनाव"]) - assert response.status_code == 201 - assert_positive_response(response.json()) - assert response.json()['data'] - assert response.json()['data'][0]['stopWord'] == "चुनाव" - assert response.json()['data'][0]['stopwordType'] == "user defined" - assert response.json()['data'][0]['active'] is True - assert len(response.json()['data']) == 1 - -# def test_create_job(): -# '''Positve tests for create job API''' -# response = client.post(JOBS_URL,headers=headers) -# assert response.status_code == 201 -# assert_positive_response(response.json()) -# assert "jobId" in response.json()['data'] -# assert "status" in response.json()['data'] -# assert response.json()['data']['status'] == 'job created' - -# def test_check_job_status(): -# '''Positve tests for checking job status API''' -# response = client.post(JOBS_URL,headers=headers) -# assert response.status_code == 201 -# job_id = response.json()['data']['jobId'] -# response = client.get(JOBS_URL+'/?job_id='+str(job_id),headers=headers) -# assert response.status_code == 200 -# assert_positive_response(response.json()) -# assert "jobId" in response.json()['data'] -# assert "status" in response.json()['data'] -# if response.json()['data']['status'] == 'job finished': -# assert 'output' in response.json()['data'] - -def get_job_status(job_id): - '''Retrieve status of a job''' - # registered user can get job status - response = client.get(JOBS_URL+'/?job_id='+str(job_id),headers=headers) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - - headers_auth['Authorization'] = "Bearer"+" "+ initial_test_users['APIUser']['token'] - response = client.get(JOBS_URL+'/?job_id='+str(job_id),headers=headers_auth) - assert response.status_code == 200 - assert_positive_response(response.json()) - assert "jobId" in response.json()['data'] - assert "status" in response.json()['data'] - return response - -def test_jobs(): - """get job test""" - #not available jobid - headers_auth['Authorization'] = "Bearer"+" "+ initial_test_users['APIUser']['token'] - response = client.get(JOBS_URL+'/?job_id='+str(999999999999999999),headers=headers_auth) - assert response.status_code == 404 - assert response.json()["error"] == "Requested Content Not Available" - -def assert_positive_sw_out(item): - '''Check for the properties in output of sw job''' - assert "stopWord" in item - assert "confidence" in item - assert isinstance(item["confidence"], float) - assert "active" in item - -def add_version(): - '''adds version in db''' - version_data = { - "versionAbbreviation": "TW", - "versionName": "test version", - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanAdmin']['token'] - headers_auth['app'] = schema_auth.AdminRoles.VACHANADMIN.value - result = client.post('/v2/resources/versions', headers=headers_auth, json=version_data) - assert result.status_code == 201 - -def add_bible_resource(): - '''creates bible resource''' - src_data = { - "resourceType": "bible", - "language": "hi", - "version": "TW", - "revision": 1, - "year": 2020, - "license": "CC-BY-SA", - "metaData": {"owner": "someone" }, - "accessPermissions": [schemas.ResourcePermissions.OPENACCESS, schemas.ResourcePermissions.CONTENT] - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanAdmin']['token'] - resource = client.post('/v2/resources', headers=headers_auth, json=src_data) - assert resource.status_code == 201 - table_name = resource.json()['data']['resourceName'] - return table_name - -def add_dict_resource(): - '''creates vocabulary resource''' - resource_data = { - "resourceType": "vocabulary", - "language": "hi", - "version": "TW", - "revision": 1, - "year": 2000, - "accessPermissions": [schemas.ResourcePermissions.OPENACCESS, schemas.ResourcePermissions.CONTENT] - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanAdmin']['token'] - resource = client.post('/v2/resources', headers=headers_auth, json=resource_data) - assert resource.status_code == 201 - table_name = resource.json()['data']['resourceName'] - return table_name - -def add_bible_books(table_name): - '''uploads bible books in db''' - data = [] - input_files = ['41-MAT.usfm', '42-MRK.usfm', '43-LUK.usfm'] - for book in input_files: - book_data = open('test/resources/' + book, 'r',encoding='utf-8').read() - data.append({"USFM":book_data}) - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanAdmin']['token'] - response = client.post('/v2/resources/bibles/'+table_name+'/books', headers=headers_auth, json=data) - assert response.status_code == 201 - assert response.json()["message"] == "Bible books uploaded and processed successfully" - -def add_tw_dict(table_name): - '''uploads tw vocabulary''' - data = json.load(open('test/resources/hindi.json')) - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanAdmin']['token'] - response = client.post('/v2/resources/vocabularies/'+table_name, headers=headers_auth, json=data) - assert response.status_code == 201 - -def test_generate_stopwords(): - '''Positve tests for generate stopwords API''' - add_version() - table_name = add_bible_resource() - add_bible_books(table_name) - - dict_table_name = add_dict_resource() - add_tw_dict(dict_table_name) - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['BcsDev']['token'] - - response = client.post(GER_URL+'?language_code=hi',headers=headers_auth) - assert response.status_code == 201 - assert_positive_response(response.json()) - assert "jobId" in response.json()['data'] - assert "status" in response.json()['data'] - for i in range(10): - job_response = get_job_status(response.json()['data']['jobId']) - status = job_response.json()['data']['status'] - if status == 'job finished': - break - log.info("sleeping for a minute in SW generate test") - time.sleep(60) - assert job_response.json()['data']['status'] == 'job finished' - assert 'output' in job_response.json()['data'] - for item in job_response.json()['data']['output']['data']: - assert_positive_sw_out(item) - assert job_response.json()['message'] == "Stopwords identified out of limited resources. Manual verification recommended" - - response = client.post(GER_URL+'?language_code=hi&use_server_data=False', - headers=headers_auth, json=sentence_list) - assert response.status_code == 201 - assert_positive_response(response.json()) - assert "jobId" in response.json()['data'] - assert "status" in response.json()['data'] - job_id = response.json()['data']['jobId'] - for i in range(5): - job_response = get_job_status(job_id) - status = job_response.json()['data']['status'] - if status == 'job finished': - break - log.info("sleeping for a minute in SW generate test") - time.sleep(60) - assert job_response.json()['data']['status'] == 'job finished' - assert job_response.json()['message'] == "Not enough data to generate stopwords" - - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['BcsDev']['token'] - response = client.post(GER_URL+'?language_code=hi',headers=headers_auth, - json=sentence_list) - assert response.status_code == 201 - assert_positive_response(response.json()) - assert "jobId" in response.json()['data'] - assert "status" in response.json()['data'] - for i in range(10): - job_response1 = get_job_status(response.json()['data']['jobId']) - status = job_response1.json()['data']['status'] - if status == 'job finished': - break - log.info("sleeping for a minute in SW generate test") - time.sleep(60) - assert job_response1.json()['data']['status'] == 'job finished' - assert 'output' in job_response1.json()['data'] - for item in job_response1.json()['data']['output']['data']: - assert_positive_sw_out(item) - assert job_response1.json()['message'] == "Stopwords identified out of limited resources. Manual verification recommended" - - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanAdmin']['token'] - response = client.post(GER_URL+'?language_code=hi&source_name='+dict_table_name, - headers=headers_auth, json=sentence_list) - assert response.status_code == 201 - assert_positive_response(response.json()) - assert "jobId" in response.json()['data'] - assert "status" in response.json()['data'] - for i in range(10): - job_response2 = get_job_status(response.json()['data']['jobId']) - status = job_response2.json()['data']['status'] - if status == 'job finished': - break - log.info("sleeping for a minute in SW generate test") - time.sleep(60) - assert job_response2.json()['data']['status'] == 'job finished' - assert 'output' in job_response2.json()['data'] - for item in job_response2.json()['data']['output']['data']: - assert_positive_sw_out(item) - assert len(job_response2.json()['data']['output']['data']) < len(job_response1.json() - ['data']['output']['data']) - assert job_response2.json()['message'] == "Automatically generated stopwords for the given language" - -def test_delete_stopword(): - '''Test the removal of a stopword''' - #Adding stopwords - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanUser']['token'] - response = client.post(UNIT_URL+'/aa?',headers=headers_auth, json=add_obj) - assert response.status_code == 201 - assert_positive_response(response.json()) - for item in response.json()['data']: - assert_positive_get_stopwords(item) - - #Ensure stopword is added - get_response = client.get(UNIT_URL+'/aa?',headers=headers_auth) - assert get_response.status_code == 200 - assert isinstance(get_response.json(), list) - for item in get_response.json(): - assert_positive_get_stopwords(item) - - #deleting stopword with no auth - Negative Test - resp =client.delete(UNIT_URL+'/aa?lang=aa&stopword=asd',headers=headers) - assert resp.status_code == 401 - assert resp.json()['details'] == "Access token not provided or user not recognized." - - #Deleting stopword with different auth of registerdUser - Positive Test - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['APIUser']['token'] - response = client.delete(UNIT_URL+'/aa?lang=aa&stopword=asd',headers=headers_auth) - assert response.status_code == 201 - assert "successfull" in response.json()['message'] - - # Ensure deleted stopword is not present - get_response = client.get(UNIT_URL+'/aa?',headers=headers_auth) - assert get_response.status_code == 200 - assert isinstance( get_response.json(), list) - assert len(get_response.json())==2 - - #Create and Delete glossary with superadmin - Positive test - # Login as Super Admin - sa_data = { - "user_email": SUPER_USER, - "password": SUPER_PASSWORD - } - response = login(sa_data) - assert response.json()['message'] == "Login Succesfull" - test_user_token = response.json()["token"] - headers_auth['Authorization'] = "Bearer"+" "+test_user_token - response = client.post(UNIT_URL+'/aa?',headers=headers_auth, json=add_obj) - response =client.delete(UNIT_URL+'/aa?lang=aa&stopword=asd',headers=headers_auth) - assert response.status_code == 201 - assert "successfull" in response.json()['message'] - - # Ensure deleted stopword is not present - get_response = client.get(UNIT_URL+'/aa?',headers=headers_auth) #, json=["asd"]) - assert get_response.status_code == 200 - assert isinstance( get_response.json(), list) - assert len(get_response.json())==2 - - #Delete with not available resource language - response =client.delete(UNIT_URL+'/aa?lang=x-ttt&stopword=asd',headers=headers_auth) - assert response.status_code == 404 - assert "Language not available" in response.json()['details'] - logout_user(test_user_token) - - -def test_restore_stopword(): - '''positive test case, checking for correct return object''' - #only Super Admin can restore deleted data - #Adding a stopword and deleting it - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanUser']['token'] - response = client.post(UNIT_URL+'/aa?',headers=headers_auth, json=add_obj) - delete_resp = client.delete(UNIT_URL+'/aa?lang=aa&stopword=asd',headers=headers_auth) - - # Ensure deleted stopword is not present - get_response = client.get(UNIT_URL+'/aa?',headers=headers_auth) - assert get_response.status_code == 200 - assert isinstance( get_response.json(), list) - assert len(get_response.json())==2 - - deleteditem_id = delete_resp.json()['data']['itemId'] - data = {"itemId": deleteditem_id} - - #Restoring data - #Restore stopword without authentication - Negative Test - response = client.put(RESTORE_URL, headers=headers, json=data) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - - #Restore stopword with other API user,VachanAdmin,AgAdmin,AgUser,VachanUser,BcsDev,'VachanContentAdmin','VachanContentViewer'-Negative Test - for user in ['APIUser','VachanAdmin','AgAdmin','AgUser','VachanUser','BcsDev','VachanContentAdmin','VachanContentViewer']: - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users[user]['token'] - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - - #Restore glossary with Super Admin - Positive Test - # Login as Super Admin - sa_data = { - "user_email": SUPER_USER, - "password": SUPER_PASSWORD - } - response = login(sa_data) - assert response.json()['message'] == "Login Succesfull" - test_user_token = response.json()["token"] - headers_auth['Authorization'] = "Bearer"+" "+test_user_token - - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 201 - assert response.json()['message'] == \ - f"Deleted Item with identity {deleteditem_id} restored successfully" - - # Check stopword is restored - get_response = client.get(UNIT_URL+'/aa?',headers=headers_auth) - assert get_response.status_code == 200 - assert isinstance( get_response.json(), list) - assert len(get_response.json())== 3 - - #restore with missing data - Negative Test - data = {} - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert_input_validation_error(response) - - #Restore with invalid item id - Negative Test - data = {"itemId":9999} - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 404 - assert response.json()['error'] == "Requested Content Not Available" - logout_user(test_user_token) \ No newline at end of file diff --git a/app/test/test_translation_suggestions.py b/app/test/test_translation_suggestions.py deleted file mode 100644 index 7f2c572d..00000000 --- a/app/test/test_translation_suggestions.py +++ /dev/null @@ -1,653 +0,0 @@ -'''tests the APIs related to translation suggestion module''' -from . import client -from . import assert_input_validation_error -from .test_agmt_translation import assert_positive_get_tokens, assert_positive_get_sentence -from .test_generic_translation import sentence_list, sample_sent -from .conftest import initial_test_users -from . test_auth_basic import login,SUPER_PASSWORD,SUPER_USER,logout_user - -UNIT_URL = '/v2/text/translate/token-based' -NLP_UNIT_URL = '/v2/nlp' -RESTORE_URL = '/v2/admin/restore' -headers = {"contentType": "application/json", "accept": "application/json"} -headers_auth = {"contentType": "application/json", - "accept": "application/json" - } - -tokens_trans = [ - {"token":"test", "translations":["ടെസ്റ്റ്"]}, - {"token":"the", "translations":[""]}, - {"token":"test case", "translations":["ടെസ്റ്റ് കേസ്"]}, - {"token":"developer", "translations":["ടെവെലപ്പര്‍"]}, - {"token":"run", "translations":["റണ്‍"]}, - {"token":"pass", "translations":["പാസ്സ്"]}, - {"token":"tested", "translations":["ടെസ്റ്റഡ്"], - "metaData":{"tense": "past"}} -] - -align_data = [ - { "sourceTokenList": ["This", "is", "a test case"], - "targetTokenList": ["ഇത്", "ഒരു ടെസ്റ്റ് കേസ്", "ആണ്"], - "alignedTokens":[ - {"sourceTokenIndex":0, "targetTokenIndex":0}, - {"sourceTokenIndex":1, "targetTokenIndex":2}, - {"sourceTokenIndex":2, "targetTokenIndex":1}] - }, - { "sourceTokenList": ["Developer", "is not", "happy"], - "targetTokenList": ["ടെവെലപ്പര്‍", "സന്തോഷവാന്‍", "അല്ല"], - "alignedTokens":[ - {"sourceTokenIndex":0, "targetTokenIndex":0}, - {"sourceTokenIndex":1, "targetTokenIndex":2}, - {"sourceTokenIndex":2, "targetTokenIndex":1}] - }, - { "sourceTokenList": ["Happy", "user", "is here"], - "targetTokenList": ["സന്തോഷവാന്‍ ആയ", "ഉപയോക്താവ്" , "ഇവിടുണ്ട്"], - "alignedTokens":[ - {"sourceTokenIndex":0, "targetTokenIndex":0}, - {"sourceTokenIndex":1, "targetTokenIndex":1}, - {"sourceTokenIndex":2, "targetTokenIndex":2}] - } - -] - -def assert_positive_get_suggetion(item): - '''check for properties in a suggestion response''' - assert "translations" in item - assert isinstance(item['translations'], dict) - for sense in item['translations']: - assert isinstance(item['translations'][sense], (int, float)) - if "metaData" in item and item['metaData'] is not None: - assert isinstance(item['metaData'], dict) - - -def test_learn_n_suggest(): - '''Positive tests for adding knowledge and getting suggestions''' - - # add vocabulary - #without auth - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanUser']['token'] - response = client.post(NLP_UNIT_URL+'/gloss?source_language=en&target_language=ml', - headers=headers, json=tokens_trans) - assert response.json()['error'] == "Authentication Error" - assert response.status_code == 401 - #with auth - response = client.post(NLP_UNIT_URL+'/gloss?source_language=en&target_language=ml', - headers=headers_auth, json=tokens_trans) - assert response.status_code == 201 - assert response.json()['message'] == "Added to glossary" - - # check if suggestions are given in token list - #without auth - token_response = client.put(UNIT_URL+'/tokens?source_language=en&target_language=ml', - headers=headers, json={"sentence_list":sentence_list}) - assert token_response.json()['error'] == "Authentication Error" - assert token_response.status_code == 401 - #with auth another registered user - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['APIUser']['token'] - token_response = client.put(UNIT_URL+'/tokens?source_language=en&target_language=ml', - headers=headers_auth, json={"sentence_list":sentence_list}) - assert token_response.status_code == 200 - assert len(token_response.json()) >10 - found_testcase = False - found_tested = False - for item in token_response.json(): - if item['token'] == "tested": - assert "ടെസ്റ്റഡ്" in item['translations'] - assert item["metaData"]["tense"] == "past" - found_tested = True - assert_positive_get_tokens(item) - if item['token'] == "test case": - assert "ടെസ്റ്റ് കേസ്" in item['translations'] - assert item['translations']["ടെസ്റ്റ് കേസ്"] == 0 - found_testcase = True - assert found_tested - assert found_testcase - - # add alignmnet - response = client.post(NLP_UNIT_URL+'/alignment?source_language=en&target_language=ml', - headers=headers, json=align_data) - assert response.json()['error'] == "Authentication Error" - assert response.status_code == 401 - #with auth - response = client.post(NLP_UNIT_URL+'/alignment?source_language=en&target_language=ml', - headers=headers_auth, json=align_data) - assert response.status_code == 201 - assert response.json()['message'] == "Alignments used for learning" - found_lower_developer = False - for item in response.json()['data']: - if item['token'] == 'developer': - found_lower_developer = True - assert found_lower_developer - - # try tokenizing again - token_response = client.put(UNIT_URL+'/tokens?source_language=en&target_language=ml', - headers=headers, json={"sentence_list":sentence_list}) - assert token_response.json()['error'] == "Authentication Error" - assert token_response.status_code == 401 - #with auth - token_response = client.put(UNIT_URL+'/tokens?source_language=en&target_language=ml', - headers=headers_auth, json={"sentence_list":sentence_list}) - assert token_response.status_code == 200 - found_atestcase = False - found_lower_developer = False - for item in token_response.json(): - assert_positive_get_tokens(item) - if item['token'] == 'a test case': - assert "ഒരു ടെസ്റ്റ് കേസ്" in item['translations'] - found_atestcase = True - if item['token'] == 'developer': - assert item['translations']['ടെവെലപ്പര്‍'] >= 1 - found_lower_developer = True - assert found_atestcase - assert found_lower_developer - - # get gloss - - # only a dict entry not in draft or alignment - response = client.get(NLP_UNIT_URL+'/gloss?source_language=en&target_language=ml&token=test', - headers=headers) - assert response.json()['error'] == "Authentication Error" - assert response.status_code == 401 - #with auth - response = client.get(NLP_UNIT_URL+'/gloss?source_language=en&target_language=ml&token=test', - headers=headers_auth) - assert response.status_code ==200 - assert isinstance(response.json(), dict) - assert len(response.json()['translations']) > 0 - assert_positive_get_suggetion(response.json()) - found_test = False - for item in response.json()['translations']: - if item == "ടെസ്റ്റ്": - found_test = True - assert found_test - - # learnt from alignment - response = client.get(NLP_UNIT_URL+ - '/gloss?source_language=en&target_language=ml&token=a%20test%20case',headers=headers) - assert response.json()['error'] == "Authentication Error" - assert response.status_code == 401 - #with auth - response = client.get(NLP_UNIT_URL+ - '/gloss?source_language=en&target_language=ml&token=a%20test%20case',headers=headers_auth) - assert response.status_code ==200 - assert_positive_get_suggetion(response.json()) - found_atestcase = False - for item in response.json()['translations']: - if item == "ഒരു ടെസ്റ്റ് കേസ്": - found_atestcase = True - assert found_atestcase - - # with different contexts - sense1 = "സന്തോഷവാന്‍" - sense2 = "സന്തോഷവാന്‍ ആയ" - - #no context - response = client.get(NLP_UNIT_URL+'/gloss?source_language=en&target_language=ml&token=happy', - headers=headers) - assert response.json()['error'] == "Authentication Error" - assert response.status_code == 401 - #with auth - response = client.get(NLP_UNIT_URL+'/gloss?source_language=en&target_language=ml&token=happy', - headers=headers_auth) - assert response.status_code ==200 - assert_positive_get_suggetion(response.json()) - found_sense1 = False - found_sense2 = False - for item in response.json()['translations']: - if item == sense1: - found_sense1 = True - score1 = response.json()['translations'][item] - if item == sense2: - found_sense2 = True - score2 = response.json()['translations'][item] - assert found_sense1 - assert found_sense2 - assert score1 == score2 - - - # context 1 - response = client.get(NLP_UNIT_URL+'/gloss?source_language=en&target_language=ml&token=happy'+ - '&context=the%20happy%20user%20went%20home',headers=headers_auth) - assert response.status_code ==200 - assert_positive_get_suggetion(response.json()) - found_sense1 = False - found_sense2 = False - for item in response.json()['translations']: - if item == sense1: - found_sense1 = True - score1 = response.json()['translations'][item] - if item == sense2: - found_sense2 = True - score2 = response.json()['translations'][item] - assert found_sense1 - assert found_sense2 - assert score1 < score2 - - # context 2 - response = client.get(NLP_UNIT_URL+'/gloss?source_language=en&target_language=ml&token=happy'+ - '&context=now%20user%20is%20not%20happy',headers=headers_auth) - assert response.status_code ==200 - assert_positive_get_suggetion(response.json()) - found_sense1 = False - found_sense2 = False - for item in response.json()['translations']: - if item == sense1: - found_sense1 = True - score1 = response.json()['translations'][item] - if item == sense2: - found_sense2 = True - score2 = response.json()['translations'][item] - assert found_sense1 - assert found_sense2 - assert score1 > score2 - - # auto translate - sentence_list[0]['sentence'] = "This his wish "+sentence_list[0]['sentence'] - response = client.put(UNIT_URL+'/suggestions?source_language=en&target_language=ml', - headers=headers, json={"sentence_list":sentence_list}) - assert response.json()['error'] == "Authentication Error" - assert response.status_code == 401 - #with auth - response = client.put(UNIT_URL+'/suggestions?source_language=en&target_language=ml', - headers=headers_auth, json={"sentence_list":sentence_list}) - - # ensures that source is tokenized in draftmeta, even when there is no suggestion - assert len(response.json()[0]["draftMeta"]) > 7 - assert len([meta for meta in response.json()[0]["draftMeta"] if meta[2]=="untranslated"]) > 4 - - draft = client.put(UNIT_URL+'/draft?doc_type=text', headers=headers_auth, json=response.json()) - draft = draft.json() - assert "ഒരു ടെസ്റ്റ് കേസ്." in draft - assert "ടെസ്റ്റ് കേസ് ടെസ്റ്റ് ചെയ്തു" in draft or "ടെസ്റ്റ് കേസ് ടെസ്റ്റഡ്" in draft - assert "ടെവെലപ്പര്‍" in draft - assert "ഇത് ആണ് ടെസ്റ്റ്" in draft - -def test_bug_fix(): - '''testing bug fix for issue #412''' - tokens_abraham = [ - { - "token": "अब्राहम से", - "translations": [ - "Abraham" - ], - "metaData": { - "for": "अब्राहम से" - } - }, - { - "token": "अब्राहम की", - "translations": [ - "Abraham's" - ], - "metaData": { - "for": "अब्राहम की" - } - } - ] - response = client.post(NLP_UNIT_URL+'/gloss?source_language=hi&target_language=en', - headers=headers_auth, json=tokens_abraham) - assert response.status_code == 201 - assert response.json()['message'] == "Added to glossary" - - response = client.get(NLP_UNIT_URL+'/gloss?source_language=hi&target_language=en&token=अब्राहम से', - headers=headers_auth) - assert response.status_code ==200 - assert response.json()["token"] == "अब्राहम से" - assert list(response.json()["translations"].keys())[0] == "Abraham".lower() - assert response.json()["metaData"]["for"] == "अब्राहम से" - - response = client.get(NLP_UNIT_URL+'/gloss?source_language=hi&target_language=en&token=अब्राहम की', - headers=headers_auth) - assert response.status_code ==200 - assert response.json()["token"] == "अब्राहम की" - assert list(response.json()["translations"].keys())[0] == "Abraham's".lower() - assert response.json()["metaData"]["for"] == "अब्राहम की" - -def test_metadata_to_same_gloss(): - '''testing metadata is added to the same token-translation pair''' - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanUser']['token'] - response = client.post(NLP_UNIT_URL+'/gloss?source_language=en&target_language=ml', - headers=headers_auth, json=tokens_trans) - assert response.status_code == 201 - assert response.json()['message'] == "Added to glossary" - - response = \ - client.get(NLP_UNIT_URL+'/gloss-entries?source_language=en&target_language=ml&token=tested', - headers=headers_auth) - assert response.status_code ==200 - # Ensuring metadata is added to the correct token-translation pair - positive test - assert response.json()[0]["token"] == "tested" - assert response.json()[0]["translation"] == "ടെസ്റ്റഡ്" - assert response.json()[0]["metaData"]["tense"] == "past" - - #Adding metadata in incorrect format - tokens_test = [ - {"token":"test", "translations":["ടെസ്റ്റ്"],"metaData":"translations"} - ] - response = client.post(NLP_UNIT_URL+'/gloss?source_language=en&target_language=ml', - headers=headers_auth, json=tokens_test) - assert response.status_code == 422 - assert response.json()['error'] == "Input Validation Error" - - #Adding a sentences and ensuring frequency is incremented for same token-translation pair - response = \ - client.get(NLP_UNIT_URL+'/gloss-entries?source_language=en&target_language=ml&token=developer', - headers=headers_auth) - freq_before = response.json()[0]['frequency'] - align_data1 = [ - { - "sourceTokenList": ["This","is","a","developer"], - "targetTokenList": ["ഇത്", "ഒരു", "ടെവെലപ്പര്‍", "ആണ്"], - "alignedTokens": [ - {"sourceTokenIndex": 0,"targetTokenIndex": 0}, - {"sourceTokenIndex": 1,"targetTokenIndex": 3}, - {"sourceTokenIndex": 2,"targetTokenIndex": 1}, - {"sourceTokenIndex": 3,"targetTokenIndex": 2} - ] - }, - { - "sourceTokenList": ["Developer","is","happy"], - "targetTokenList": ["ടെവെലപ്പര്‍","സന്തോഷവാന്‍", "ആണ്"], - "alignedTokens": [ - {"sourceTokenIndex": 0,"targetTokenIndex": 0}, - {"sourceTokenIndex": 1,"targetTokenIndex": 2}, - {"sourceTokenIndex": 2,"targetTokenIndex": 1} - ] - } - ] - response = client.post(NLP_UNIT_URL+'/alignment?source_language=en&target_language=ml', - headers=headers_auth, json=align_data1) - assert response.status_code == 201 - assert response.json()['message'] == "Alignments used for learning" - response = \ - client.get(NLP_UNIT_URL+'/gloss-entries?source_language=en&target_language=ml&token=developer', - headers=headers_auth) - freq_after = response.json()[0]['frequency'] - assert freq_after == freq_before + 2 - -def test_update_glossary(): - '''Test the updation of translation and metadata fields of glossary''' - - # Adding glossary - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanUser']['token'] - response = client.post(NLP_UNIT_URL+'/gloss?source_language=en&target_language=ml', - headers=headers_auth, json=tokens_trans) - assert response.status_code == 201 - assert response.json()['message'] == "Added to glossary" - response = \ - client.get(NLP_UNIT_URL+'/gloss-entries?source_language=en&target_language=ml&token=tested', - headers=headers_auth) - assert response.status_code ==200 - - # Ensuring metadata is added to the correct token-translation pair - positive test - assert response.json()[0]["token"] == "tested" - assert response.json()[0]["translation"] == "ടെസ്റ്റഡ്" - assert response.json()[0]["metaData"]["tense"] == "past" - token_id = response.json()[0]["tmID"] - data = { - "tmID": token_id, - "token": "tested", - "translation": "പരീക്ഷിക്കുന്നു", - "metaData": {"tense": "present" } - } - # update without auth - negative test - response = client.put(NLP_UNIT_URL+'/gloss',headers=headers, json=data) - assert response.json()['error'] == "Authentication Error" - assert response.status_code == 401 - - #with auth - updating both translation and metadata - positive test - response = client.put(NLP_UNIT_URL+'/gloss',headers=headers_auth, json=data) - assert response.status_code ==200 - assert response.json()['message'] == 'Glossary Updated' - assert response.json()['data']['token'] == "tested" - assert response.json()['data']['translation'] == "പരീക്ഷിക്കുന്നു" - assert response.json()['data']['metaData']['tense'] == "present" - - #updating translation only - positive test - data = { - "tmID": token_id, - "token": "tested", - "translation": "പരീക്ഷിക്കുന്നു" - } - response = client.put(NLP_UNIT_URL+'/gloss',headers=headers_auth, json=data) - assert response.json()['data']['translation'] == "പരീക്ഷിക്കുന്നു" - assert response.json()['data']["metaData"]["tense"] == "present" - - #updating on invalid token - negative test - data = { - "tmID": 9999, - "token": "tested", - "translation": "പരീക്ഷിക്കുന്നു", - "metaData": {"tense": "present"} - } - response = client.put(NLP_UNIT_URL+'/gloss',headers=headers_auth, json=data) - assert response.status_code == 404 - assert response.json()['error'] == "Requested Content Not Available" - - #updating metadata in incorrect format - negative test - data = { - "tmID": token_id, - "token": "tested", - "translation": "പരീക്ഷിക്കുന്നു", - "metaData": "tense" - } - response = client.put(NLP_UNIT_URL+'/gloss',headers=headers_auth, json=data) - assert response.status_code == 422 - assert response.json()['error'] == "Input Validation Error" - -def test_delete_glossary(): - '''Test the removal of a suggestion/glossary''' - - #Adding a suggestion for translation - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanUser']['token'] - response = client.post(NLP_UNIT_URL+'/gloss?source_language=en&target_language=ml', - headers=headers_auth, json=tokens_trans) - assert response.status_code == 201 - assert response.json()['message'] == "Added to glossary" - - # Check glossary is added - response = client.get(NLP_UNIT_URL+'/gloss?source_language=en&target_language=ml&token=test', - headers=headers_auth) - assert response.status_code ==200 - assert isinstance(response.json(), dict) - assert len(response.json()['translations']) > 0 - assert_positive_get_suggetion(response.json()) - found_test = False - for item in response.json()['translations']: - if item == "ടെസ്റ്റ്": - found_test = True - assert found_test - - #deleting glossary with no auth - Negative Test - resp =client.delete(NLP_UNIT_URL+ - '/gloss?source_lang=en&target_lang=ml&token=test&translation=ടെസ്റ്റ്', - headers=headers) - assert resp.status_code == 401 - assert resp.json()['details'] == "Access token not provided or user not recognized." - - #Delete content with other API user,AgAdmin,AgUser,VachanUser,BcsDev,VachanContentViewer - for user in ['APIUser','AgUser','VachanUser','SanketMASTUser','BcsDev','VachanContentViewer']: - headers_au = {"contentType": "application/json", - "accept": "application/json", - 'Authorization': "Bearer"+" "+initial_test_users[user]['token'] - } - response = client.delete(NLP_UNIT_URL+ - '/gloss?source_lang=en&target_lang=ml&token=test&translation=ടെസ്റ്റ്', - headers=headers_au) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - - #Deleting glossary with AgAdmin used - Positive Test - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - response = client.delete(NLP_UNIT_URL+ - '/gloss?source_lang=en&target_lang=ml&token=developer&translation=ടെവെലപ്പര്‍', - headers=headers_auth) - assert response.status_code == 201 - assert "successfull" in response.json()['message'] - - # Ensure deleted glossary is not present - get_response =client.get(NLP_UNIT_URL+'/gloss?source_language=en&target_language=ml&token=developer', - headers=headers_auth) - assert get_response.status_code == 200 - assert isinstance(get_response.json(), dict) - assert len(get_response.json()['translations']) == 0 - - #Deleting glossary with VachanAdmin used - Positive Test - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanAdmin']['token'] - response = client.delete(NLP_UNIT_URL+ - '/gloss?source_lang=en&target_lang=ml&token=test case&translation=ടെസ്റ്റ് കേസ്', - headers=headers_auth) - assert response.status_code == 201 - assert "successfull" in response.json()['message'] - - # Ensure deleted glossary is not present - get_response =client.get(NLP_UNIT_URL+'/gloss?source_language=en&target_language=ml&token=test case', - headers=headers_auth) - assert get_response.status_code == 200 - assert isinstance(get_response.json(), dict) - assert len(get_response.json()['translations']) == 0 - - #Deleting glossary with SanketMASTAdmin used - Positive Test - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['SanketMASTAdmin']['token'] - response = client.delete(NLP_UNIT_URL+ - '/gloss?source_lang=en&target_lang=ml&token=test&translation=ടെസ്റ്റ്', - headers=headers_auth) - assert response.status_code == 201 - assert "successfull" in response.json()['message'] - - # Ensure deleted glossary is not present - get_response =client.get(NLP_UNIT_URL+'/gloss?source_language=en&target_language=ml&token=test', - headers=headers_auth) - assert get_response.status_code == 200 - assert isinstance(get_response.json(), dict) - assert len(get_response.json()['translations']) == 0 - - #Deleting glossary with empty translation - Positive Test - response =client.delete(NLP_UNIT_URL+ - '/gloss?source_lang=en&target_lang=ml&token=the&translation=', - headers=headers_auth) - assert response.status_code == 201 - assert "successfull" in response.json()['message'] - - # Ensure deleted glossary is not present - get_response =client.get(NLP_UNIT_URL+'/gloss?source_language=en&target_language=ml&token=the', - headers=headers_auth) - assert get_response.status_code == 200 - assert isinstance(get_response.json(), dict) - assert len(get_response.json()['translations']) == 0 - - #Create and Delete glossary with superadmin - Positive test - # Login as Super Admin - sa_data = { - "user_email": SUPER_USER, - "password": SUPER_PASSWORD - } - response = login(sa_data) - assert response.json()['message'] == "Login Succesfull" - test_user_token = response.json()["token"] - headers_auth['Authorization'] = "Bearer"+" "+test_user_token - response = client.post(NLP_UNIT_URL+'/gloss?source_language=en&target_language=ml', - headers=headers_auth, json=tokens_trans) - response = client.delete(NLP_UNIT_URL+ - '/gloss?source_lang=en&target_lang=ml&token=test&translation=ടെസ്റ്റ്', - headers=headers_auth) - assert response.status_code == 201 - assert "successfull" in response.json()['message'] - - # Ensure deleted sentence is not present - get_response =client.get(NLP_UNIT_URL+'/gloss?source_language=en&target_language=ml&token=test', - headers=headers_auth) - assert get_response.status_code == 200 - assert isinstance(get_response.json(), dict) - assert len(get_response.json()['translations']) == 0 - - #Delete with not available source language - response = client.delete(NLP_UNIT_URL+ - '/gloss?source_lang=x-ttt&target_lang=ml&token=test&translation=ടെസ്റ്റ്', - headers=headers_auth) - assert response.status_code == 404 - assert "Source language not available" in response.json()['details'] - - #Delete not available target language - response = client.delete(NLP_UNIT_URL+ - '/gloss?source_lang=en&target_lang=x-ttt&token=test&translation=ടെസ്റ്റ്', - headers=headers_auth) - assert response.status_code == 404 - assert "Target language not available" in response.json()['details'] - logout_user(test_user_token) - - -def test_restore_glossary(): - '''positive test case, checking for correct return object''' - #only Super Admin can restore deleted data - #Adding a suggestion for translation - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanAdmin']['token'] - response = client.post(NLP_UNIT_URL+'/gloss?source_language=en&target_language=ml', - headers=headers_auth, json=tokens_trans) - # Deleting - delete_resp = client.delete(NLP_UNIT_URL+ - '/gloss?source_lang=en&target_lang=ml&token=test&translation=ടെസ്റ്റ്', - headers=headers_auth) - - # Ensure deleted glossary is not present - get_response =client.get(NLP_UNIT_URL+'/gloss?source_language=en&target_language=ml&token=test', - headers=headers_auth) - assert get_response.status_code == 200 - assert isinstance(get_response.json(), dict) - assert len(get_response.json()['translations']) == 0 - - deleteditem_id = delete_resp.json()['data']['itemId'] - data = {"itemId": deleteditem_id} - - #Restoring data - #Restore glossary without authentication - Negative Test - response = client.put(RESTORE_URL, headers=headers, json=data) - assert response.status_code == 401 - assert response.json()['error'] == 'Authentication Error' - - #Restore glossary with other API user,VachanAdmin,AgAdmin,AgUser,VachanUser,BcsDev,'VachanContentAdmin','VachanContentViewer'-Negative Test - for user in ['APIUser','VachanAdmin','AgAdmin','AgUser','VachanUser','BcsDev','VachanContentAdmin','VachanContentViewer']: - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users[user]['token'] - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 403 - assert response.json()['error'] == 'Permission Denied' - - #Restore glossary with Super Admin - Positive Test - # Login as Super Admin - sa_data = { - "user_email": SUPER_USER, - "password": SUPER_PASSWORD - } - response = login(sa_data) - assert response.json()['message'] == "Login Succesfull" - test_user_token = response.json()["token"] - headers_auth['Authorization'] = "Bearer"+" "+test_user_token - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 201 - assert response.json()['message'] == \ - f"Deleted Item with identity {deleteditem_id} restored successfully" - - # Check glossary is restored - response = client.get(NLP_UNIT_URL+'/gloss?source_language=en&target_language=ml&token=test', - headers=headers_auth) - assert response.status_code ==200 - assert isinstance(response.json(), dict) - assert len(response.json()['translations']) > 0 - assert_positive_get_suggetion(response.json()) - found_test = False - for item in response.json()['translations']: - if item == "ടെസ്റ്റ്": - found_test = True - assert found_test - - #restore with missing data - Negative Test - data = {} - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert_input_validation_error(response) - - #Restore with invalid item id - Negative Test - data = {"itemId":9999} - response = client.put(RESTORE_URL, headers=headers_auth, json=data) - assert response.status_code == 404 - assert response.json()['error'] == "Requested Content Not Available" - logout_user(test_user_token) diff --git a/app/test/test_translation_workflow.py b/app/test/test_translation_workflow.py deleted file mode 100644 index adde0c9a..00000000 --- a/app/test/test_translation_workflow.py +++ /dev/null @@ -1,441 +0,0 @@ -'''Executes and tests the AgMT and generic translation workflows. -This is not part of the automated tests, and data added to DB by running this script will persist. -This is to be used manually during develepment testing''' -import json -import requests -from . import client -from .test_versions import check_post -from .test_resources import check_post as source_post -from .conftest import initial_test_users - -BASE_URL = "v2/" -headers = {"contentType": "application/json", "accept": "application/json"} - -# have a bible source to be used -source_name = "hi_XYZ_1_bible" # pylint: disable=C0103 -project_id = None # pylint: disable=C0103 - -ver_data = { - "versionAbbreviation": "XYZ", - "versionName": "Xyz version to test", - "revision": "1", - "metaData": {"owner": "someone", "access-key": "123xyz"} -} - -src_data = { - "resourceType": "bible", - "language": "hi", - "version": "XYZ", - "revision": 1, - "accessPermissions": [ - "content","open-access" - ], - "year": 2020, - "license": "CC-BY-SA", - "metaData": {"owner": "someone", "access-key": "123xyz"} -} - -gospel_books_data = [ - {"USFM":"\\id mat\n\\c 1\n\\p\n\\v 1 test verse one\n\\v 2 test verse two", - "JSON":json.loads('''{ - "book": { - "bookCode": "MAT" - }, - "chapters": [ - { - "chapterNumber": "1", - "contents": [ - { - "p": null - }, - { - "verseNumber": "1", - "verseText": "test verse one", - "contents": [ - "test verse one" - ] - }, - { - "verseNumber": "2", - "verseText": "test verse two", - "contents": [ - "test verse two" - ] - } - ] - } - ], - "_messages": { - "_warnings": [ - "Empty lines present. ", - "Book code is in lowercase. " - ] - } - }''')}, - {"USFM":"\\id mrk\n\\c 1\n\\p\n\\v 1 test verse one\n\\v 2 test verse two", - "JSON":json.loads('''{ - "book": { - "bookCode": "MRK" - }, - "chapters": [ - { - "chapterNumber": "1", - "contents": [ - { - "p": null - }, - { - "verseNumber": "1", - "verseText": "test verse one", - "contents": [ - "test verse one" - ] - }, - { - "verseNumber": "2", - "verseText": "test verse two", - "contents": [ - "test verse two" - ] - } - ] - } - ], - "_messages": { - "_warnings": [ - "Empty lines present. ", - "Book code is in lowercase. " - ] - } - }''')}, - {"USFM":"\\id luk\n\\c 1\n\\p\n\\v 1 test verse one\n\\v 2 test verse two", - "JSON":json.loads('''{ - "book": { - "bookCode": "LUK" - }, - "chapters": [ - { - "chapterNumber": "1", - "contents": [ - { - "p": null - }, - { - "verseNumber": "1", - "verseText": "test verse one", - "contents": [ - "test verse one" - ] - }, - { - "verseNumber": "2", - "verseText": "test verse two", - "contents": [ - "test verse two" - ] - } - ] - } - ], - "_messages": { - "_warnings": [ - "Empty lines present. ", - "Book code is in lowercase. " - ] - } - }''')}, - {"USFM":"\\id jhn\n\\c 1\n\\p\n\\v 1 test verse one\n\\v 2 test verse two", - "JSON":json.loads('''{ - "book": { - "bookCode": "JHN" - }, - "chapters": [ - { - "chapterNumber": "1", - "contents": [ - { - "p": null - }, - { - "verseNumber": "1", - "verseText": "test verse one", - "contents": [ - "test verse one" - ] - }, - { - "verseNumber": "2", - "verseText": "test verse two", - "contents": [ - "test verse two" - ] - } - ] - } - ], - "_messages": { - "_warnings": [ - "Empty lines present. ", - "Book code is in lowercase. " - ] - } - }''')}, -] - -project_post_data = { - "projectName": "Test project 4", - "sourceLanguageCode": "hi", - "targetLanguageCode": "ml" -} - -bible_books = { - "mat": "\\id MAT\n\\c 1\n\\p\n\\v 1 इब्राहीम के वंशज दाऊद के पुत्र यीशु मसीह की वंशावली इस "+ - "प्रकार है:\n\\v 2 इब्राहीम का पुत्र था इसहाक और इसहाक का पुत्र हुआ याकूब। फिर याकूब "+ - "से यहूदा और उसके भाई उत्पन्न हुए।\n\\v 3 यहूदा के बेटे थे फिरिस और जोरह। (उनकी माँ "+ - "का नाम तामार था।) फिरिस, हिस्रोन का पिता था। हिस्रोन राम का पिता था।", - "mrk": "\\id MRK\n\\c 1\n\\p\n\\v 1 यह परमेश्वर के पुत्र यीशु मसीह के शुभ संदेश का प्रारम्भ"+ - " है।\n\\v 2 भविष्यवक्ता यशायाह की पुस्तक में लिखा है कि: “सुन! मैं अपने दूत को तुझसे"+ - " पहले भेज रहा हूँ। वह तेरे लिये मार्ग तैयार करेगा।”\n\\v 3 “जंगल में किसी पुकारने "+ - "वाले का शब्द सुनाई दे रहा है: ‘प्रभु के लिये मार्ग तैयार करो। और उसके लिये राहें "+ - "सीधी बनाओ।’”\n\\v 4 यूहन्ना लोगों को जंगल में बपतिस्मा देते आया था। उसने लोगों से"+ - " पापों की क्षमा के लिए मन फिराव का बपतिस्मा लेने को कहा।\n\\v 5 फिर समूचे यहूदिया"+ - " देश के और यरूशलेम के लोग उसके पास गये और उस ने यर्दन नदी में उन्हें बपतिस्मा दिया"+ - "। क्योंकि उन्होंने अपने पाप मान लिये थे।" -} - -project_update_data = { - "projectId":project_id, - "uploadedUSFMs":[bible_books['mat'], bible_books['mrk']], - "selectedBooks":{"bible": source_name, - "books": ['luk', 'jhn']} - -} - -token_update_data = [ - { - "token":"यीशु मसीह", - "occurrences":[ - {"sentenceId":41001001, "offset":[31, 40]}], - "translation":"യേശു ക്രിസ്തു" - }, - { - "token":"पुत्र", - "occurrences":[ - {"sentenceId":41001001, "offset":[25,30]}], - "translation":"പുത്രന്‍" - }, - { - "token":"इब्राहीम", - "occurrences":[ - {"sentenceId":41001002, "offset":[0, 8]}], - "translation":"അബ്രഹാം" - } -] - -NEW_USER_ID = initial_test_users['AgUser']['test_user_id'] -user_data = { - "project_id": project_id, - "userId": NEW_USER_ID, - "userRole": "test role", - "metaData": {"somekey": "value"}, - "active": False -} - -ALIGNMENT_SRC = "hi" -ALIGNMENT_TRG = "ml" -alignment_data = [ -{ - "sourceTokenList": [ - "क्योंकि","परमेश्वर","ने","जगत","से","ऐसा","प्रेम","रखा","कि","उसने", - "अपना","एकलौता","पुत्र","दे","दिया","ताकि","जो","कोई","उस","पर","विश्वास","करे", - "वह","नाश","न","हो","परन्तु","अनन्त","जीवन","पाए" - ], - "targetTokenList": [ - "തന്റെ", - "ഏകജാതനായ","പുത്രനിൽ", - "വിശ്വസിക്കുന്ന","ഏവനും","നശിച്ചുപോകാതെ","നിത്യജീവൻ", - "പ്രാപിക്കേണ്ടതിന്","ദൈവം","അവനെ","നല്കുവാൻ","തക്കവണ്ണം", - "ലോകത്തെ","സ്നേഹിച്ചു" - ], - "alignedTokens": [ - { - "sourceTokenIndex": 1, - "targetTokenIndex": 8 - }, - # { - # "sourceTokenIndex": 2, - # "targetTokenIndex": 8 - # }, - { - "sourceTokenIndex": 3, - "targetTokenIndex": 12 - }, - { - "sourceTokenIndex": 4, - "targetTokenIndex": 12 - }, - { - "sourceTokenIndex": 5, - "targetTokenIndex": 11 - }, - { - "sourceTokenIndex": 6, - "targetTokenIndex": 13 - }, - { - "sourceTokenIndex": 7, - "targetTokenIndex": 13 - }, - { - "sourceTokenIndex": 8, - "targetTokenIndex": 13 - }, - { - "sourceTokenIndex": 9, - "targetTokenIndex": 0 - }, - { - "sourceTokenIndex": 10, - "targetTokenIndex": 0 - }, - { - "sourceTokenIndex": 11, - "targetTokenIndex": 1 - }, - { - "sourceTokenIndex": 12, - "targetTokenIndex": 1 - }, - { - "sourceTokenIndex": 11, - "targetTokenIndex": 2 - }, - { - "sourceTokenIndex": 12, - "targetTokenIndex": 2 - }, - { - "sourceTokenIndex": 13, - "targetTokenIndex": 10 - }, - { - "sourceTokenIndex": 14, - "targetTokenIndex": 10 - }, - { - "sourceTokenIndex": 20, - "targetTokenIndex": 3 - }, - { - "sourceTokenIndex": 21, - "targetTokenIndex": 3 - }, - { - "sourceTokenIndex": 18, - "targetTokenIndex": 4 - }, - { - "sourceTokenIndex": 19, - "targetTokenIndex": 4 - }, - - { - "sourceTokenIndex": 23, - "targetTokenIndex": 5 - }, - { - "sourceTokenIndex": 24, - "targetTokenIndex": 5 - }, - { - "sourceTokenIndex": 25, - "targetTokenIndex": 5 - }, - { - "sourceTokenIndex": 27, - "targetTokenIndex": 6 - }, - { - "sourceTokenIndex": 28, - "targetTokenIndex": 6 - }, - { - "sourceTokenIndex": 29, - "targetTokenIndex": 7 - } - ] -} -] - -def test_end_to_end_translation(): - '''happy path test for AGMT translation workflow''' - # resp = client.post(BASE_URL+"versions", headers=headers, json=ver_data) - resp = check_post(ver_data) - assert resp.json()['message'] == "Version created successfully" - - - # resp = client.post(BASE_URL+"sources", headers=headers, json=src_data) - resp = source_post(src_data) - assert resp.json()['message'] == "Resource created successfully" - source_name = resp.json()['data']['resourceName'] - - headers_auth = {"contentType": "application/json", - "accept": "application/json", - "app":"Autographa" - } - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['VachanAdmin']['token'] - resp = client.post(BASE_URL+"resources/bibles/"+source_name+"/books", headers=headers_auth, - json=gospel_books_data) - assert resp.json()['message'] == "Bible books uploaded and processed successfully" - - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - resp = client.post(BASE_URL+"text/translate/token-based/projects", headers=headers_auth, json=project_post_data) - assert resp.json()['message'] == "Project created successfully" - proj_id = resp.json()['data']['projectId'] - - resp = client.put(BASE_URL+"text/translate/token-based/projects"+'?project_id='+str(proj_id), \ - headers=headers_auth, json=project_update_data) - assert resp.json()['message'] == "Project updated successfully" - - # tokenize - resp = client.get(BASE_URL+"text/translate/token-based/project/tokens?project_id="+str(proj_id),headers=headers_auth) - assert resp.status_code == 200 - - # translate - resp = client.put(BASE_URL+"text/translate/token-based/project/tokens?project_id="+str(proj_id), - headers=headers_auth, json=token_update_data) - assert resp.json()['message'] == "Token translations saved" - - # Additional user - NEW_USER_ID = initial_test_users['AgUser']['test_user_id'] - headers_auth['Authorization'] = "Bearer"+" "+initial_test_users['AgAdmin']['token'] - resp = client.post(BASE_URL+"text/translate/token-based/project/user?project_id="+str(proj_id)+ - "&user_id="+str(NEW_USER_ID), headers=headers_auth) - assert resp.json()['message'] == "User added to project successfully" - - user_data['userId'] = NEW_USER_ID - resp = client.put(BASE_URL+"text/translate/token-based/project/user"'?project_id='+str(proj_id),\ - headers=headers_auth, json=user_data) - # print(resp.json()) - assert resp.json()['message'] == "User updated in project successfully" - - resp = client.get(BASE_URL+"text/translate/token-based/projects?user_id="+str(NEW_USER_ID),headers=headers_auth) - assert len(resp.json()) > 0 - - # # Suggestions - - resp = client.post(BASE_URL+"nlp/alignment?source_language="+ALIGNMENT_SRC+ - "&target_language="+ALIGNMENT_TRG, headers=headers_auth, json=alignment_data) - # print("**",resp) - assert resp.status_code == 201 - # print(resp) - - - # tokenize after adding token "परमेश्वर" via alignment - resp = client.put(BASE_URL+"text/translate/token-based/project/suggestions?project_id="+str(proj_id)+ - "&sentence_id_list=42001001", - headers=headers_auth) - draft = resp.json()[0]['draft'] - assert "ദൈവം" in draft - assert "പുത്രന്‍" in draft - assert "യേശു ക്രിസ്തു" in draft \ No newline at end of file