Exemple #1
0
    def test_product_fixture(self):
        user = self.user
        fixture_xml = self.generate_product_fixture_xml(user)
        index_schema, fixture = call_fixture_generator(
            product_fixture_generator, user)

        self.assertXmlEqual(fixture_xml, fixture)

        schema_xml = """
            <schema id="commtrack:products">
                <indices>
                    <index>@id</index>
                    <index>category</index>
                    <index>code</index>
                    <index>program_id</index>
                </indices>
            </schema>
        """
        self.assertXmlEqual(schema_xml, ElementTree.tostring(index_schema))

        # test restore with different user
        user2 = create_restore_user(self.domain, username='******')
        self.addCleanup(user2._couch_user.delete)
        fixture_xml = self.generate_product_fixture_xml(user2)
        index_schema, fixture = call_fixture_generator(
            product_fixture_generator, user2)

        self.assertXmlEqual(fixture_xml, fixture)
Exemple #2
0
    def test_program_fixture_cache(self):
        user = self.user
        Program(domain=user.domain, name="test1", code="t1").save()

        program_list = list(Program.by_domain(user.domain))
        program_xml = self.generate_program_xml(program_list, user)

        fixture = call_fixture_generator(program_fixture_generator, user)

        self.assertXmlEqual(program_xml, fixture[0])

        program = program_list[0]
        program.name = 'new_name'
        super(Program, program).save()  # save but skip clearing the cache

        fixture_cached = call_fixture_generator(program_fixture_generator,
                                                user)
        self.assertXmlEqual(program_xml, fixture_cached[0])

        program.save()
        program_xml_new = self.generate_program_xml(program_list, user)

        fixture_regen = call_fixture_generator(program_fixture_generator, user)
        self.assertXmlEqual(program_xml_new, fixture_regen[0])
        self.assertXMLNotEqual(program_xml_new, program_xml)
Exemple #3
0
    def test_should_sync_when_changed(self, *args):
        self.user._couch_user.add_to_assigned_locations(
            self.locations['Boston'])
        last_sync_time = datetime.utcnow()
        sync_log = SyncLog(date=last_sync_time)
        locations_queryset = SQLLocation.objects.filter(
            pk=self.locations['Boston'].pk)

        restore_state = MockRestoreState(self.user, RestoreParams())

        self.assertFalse(
            should_sync_locations(sync_log, locations_queryset, restore_state))
        self.assertEquals(
            len(
                call_fixture_generator(related_locations_fixture_generator,
                                       self.user,
                                       last_sync=sync_log)), 0)

        LocationRelation.objects.create(location_a=self.locations["Revere"],
                                        location_b=self.locations["Boston"])
        self.assertTrue(
            should_sync_locations(SyncLog(date=last_sync_time),
                                  locations_queryset, restore_state))

        # length 2 for index definition + data
        self.assertEquals(
            len(
                call_fixture_generator(related_locations_fixture_generator,
                                       self.user,
                                       last_sync=sync_log)), 2)
Exemple #4
0
    def test_program_fixture(self):
        user = self.user
        Program(
            domain=user.domain,
            name="test1",
            code="t1"
        ).save()

        program_list = Program.by_domain(user.domain)
        program_xml = self.generate_program_xml(program_list, user)

        fixture = call_fixture_generator(program_fixture_generator, user)

        self.assertXmlEqual(
            program_xml,
            ElementTree.tostring(fixture[0])
        )

        # test restore with different user
        user2 = create_restore_user(self.domain, username='******')
        self.addCleanup(user2._couch_user.delete)
        program_xml = self.generate_program_xml(program_list, user2)
        fixture = call_fixture_generator(program_fixture_generator, user2)

        self.assertXmlEqual(
            program_xml,
            ElementTree.tostring(fixture[0])
        )
Exemple #5
0
    def test_selective_product_sync(self):
        user = self.user

        expected_xml = self.generate_product_fixture_xml(user)

        product_list = Product.by_domain(user.domain)
        self._initialize_product_names(len(product_list))

        fixture_original = call_fixture_generator(product_fixture_generator, user)[1]
        deprecated_generate_restore_payload(self.domain_obj, user)
        self.assertXmlEqual(
            expected_xml,
            ElementTree.tostring(fixture_original)
        )

        first_sync = sorted(SyncLog.view(
            "phone/sync_logs_by_user",
            include_docs=True,
            reduce=False
        ).all(), key=lambda x: x.date)[-1]

        # make sure the time stamp on this first sync is
        # not on the same second that the products were created
        first_sync.date += datetime.timedelta(seconds=1)

        # second sync is before any changes are made, so there should
        # be no products synced
        fixture_pre_change = call_fixture_generator(product_fixture_generator, user, last_sync=first_sync)
        deprecated_generate_restore_payload(self.domain_obj, user)
        self.assertEqual(
            [],
            fixture_pre_change,
            "Fixture was not empty on second sync"
        )

        second_sync = sorted(SyncLog.view(
            "phone/sync_logs_by_user",
            include_docs=True,
            reduce=False
        ).all(), key=lambda x: x.date)[-1]

        self.assertTrue(first_sync._id != second_sync._id)

        # save should make the product more recently updated than the
        # last sync
        for product in product_list:
            product.save()

        # now that we've updated a product, we should get
        # product data in sync again
        fixture_post_change = call_fixture_generator(product_fixture_generator, user, last_sync=second_sync)[1]

        # regenerate the fixture xml to make sure it is still legit
        self.assertXmlEqual(
            expected_xml,
            ElementTree.tostring(fixture_post_change)
        )
Exemple #6
0
    def test_selective_program_sync(self):
        user = self.user
        Program(
            domain=user.domain,
            name="test1",
            code="t1"
        ).save()

        program_list = Program.by_domain(user.domain)
        program_xml = self.generate_program_xml(program_list, user)

        fixture_original = call_fixture_generator(program_fixture_generator, user)

        deprecated_generate_restore_payload(self.domain_obj, user)
        self.assertXmlEqual(
            program_xml,
            ElementTree.tostring(fixture_original[0])
        )

        first_sync = self._get_latest_synclog()
        # make sure the time stamp on this first sync is
        # not on the same second that the programs were created
        first_sync.date += datetime.timedelta(seconds=1)

        # second sync is before any changes are made, so there should
        # be no programs synced
        fixture_pre_change = call_fixture_generator(program_fixture_generator, user, last_sync=first_sync)
        deprecated_generate_restore_payload(self.domain_obj, user)
        self.assertEqual(
            [],
            fixture_pre_change,
            "Fixture was not empty on second sync"
        )

        second_sync = self._get_latest_synclog()

        self.assertTrue(first_sync._id != second_sync._id)

        # save should make the program more recently updated than the
        # last sync
        for program in program_list:
            program.save()

        # now that we've updated a program, we should get
        # program data in sync again
        fixture_post_change = call_fixture_generator(program_fixture_generator, user, last_sync=second_sync)

        # regenerate the fixture xml to make sure it is still legit
        self.assertXmlEqual(
            program_xml,
            ElementTree.tostring(fixture_post_change[0])
        )
Exemple #7
0
    def test_selective_program_sync(self):
        user = self.user
        Program(
            domain=user.domain,
            name="test1",
            code="t1"
        ).save()

        program_list = Program.by_domain(user.domain)
        program_xml = self.generate_program_xml(program_list, user)

        fixture_original = call_fixture_generator(program_fixture_generator, user)

        deprecated_generate_restore_payload(self.domain_obj, user)
        self.assertXmlEqual(
            program_xml,
            ElementTree.tostring(fixture_original[0])
        )

        first_sync = self._get_latest_synclog()
        # make sure the time stamp on this first sync is
        # not on the same second that the programs were created
        first_sync.date += datetime.timedelta(seconds=1)

        # second sync is before any changes are made, so there should
        # be no programs synced
        fixture_pre_change = call_fixture_generator(program_fixture_generator, user, last_sync=first_sync)
        deprecated_generate_restore_payload(self.domain_obj, user)
        self.assertEqual(
            [],
            fixture_pre_change,
            "Fixture was not empty on second sync"
        )

        second_sync = self._get_latest_synclog()

        self.assertTrue(first_sync._id != second_sync._id)

        # save should make the program more recently updated than the
        # last sync
        for program in program_list:
            program.save()

        # now that we've updated a program, we should get
        # program data in sync again
        fixture_post_change = call_fixture_generator(program_fixture_generator, user, last_sync=second_sync)

        # regenerate the fixture xml to make sure it is still legit
        self.assertXmlEqual(
            program_xml,
            ElementTree.tostring(fixture_post_change[0])
        )
Exemple #8
0
    def test_selective_product_sync(self):
        user = self.user

        expected_xml = self.generate_product_fixture_xml(user)

        product_list = Product.by_domain(user.domain)
        self._initialize_product_names(len(product_list))

        fixture_original = call_fixture_generator(product_fixture_generator, user)[1]
        deprecated_generate_restore_payload(self.domain_obj, user)
        self.assertXmlEqual(
            expected_xml,
            ElementTree.tostring(fixture_original)
        )

        first_sync = self._get_latest_synclog()

        # make sure the time stamp on this first sync is
        # not on the same second that the products were created
        first_sync.date += datetime.timedelta(seconds=1)

        # second sync is before any changes are made, so there should
        # be no products synced
        fixture_pre_change = call_fixture_generator(product_fixture_generator, user, last_sync=first_sync)
        deprecated_generate_restore_payload(self.domain_obj, user)
        self.assertEqual(
            [],
            fixture_pre_change,
            "Fixture was not empty on second sync"
        )

        second_sync = self._get_latest_synclog()

        self.assertTrue(first_sync._id != second_sync._id)

        # save should make the product more recently updated than the
        # last sync
        for product in product_list:
            product.save()

        # now that we've updated a product, we should get
        # product data in sync again
        fixture_post_change = call_fixture_generator(product_fixture_generator, user, last_sync=second_sync)[1]

        # regenerate the fixture xml to make sure it is still legit
        self.assertXmlEqual(
            expected_xml,
            ElementTree.tostring(fixture_post_change)
        )
Exemple #9
0
 def test_no_user_locations_returns_empty(self):
     empty_fixture = EMPTY_LOCATION_FIXTURE_TEMPLATE.format(
         self.user.user_id)
     fixture = ElementTree.tostring(call_fixture_generator(
         location_fixture_generator, self.user)[0],
                                    encoding='utf-8')
     self.assertXmlEqual(empty_fixture, fixture)
Exemple #10
0
    def test_callcenter_fixture_commcare_user(self):
        user = CommCareUser(_id='123', username="******")
        indicator_set = MockIndicatorSet(name='test',
                                         indicators=OrderedDict([
                                             ('user_case1', {
                                                 'i1': 1,
                                                 'i2': 2
                                             }),
                                             ('user_case2', {
                                                 'i1': 0,
                                                 'i2': 3
                                             })
                                         ]))
        restore_user = type(
            'OTARestoreCommCareUserFake', (OTARestoreCommCareUser, ), {
                'project': Domain(name='test', default_timezone='UTC'),
                'get_call_center_indicators':
                lambda self, config: indicator_set,
            })('test', user)

        fixture, = call_fixture_generator(mock_indicators_fixture_generator,
                                          restore_user)
        check_xml_line_by_line(
            self, ElementTree.tostring(fixture),
            ElementTree.tostring(gen_fixture(restore_user, indicator_set)))
Exemple #11
0
    def test_fixture_provider(self, _get_permission_checker):
        _get_permission_checker().can_view_registry_data.return_value = True
        list_fixture, domains_fixture = call_fixture_generator(
            registry_fixture_generator,
            self.restore_user,
            project=self.domain_obj)

        expected_list_fixture = f"""
        <fixture id="registry:list">
           <registry_list>
               <registry slug="{self.registry.slug}" owner="{self.domain}" active="true">
                   <name>{self.registry.name}</name>
                   <description></description>
               </registry>
           </registry_list>
        </fixture>"""

        expected_domains_fixture = f"""
        <fixture id="registry:domains:{self.registry.slug}">
           <domains>
               <domain name="domain1">domain1</domain>
               <domain name="domain2">domain2</domain>
               <domain name="{self.domain}">{self.domain_obj.hr_name}</domain>
           </domains>
        </fixture>
        """

        self.assertXmlEqual(
            expected_list_fixture,
            ElementTree.tostring(list_fixture, encoding='utf-8'))
        self.assertXmlEqual(
            expected_domains_fixture,
            ElementTree.tostring(domains_fixture, encoding='utf-8'))
    def test_report_fixtures_provider_with_cloudcare(self):
        """
        ReportFixturesProvider should iterate only allowed apps if sync is from cloudcare
        """
        from corehq.apps.userreports.reports.data_source import ConfigurableReportDataSource
        role = UserRole(
            domain=self.domain,
            permissions=Permissions(
                view_web_apps=False,
                view_web_apps_list=[self.app1._id]
            ),
            name='WebApp Restricted'
        )
        role.save()
        self.user._couch_user.set_role(self.domain, role.get_qualified_id())

        with patch.object(ConfigurableReportDataSource, 'get_data') as get_data_mock:
            get_data_mock.return_value = self.rows
            with mock_datasource_config():
                fixtures = call_fixture_generator(
                    report_fixture_generator,
                    self.user,
                    device_id="WebAppsLogin|[email protected]"
                )
        reports = fixtures[0].findall('.//report')
        self.assertEqual(len(reports), 1)
        self.assertEqual(reports[0].attrib.get('id'), '123456')
    def test_ownership(self):
        self.assertItemsEqual([self.data_item.get_id],
                              FixtureDataItem.by_user(self.user, wrap=False))
        self.assertItemsEqual([self.user.get_id],
                              self.data_item.get_all_users(wrap=False))

        fixture, = call_fixture_generator(fixturegenerators.item_lists,
                                          self.user.to_ota_restore_user())

        check_xml_line_by_line(
            self, """
        <fixture id="item-list:district" user_id="%s">
            <district_list>
                <district>
                    <state_name>Delhi_state</state_name>
                    <district_name lang="hin">Delhi_in_HIN</district_name>
                    <district_name lang="eng">Delhi_in_ENG</district_name>
                    <district_id>Delhi_id</district_id>
                </district>
            </district_list>
        </fixture>
        """ % self.user.user_id, ElementTree.tostring(fixture))

        self.data_item.remove_user(self.user)
        self.assertItemsEqual([], self.data_item.get_all_users())

        self.fixture_ownership = self.data_item.add_user(self.user)
        self.assertItemsEqual([self.user.get_id],
                              self.data_item.get_all_users(wrap=False))
    def test_should_sync_when_changed(self, *args):
        self.user._couch_user.add_to_assigned_locations(self.locations['Boston'])
        last_sync_time = datetime.utcnow()
        sync_log = SyncLog(date=last_sync_time)
        locations_queryset = SQLLocation.objects.filter(pk=self.locations['Boston'].pk)

        self.assertFalse(should_sync_locations(sync_log, locations_queryset, self.user))
        self.assertEquals(
            len(call_fixture_generator(related_locations_fixture_generator, self.user, last_sync=sync_log)), 0)

        LocationRelation.objects.create(location_a=self.locations["Revere"], location_b=self.locations["Boston"])
        self.assertTrue(should_sync_locations(SyncLog(date=last_sync_time), locations_queryset, self.user))

        # length 2 for index definition + data
        self.assertEquals(
            len(call_fixture_generator(related_locations_fixture_generator, self.user, last_sync=sync_log)), 2)
Exemple #15
0
 def test_fixture_defaults(self):
     user = CommCareUser(_id=uuid.uuid4().hex, domain='test-calendar-defaults')
     fixture = call_fixture_generator(calendar_fixture_generator, user)[0]
     self.assertEqual(user._id, fixture.attrib['user_id'])
     today = datetime.today()
     self._check_first_date(fixture, today - timedelta(days=DEFAULT_DAYS_BEFORE))
     self._check_last_date(fixture, today + timedelta(days=DEFAULT_DAYS_AFTER))
Exemple #16
0
    def test_report_fixtures_provider_with_cloudcare(self):
        """
        ReportFixturesProvider should iterate only allowed apps if sync is from cloudcare
        """
        from corehq.apps.userreports.reports.data_source import ConfigurableReportDataSource
        role = UserRole(
            domain=self.domain,
            permissions=Permissions(
                view_web_apps=False,
                view_web_apps_list=[self.app1._id]
            ),
            name='WebApp Restricted'
        )
        role.save()
        self.user._couch_user.set_role(self.domain, role.get_qualified_id())

        with patch.object(ConfigurableReportDataSource, 'get_data') as get_data_mock:
            get_data_mock.return_value = self.rows
            with mock_sql_backend():
                with mock_datasource_config():
                    fixtures = call_fixture_generator(
                        report_fixture_generator,
                        self.user,
                        device_id="WebAppsLogin|[email protected]"
                    )
        reports = fixtures[0].findall('.//report')
        self.assertEqual(len(reports), 1)
        self.assertEqual(reports[0].attrib.get('id'), '123456')
    def test_fixture_is_indexed(self):
        self.data_type.fields[
            2].is_indexed = True  # Set "district_id" as indexed
        self.data_type.save()

        fixtures = call_fixture_generator(fixturegenerators.item_lists,
                                          self.user.to_ota_restore_user())
        self.assertEqual(len(fixtures), 2)
        check_xml_line_by_line(
            self, """
            <fixtures>
                <schema id="item-list:district">
                    <indices>
                        <index>district_id</index>
                    </indices>
                </schema>
                <fixture id="item-list:district" indexed="true" user_id="{}">
                    <district_list>
                        <district>
                            <state_name>Delhi_state</state_name>
                            <district_name lang="hin">Delhi_in_HIN</district_name>
                            <district_name lang="eng">Delhi_in_ENG</district_name>
                            <district_id>Delhi_id</district_id>
                        </district>
                    </district_list>
                </fixture>
            </fixtures>
            """.format(self.user.user_id), """
            <fixtures>
                {}
                {}
            </fixtures>
            """.format(
                *[ElementTree.tostring(fixture) for fixture in fixtures]))
    def test_callcenter_fixture_web_user(self):
        user = WebUser(_id='123')
        restore_user = type('OTARestoreWebUserFake', (OTARestoreWebUser,), {
            'project': Domain(name='test', default_timezone='UTC'),
        })('test', user)

        fixtures = call_fixture_generator(mock_indicators_fixture_generator, restore_user)
        self.assertEqual(fixtures, [])
 def _assert_fixture_matches_file(self, xml_name, desired_locations, flat=False):
     if flat:
         generator = flat_location_fixture_generator
     else:
         generator = location_fixture_generator
     fixture = ElementTree.tostring(call_fixture_generator(generator, self.user)[-1], encoding='utf-8')
     desired_fixture = self._assemble_expected_fixture(xml_name, desired_locations)
     self.assertXmlEqual(desired_fixture, fixture)
    def test_callcenter_fixture_web_user(self):
        user = WebUser(_id='123')
        restore_user = type('OTARestoreWebUserFake' if six.PY3 else b'OTARestoreWebUserFake', (OTARestoreWebUser,), {
            'project': Domain(name='test', default_timezone='UTC'),
        })('test', user)

        fixtures = call_fixture_generator(mock_indicators_fixture_generator, restore_user)
        self.assertEqual(fixtures, [])
Exemple #21
0
    def test_default_mobile_ucr_sync_interval(self):
        """
        When sync interval is set, ReportFixturesProvider should provide reports only if
        the interval has passed since the last sync or a new build is being requested.
        """
        from corehq.apps.userreports.reports.data_source import ConfigurableReportDataSource
        with patch.object(ConfigurableReportDataSource,
                          'get_data') as get_data_mock:
            get_data_mock.return_value = self.rows
            with mock_datasource_config():
                self.domain_obj.default_mobile_ucr_sync_interval = 4  # hours
                two_hours_ago = datetime.utcnow() - timedelta(hours=2)
                recent_sync = SimplifiedSyncLog(
                    domain=self.domain_obj.name,
                    date=two_hours_ago,
                    user_id='456',
                    build_id=self.app1.get_id,
                )
                recent_sync.save()
                fixtures = call_fixture_generator(report_fixture_generator,
                                                  self.user,
                                                  app=self.app1,
                                                  last_sync=recent_sync,
                                                  project=self.domain_obj)
                reports = self._get_fixture(fixtures,
                                            ReportFixturesProviderV1.id)
                self.assertIsNone(reports)

                recent_sync_new_build = SimplifiedSyncLog(
                    domain=self.domain_obj.name,
                    date=two_hours_ago,
                    user_id='456',
                    build_id='123',
                )
                recent_sync_new_build.save()
                fixtures = call_fixture_generator(
                    report_fixture_generator,
                    self.user,
                    app=self.app1,
                    last_sync=recent_sync_new_build,
                    project=self.domain_obj)
                reports = self._get_fixture(
                    fixtures, ReportFixturesProviderV1.id).findall('.//report')
                self.assertEqual(len(reports), 1)
                self.assertEqual(reports[0].attrib.get('id'), '123456')
                self.domain_obj.default_mobile_ucr_sync_interval = None
Exemple #22
0
 def test_force_empty_when_user_has_no_locations(self, *args):
     sync_log = SyncLog(date=datetime.utcnow())
     # no relations have been touched since this synclog, but it still pushes down the empty list
     self.assertEquals(
         len(
             call_fixture_generator(related_locations_fixture_generator,
                                    self.user,
                                    last_sync=sync_log)), 2)
 def test_location_fixture_generator_domain_no_locations(self):
     """
     Ensures that a domain that doesn't use locations doesn't send an empty
     location fixture
     """
     assert self.user.location is None
     fixture = call_fixture_generator(location_fixture_generator,
                                      self.user.to_ota_restore_user())
     self.assertEqual(len(fixture), 0)
 def test_location_fixture_generator_no_user_location(self):
     """
     Ensures that a user without a location will still receive an empty fixture
     """
     assert self.user.location is None
     fixture = call_fixture_generator(location_fixture_generator,
                                      self.user.to_ota_restore_user())
     self.assertEqual(len(fixture), 1)
     self.assertEqual(len(fixture[0].findall('.//state')), 0)
Exemple #25
0
    def setUpClass(cls):
        super(ReportFiltersSuiteTest, cls).setUpClass()
        delete_all_users()
        cls.report_id = '7b97e8b53d00d43ca126b10093215a9d'
        cls.report_config_uuid = 'a98c812873986df34fd1b4ceb45e6164ae9cc664'
        cls.domain = 'report-filter-test-domain'
        cls.user = create_restore_user(
            domain=cls.domain,
            username='******',
        )
        update_toggle_cache(MOBILE_UCR.slug, cls.domain, True, NAMESPACE_DOMAIN)

        report_configuration = cls.make_report_config(cls.domain, cls.report_id)
        cls.report_configs_by_id = {
            cls.report_id: report_configuration
        }
        cls.app = Application.new_app(cls.domain, "Report Filter Test App")
        module = cls.app.add_module(ReportModule.new_module("Report Module", 'en'))
        module.report_configs.append(
            ReportAppConfig(
                report_id=cls.report_id,
                header={},
                description="",
                complete_graph_configs={
                    '7451243209119342931': GraphConfiguration(
                        graph_type="bar",
                        series=[GraphSeries(
                            config={},
                            locale_specific_config={},
                            data_path="",
                            x_function="",
                            y_function="",
                        )],
                    )
                },
                filters={
                    'computed_owner_name_40cc88a0_1': MobileSelectFilter(),
                    'fav_fruit_abc123_1': MobileSelectFilter()
                },
                uuid=cls.report_config_uuid,
            )
        )
        with mock_report_configurations(cls.report_configs_by_id):
            cls.suite = cls.app.create_suite()
        cls.data = [
            {'color_94ec39e6': 'red', 'count': 2, 'computed_owner_name_40cc88a0': 'cory', 'fav_fruit_abc123': 'c'},
            {'color_94ec39e6': 'black', 'count': 1, 'computed_owner_name_40cc88a0': 'ctsims', 'fav_fruit_abc123': 'b'},
            {'color_94ec39e6': 'red', 'count': 3, 'computed_owner_name_40cc88a0': 'daniel', 'fav_fruit_abc123': 'b'},
        ]
        with mock_report_data(cls.data):
            with mock_report_configuration_get(cls.report_configs_by_id):
                with mock.patch('corehq.apps.app_manager.fixtures.mobile_ucr.get_apps_in_domain',
                                lambda domain, include_remote: [cls.app]):
                    with mock_sql_backend():
                        with mock_datasource_config():
                            fixture, = call_fixture_generator(report_fixture_generator, cls.user)
        cls.fixture = ElementTree.tostring(fixture)
 def test_location_fixture_generator_domain_no_locations(self):
     """
     Ensures that a domain that doesn't use locations doesn't send an empty
     location fixture
     """
     self.user.unset_location()
     self.addCleanup(self.user.set_location, self.loc)
     fixture = call_fixture_generator(location_fixture_generator, self.user.to_ota_restore_user())
     self.assertEqual(len(fixture), 0)
 def test_location_fixture_generator_domain_no_locations(self):
     """
     Ensures that a domain that doesn't use locations doesn't send an empty
     location fixture
     """
     self.user.unset_location()
     self.addCleanup(self.user.set_location, self.loc)
     fixture = call_fixture_generator(location_fixture_generator, self.user.to_ota_restore_user())
     self.assertEqual(len(fixture), 0)
Exemple #28
0
 def test_report_fixtures_provider_with_app_that_doesnt_have_reports(self):
     from corehq.apps.userreports.reports.data_source import ConfigurableReportDataSource
     with patch.object(ConfigurableReportDataSource,
                       'get_data') as get_data_mock:
         get_data_mock.return_value = self.rows
         fixtures = call_fixture_generator(report_fixture_generator,
                                           self.user,
                                           app=self.app3)
     self.assertEqual(len(fixtures), 0)
    def setUpClass(cls):
        super(ReportFiltersSuiteTest, cls).setUpClass()
        delete_all_users()
        cls.report_id = '7b97e8b53d00d43ca126b10093215a9d'
        cls.report_config_uuid = 'a98c812873986df34fd1b4ceb45e6164ae9cc664'
        cls.domain = 'report-filter-test-domain'
        cls.user = create_restore_user(
            domain=cls.domain,
            username='******',
        )
        MOBILE_UCR.set(cls.domain, True, NAMESPACE_DOMAIN)

        report_configuration = cls.make_report_config(cls.domain, cls.report_id)
        cls.report_configs_by_id = {
            cls.report_id: report_configuration
        }
        cls.app = Application.new_app(cls.domain, "Report Filter Test App")
        module = cls.app.add_module(ReportModule.new_module("Report Module", 'en'))
        module.report_configs.append(
            ReportAppConfig(
                report_id=cls.report_id,
                header={},
                description="",
                complete_graph_configs={
                    '7451243209119342931': GraphConfiguration(
                        graph_type="bar",
                        series=[GraphSeries(
                            config={},
                            locale_specific_config={},
                            data_path="",
                            x_function="",
                            y_function="",
                        )],
                    )
                },
                filters=OrderedDict([
                    ('fav_fruit_abc123_1', MobileSelectFilter()),
                    ('computed_owner_name_40cc88a0_1', MobileSelectFilter()),
                ]),
                uuid=cls.report_config_uuid,
            )
        )
        with mock_report_configurations(cls.report_configs_by_id):
            cls.suite = cls.app.create_suite()
        cls.data = [
            {'color_94ec39e6': 'red', 'count': 2, 'computed_owner_name_40cc88a0': 'cory', 'fav_fruit_abc123': 'c'},
            {'color_94ec39e6': 'black', 'count': 1, 'computed_owner_name_40cc88a0': 'ctsims', 'fav_fruit_abc123': 'b'},
            {'color_94ec39e6': 'red', 'count': 3, 'computed_owner_name_40cc88a0': 'daniel', 'fav_fruit_abc123': 'b'},
        ]
        with mock_report_data(cls.data):
            with mock_report_configuration_get(cls.report_configs_by_id):
                with mock.patch('corehq.apps.app_manager.fixtures.mobile_ucr.get_apps_in_domain',
                                lambda domain, include_remote: [cls.app]):
                    with mock_datasource_config():
                        fixture, = call_fixture_generator(report_fixture_generator, cls.user)
        cls.fixture = ElementTree.tostring(fixture)
Exemple #30
0
 def _assert_fixture_has_locations(self,
                                   xml_name,
                                   desired_locations,
                                   flat=False):
     generator = flat_location_fixture_generator if flat else location_fixture_generator
     fixture = ElementTree.tostring(
         call_fixture_generator(generator, self.user)[-1])
     desired_fixture = self._assemble_expected_fixture(
         xml_name, desired_locations)
     self.assertXmlEqual(desired_fixture, fixture)
Exemple #31
0
    def test_program_fixture(self):
        user = self.user
        Program(domain=user.domain, name="test1", code="t1").save()

        program_list = Program.by_domain(user.domain)
        program_xml = self.generate_program_xml(program_list, user)

        fixture = call_fixture_generator(program_fixture_generator, user)

        self.assertXmlEqual(program_xml, ElementTree.tostring(fixture[0]))
    def test_cached_global_fixture_user_id(self):
        sandwich = self.make_data_type("sandwich", is_global=True)
        self.make_data_item(sandwich, "7.39")
        frank = self.user.to_ota_restore_user()
        sammy = CommCareUser.create(self.domain, 'sammy',
                                    '***').to_ota_restore_user()

        fixtures = call_fixture_generator(fixturegenerators.item_lists, frank)
        self.assertEqual({item.attrib['user_id']
                          for item in fixtures}, {frank.user_id})
        self.assertTrue(get_blob_db().exists(self.domain, FIXTURE_BUCKET))

        bytes_ = six.binary_type
        fixtures = [
            ElementTree.fromstring(f) if isinstance(f, bytes_) else f for f in
            call_fixture_generator(fixturegenerators.item_lists, sammy)
        ]
        self.assertEqual({item.attrib['user_id']
                          for item in fixtures}, {sammy.user_id})
    def test_location_fixture_generator_no_user_location(self):
        """
        Ensures that a user without a location will still receive an empty fixture
        """
        self.user.unset_location()
        self.addCleanup(self.user.set_location, self.loc)

        fixture = call_fixture_generator(location_fixture_generator, self.user.to_ota_restore_user())
        self.assertEqual(len(fixture), 1)
        self.assertEquals(len(fixture[0].findall('.//state')), 0)
 def _assert_fixture_matches_file(self, xml_name, desired_locations, flat=False, related=False):
     if flat:
         generator = flat_location_fixture_generator
     elif related:
         generator = related_locations_fixture_generator
     else:
         generator = location_fixture_generator
     fixture = ElementTree.tostring(call_fixture_generator(generator, self.user)[-1])
     desired_fixture = self._assemble_expected_fixture(xml_name, desired_locations)
     self.assertXmlEqual(desired_fixture, fixture)
Exemple #35
0
 def test_metadata_added_to_all_nodes(self):
     mass = self.locations['Massachusetts']
     self.user._couch_user.set_location(mass)
     fixture = call_fixture_generator(flat_location_fixture_generator, self.user)[1]  # first node is index
     location_nodes = fixture.findall('locations/location')
     self.assertEqual(7, len(location_nodes))
     for location_node in location_nodes:
         location_data_nodes = [child for child in location_node.find('location_data')]
         self.assertEqual(2, len(location_data_nodes))
         tags = {n.tag for n in location_data_nodes}
         self.assertItemsEqual(tags, self.field_slugs)
 def test_metadata_added_to_all_nodes(self):
     mass = self.locations['Massachusetts']
     self.user._couch_user.set_location(mass)
     fixture = call_fixture_generator(flat_location_fixture_generator, self.user)[1]  # first node is index
     location_nodes = fixture.findall('locations/location')
     self.assertEqual(7, len(location_nodes))
     for location_node in location_nodes:
         location_data_nodes = [child for child in location_node.find('location_data')]
         self.assertEqual(2, len(location_data_nodes))
         tags = {n.tag for n in location_data_nodes}
         self.assertItemsEqual(tags, self.field_slugs)
 def test_report_fixtures_provider_with_app(self):
     """
     ReportFixturesProvider should not iterate all apps if app given
     """
     from corehq.apps.userreports.reports.data_source import ConfigurableReportDataSource
     with patch.object(ConfigurableReportDataSource, 'get_data') as get_data_mock:
         get_data_mock.return_value = self.rows
         with mock_datasource_config():
             fixtures = call_fixture_generator(report_fixture_generator, self.user, app=self.app1)
     reports = fixtures[0].findall('.//report')
     self.assertEqual(len(reports), 1)
     self.assertEqual(reports[0].attrib.get('id'), '123456')
    def test_empty_user_data_types(self):
        self.make_data_type("cookie", is_global=False)

        fixtures = call_fixture_generator(fixturegenerators.item_lists,
                                          self.user.to_ota_restore_user())
        # make sure each fixture is there, and only once
        self.assertEqual(
            [item.attrib['id'] for item in fixtures],
            [
                'item-list:cookie-index',
                'item-list:district',
            ],
        )
Exemple #39
0
 def test_report_fixtures_provider_with_app(self):
     """
     ReportFixturesProvider should not iterate all apps if app given
     """
     from corehq.apps.userreports.reports.data_source import ConfigurableReportDataSource
     with patch.object(ConfigurableReportDataSource, 'get_data') as get_data_mock:
         get_data_mock.return_value = self.rows
         with mock_sql_backend():
             with mock_datasource_config():
                 fixtures = call_fixture_generator(report_fixture_generator, self.user, app=self.app1)
     reports = fixtures[0].findall('.//report')
     self.assertEqual(len(reports), 1)
     self.assertEqual(reports[0].attrib.get('id'), '123456')
Exemple #40
0
 def test_fixture_provider_no_permission(self, _get_permission_checker):
     _get_permission_checker().can_view_registry_data.return_value = False
     fixtures = call_fixture_generator(registry_fixture_generator,
                                       self.restore_user,
                                       project=self.domain_obj)
     self.assertEqual(1, len(fixtures))
     expected_list_fixture = """
     <fixture id="registry:list">
        <registry_list></registry_list>
     </fixture>"""
     self.assertXmlEqual(
         expected_list_fixture,
         ElementTree.tostring(fixtures[0], encoding='utf-8'))
Exemple #41
0
    def test_include_without_expanding_includes_all_ancestors(self):
        self.user._couch_user.set_location(self.locations['DTO'])
        location_type = self.locations['DTO'].location_type

        location_type.include_without_expanding = self.locations['DTO'].location_type
        location_type.save()

        fixture = ElementTree.tostring(call_fixture_generator(flat_location_fixture_generator, self.user)[-1])

        for location_name in ('CDST1', 'CDST', 'DRTB1', 'DRTB', 'DTO1', 'DTO', 'CTO', 'CTO1', 'CTD'):
            self.assertTrue(location_name in fixture)

        for location_name in ('PHI1', 'TU1', 'DMC1'):
            self.assertFalse(location_name in fixture)
    def test_include_without_expanding_includes_all_ancestors(self):
        self.user._couch_user.set_location(self.locations['DTO'])
        location_type = self.locations['DTO'].location_type

        location_type.include_without_expanding = self.locations['DTO'].location_type
        location_type.save()

        fixture = ElementTree.tostring(call_fixture_generator(flat_location_fixture_generator, self.user)[-1]).decode('utf-8')

        for location_name in ('CDST1', 'CDST', 'DRTB1', 'DRTB', 'DTO1', 'DTO', 'CTO', 'CTO1', 'CTD'):
            self.assertTrue(location_name in fixture)

        for location_name in ('PHI1', 'TU1', 'DMC1'):
            self.assertFalse(location_name in fixture)
    def test_callcenter_fixture_commcare_user(self):
        user = CommCareUser(_id='123', username="******")
        indicator_set = MockIndicatorSet(name='test', indicators=OrderedDict([
            ('user_case1', {'i1': 1, 'i2': 2}),
            ('user_case2', {'i1': 0, 'i2': 3})
        ]))
        restore_user = type('OTARestoreCommCareUserFake' if six.PY3 else b'OTARestoreCommCareUserFake', (OTARestoreCommCareUser,), {
            'project': Domain(name='test', default_timezone='UTC'),
            'get_call_center_indicators': lambda self, config: indicator_set,
        })('test', user)

        fixture, = call_fixture_generator(mock_indicators_fixture_generator, restore_user)
        check_xml_line_by_line(
            self, ElementTree.tostring(fixture),
            ElementTree.tostring(gen_fixture(restore_user, indicator_set)))
Exemple #44
0
    def test_program_fixture(self):
        user = self.user
        Program(
            domain=user.domain,
            name="test1",
            code="t1"
        ).save()

        program_list = Program.by_domain(user.domain)
        program_xml = self.generate_program_xml(program_list, user)

        fixture = call_fixture_generator(program_fixture_generator, user)

        self.assertXmlEqual(
            program_xml,
            ElementTree.tostring(fixture[0])
        )
    def test_additional_metadata_not_included(self):
        mass = self.locations['Massachusetts']
        mass.metadata = {'driver_friendliness': 'poor'}
        mass.save()

        def _clear_metadata():
            mass.metadata = {}
            mass.save()

        self.addCleanup(_clear_metadata)
        self.user._couch_user.set_location(mass)
        fixture = call_fixture_generator(flat_location_fixture_generator, self.user)[1]  # first node is index
        mass_data = [
            field for field in fixture.find('locations/location[@id="{}"]/location_data'.format(mass.location_id))
        ]
        self.assertEqual(2, len(mass_data))
        self.assertItemsEqual(self.field_slugs, [f.tag for f in mass_data])
Exemple #46
0
    def test_product_fixture(self):
        user = self.user
        fixture_xml = self.generate_product_fixture_xml(user)
        index_schema, fixture = call_fixture_generator(product_fixture_generator, user)

        self.assertXmlEqual(fixture_xml, ElementTree.tostring(fixture))

        schema_xml = """
            <schema id="commtrack:products">
                <indices>
                    <index>@id</index>
                    <index>category</index>
                    <index>code</index>
                    <index>program_id</index>
                </indices>
            </schema>
        """
        self.assertXmlEqual(schema_xml, ElementTree.tostring(index_schema))
    def test_index_location_fixtures(self):
        self.user._couch_user.set_location(self.locations['Massachusetts'])
        expected_result = self._assemble_expected_fixture(
            'index_location_fixtures',
            ['Massachusetts', 'Suffolk', 'Boston', 'Revere', 'Middlesex', 'Cambridge', 'Somerville'],
        )
        fixture_nodes = call_fixture_generator(flat_location_fixture_generator, self.user)
        self.assertEqual(len(fixture_nodes), 2)  # fixture schema, then fixture

        # check the fixture like usual
        fixture = extract_xml_partial(ElementTree.tostring(fixture_nodes[1]), '.')
        expected_fixture = extract_xml_partial(expected_result, './fixture')
        self.assertXmlEqual(expected_fixture, fixture)

        # check the schema
        schema = extract_xml_partial(ElementTree.tostring(fixture_nodes[0]), '.')
        expected_schema = extract_xml_partial(expected_result, './schema')
        self.assertXmlEqual(expected_schema, schema)
    def test_existing_metadata_works(self):
        mass = self.locations['Massachusetts']
        mass.metadata = {'baseball_team': 'Red Sox'}
        mass.save()

        def _clear_metadata():
            mass.metadata = {}
            mass.save()

        self.addCleanup(_clear_metadata)
        self.user._couch_user.set_location(mass)
        fixture = call_fixture_generator(flat_location_fixture_generator, self.user)[1]  # first node is index
        self.assertEqual(
            'Red Sox',
            fixture.find(
                'locations/location[@id="{}"]/location_data/baseball_team'.format(mass.location_id)
            ).text
        )
 def test_report_fixtures_provider_with_app_that_doesnt_have_reports(self):
     from corehq.apps.userreports.reports.data_source import ConfigurableReportDataSource
     with patch.object(ConfigurableReportDataSource, 'get_data') as get_data_mock:
         get_data_mock.return_value = self.rows
         fixtures = call_fixture_generator(report_fixture_generator, self.user, app=self.app3)
     self.assertEqual(len(fixtures), 0)
    def setUpClass(cls):
        super(ReportFiltersSuiteTest, cls).setUpClass()
        delete_all_users()
        cls.report_id = '7b97e8b53d00d43ca126b10093215a9d'
        cls.report_config_mobile_id = 'a98c812873986df34fd1b4ceb45e6164ae9cc664'
        cls.domain = 'report-filter-test-domain'
        create_domain(cls.domain)
        cls.user = create_restore_user(
            domain=cls.domain,
            username='******',
        )
        MOBILE_UCR.set(cls.domain, True, NAMESPACE_DOMAIN)

        report_configuration = cls.make_report_config(cls.domain, cls.report_id)

        # also make a report with a hidden column
        cls.hidden_column_report_id = 'bd2a43018ad9463682165c1bc16347ac'
        cls.hidden_column_mobile_id = '45152061d8dc4d2a8d987a0568abe1ae'
        report_configuration_with_hidden_column = MAKE_REPORT_CONFIG(
            cls.domain,
            cls.hidden_column_report_id,
            columns=[
                FieldColumn(
                    type='field',
                    aggregation="simple",
                    column_id="color_94ec39e6",
                    display="color",
                    field="color_94ec39e6"
                ).to_json(),
                FieldColumn(
                    type='field',
                    aggregation="simple",
                    column_id="hidden_color_94ec39e6",
                    display="color",
                    field="color_94ec39e6",
                    visible=False,
                ).to_json(),
            ]
        )
        cls.report_configs_by_id = {
            cls.report_id: report_configuration,
            cls.hidden_column_report_id: report_configuration_with_hidden_column
        }
        cls.app = Application.new_app(cls.domain, "Report Filter Test App")
        module = cls.app.add_module(ReportModule.new_module("Report Module", 'en'))
        module.report_configs.append(
            ReportAppConfig(
                report_id=cls.report_id,
                header={},
                description="",
                complete_graph_configs={
                    '7451243209119342931': GraphConfiguration(
                        graph_type="bar",
                        series=[GraphSeries(
                            config={},
                            locale_specific_config={},
                            data_path="",
                            x_function="",
                            y_function="",
                        )],
                    )
                },
                filters=OrderedDict([
                    ('fav_fruit_abc123_1', MobileSelectFilter()),
                    ('computed_owner_name_40cc88a0_1', MobileSelectFilter()),
                ]),
                uuid=cls.report_config_mobile_id,
            )
        )
        module.report_configs.append(
            ReportAppConfig(
                report_id=cls.hidden_column_report_id,
                header={},
                description="",
                complete_graph_configs={},
                filters={},
                uuid=cls.hidden_column_mobile_id,
            )
        )
        with mock_report_configurations(cls.report_configs_by_id):
            cls.suite = cls.app.create_suite()
        cls.data = [
            {'color_94ec39e6': 'red', 'count': 2, 'computed_owner_name_40cc88a0': 'cory', 'fav_fruit_abc123': 'c'},
            {'color_94ec39e6': 'black', 'count': 1, 'computed_owner_name_40cc88a0': 'ctsims', 'fav_fruit_abc123': 'b'},
            {'color_94ec39e6': 'red', 'count': 3, 'computed_owner_name_40cc88a0': 'daniel', 'fav_fruit_abc123': 'b'},
        ]
        with mock_report_data(cls.data):
            with mock_report_configuration_get(cls.report_configs_by_id):
                with mock.patch('corehq.apps.app_manager.fixtures.mobile_ucr.get_apps_in_domain',
                                lambda domain, include_remote: [cls.app]):
                    with mock_datasource_config():
                        fixture, = call_fixture_generator(report_fixture_generator, cls.user)
        cls.fixture = ElementTree.tostring(fixture)
 def test_force_empty_when_user_has_no_locations(self, *args):
     sync_log = SyncLog(date=datetime.utcnow())
     # no relations have been touched since this synclog, but it still pushes down the empty list
     self.assertEquals(
         len(call_fixture_generator(related_locations_fixture_generator, self.user, last_sync=sync_log)), 2)
 def test_no_user_locations_returns_empty(self):
     empty_fixture = EMPTY_LOCATION_FIXTURE_TEMPLATE.format(self.user.user_id)
     fixture = ElementTree.tostring(call_fixture_generator(location_fixture_generator, self.user)[0])
     self.assertXmlEqual(empty_fixture, fixture)