def test_workspace_counter(self): instance = models.WorkspaceCounter() instance.workspace = CountryFactory.build(name='xyz') self.assertEqual(str(instance), 'xyz') instance = models.WorkspaceCounter() instance.workspace = CountryFactory.build(name='Magyarorsz\xe1g') self.assertEqual(str(instance), 'Magyarorsz\xe1g')
def test_business_area_code(self): workspace = CountryFactory(schema_name='test1', business_area_code='0001') workspace_override = CountryFactory(schema_name='test2', business_area_code='0002') workspace_invalid_business_area = CountryFactory( schema_name='test3', business_area_code='0003') business_area_0001 = PublicsBusinessAreaFactory(code='0001') business_area_0002 = PublicsBusinessAreaFactory(code='0002') profile = self.unicef_staff.profile # Check if no country set response = self.forced_auth_req('get', '/users/api/profile/', user=self.unicef_staff) response_json = json.loads(response.rendered_content) self.assertEqual(response_json['t2f']['business_area'], None) # Check if country set profile.country = workspace profile.save() response = self.forced_auth_req('get', '/users/api/profile/', user=self.unicef_staff) response_json = json.loads(response.rendered_content) self.assertEqual(response_json['t2f']['business_area'], business_area_0001.id) # Check if country override set profile.country_override = workspace_override profile.save() response = self.forced_auth_req('get', '/users/api/profile/', user=self.unicef_staff) response_json = json.loads(response.rendered_content) self.assertEqual(response_json['t2f']['business_area'], business_area_0002.id) # Check if no matching business area found profile.country_override = workspace_invalid_business_area profile.save() response = self.forced_auth_req('get', '/users/api/profile/', user=self.unicef_staff) response_json = json.loads(response.rendered_content) self.assertEqual(response_json['t2f']['business_area'], None)
def test_get_country_business_area_code(self): """Check that we get country that matches business area code""" area_code = "10101" with schema_context(SCHEMA_NAME): country_uat = CountryFactory(name="UAT") self.mapper.countries = {"UAT": country_uat} country = CountryFactory(business_area_code=area_code) res = self.mapper._get_country(area_code) self.assertEqual(res, country) self.assertCountEqual(self.mapper.countries, { area_code: country, "UAT": country_uat })
def test_get_country_exists(self): """Check that if country exists and is set, we handle that properly""" area_code = "20202" country_uat = CountryFactory(name="UAT") country = CountryFactory(business_area_code=area_code) self.mapper.countries = { "UAT": country_uat, area_code: country, } res = self.mapper._get_country(area_code) self.assertEqual(res, country) self.assertCountEqual(self.mapper.countries, { "UAT": country_uat, area_code: country, })
def test_get_country_uat(self): """Check that we get country UAT if no match for business area code""" with schema_context(SCHEMA_NAME): country_uat = CountryFactory(name="UAT") res = self.mapper._get_country("UAT") self.assertEqual(res, country_uat) self.assertEqual(self.mapper.countries, {"UAT": country_uat})
def test_post_country_forbidden(self): country = CountryFactory() response = self.forced_auth_req("post", self.url, user=self.unicef_staff, data={"country": country.pk}) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_post_country_forbidden(self): country = CountryFactory(schema_name='test1') # we can't use current country as no switch will be performed response = self.forced_auth_req( "post", self.url, user=self.unicef_staff, data={"country": country.pk} ) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_set_special_attr_country_match(self): """If country attribute matches, then False""" name = "test" country = CountryFactory(name=name) profile = UserProfile(country=country) self.mapper.countries = {name: country, "UAT": country} res = self.mapper._set_special_attr(profile, "country", country) self.assertEqual(profile.country, country) self.assertFalse(res)
def test_public_schema_urlconf(self): """ This just tests a code path that was copied from the django-tenants middleware when we copy/pasted. eTools does not use it. """ country = CountryFactory(schema_name='public') superuser = UserFactory(is_superuser=True, profile__country=country) self.request.user = superuser EToolsTenantMiddleware().process_request(self.request) self.assertEqual(self.request.urlconf, 'foo')
def test_list_switches_only(self): country = CountryFactory() connection.tenant = country tenant_switch = TenantSwitchFactory(countries=[country]) nontenant_switch = TenantSwitchFactory(countries=[]) rsp = self.forced_auth_req('get', self.url) self.assertEqual(rsp.status_code, status.HTTP_200_OK) active_flags = json.loads(rsp.content)['active_flags'] self.assertIn(tenant_switch.name, active_flags) self.assertNotIn(nontenant_switch.name, active_flags)
def test_flag_and_switch_have_same_name(self): country = CountryFactory() connection.tenant = country same_name = 'identical' TenantSwitchFactory(countries=[country], name=same_name) TenantFlagFactory(everyone=True, name=same_name) rsp = self.forced_auth_req('get', self.url) self.assertEqual(rsp.status_code, status.HTTP_200_OK) active_flags = json.loads(rsp.content)['active_flags'] self.assertEqual(len(active_flags), 1) self.assertIn(same_name, active_flags)
def test_good_country_sync_error(self, mock_synchronizer): """ If Synchronizer throws an error, then return 500. """ country = CountryFactory() request_data = { 'country': country.name, } mock_synchronizer.return_value.sync.side_effect = Exception response = self.forced_auth_req('get', self.url, user=self.superuser, data=request_data) self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
def test_set_attribute_special_field(self): """If special field, use _set_special_attr method""" name = "test" country = CountryFactory(name=name) self.mapper.countries = {name: country, "UAT": country} profile = ProfileFactory(country=None) self.assertIsNone(profile.country) self.assertFalse(profile.countries_available.count()) res = self.mapper._set_attribute(profile, "country", name) self.assertTrue(res) self.assertEqual(profile.country, country) self.assertTrue(profile.countries_available.count())
def _build_country(name): '''Given a name (e.g. 'test1'), creates a Country object via FactoryBoy. The object is not saved to the database. It exists only in memory. We must be careful not to save this because creating a new Country in the database complicates schemas. ''' country = CountryFactory.build(name=u'Country {}'.format(name.title()), schema_name=name, domain_url=u'{}.example.com'.format(name)) # Mock save() to prevent inadvertent database changes. country.save = mock.Mock() return country
def test_returns_both_flags_and_switches(self): country = CountryFactory() connection.tenant = country tenant_switch = TenantSwitchFactory(countries=[country]) nontenant_switch = TenantSwitchFactory(countries=[]) everyone_flag = TenantFlagFactory(everyone=True) rsp = self.forced_auth_req('get', self.url) self.assertEqual(rsp.status_code, status.HTTP_200_OK) active_flags = json.loads(rsp.content)['active_flags'] self.assertIn(everyone_flag.name, active_flags) self.assertIn(tenant_switch.name, active_flags) self.assertNotIn(nontenant_switch.name, active_flags)
def test_good_country(self, mock_synchronizer): """ Sync country and return success response. """ country = CountryFactory() request_data = { 'country': country.name, } response = self.forced_auth_req('get', self.url, user=self.superuser, data=request_data) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['success'], 'Country = {}'.format(country.name)) # assert that sync was called mock_synchronizer.return_value.sync.assert_called()
def setUp(self): self.unicef_staff = UserFactory(is_superuser=True) group = GroupFactory() self.unicef_staff.groups.add(group) # The tested endpoints require the country id in the query string self.country = CountryFactory() self.unicef_staff.profile.country = self.country self.unicef_staff.save() self.location_no_geom = LocationFactory(name="Test no geom") self.location_with_geom = LocationFactory( name="Test with geom", geom="MultiPolygon(((10 10, 10 20, 20 20, 20 15, 10 10)), ((10 10, 10 20, 20 20, 20 15, 10 10)))" )
def test_set_special_attr(self): """If country attribute, no override and country does not match current county, then set and return True """ name = "test" country = CountryFactory(name=name) self.mapper.countries = {name: country, "UAT": country} profile = ProfileFactory(country=None) self.assertIsNone(profile.country) self.assertFalse(profile.countries_available.count()) res = self.mapper._set_special_attr(profile, "country", name) self.assertTrue(res) self.assertEqual(profile.country, country) self.assertTrue(profile.countries_available.count())
def setUp(self): super().setUp() self.unicef_staff = UserFactory(is_superuser=True) group = GroupFactory() self.unicef_staff.groups.add(group) self.country = CountryFactory() self.unicef_staff.profile.country = self.country self.unicef_staff.save() self.partner = PartnerFactory() self.agreement = AgreementFactory(partner=self.partner) self.intervention = InterventionFactory(agreement=self.agreement) self.location_no_geom = LocationFactory(name="Test no geom") self.location_with_geom = LocationFactory( name="Test with geom", geom= "MultiPolygon(((10 10, 10 20, 20 20, 20 15, 10 10)), ((10 10, 10 20, 20 20, 20 15, 10 10)))" ) self.inactive_location = LocationFactory(is_active=False) self.locations = [self.location_no_geom, self.location_with_geom]
def test_set_special_attr_country_override(self): """If country attribute, but override is set then False""" country = CountryFactory() profile = UserProfile(country_override=country) res = self.mapper._set_special_attr(profile, "country", "Change") self.assertFalse(res)
def test_vision_sync_log(self): country = CountryFactory.build(name='M\xe9xico', schema_name='Mexico') instance = VisionSyncLog(country=country) self.assertTrue(str(instance).startswith('M\xe9xico'))
def setUpTestData(cls): cls.country = CountryFactory() cls.profile = ProfileFactory(country=cls.country) cls.profile.countries_available.add(cls.country)
def test_country(self): instance = CountryFactory.build(name='xyz') self.assertEqual(str(instance), 'xyz') instance = CountryFactory.build(name='Magyarorsz\xe1g') self.assertEqual(str(instance), 'Magyarorsz\xe1g')
def setUpTestData(cls): cls.tenant_switch = TenantSwitchFactory(active=True) connection.tenant = CountryFactory()
def setUpTestData(cls): cls.tenant_flag = TenantFlagFactory(superusers=False) cls.country = CountryFactory() cls.user = UserFactory()