From 060430a66200bb4adca1a2e2934d1f3baee180b2 Mon Sep 17 00:00:00 2001 From: lao605 Date: Thu, 6 Aug 2015 13:36:16 +0800 Subject: [PATCH] Explicitly raise an error when updating release component with unknown fields. Params like `release` and `global_component` will not pass cliently now. --- pdc/apps/component/serializers.py | 7 ++++++- pdc/apps/component/tests.py | 7 ++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pdc/apps/component/serializers.py b/pdc/apps/component/serializers.py index 7104fa97..bedc3240 100644 --- a/pdc/apps/component/serializers.py +++ b/pdc/apps/component/serializers.py @@ -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 @@ -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. if self.instance: + allowed_keys = self.get_allowed_keys() - set(['release', 'global_component']) + extra_fields = set(data.keys()) - allowed_keys + if extra_fields: + 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) diff --git a/pdc/apps/component/tests.py b/pdc/apps/component/tests.py index fd92661b..b835e6d1 100644 --- a/pdc/apps/component/tests.py +++ b/pdc/apps/component/tests.py @@ -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'] @@ -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".')