]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
REST: Filter projects by 'linkname', not 'name'
authorStephen Finucane <stephen@that.guru>
Thu, 31 Aug 2017 08:48:51 +0000 (09:48 +0100)
committerDaniel Axtens <dja@axtens.net>
Tue, 5 Sep 2017 16:22:28 +0000 (02:22 +1000)
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 <stephen@that.guru>
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@axtens.net>
[dja: drop mangling of value]
Signed-off-by: Daniel Axtens <dja@axtens.net>
(cherry picked from commit 174de19d41d76f8387027e789e5975fc078f2d08)

patchwork/api/filters.py
patchwork/tests/test_rest_api.py

index 9966543b7b530e11b527d3d8ef8db86d9baf3932..198d64f4d7999fdac80969d38a31676998f77f27 100644 (file)
@@ -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)
index abffd17fddec4c862284be749fb44a25df88be62..14e53b28644f2aa2d6072d09a730673761b741b5 100644 (file)
@@ -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(