Example #1
0
 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'])
Example #2
0
    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 test_update(self):
        # Initial update of linked domain
        update_custom_data_models(self.domain_link,
                                  limit_types=[UserFieldsView.field_type])
        model = CustomDataFieldsDefinition.objects.get(
            domain=self.linked_domain, field_type=UserFieldsView.field_type)
        fields = model.get_fields()
        self.assertEqual(fields[0].slug, "has_legs")
        self.assertEqual(fields[0].label, "Has legs")
        self.assertEqual(fields[0].choices, ["yes", "no"])
        self.assertEqual(fields[1].slug, "can_swim")
        self.assertEqual(fields[2].slug, "color")

        profiles_by_name = {p.name: p for p in model.get_profiles()}
        self.assertEqual(profiles_by_name[self.coral_profile.name].fields,
                         self.coral_profile.fields)
        self.assertEqual(profiles_by_name[self.fish_profile.name].fields,
                         self.fish_profile.fields)

        # Add, update, and remove a profile
        lamprey_profile = CustomDataFieldsProfile(
            name='Lamprey',
            fields={
                'has_legs': 'no',
                'can_swim': 'yes'
            },
            definition=self.definition,
        )
        lamprey_profile.save()
        self.fish_profile.fields = {
            'has_legs': 'no',
            'can_swim': 'no',
        }
        self.fish_profile.save()
        self.coral_profile.delete()

        update_custom_data_models(self.domain_link,
                                  limit_types=[UserFieldsView.field_type])
        model = CustomDataFieldsDefinition.objects.get(
            domain=self.linked_domain, field_type=UserFieldsView.field_type)
        profiles_by_name = {p.name: p for p in model.get_profiles()}
        self.assertTrue(bool(profiles_by_name[
            self.coral_profile.name]))  # updating doesn't delete profiles
        self.assertEqual(profiles_by_name[self.fish_profile.name].fields,
                         self.fish_profile.fields)
        self.assertEqual(profiles_by_name[lamprey_profile.name].fields,
                         lamprey_profile.fields)
Example #4
0
 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
Example #5
0
    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)