From 8c04e228e9742f1a73c6dd6d216528a747f37a44 Mon Sep 17 00:00:00 2001 From: Kelly McDonald Date: Tue, 1 Jun 2021 11:46:43 -0400 Subject: [PATCH] Add test and fix to make sure that an empty study associates list (or a blank list) will effectively clear the extra study associates --- crc/scripts/update_study_associates.py | 32 ++++---- .../study_sponsors_associates_delete.bpmn | 79 +++++++++++++++++++ tests/study/test_study_associate_script.py | 27 +++++++ 3 files changed, 124 insertions(+), 14 deletions(-) create mode 100644 tests/data/study_sponsors_associates_delete/study_sponsors_associates_delete.bpmn diff --git a/crc/scripts/update_study_associates.py b/crc/scripts/update_study_associates.py index a11830c7..8ee5b58b 100644 --- a/crc/scripts/update_study_associates.py +++ b/crc/scripts/update_study_associates.py @@ -4,13 +4,11 @@ from crc.services.study_service import StudyService class UpdateStudyAssociates(Script): - argument_error_message = "You must supply at least one argument to the " \ "update_study_associates task, an array of objects in the form " \ "{'uid':'someid', 'role': 'text', 'send_email: 'boolean', " \ "'access':'boolean'} " - def get_description(self): return """ Allows you to associate other users with a study - only 'uid' is required in the @@ -26,20 +24,26 @@ associations already in place. example : update_study_associates([{'uid':'sbp3ey','role':'Unicorn Herder', 'send_email': False, 'access':True}]) """ - def validate_arg(self,arg): - if not isinstance(arg,list): + + def validate_arg(self, arg): + if not isinstance(arg, list): raise ApiError("invalid parameter", "This function is expecting a list of dictionaries") - if not len(arg) > 0 and not isinstance(arg[0],dict): - raise ApiError("invalid paramemter","This function is expecting a list of dictionaries") + if len(arg[0]) > 0: + if not len(arg) > 0 and not isinstance(arg[0], dict): + raise ApiError("invalid paramemter", "This function is expecting a list of dictionaries") def do_task_validate_only(self, task, study_id, workflow_id, *args, **kwargs): - items = args[0] - self.validate_arg(items) - return all([x.get('uid',False) for x in items]) - + if len(args) == 0: + items = [] + else: + items = args[0] + self.validate_arg(items) + return all([x.get('uid', False) for x in items]) def do_task(self, task, study_id, workflow_id, *args, **kwargs): - access_list = args[0] - self.validate_arg(access_list) - return StudyService.update_study_associates(study_id,access_list) - + if len(args) == 0: + access_list = [] + else: + access_list = args[0] + self.validate_arg(access_list) + return StudyService.update_study_associates(study_id, access_list) diff --git a/tests/data/study_sponsors_associates_delete/study_sponsors_associates_delete.bpmn b/tests/data/study_sponsors_associates_delete/study_sponsors_associates_delete.bpmn new file mode 100644 index 00000000..a4d96fe8 --- /dev/null +++ b/tests/data/study_sponsors_associates_delete/study_sponsors_associates_delete.bpmn @@ -0,0 +1,79 @@ + + + + + SequenceFlow_1nfe5m9 + + + + SequenceFlow_1nfe5m9 + SequenceFlow_1bqiin0 + sponsors = study_info('sponsors') + + + + + SequenceFlow_1bqiin0 + Flow_09cika8 + update_study_associate(uid='lb3dp',role='SuperGal',send_email=False,access=True) +update_study_associate(uid='lje5u',role='SuperGal2',send_email=False,access=False) + + + Flow_0axwrzg + + + This should just leave us a task to complete after the update_study_assocate script + Flow_1xi8k3i + Flow_0axwrzg + + + + + Flow_09cika8 + Flow_1xi8k3i + update_study_associates() + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/study/test_study_associate_script.py b/tests/study/test_study_associate_script.py index 75faf07f..07260822 100644 --- a/tests/study/test_study_associate_script.py +++ b/tests/study/test_study_associate_script.py @@ -161,3 +161,30 @@ class TestSudySponsorsScript(BaseTest): app.config['PB_ENABLED'] = False output = user_studies() self.assertEqual(len(output),0) + + + @patch('crc.services.protocol_builder.requests.get') + def test_study_sponsors_script_ensure_delete(self, mock_get): + mock_get.return_value.ok = True + mock_get.return_value.text = self.protocol_builder_response('sponsors.json') + flask.g.user = UserModel(uid='dhf8r') + app.config['PB_ENABLED'] = True + + self.load_example_data() + self.create_reference_document() + study = session.query(StudyModel).first() + workflow_spec_model = self.load_test_spec("study_sponsors_associates_delete") + workflow_model = StudyService._create_workflow_model(study, workflow_spec_model) + WorkflowService.test_spec("study_sponsors_associates_delete") + processor = WorkflowProcessor(workflow_model) + processor.do_engine_steps() + # change user and make sure we can access the study + flask.g.user = UserModel(uid='lb3dp') + flask.g.token = 'my spiffy token' + app.config['PB_ENABLED'] = False + output = user_studies() + self.assertEqual(len(output),0) + flask.g.token = 'my spiffy token' + app.config['PB_ENABLED'] = False + output = user_studies() + self.assertEqual(len(output),0)