Example #1
0
    def test_default_app_strings_with_build_profiles(self):
        factory = AppFactory(build_version='2.40.0')
        factory.app.langs = ['en', 'es']
        factory.app.build_profiles = OrderedDict({
            'en': BuildProfile(langs=['en'], name='en-profile'),
            'es': BuildProfile(langs=['es'], name='es-profile'),
        })
        module, form = factory.new_basic_module('my_module', 'cases')
        module.name = {
            'en': 'Alive',
            'es': 'Viva',
        }
        form.name = {
            'en': 'Human',
            'es': 'Humana',
        }

        all_default_strings = self._generate_app_strings(factory.app, 'default')
        self.assertEqual(all_default_strings['modules.m0'], module.name['en'])
        self.assertEqual(all_default_strings['forms.m0f0'], form.name['en'])

        en_default_strings = self._generate_app_strings(factory.app, 'default', build_profile_id='en')
        self.assertEqual(en_default_strings['modules.m0'], module.name['en'])
        self.assertEqual(en_default_strings['forms.m0f0'], form.name['en'])

        es_default_strings = self._generate_app_strings(factory.app, 'default', build_profile_id='es')
        self.assertEqual(es_default_strings['modules.m0'], module.name['es'])
        self.assertEqual(es_default_strings['forms.m0f0'], form.name['es'])
Example #2
0
    def test_suite_media_with_app_profile(self, *args):
        # Test that suite includes only media relevant to the profile

        app = Application.new_app('domain', "my app")
        app.add_module(Module.new_module("Module 1", None))
        app.new_form(0, "Form 1", None)
        app.build_spec = BuildSpec.from_string('2.21.0/latest')
        app.build_profiles = OrderedDict({
            'en': BuildProfile(langs=['en'], name='en-profile'),
            'hin': BuildProfile(langs=['hin'], name='hin-profile'),
            'all': BuildProfile(langs=['en', 'hin'], name='all-profile'),
        })
        app.langs = ['en', 'hin']

        image_path = 'jr://file/commcare/module0_en.png'
        audio_path = 'jr://file/commcare/module0_en.mp3'
        app.get_module(0).set_icon('en', image_path)
        app.get_module(0).set_audio('en', audio_path)

        # Generate suites and default app strings for each profile
        suites = {}
        locale_ids = {}
        for build_profile_id in app.build_profiles.keys():
            suites[build_profile_id] = app.create_suite(build_profile_id=build_profile_id)
            default_app_strings = app.create_app_strings('default', build_profile_id)
            locale_ids[build_profile_id] = {line.split('=')[0] for line in default_app_strings.splitlines()}

        # Suite should have only the relevant images
        media_xml = self.makeXML("modules.m0", "modules.m0.icon", "modules.m0.audio")
        self.assertXmlPartialEqual(media_xml, suites['all'], "././menu[@id='m0']/display")

        no_media_xml = self.XML_without_media("modules.m0")
        self.assertXmlPartialEqual(media_xml, suites['en'], "././menu[@id='m0']/display")

        no_media_xml = self.XML_without_media("modules.m0")
        self.assertXmlPartialEqual(no_media_xml, suites['hin'], "././menu[@id='m0']/text")

        # Default app strings should have only the relevant locales
        self.assertIn('modules.m0', locale_ids['all'])
        self.assertIn('modules.m0.icon', locale_ids['all'])
        self.assertIn('modules.m0.audio', locale_ids['all'])

        self.assertIn('modules.m0', locale_ids['en'])
        self.assertIn('modules.m0.icon', locale_ids['en'])
        self.assertIn('modules.m0.audio', locale_ids['en'])

        self.assertIn('modules.m0', locale_ids['hin'])
        self.assertNotIn('modules.m0.icon', locale_ids['hin'])
        self.assertNotIn('modules.m0.audio', locale_ids['hin'])
Example #3
0
    def post(self, request, domain, app_id, *args, **kwargs):
        profiles = json.loads(request.body.decode('utf-8')).get('profiles')
        app = get_app(domain, app_id)
        build_profiles = {}
        if profiles:
            if app.is_remote_app() and len(profiles) > 1:
                # return bad request if they attempt to save more than one profile to a remote app
                return HttpResponse(status=400)
            for profile in profiles:
                id = profile.get('id')
                if not id:
                    id = uuid.uuid4().hex

                def practice_user_id():
                    if not app.enable_practice_users:
                        return ''
                    try:
                        practice_user_id = profile.get('practice_user_id')
                        if practice_user_id:
                            get_and_assert_practice_user_in_domain(
                                practice_user_id, domain)
                        return practice_user_id
                    except PracticeUserException:
                        return HttpResponse(status=400)

                build_profiles[id] = BuildProfile(
                    langs=profile['langs'],
                    name=profile['name'],
                    practice_mobile_worker_id=practice_user_id())
        app.build_profiles = build_profiles
        app.save()
        return HttpResponse()
Example #4
0
    def setUp(self):
        # app with no practice user
        self.domain = "test-domain"
        self.app1 = AppFactory(build_version='2.30.0', domain=self.domain).app

        self.app2 = AppFactory(build_version='2.30.0', domain=self.domain).app
        self.app2.practice_mobile_worker_id = "user1"

        self.app3 = AppFactory(build_version='2.30.0', domain=self.domain).app
        self.app3.build_profiles['build1'] = BuildProfile(
            langs=['en'], name='en-profile', practice_mobile_worker_id="user1")
        self.app3.build_profiles['build2'] = BuildProfile(
            langs=['en'], name='en-profile', practice_mobile_worker_id="user2")
        self.apps = [self.app1, self.app2, self.app3]
        for app in self.apps:
            app.save()
Example #5
0
    def test_form_media_with_app_profile(self, *args):
        # Test that media for languages not in the profile are removed from the media suite

        app = Application.wrap(self.get_json('app'))
        app.build_profiles = OrderedDict({
            'en': BuildProfile(langs=['en'], name='en-profile'),
            'hin': BuildProfile(langs=['hin'], name='hin-profile'),
            'all': BuildProfile(langs=['en', 'hin'], name='all-profile'),
        })
        app.langs = ['en', 'hin']

        image_path = 'jr://file/commcare/module0_en.png'
        audio_path = 'jr://file/commcare/module0_{}.mp3'
        app.get_module(0).set_icon('en', image_path)
        app.get_module(0).set_audio('en', audio_path.format('en'))
        app.get_module(0).set_audio('hin', audio_path.format('hin'))

        app.create_mapping(CommCareImage(_id='123'), image_path, save=False)
        app.create_mapping(CommCareAudio(_id='456'), audio_path.format('en'), save=False)
        app.create_mapping(CommCareAudio(_id='789'), audio_path.format('hin'), save=False)

        form_xml = self.get_xml('form_with_media_refs').decode('utf-8')
        form = app.get_module(0).new_form('form_with_media', 'en', attachment=form_xml)
        xform = form.wrapped_xform()
        for i, path in enumerate(reversed(sorted(xform.media_references(form="audio")))):
            app.create_mapping(CommCareAudio(_id='form_audio_{}'.format(i)), path, save=False)
        for i, path in enumerate(sorted(xform.media_references(form="image"))):
            app.create_mapping(CommCareImage(_id='form_image_{}'.format(i)), path, save=False)

        app.set_media_versions()
        app.remove_unused_mappings()

        # includes all media
        self._assertMediaSuiteResourcesEqual(self.get_xml('form_media_suite'), app.create_media_suite())

        # generate all suites at once to mimic create_build_files_for_all_app_profiles
        suites = {id: app.create_media_suite(build_profile_id=id) for id in app.build_profiles.keys()}

        # include all app media and only language-specific form media
        self._assertMediaSuiteResourcesEqual(self.get_xml('form_media_suite_en'), suites['en'])
        self._assertMediaSuiteResourcesEqual(self.get_xml('form_media_suite_hin'), suites['hin'])
        self._assertMediaSuiteResourcesEqual(self.get_xml('form_media_suite_all'), suites['all'])
 def setUp(self):
     self.build_profile_id = uuid.uuid4().hex
     self.app = Application(build_spec=BuildSpec(version='2.7.2'),
                            name="TÉST ÁPP",
                            domain="potter",
                            langs=['en'],
                            build_profiles={
                                self.build_profile_id:
                                BuildProfile(langs=['en'],
                                             name='en-profile')
                            })
Example #7
0
    def test_form_media_with_app_profile(self, *args):
        # Test that media for languages not in the profile are removed from the media suite

        app = Application.wrap(self.get_json('app'))
        app.build_profiles['profid'] = BuildProfile(langs=['en'],
                                                    name='en-profile')
        app.langs = ['en', 'hin']

        image_path = 'jr://file/commcare/module0_en.png'
        audio_path = 'jr://file/commcare/module0_{}.mp3'
        app.get_module(0).set_icon('en', image_path)
        # app.get_module(0).case_list_form.set_icon('en', image_path)
        app.get_module(0).set_audio('en', audio_path.format('en'))
        app.get_module(0).set_audio('hin', audio_path.format('hin'))

        app.create_mapping(CommCareImage(_id='123'), image_path, save=False)
        app.create_mapping(CommCareAudio(_id='456'),
                           audio_path.format('en'),
                           save=False)
        app.create_mapping(CommCareAudio(_id='789'),
                           audio_path.format('hin'),
                           save=False)

        form_xml = self.get_xml('form_with_media_refs').decode('utf-8')
        form = app.get_module(0).new_form('form_with_media',
                                          'en',
                                          attachment=form_xml)
        xform = form.wrapped_xform()
        for i, path in enumerate(xform.media_references(form="audio")):
            app.create_mapping(CommCareAudio(_id='form_audio_{}'.format(i)),
                               path,
                               save=False)
        for i, path in enumerate(xform.media_references(form="image")):
            app.create_mapping(CommCareImage(_id='form_image_{}'.format(i)),
                               path,
                               save=False)

        app.set_media_versions()
        app.remove_unused_mappings()

        # includes all media
        self._assertMediaSuiteResourcesEqual(self.get_xml('form_media_suite'),
                                             app.create_media_suite())

        # includes all app media and only form media for 'en'
        self._assertMediaSuiteResourcesEqual(
            self.get_xml('form_media_suite_en'),
            app.create_media_suite('profid'))
Example #8
0
 def post(self, request, domain, app_id, *args, **kwargs):
     profiles = json.loads(request.body).get('profiles')
     app = get_app(domain, app_id)
     build_profiles = {}
     if profiles:
         if app.is_remote_app() and len(profiles) > 1:
             # return bad request if they attempt to save more than one profile to a remote app
             return HttpResponse(status=400)
         for profile in profiles:
             id = profile.get('id')
             if not id:
                 id = uuid.uuid4().hex
             build_profiles[id] = BuildProfile(langs=profile['langs'], name=profile['name'])
     app.build_profiles = build_profiles
     app.save()
     return HttpResponse()
Example #9
0
    def test_profile_specific(self, mock):
        turn_on_demo_mode(self.user, self.domain)
        app = self.factory.app
        build_profile_id = "some_uuid"
        app.build_profiles[build_profile_id] = BuildProfile(
            langs=['en'], name='en-profile', practice_mobile_worker_id=self.user._id
        )

        self.assertXmlPartialEqual(
            self._get_restore_resource(self.user.demo_restore_id, build_profile_id),
            app.create_suite(build_profile_id=build_profile_id),
            "./user-restore"
        )
        app.create_build_files(build_profile_id=build_profile_id)
        app.save()
        self.assertTrue(app.lazy_fetch_attachment('files/{profile}/practice_user_restore.xml'.format(
            profile=build_profile_id
        )))
Example #10
0
    def setUpClass(cls):
        super(TestGlobalAppConfig, cls).setUpClass()
        cls.project = Domain(name=cls.domain)
        cls.project.save()

        cls.build_profile_id = 'english'
        app = Application(
            domain=cls.domain,
            name='foo',
            langs=["en"],
            version=1,
            modules=[Module()],
            build_profiles={
                cls.build_profile_id: BuildProfile(langs=['en'], name='English only'),
            }
        )  # app is v1

        app.save()  # app is now v2

        cls.v2_build = app.make_build()
        cls.v2_build.is_released = True
        cls.v2_build.save()  # v2 is starred

        app.save()  # app is now v3
        cls.v3_build = app.make_build()
        cls.v3_build.is_released = True
        cls.v3_build.save()  # v3 is starred

        app.save()  # app is v4

        # Add a build-profile-specific release at v2
        cls.latest_profile = LatestEnabledBuildProfiles(
            domain=cls.domain,
            app_id=app.get_id,
            build_profile_id=cls.build_profile_id,
            version=cls.v2_build.version,
            build_id=cls.v2_build.get_id,
            active=True,
        )
        cls.latest_profile.save()

        cls.app = app
Example #11
0
    def setUpClass(cls):
        super(TestGlobalAppConfig, cls).setUpClass()
        cls.project = Domain.get_or_create_with_name(cls.domain)

        cls.build_profile_id = 'english'
        factory = AppFactory(cls.domain, 'foo')
        m0, f0 = factory.new_basic_module("bar", "bar")
        f0.source = get_simple_form(xmlns=f0.unique_id)
        app = factory.app
        app.build_profiles = {
            cls.build_profile_id: BuildProfile(langs=['en'], name='English only'),
        }
        app.langs = ["en"]
        app.version = 1
        app.save()  # app is now v2

        cls.v2_build = app.make_build()
        cls.v2_build.is_released = True
        cls.v2_build.save()  # v2 is starred

        app.save()  # app is now v3
        cls.v3_build = app.make_build()
        cls.v3_build.is_released = True
        cls.v3_build.save()  # v3 is starred

        app.save()  # app is v4

        # Add a build-profile-specific release at v2
        cls.latest_profile = LatestEnabledBuildProfiles(
            domain=cls.domain,
            app_id=app.get_id,
            build_profile_id=cls.build_profile_id,
            version=cls.v2_build.version,
            build_id=cls.v2_build.get_id,
            active=True,
        )
        cls.latest_profile.save()

        cls.app = app
Example #12
0
    def test_modules_case_search_app_strings(self):
        factory = AppFactory(build_version='2.40.0')
        factory.app.langs = ['en', 'es']
        factory.app.build_profiles = OrderedDict({
            'en':
            BuildProfile(langs=['en'], name='en-profile'),
            'es':
            BuildProfile(langs=['es'], name='es-profile'),
        })
        module, form = factory.new_basic_module('my_module', 'cases')
        module.search_config = CaseSearch(
            search_label=CaseSearchLabel(
                label={
                    'en': 'Get them',
                    'es': 'Conseguirlos'
                },
                media_image={
                    'en': "jr://file/commcare/image/1.png",
                    'es': "jr://file/commcare/image/1_es.png"
                },
                media_audio={'en': "jr://file/commcare/image/2.mp3"}),
            search_again_label=CaseSearchAgainLabel(
                label={'en': 'Get them all'}),
            properties=[CaseSearchProperty(name="name", label={'en': 'Name'})])
        # wrap to have assign_references called
        app = Application.wrap(factory.app.to_json())

        with flag_disabled('USH_CASE_CLAIM_UPDATES'):
            # default language
            self.assertEqual(app.default_language, 'en')
            en_app_strings = self._generate_app_strings(app,
                                                        'default',
                                                        build_profile_id='en')
            self.assertEqual(en_app_strings['case_search.m0'],
                             'Search All Cases')
            self.assertEqual(en_app_strings['case_search.m0.again'],
                             'Search Again')
            self.assertFalse('case_search.m0.icon' in en_app_strings)
            self.assertFalse('case_search.m0.audio' in en_app_strings)

            # non-default language
            es_app_strings = self._generate_app_strings(app,
                                                        'es',
                                                        build_profile_id='es')
            self.assertEqual(es_app_strings['case_search.m0'],
                             'Search All Cases')
            self.assertEqual(es_app_strings['case_search.m0.again'],
                             'Search Again')

        with flag_enabled('USH_CASE_CLAIM_UPDATES'):
            # default language
            en_app_strings = self._generate_app_strings(app,
                                                        'default',
                                                        build_profile_id='en')
            self.assertEqual(en_app_strings['case_search.m0'], 'Get them')
            self.assertEqual(en_app_strings['case_search.m0.icon'],
                             'jr://file/commcare/image/1.png')
            self.assertEqual(en_app_strings['case_search.m0.audio'],
                             'jr://file/commcare/image/2.mp3')
            self.assertEqual(en_app_strings['case_search.m0.again'],
                             'Get them all')

            # non-default language
            es_app_strings = self._generate_app_strings(app,
                                                        'es',
                                                        build_profile_id='es')
            self.assertEqual(es_app_strings['case_search.m0'], 'Conseguirlos')
            self.assertEqual(es_app_strings['case_search.m0.icon'],
                             'jr://file/commcare/image/1_es.png')
            self.assertEqual(es_app_strings['case_search.m0.again'],
                             'Get them all')