def test_user_data_profile(self): definition = CustomDataFieldsDefinition( domain='cloudcare-tests', field_type=UserFieldsView.field_type) definition.save() definition.set_fields([ Field(slug='word', label='A Word'), ]) definition.save() self.addCleanup(definition.delete) profile = CustomDataFieldsProfile(name='prof', fields={'word': 'supernova'}, definition=definition) profile.save() user = CommCareUser.create( 'cloudcare-tests', '*****@*****.**', 'do you want to know a secret', None, None, uuid=uuid.uuid4().hex, metadata={PROFILE_SLUG: profile.id}, ) self.addCleanup(user.delete, None, None) user_data = get_user_contributions_to_touchforms_session( 'cloudcare-tests', user)['user_data'] self.assertEqual(profile.id, user_data[PROFILE_SLUG]) self.assertEqual('supernova', user_data['word'])
def test_sync_custom_user_data(self): definition = CustomDataFieldsDefinition( domain=TEST_DOMAIN, field_type=UserFieldsView.field_type) definition.save() definition.set_fields([ Field(slug='from_profile', label='From Profile'), ]) definition.save() profile = CustomDataFieldsProfile( name='callcenter_profile', fields={'from_profile': 'yes'}, definition=definition, ) profile.save() self.user.update_metadata({ '': 'blank_key', 'blank_val': '', 'ok': 'good', 'name with spaces': 'ok', '8starts_with_a_number': '0', 'xml_starts_with_xml': '0', '._starts_with_punctuation': '0', PROFILE_SLUG: profile.id, }) sync_call_center_user_case(self.user) case = self._get_user_case() self.assertIsNotNone(case) self.assertEqual(case.get_case_property('blank_val'), '') self.assertEqual(case.get_case_property('ok'), 'good') self.assertEqual(case.get_case_property(PROFILE_SLUG), str(profile.id)) self.assertEqual(case.get_case_property('from_profile'), 'yes') self.user.pop_metadata(PROFILE_SLUG) definition.delete()
def _setup_profile(self): definition = CustomDataFieldsDefinition( domain=self.domain, field_type=UserFieldsView.field_type) definition.save() definition.set_fields([ Field( slug='conflicting_field', label='Conflicting Field', choices=['yes', 'no'], ), ]) definition.save() profile = CustomDataFieldsProfile( name='character', fields={'conflicting_field': 'yes'}, definition=definition, ) profile.save() return profile.id
def test_metadata_with_profile(self): definition = CustomDataFieldsDefinition( domain='my-domain', field_type=UserFieldsView.field_type) definition.save() definition.set_fields([Field(slug='start')]) definition.save() profile = CustomDataFieldsProfile( name='low', fields={'start': 'sometimes'}, definition=definition, ) profile.save() conflict_message = "metadata properties conflict with profile: start" # Custom user data profiles get their data added to metadata automatically for mobile users self.user.update_metadata({PROFILE_SLUG: profile.id}) self.assertEqual( self.user.metadata, { 'commcare_project': 'my-domain', PROFILE_SLUG: profile.id, 'start': 'sometimes', }) # Remove profile should remove it and related fields self.user.pop_metadata(PROFILE_SLUG) self.assertEqual(self.user.metadata, { 'commcare_project': 'my-domain', }) # Can't add profile that conflicts with existing data self.user.update_metadata({ 'start': 'never', 'end': 'yesterday', }) with self.assertRaisesMessage(ValueError, conflict_message): self.user.update_metadata({ PROFILE_SLUG: profile.id, }) # Can't add data that conflicts with existing profile self.user.pop_metadata('start') self.user.update_metadata({PROFILE_SLUG: profile.id}) with self.assertRaisesMessage(ValueError, conflict_message): self.user.update_metadata({'start': 'never'}) # Can't add both a profile and conflicting data self.user.pop_metadata(PROFILE_SLUG) with self.assertRaisesMessage(ValueError, conflict_message): self.user.update_metadata({ PROFILE_SLUG: profile.id, 'start': 'never', }) # Custom user data profiles don't get populated for web users web_user = WebUser.create(None, "imogen", "*****", None, None) self.assertEqual(web_user.metadata, { 'commcare_project': None, }) web_user.update_metadata({PROFILE_SLUG: profile.id}) self.assertEqual(web_user.metadata, { 'commcare_project': None, PROFILE_SLUG: profile.id, }) definition.delete() web_user.delete(self.domain, deleted_by=None)