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_get_portal_pd(self): """The same query made by the portal client, not scoped to pc.""" # A new participant should have no pd. new_pid = 'Participant_new' results = ParticipantData.get_by_participant(new_pid) self.assertEqual(len(results), 0) # An existing participant should have some pd. pid = self.test_create_portal_pd() results = ParticipantData.get_by_participant(pid) self.assertGreater(len(results), 0) # Some returned pd should be from other pcs. self.assertFalse( all(pd.project_cohort_id == 'ProjectCohort_12345678' for pd in results))
def test_portal_pd_includes_testing(self): """A testing user should still have a normal portal experience.""" # A testing participant should have some pd. pid = self.test_create_portal_pd(testing=True) results = ParticipantData.get_by_participant(pid, 'ProjectCohort_12345678') self.assertGreater(len(results), 0)
def test_update_cross_site_pd_with_descriptor(self): """Tests two ways: as a param, and as a compound survey id.""" pc, survey, participant = self.create_pd_context() survey_descriptor = 'cycle-1' compound_id = '{}:{}'.format(survey.uid, survey_descriptor) # As a param. url = ('/api/participants/{participant_id}/data/cross_site.gif?' 'survey_id={survey_id}&survey_descriptor={survey_descriptor}&' 'key=progress&value={value}').format( participant_id=participant.uid, survey_id=survey.uid, survey_descriptor=survey_descriptor, value='1', ) self.testapp.get(url) # As a compound id. url = ('/api/participants/{participant_id}/data/cross_site.gif?' 'survey_id={survey_id}&key=progress&value={value}').format( participant_id=participant.uid, survey_id=compound_id, value='100', ) self.testapp.get(url) pds = ParticipantData.get_by_participant(participant.uid, survey.project_cohort_id) self.assertEqual(len(pds), 1) self.assertEqual(pds[0].survey_id, compound_id) self.assertEqual(pds[0].value, '100')
def test_update_cross_site_pd(self): pc, survey, participant = self.create_pd_context() user = User.create( email='*****@*****.**', owned_organizations=[pc.organization_id], ) user.put() def write_value(value): url = ('/api/participants/{participant_id}/data/cross_site.gif?' 'survey_id={survey_id}&key=progress&value={value}').format( participant_id=participant.uid, survey_id=survey.uid, value=value, ) self.testapp.get(url) # Query for participation between each write to make sure there are # no errors while clearing and re-populating the participation cache. self.query_pc_participation(user, pc) write_value('1') self.query_pc_participation(user, pc) write_value('100') pd = ParticipantData.get_by_participant(participant.uid, survey.project_cohort_id)[0] self.assertEqual(pd.value, '100')
def test_write_cross_site_pd(self, testing=False): pc, survey, participant = self.create_pd_context() url = ('/api/participants/{participant_id}/data/cross_site.gif?' 'survey_id={survey_id}&key=progress&value=1{testing_param}' ).format( participant_id=participant.uid, survey_id=survey.uid, testing_param='&testing=true' if testing else '', ) self.testapp.get(url) # The response is not designed to be useful (it's a gif), so check the # db to ensure the pd was written. result = ParticipantData.get_by_participant(participant.uid, survey.project_cohort_id) pd = result[0] self.assertEqual(pd.participant_id, participant.uid) self.assertEqual(pd.project_cohort_id, pc.uid) self.assertEqual(pd.code, pc.code) self.assertEqual(pd.survey_id, survey.uid) self.assertEqual(pd.key, 'progress') self.assertEqual(pd.value, '1') self.assertEqual(pd.testing, testing)
def test_get_by_project_cohort(self): """Retrieve pd for a participant, for a given pc.""" # A new participant should have no pd. new_pid = 'Participant_new' results = ParticipantData.get_by_participant(new_pid, 'ProjectCohort_12345678') self.assertEqual(len(results), 0) # An existing participant should have some pd. pid = self.test_create_portal_pd() results = ParticipantData.get_by_participant(pid, 'ProjectCohort_12345678') self.assertGreater(len(results), 0) # All returned pd should match the project cohort id. self.assertTrue( all(pd.project_cohort_id == 'ProjectCohort_12345678' for pd in results))
def test_downgrade_progress_fails_cross_site(self): pc, survey, participant = self.create_pd_context() def write_value(value): url = ('/api/participants/{participant_id}/data/cross_site.gif?' 'survey_id={survey_id}&key=progress&value={value}').format( participant_id=participant.uid, survey_id=survey.uid, value=value, ) self.testapp.get(url) write_value('100') write_value('1') # should silently fail # Recorded pd is still 100. pd = ParticipantData.get_by_participant(participant.uid, survey.project_cohort_id)[0] self.assertEqual(pd.value, '100')
def test_downgrade_progress_fails_local(self): pc, survey, participant = self.create_pd_context() def write_value(value, status): return self.testapp.post_json( '/api/participants/{participant_id}/data/{key}'.format( participant_id=participant.uid, key='progress', ), { 'value': value, 'survey_id': survey.uid }, status=status, ) write_value('100', 200) write_value('1', 400) # should explicitly fail # Recorded pd is still 100. pd = ParticipantData.get_by_participant(participant.uid, survey.project_cohort_id)[0] self.assertEqual(pd.value, '100')