@@ -114,7 +114,9 @@ async def applet_one_sam_subject(session: AsyncSession, applet_one: AppletFull,
114114
115115
116116@pytest .fixture
117- async def bob_reviewer_in_applet_with_reviewable_activity (session , tom , bob , applet_with_reviewable_activity ) -> User :
117+ async def bob_reviewer_in_applet_with_reviewable_activity (
118+ session : AsyncSession , tom , bob , applet_with_reviewable_activity
119+ ) -> User :
118120 tom_subject = await SubjectsService (session , tom .id ).get_by_user_and_applet (
119121 tom .id , applet_with_reviewable_activity .id
120122 )
@@ -281,6 +283,35 @@ async def tom_answer_on_reviewable_applet(
281283 )
282284
283285
286+ @pytest .fixture
287+ async def lucy_answer_on_reviewable_applet (
288+ session : AsyncSession ,
289+ lucy_manager_in_applet_with_reviewable_activity : User ,
290+ applet_with_reviewable_activity : AppletFull ,
291+ ) -> AnswerSchema :
292+ await UserAppletAccessService (
293+ session , lucy_manager_in_applet_with_reviewable_activity .id , applet_with_reviewable_activity .id
294+ ).add_role (lucy_manager_in_applet_with_reviewable_activity .id , Role .RESPONDENT )
295+
296+ answer_service = AnswerService (session , lucy_manager_in_applet_with_reviewable_activity .id )
297+ return await answer_service .create_answer (
298+ AppletAnswerCreate (
299+ applet_id = applet_with_reviewable_activity .id ,
300+ version = applet_with_reviewable_activity .version ,
301+ submit_id = uuid .uuid4 (),
302+ activity_id = applet_with_reviewable_activity .activities [0 ].id ,
303+ answer = ItemAnswerCreate (
304+ item_ids = [applet_with_reviewable_activity .activities [0 ].items [0 ].id ],
305+ start_time = datetime .datetime .now (datetime .UTC ),
306+ end_time = datetime .datetime .now (datetime .UTC ),
307+ user_public_key = str (lucy_manager_in_applet_with_reviewable_activity .id ),
308+ ),
309+ client = ClientMeta (app_id = f"{ uuid .uuid4 ()} " , app_version = "1.1" , width = 984 , height = 623 ),
310+ consent_to_share = False ,
311+ )
312+ )
313+
314+
284315@pytest .fixture
285316async def lucy_answer (session : AsyncSession , lucy : User , applet : AppletFull ) -> AnswerSchema :
286317 answer_service = AnswerService (session , lucy .id )
@@ -4518,6 +4549,83 @@ def _mock_download_ehr_zip(storage_path, data, file_buffer):
45184549 assert file_list == file_names
45194550 assert str (tom_answer_activity_flow .submit_id ) in file_names [0 ]
45204551
4552+ @pytest .mark .parametrize (
4553+ "user_fixture,role" ,
4554+ (
4555+ ("tom" , Role .OWNER ),
4556+ ("bob" , Role .REVIEWER ),
4557+ ),
4558+ )
4559+ async def test_applet_ehr_data_endpoint_reviewer (
4560+ self ,
4561+ client ,
4562+ session ,
4563+ tom ,
4564+ bob_reviewer_in_applet_with_reviewable_activity ,
4565+ lucy_answer_on_reviewable_applet ,
4566+ tom_answer_on_reviewable_applet ,
4567+ user_fixture ,
4568+ role ,
4569+ request ,
4570+ ):
4571+ # Create EHR record
4572+ answer_ehr = AnswerEHR (
4573+ submit_id = lucy_answer_on_reviewable_applet .submit_id ,
4574+ ehr_ingestion_status = EHRIngestionStatus .COMPLETED ,
4575+ ehr_storage_uri = "fake/ehr/storage/uri" ,
4576+ activity_id = uuid .UUID (lucy_answer_on_reviewable_applet .activity_history_id .split ("_" )[0 ]),
4577+ )
4578+ await AnswersEHRCRUD (session = session ).upsert (answer_ehr )
4579+
4580+ # Create EHR record
4581+ answer_ehr = AnswerEHR (
4582+ submit_id = tom_answer_on_reviewable_applet .submit_id ,
4583+ ehr_ingestion_status = EHRIngestionStatus .COMPLETED ,
4584+ ehr_storage_uri = "tom/fake/ehr/storage/uri" ,
4585+ activity_id = uuid .UUID (tom_answer_on_reviewable_applet .activity_history_id .split ("_" )[0 ]),
4586+ )
4587+ await AnswersEHRCRUD (session = session ).upsert (answer_ehr )
4588+
4589+ file_names = []
4590+
4591+ def _mock_download_ehr_zip (storage_path , data , file_buffer ):
4592+ file_buffer .write (b"mocked EHR zip data" )
4593+ file_buffer .seek (0 )
4594+
4595+ file_name = EHRStorage .ehr_zip_filename (data )
4596+ file_names .append (file_name )
4597+
4598+ return file_name
4599+
4600+ with patch (
4601+ "apps.integrations.oneup_health.service.ehr_storage.EHRStorage.download_ehr_zip"
4602+ ) as download_ehr_zip :
4603+ download_ehr_zip .side_effect = _mock_download_ehr_zip
4604+
4605+ login_user = request .getfixturevalue (user_fixture )
4606+ client .login (login_user )
4607+
4608+ # Request EHR data
4609+ response = await client .get (
4610+ self .applet_ehr_answers_export_url .format (applet_id = lucy_answer_on_reviewable_applet .applet_id )
4611+ )
4612+ assert response .status_code == 200
4613+ assert response .headers ["Content-Type" ] == "application/zip"
4614+ assert response .headers ["Content-Disposition" ] == "attachment; filename=EHR.zip"
4615+
4616+ response_body = response .read ()
4617+ with zipfile .ZipFile (io .BytesIO (response_body ), "r" ) as zip_file :
4618+ file_list = zip_file .namelist ()
4619+ if role == Role .REVIEWER :
4620+ assert len (file_list ) == 1
4621+ assert file_list == file_names
4622+ assert str (tom_answer_on_reviewable_applet .submit_id ) in file_names [0 ]
4623+ else :
4624+ assert len (file_list ) == 2
4625+ assert file_list == file_names
4626+ assert str (tom_answer_on_reviewable_applet .submit_id ) in file_names [0 ]
4627+ assert str (lucy_answer_on_reviewable_applet .submit_id ) in file_names [1 ]
4628+
45214629 @pytest .mark .asyncio
45224630 async def test_applet_ehr_data_endpoint_without_ehr (self , client , session , tom , tom_answer ):
45234631 # Create EHR record
0 commit comments