def test_pd_whitelist(self): """Any pd keys other than those whitelisted should not be returned.""" pid = 'Participant_0123456789ABCDEF' context_params = { 'participant_id': pid, 'program_label': 'demo-program', 'project_id': 'Project_12345678', 'code': 'trout viper', 'cohort_label': '2017_spring', 'project_cohort_id': 'ProjectCohort_12345678', 'survey_id': 'Survey_12345678', } portal_pd = [ { 'key': 'link', # whitelisted 'value': '', }, { 'key': 'not_whitelisted', 'value': 'secret', }, ] portal_pd = [ ParticipantData.create(**dict(pd, **context_params)) for pd in portal_pd ] ParticipantData.put_multi(portal_pd) results = ParticipantData.get_by_participant(pid, 'ProjectCohort_12345678') self.assertEqual(len(results), 1) self.assertNotEqual(results[0].value, 'secret')
def test_nobody_done(self): """If no one finished the survey, counts are zero.""" survey = Survey.create([], ordinal=1, program_label=self.program_label) survey.put() kwargs = { 'key': 'progress', 'program_label': self.program_label, 'project_id': 'Project_12345678', 'cohort_label': '2017_spring', 'project_cohort_id': 'ProjectCohort_12345678', 'code': 'trout viper', 'survey_id': survey.uid, 'survey_ordinal': survey.ordinal, } pd = [ ParticipantData.create(participant_id='Participant_unfinished1', value=1, **kwargs), ParticipantData.create(participant_id='Participant_unfinished2', value=1, **kwargs), ] ParticipantData.put_multi(pd) result = ParticipantData.participation(survey_id=pd[0].survey_id) expected = [ { 'value': '1', 'n': 2, 'survey_ordinal': 1 }, ] self.assertEqual(result, expected)
def test_put_multi(self): """If no indexes collide: succeeds, otherwise: raises.""" params1 = dict(self.context_params, value='1', survey_id='Survey_1') pd1 = ParticipantData.create(**params1) params2 = dict(self.context_params, value='1', survey_id='Survey_2') pd2 = ParticipantData.create(**params2) affected_rows = ParticipantData.put_multi([pd1, pd2]) self.assertEqual(affected_rows, 2) self.assertIsNotNone(ParticipantData.get_by_id(pd1.uid)) self.assertIsNotNone(ParticipantData.get_by_id(pd2.uid)) pd3 = ParticipantData.create(**params1) with self.assertRaises(IntegrityError): ParticipantData.put_multi([pd3])
def test_whitelist(self): """Certain pd values should readable, other's shouldn't.""" project_cohort, survey, participant = self.create_pd_context() keys = ( 'progress', 'link', 'condition', 'ep_assent', 'last_login', 'saw_baseline', 'saw_demographics', 'saw_validation', 'secret', # NOT on whitelist; should remain secret ) pds = [ ParticipantData.create( key=k, value='foo', participant_id=participant.uid, program_label=self.program_label, cohort_label=self.cohort_label, project_cohort_id=project_cohort.uid, code=project_cohort.code, survey_id=survey.uid, survey_ordinal=survey.ordinal, ) for k in keys ] ParticipantData.put_multi(pds) url = '/api/participants/{}/data?project_cohort_id={}'.format( participant.uid, project_cohort.uid) result = self.testapp.get(url) result_dict = json.loads(result.body) self.assertEqual(len(result_dict), len(keys) - 1) secret_pd = [pd for pd in result_dict if pd['key'] == 'secret'] self.assertEqual(len(secret_pd), 0)
def test_create_portal_pd(self, testing=False): """Set all the pd potentially required by the portal.""" # Using the same project and project cohort ids from the mock function. context_params = { 'participant_id': 'Participant_0123456789ABCDEF', 'program_label': 'demo-program', 'project_id': 'Project_12345678', 'cohort_label': '2017_spring', 'project_cohort_id': 'ProjectCohort_12345678', 'code': 'trout viper', 'testing': testing, } portal_pd = [ { 'key': 'link', 'value': '', 'survey_id': 'Survey_1', 'survey_ordinal': 1, }, { 'key': 'progress', 'value': '100', 'survey_id': 'Survey_1', 'survey_ordinal': 1, }, { 'key': 'link', 'value': '', 'survey_id': 'Survey_2', 'survey_ordinal': 2, }, { 'key': 'progress', 'value': '33', 'survey_id': 'Survey_2', 'survey_ordinal': 2, }, { 'key': 'consent', 'value': 'true', 'survey_id': None, }, { 'key': 'condition', 'value': 'treatment', 'survey_id': None, }, ] portal_pd = [ ParticipantData.create(**dict(pd, **context_params)) for pd in portal_pd ] ParticipantData.put_multi(portal_pd) # Save one more in a different project cohort, since participants # have an identity at the organization level. other_pc_params = dict( context_params, project_cohort_id='ProjectCohort_other', code='other octopus', key='saw_validation', # N.B. this is whitelisted value='bar', survey_id='Survey_other', ) ParticipantData.create(**other_pc_params).put() return context_params['participant_id']