class ClientConfigViewTest(OCLAPITestCase): def setUp(self): super().setUp() self.org = OrganizationFactory() self.user = self.org.created_by self.token = self.user.get_token() self.dummy_config = dict(tabs=[dict(default=True)]) self.config = ClientConfig(config=self.dummy_config, name='foobar', resource=self.org) self.config.save() def tearDown(self): ClientConfig.objects.all().delete() super().tearDown() def test_put(self): response = self.client.get('/client-configs/12356/', dict(name='updated'), HTTP_AUTHORIZATION='Token ' + self.token, format='json') self.assertEqual(response.status_code, 404) response = self.client.put(self.config.uri, dict(name='updated'), HTTP_AUTHORIZATION='Token ' + self.token, format='json') self.assertEqual(response.status_code, 200) self.config.refresh_from_db() self.assertTrue(response.data['name'] == self.config.name == 'updated') def test_delete(self): random_user1 = UserProfileFactory() random_user2 = UserProfileFactory() response = self.client.delete(self.config.uri, HTTP_AUTHORIZATION='Token ' + random_user1.get_token(), format='json') self.assertEqual(response.status_code, 403) self.assertEqual(ClientConfig.objects.count(), 1) template = ClientConfig(is_template=True, config=self.dummy_config, name='foobar', resource=self.org, created_by=random_user1) template.save() response = self.client.delete(template.uri, HTTP_AUTHORIZATION='Token ' + random_user2.get_token(), format='json') self.assertEqual(response.status_code, 403) self.assertEqual(ClientConfig.objects.count(), 2) response = self.client.delete(template.uri, HTTP_AUTHORIZATION='Token ' + random_user1.get_token(), format='json') self.assertEqual(response.status_code, 204) self.assertEqual(ClientConfig.objects.count(), 1)
def test_delete(self): random_user1 = UserProfileFactory() random_user2 = UserProfileFactory() response = self.client.delete(self.config.uri, HTTP_AUTHORIZATION='Token ' + random_user1.get_token(), format='json') self.assertEqual(response.status_code, 403) self.assertEqual(ClientConfig.objects.count(), 1) template = ClientConfig(is_template=True, config=self.dummy_config, name='foobar', resource=self.org, created_by=random_user1) template.save() response = self.client.delete(template.uri, HTTP_AUTHORIZATION='Token ' + random_user2.get_token(), format='json') self.assertEqual(response.status_code, 403) self.assertEqual(ClientConfig.objects.count(), 2) response = self.client.delete(template.uri, HTTP_AUTHORIZATION='Token ' + random_user1.get_token(), format='json') self.assertEqual(response.status_code, 204) self.assertEqual(ClientConfig.objects.count(), 1)
def setUp(self): super().setUp() self.org = OrganizationFactory() self.user = self.org.created_by self.token = self.user.get_token() self.dummy_config = dict(tabs=[dict(default=True)]) self.config = ClientConfig(config=self.dummy_config, name='foobar', resource=self.org) self.config.save()
def create(self, validated_data): user = self.context['request'].user from core.common.utils import get_content_type_from_resource_name resource_type = get_content_type_from_resource_name( validated_data.pop('resource_type', None)) instance = ClientConfig(**validated_data, created_by=user, updated_by=user, resource_type=resource_type) if not instance.type: instance.type = HOME_TYPE if instance.is_default is None: instance.is_default = False try: instance.full_clean() instance.save() if instance.id and instance.is_default: instance.siblings.filter(is_default=True).update( is_default=False) except ValidationError as ex: self._errors.update(ex.message_dict) if self._errors: raise serializers.ValidationError(self._errors) return instance
def test_get(self): response = self.client.get(self.org.url + 'client-configs/', HTTP_AUTHORIZATION='Token ' + self.token, format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 0) config = ClientConfig(config=self.dummy_config, name='foobar', resource=self.org) config.save() response = self.client.get(self.org.url + 'client-configs/', HTTP_AUTHORIZATION='Token ' + self.token, format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['id'], config.id)
def test_format_home_config_tabs(self): client_config = ClientConfig(config=dict(tabs=[dict(fields=[])])) client_config.format_home_config_tabs() self.assertEqual(client_config.config, dict(tabs=[dict(fields=[])])) client_config.config['tabs'] = [ dict(fields={ 'source_type': 'Source Type', 'extras.foo': "foobar" }) ] client_config.format_home_config_tabs() self.assertEqual(client_config.config['tabs'][0]['fields'], [{ 'source_type': 'Source Type' }, { 'extras.foo': 'foobar' }]) client_config.config['tabs'] = [ dict(fields=[{ 'source_type': 'Source Type' }, { 'extras.foo': 'foobar' }]) ] client_config.format_home_config_tabs() self.assertEqual(client_config.config['tabs'][0]['fields'], [{ 'source_type': 'Source Type' }, { 'extras.foo': 'foobar' }])
def test_siblings(self): org = OrganizationFactory() config1 = ClientConfig( name='first', resource=org, config=dict(tabs=[dict(foo='bar', default=True)])) config1.save() self.assertEqual(config1.siblings.count(), 0) config2 = ClientConfig( name='second', resource=org, config=dict(tabs=[dict(foo='bar', default=True)])) config2.save() self.assertEqual(config1.siblings.count(), 1) self.assertEqual(config1.siblings.first().id, config2.id) self.assertEqual(config2.siblings.count(), 1) self.assertEqual(config2.siblings.first().id, config1.id)
def test_home_config_validation(self): client_config = ClientConfig(config=dict()) with self.assertRaises(ValidationError) as ex: client_config.full_clean() self.assertEqual( ex.exception.message_dict, dict( config=['This field cannot be blank.'], resource_type=['This field cannot be null.'], resource_id=['This field cannot be null.'], tabs=['At least one tab config is mandatory.'], )) org = OrganizationFactory() client_config.resource = org client_config.config = dict(foo='bar') with self.assertRaises(ValidationError) as ex: client_config.full_clean() self.assertEqual(ex.exception.message_dict, dict(tabs=['At least one tab config is mandatory.'])) client_config.config = dict(tabs='foobar') with self.assertRaises(ValidationError) as ex: client_config.full_clean() self.assertEqual(ex.exception.message_dict, dict(tabs=['Tabs config must be a list.'])) client_config.config = dict(tabs=['foobar']) with self.assertRaises(ValidationError) as ex: client_config.full_clean() self.assertEqual(ex.exception.message_dict, dict(tabs=['Invalid Tabs config.'])) client_config.config = dict(tabs=[dict(foo='bar')]) with self.assertRaises(ValidationError) as ex: client_config.full_clean() self.assertEqual( ex.exception.message_dict, dict(tabs=['Exactly one of the Tabs must be default.'])) client_config.config = dict(tabs=[ dict(foo='bar', default=True), dict(foo='bar', default=True) ]) with self.assertRaises(ValidationError) as ex: client_config.full_clean() self.assertEqual( ex.exception.message_dict, dict(tabs=['Exactly one of the Tabs must be default.'])) client_config.config = dict(tabs=[ dict(foo='bar', default=True), dict(foo='bar', default=False) ]) client_config.full_clean() client_config.config = dict(tabs=[ dict(foo='bar', default=True, sortAsc='foo', sortDesc='bar', type='concepts'), dict(foo='bar', default=False) ]) with self.assertRaises(ValidationError) as ex: client_config.full_clean() self.assertEqual( ex.exception.message_dict, dict(tabs=[ 'Sort either by asc (sortAsc) or desc (sortDesc) order.' ])) client_config.config = dict(tabs=[ dict(foo='bar', default=True, sortAsc='foo', type='concepts'), dict(foo='bar', default=False) ]) with self.assertRaises(ValidationError) as ex: client_config.full_clean() self.assertEqual(ex.exception.message_dict, dict(tabs=['Unsupported sort attribute.'])) client_config.config = dict(tabs=[ dict(foo='bar', default=True, sortAsc='id', type='concepts'), dict(foo='bar', default=False) ]) client_config.full_clean()
def test_uri(self): self.assertEqual(ClientConfig(id=1).uri, '/client-configs/1/') self.assertEqual(ClientConfig(id=400).uri, '/client-configs/400/')
def test_is_home(self): self.assertTrue(ClientConfig().is_home) self.assertTrue(ClientConfig(type='home').is_home) self.assertFalse(ClientConfig(type='blah').is_home)