コード例 #1
0
ファイル: test_session.py プロジェクト: soitun/commcare-hq
 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'])
コード例 #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()
コード例 #3
0
    def test_migration(self):
        doc = CustomDataFieldsDefinition(
            domain='botswana',
            field_type='UserFields',
            fields=[
                CustomDataField(
                    slug='color',
                    is_required=True,
                    label='Color',
                    choices=['red', 'orange', 'yellow'],
                ),
            ],
        )
        doc.save(sync_to_sql=False)
        call_command('populate_custom_data_fields')
        self.assertIsNone(
            Command.diff_couch_and_sql(
                doc.to_json(),
                SQLCustomDataFieldsDefinition.objects.get(
                    couch_id=doc.get_id)))

        # Additional call should apply any updates
        doc = CustomDataFieldsDefinition.get(doc.get_id)
        doc.field_type = 'LocationFields'
        doc.save(sync_to_sql=False)
        call_command('populate_custom_data_fields')
        self.assertIsNone(
            Command.diff_couch_and_sql(
                doc.to_json(),
                SQLCustomDataFieldsDefinition.objects.get(
                    couch_id=doc.get_id)))
コード例 #4
0
    def test_sync_to_sql(self):
        doc = CustomDataFieldsDefinition(
            domain='botswana',
            field_type='UserFields',
            fields=[
                CustomDataField(
                    slug='color',
                    is_required=True,
                    label='Color',
                    choices=['red', 'orange', 'yellow'],
                ),
                CustomDataField(
                    slug='size',
                    is_required=False,
                    label='Size',
                    regex='^[0-9]+$',
                    regex_msg='Εισαγάγετε',
                ),
            ],
        )
        doc.save()
        self.assertIsNone(
            Command.diff_couch_and_sql(
                doc.to_json(),
                SQLCustomDataFieldsDefinition.objects.get(
                    couch_id=doc.get_id)))

        doc.fields[0].label = 'Hue'
        doc.save()
        self.assertEquals(
            'Hue',
            SQLCustomDataFieldsDefinition.objects.get(
                couch_id=doc.get_id).get_fields()[0].label)
コード例 #5
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
コード例 #6
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)