Skip to content

Commit

Permalink
Explicitly raise an error when updating release component with unknow…
Browse files Browse the repository at this point in the history
…n fields.

Params like `release` and `global_component` will not pass cliently now.
  • Loading branch information
zkl94 committed Aug 6, 2015
1 parent 25fd317 commit 060430a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 6 additions & 1 deletion pdc/apps/component/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.shortcuts import get_object_or_404
from django.utils import six
from django.utils.text import capfirst
from django.core.exceptions import FieldError

from rest_framework import serializers
from rest_framework.validators import UniqueTogetherValidator
Expand Down Expand Up @@ -370,8 +371,12 @@ def to_representation(self, instance):
return ret

def to_internal_value(self, data):
# For Update request, restore release and global_component for unique validation.
# Raise error explictly when release and global_component is given.

This comment has been minimized.

Copy link
@erichuanggit

erichuanggit Aug 6, 2015

Collaborator

ignore this comment. I did not see the detail

if self.instance:
allowed_keys = self.get_allowed_keys() - set(['release', 'global_component'])
extra_fields = set(data.keys()) - allowed_keys
if extra_fields:

This comment has been minimized.

Copy link
@lubomir

lubomir Aug 6, 2015

Member

StrictSerializerMixin provides a method maybe_raise_error, which should be used here.

This comment has been minimized.

Copy link
@zkl94

zkl94 via email Aug 6, 2015

Author Collaborator
raise FieldError('Unknown fields: %s.' % ', '.join('"%s"' % f for f in extra_fields))
data['release'] = self.instance.release
data['global_component'] = self.instance.global_component
return super(ReleaseComponentSerializer, self).to_internal_value(data)
Expand Down
7 changes: 4 additions & 3 deletions pdc/apps/component/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ def test_create_release_component_with_empty_body(self):

def test_update_release_component(self):
url = reverse('releasecomponent-detail', kwargs={'pk': 1})
data = {'release': {'release_id': 'release-1.0', "active": True}, 'global_component': 'python', 'name': 'python26'}
data = {'name': 'python26'}
response = self.client.put(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
del response.data['dist_git_web_url']
Expand All @@ -957,14 +957,15 @@ def test_update_release_component(self):
del response.data['bugzilla_component']
del response.data['brew_package']
del response.data['type']
del response.data['release']
del response.data['global_component']
data.update({'active': True})
self.assertEqual(response.data, data)
self.assertNumChanges([1])

def test_update_release_component_extra_fields(self):
url = reverse('releasecomponent-detail', kwargs={'pk': 1})
data = {'release': {'release_id': 'release-1.0', "active": True},
'global_component': 'python', 'name': 'python26', 'foo': 'bar'}
data = {'name': 'python26', 'foo': 'bar'}
response = self.client.put(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data.get('detail'), 'Unknown fields: "foo".')
Expand Down

0 comments on commit 060430a

Please sign in to comment.