예제 #1
0
    def test_workspace_counter(self):
        instance = models.WorkspaceCounter()
        instance.workspace = CountryFactory.build(name=b'xyz')
        self.assertEqual(six.text_type(instance), u'xyz')

        instance = models.WorkspaceCounter()
        instance.workspace = CountryFactory.build(name=u'Magyarorsz\xe1g')
        self.assertEqual(six.text_type(instance), u'Magyarorsz\xe1g')
예제 #2
0
    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)
예제 #3
0
 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)
예제 #4
0
 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)
예제 #5
0
 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)
예제 #6
0
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
예제 #7
0
 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)
예제 #8
0
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='Country {}'.format(name.title()),
                                   schema_name=name,
                                   domain_url='{}.example.com'.format(name))
    country.vision_sync_enabled = True
    # We'll want to check vision_last_synced as part of the tests, so set it to a known value.
    country.vision_last_synced = None
    # We mock save() so we can see if it was called or not, also to prevent database changes.
    country.save = mock.Mock()

    return country
예제 #9
0
 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)
예제 #10
0
 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()
예제 #11
0
 def setUpTestData(cls):
     cls.country = CountryFactory()
     cls.profile = ProfileFactory(country=cls.country)
     cls.profile.countries_available.add(cls.country)
예제 #12
0
 def test_vision_sync_log(self):
     country = CountryFactory.build(name='M\xe9xico',
                                    schema_name='Mexico',
                                    domain_url='mexico.example.com')
     instance = VisionSyncLog(country=country)
     self.assertTrue(six.text_type(instance).startswith(u'M\xe9xico'))
예제 #13
0
    def test_country(self):
        instance = CountryFactory.build(name=b'xyz')
        self.assertEqual(six.text_type(instance), u'xyz')

        instance = CountryFactory.build(name=u'Magyarorsz\xe1g')
        self.assertEqual(six.text_type(instance), u'Magyarorsz\xe1g')
예제 #14
0
 def setUpTestData(cls):
     cls.tenant_flag = TenantFlagFactory(superusers=False)
     cls.country = CountryFactory()
     cls.user = UserFactory()
예제 #15
0
 def setUpTestData(cls):
     cls.tenant_switch = TenantSwitchFactory(active=True)
     connection.tenant = CountryFactory()