From: Stephen Finucane Date: Thu, 31 Aug 2017 08:48:51 +0000 (+0100) Subject: REST: Filter projects by 'linkname', not 'name' X-Git-Tag: v2.1.0-rc1~121 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=174de19d41d76f8387027e789e5975fc078f2d08;p=thirdparty%2Fpatchwork.git REST: Filter projects by 'linkname', not 'name' Based on a report on the OVS mailing list, it appears that projects are being filtered on the 'Project.name' field instead of the 'Project.linkname' field. Correct this and add regression tests to prevent it happening again. Signed-off-by: Stephen Finucane Fixes: 08b2115 ("REST: Allow filtering by both project ID and linkname") Closes-bug: #117 ("Projects are filtered on the wrong field") Cc: Daniel Axtens [dja: drop mangling of value] Signed-off-by: Daniel Axtens --- diff --git a/patchwork/api/filters.py b/patchwork/api/filters.py index 9966543b..198d64f4 100644 --- a/patchwork/api/filters.py +++ b/patchwork/api/filters.py @@ -50,7 +50,7 @@ class ProjectChoiceField(ModelChoiceField): try: filters = {'pk': int(value)} except ValueError: - filters = {'name__iexact': ' '.join(value.split('-'))} + filters = {'linkname__iexact': value} try: value = self.queryset.get(**filters) diff --git a/patchwork/tests/test_rest_api.py b/patchwork/tests/test_rest_api.py index abffd17f..14e53b28 100644 --- a/patchwork/tests/test_rest_api.py +++ b/patchwork/tests/test_rest_api.py @@ -97,7 +97,23 @@ class TestProjectAPI(APITestCase): self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertSerialized(project, resp.data) - def test_get_numeric_linkname(self): + def test_get_by_id(self): + """Validate that it's possible to filter by pk.""" + project = create_project() + + resp = self.client.get(self.api_url(project.pk)) + self.assertEqual(status.HTTP_200_OK, resp.status_code) + self.assertSerialized(project, resp.data) + + def test_get_by_linkname(self): + """Validate that it's possible to filter by linkname.""" + project = create_project(linkname='project', name='Sample project') + + resp = self.client.get(self.api_url('project')) + self.assertEqual(status.HTTP_200_OK, resp.status_code) + self.assertSerialized(project, resp.data) + + def test_get_by_numeric_linkname(self): """Validate we try to do the right thing for numeric linkname""" project = create_project(linkname='12345') @@ -326,7 +342,8 @@ class TestPatchAPI(APITestCase): self.assertEqual(0, len(resp.data)) state_obj = create_state(name='Under Review') - patch_obj = create_patch(state=state_obj) + project_obj = create_project(linkname='myproject') + patch_obj = create_patch(state=state_obj, project=project_obj) # anonymous user resp = self.client.get(self.api_url()) @@ -338,12 +355,6 @@ class TestPatchAPI(APITestCase): self.assertNotIn('content', patch_rsp) self.assertNotIn('diff', patch_rsp) - # test filtering by state - resp = self.client.get(self.api_url(), {'state': 'under-review'}) - self.assertEqual([patch_obj.id], [x['id'] for x in resp.data]) - resp = self.client.get(self.api_url(), {'state': 'missing-state'}) - self.assertEqual(0, len(resp.data)) - # authenticated user user = create_user() self.client.force_authenticate(user=user) @@ -353,6 +364,18 @@ class TestPatchAPI(APITestCase): patch_rsp = resp.data[0] self.assertSerialized(patch_obj, patch_rsp) + # test filtering by state + resp = self.client.get(self.api_url(), {'state': 'under-review'}) + self.assertEqual([patch_obj.id], [x['id'] for x in resp.data]) + resp = self.client.get(self.api_url(), {'state': 'missing-state'}) + self.assertEqual(0, len(resp.data)) + + # test filtering by project + resp = self.client.get(self.api_url(), {'project': 'myproject'}) + self.assertEqual([patch_obj.id], [x['id'] for x in resp.data]) + resp = self.client.get(self.api_url(), {'project': 'invalidproject'}) + self.assertEqual(0, len(resp.data)) + def test_detail(self): """Validate we can get a specific patch.""" patch = create_patch(