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_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)
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_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_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)