from rest_framework.relations import RelatedField
from rest_framework.reverse import reverse
from rest_framework.serializers import SerializerMethodField
+from rest_framework.serializers import ValidationError
from patchwork.api.base import BaseHyperlinkedModelSerializer
from patchwork.api.base import PatchworkPermission
# model
return {}
+ def validate_delegate(self, value):
+ """Check that the delgate is a maintainer of the patch's project."""
+ if not self.instance.project.maintainer_project.filter(
+ id=value.id).exists():
+ raise ValidationError("User '%s' is not a maintainer for project "
+ "'%s'" % (value, self.instance.project))
+ return value
+
class Meta:
model = Patch
fields = ('id', 'url', 'web_url', 'project', 'msgid', 'date', 'name',
# maintainer
user = create_maintainer(project)
self.client.force_authenticate(user=user)
- resp = self.client.patch(self.api_url(patch.id), {'state': state.name})
- self.assertEqual(status.HTTP_200_OK, resp.status_code)
+ resp = self.client.patch(self.api_url(patch.id), {
+ 'state': state.name, 'delegate': user.id})
+ self.assertEqual(status.HTTP_200_OK, resp.status_code, resp)
self.assertEqual(Patch.objects.get(id=patch.id).state, state)
+ # TODO(stephenfin): This is currently broken due to #216
+ # self.assertEqual(Patch.objects.get(id=patch.id).delegate, user)
def test_update_invalid(self):
- """Ensure we handle invalid Patch states."""
+ """Ensure we handle invalid Patch updates."""
project = create_project()
state = create_state()
patch = create_patch(project=project, state=state)
self.assertContains(resp, 'Expected one of: %s.' % state.name,
status_code=status.HTTP_400_BAD_REQUEST)
+ # invalid delegate
+ user_b = create_user()
+ resp = self.client.patch(self.api_url(patch.id),
+ {'delegate': user_b.id})
+ # TODO(stephenfin): This is currently broken due to #216
+ # self.assertEqual(status.HTTP_400_BAD_REQUEST, resp.status_code)
+ # self.assertContains(resp, "User '%s' is not a maintainer" % user_b,
+ # status_code=status.HTTP_400_BAD_REQUEST)
+
def test_delete(self):
"""Ensure deletions are always rejected."""
project = create_project()