Skip to content

Commit 1bd87ee

Browse files
committed
REST: Validate patch delegate
At present, only users who are maintainers of projects can be delegated a project. Validate this. This is currently broken due to #216 but that will be fixed in a future change. Signed-off-by: Stephen Finucane <stephen@that.guru> (cherry picked from commit b690746)
1 parent 1552b7d commit 1bd87ee

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

patchwork/api/patch.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from rest_framework.relations import RelatedField
2626
from rest_framework.reverse import reverse
2727
from rest_framework.serializers import SerializerMethodField
28+
from rest_framework.serializers import ValidationError
2829

2930
from patchwork.api.base import BaseHyperlinkedModelSerializer
3031
from patchwork.api.base import PatchworkPermission
@@ -113,6 +114,14 @@ def get_tags(self, instance):
113114
# model
114115
return {}
115116

117+
def validate_delegate(self, value):
118+
"""Check that the delgate is a maintainer of the patch's project."""
119+
if not self.instance.project.maintainer_project.filter(
120+
id=value.id).exists():
121+
raise ValidationError("User '%s' is not a maintainer for project "
122+
"'%s'" % (value, self.instance.project))
123+
return value
124+
116125
class Meta:
117126
model = Patch
118127
fields = ('id', 'url', 'web_url', 'project', 'msgid', 'date', 'name',

patchwork/tests/api/test_patch.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,15 @@ def test_update(self):
218218
# maintainer
219219
user = create_maintainer(project)
220220
self.client.force_authenticate(user=user)
221-
resp = self.client.patch(self.api_url(patch.id), {'state': state.name})
222-
self.assertEqual(status.HTTP_200_OK, resp.status_code)
221+
resp = self.client.patch(self.api_url(patch.id), {
222+
'state': state.name, 'delegate': user.id})
223+
self.assertEqual(status.HTTP_200_OK, resp.status_code, resp)
223224
self.assertEqual(Patch.objects.get(id=patch.id).state, state)
225+
# TODO(stephenfin): This is currently broken due to #216
226+
# self.assertEqual(Patch.objects.get(id=patch.id).delegate, user)
224227

225228
def test_update_invalid(self):
226-
"""Ensure we handle invalid Patch states."""
229+
"""Ensure we handle invalid Patch updates."""
227230
project = create_project()
228231
state = create_state()
229232
patch = create_patch(project=project, state=state)
@@ -236,6 +239,15 @@ def test_update_invalid(self):
236239
self.assertContains(resp, 'Expected one of: %s.' % state.name,
237240
status_code=status.HTTP_400_BAD_REQUEST)
238241

242+
# invalid delegate
243+
user_b = create_user()
244+
resp = self.client.patch(self.api_url(patch.id),
245+
{'delegate': user_b.id})
246+
# TODO(stephenfin): This is currently broken due to #216
247+
# self.assertEqual(status.HTTP_400_BAD_REQUEST, resp.status_code)
248+
# self.assertContains(resp, "User '%s' is not a maintainer" % user_b,
249+
# status_code=status.HTTP_400_BAD_REQUEST)
250+
239251
def test_delete(self):
240252
"""Ensure deletions are always rejected."""
241253
project = create_project()

0 commit comments

Comments
 (0)