From 7be99581f1d1b35274987e43ec57272daff94f02 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Sun, 14 Oct 2018 15:29:47 +0100 Subject: [PATCH] REST: Allow unsetting of delegate While we recently fixed setting of this field via the API, we didn't resolve unsetting. Fix this now. Signed-off-by: Stephen Finucane --- patchwork/api/patch.py | 5 ++++- patchwork/tests/api/test_patch.py | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/patchwork/api/patch.py b/patchwork/api/patch.py index b9a134b1..92423cbf 100644 --- a/patchwork/api/patch.py +++ b/patchwork/api/patch.py @@ -68,7 +68,7 @@ class PatchListSerializer(BaseHyperlinkedModelSerializer): project = ProjectSerializer(read_only=True) state = StateField() submitter = PersonSerializer(read_only=True) - delegate = UserSerializer() + delegate = UserSerializer(allow_null=True) mbox = SerializerMethodField() series = SeriesSerializer(many=True, read_only=True) comments = SerializerMethodField() @@ -102,6 +102,9 @@ class PatchListSerializer(BaseHyperlinkedModelSerializer): def validate_delegate(self, value): """Check that the delgate is a maintainer of the patch's project.""" + if not value: + return value + if not self.instance.project.maintainer_project.filter( id=value.id).exists(): raise ValidationError("User '%s' is not a maintainer for project " diff --git a/patchwork/tests/api/test_patch.py b/patchwork/tests/api/test_patch.py index 497cb2de..df30d510 100644 --- a/patchwork/tests/api/test_patch.py +++ b/patchwork/tests/api/test_patch.py @@ -210,6 +210,13 @@ class TestPatchAPI(APITestCase): self.assertEqual(Patch.objects.get(id=patch.id).state, state) self.assertEqual(Patch.objects.get(id=patch.id).delegate, user) + # (who can unset fields too) + # we need to send as JSON due to https://stackoverflow.com/q/30677216/ + resp = self.client.patch(self.api_url(patch.id), {'delegate': None}, + format='json') + self.assertEqual(status.HTTP_200_OK, resp.status_code, resp) + self.assertIsNone(Patch.objects.get(id=patch.id).delegate) + def test_update_invalid(self): """Ensure we handle invalid Patch updates.""" project = create_project() -- 2.47.3