Skip to content

Commit b23f206

Browse files
stephenfindaxtens
authored andcommitted
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 <[email protected]> Fixes: 08b2115 ("REST: Allow filtering by both project ID and linkname") Closes-bug: #117 ("Projects are filtered on the wrong field") Cc: Daniel Axtens <[email protected]> [dja: drop mangling of value] Signed-off-by: Daniel Axtens <[email protected]> (cherry picked from commit 174de19)
1 parent e5da62d commit b23f206

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

patchwork/api/filters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def to_python(self, value):
5050
try:
5151
filters = {'pk': int(value)}
5252
except ValueError:
53-
filters = {'name__iexact': ' '.join(value.split('-'))}
53+
filters = {'linkname__iexact': value}
5454

5555
try:
5656
value = self.queryset.get(**filters)

patchwork/tests/test_rest_api.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,23 @@ def test_detail(self):
9797
self.assertEqual(status.HTTP_200_OK, resp.status_code)
9898
self.assertSerialized(project, resp.data)
9999

100-
def test_get_numeric_linkname(self):
100+
def test_get_by_id(self):
101+
"""Validate that it's possible to filter by pk."""
102+
project = create_project()
103+
104+
resp = self.client.get(self.api_url(project.pk))
105+
self.assertEqual(status.HTTP_200_OK, resp.status_code)
106+
self.assertSerialized(project, resp.data)
107+
108+
def test_get_by_linkname(self):
109+
"""Validate that it's possible to filter by linkname."""
110+
project = create_project(linkname='project', name='Sample project')
111+
112+
resp = self.client.get(self.api_url('project'))
113+
self.assertEqual(status.HTTP_200_OK, resp.status_code)
114+
self.assertSerialized(project, resp.data)
115+
116+
def test_get_by_numeric_linkname(self):
101117
"""Validate we try to do the right thing for numeric linkname"""
102118
project = create_project(linkname='12345')
103119

@@ -326,7 +342,8 @@ def test_list(self):
326342
self.assertEqual(0, len(resp.data))
327343

328344
state_obj = create_state(name='Under Review')
329-
patch_obj = create_patch(state=state_obj)
345+
project_obj = create_project(linkname='myproject')
346+
patch_obj = create_patch(state=state_obj, project=project_obj)
330347

331348
# anonymous user
332349
resp = self.client.get(self.api_url())
@@ -338,12 +355,6 @@ def test_list(self):
338355
self.assertNotIn('content', patch_rsp)
339356
self.assertNotIn('diff', patch_rsp)
340357

341-
# test filtering by state
342-
resp = self.client.get(self.api_url(), {'state': 'under-review'})
343-
self.assertEqual([patch_obj.id], [x['id'] for x in resp.data])
344-
resp = self.client.get(self.api_url(), {'state': 'missing-state'})
345-
self.assertEqual(0, len(resp.data))
346-
347358
# authenticated user
348359
user = create_user()
349360
self.client.force_authenticate(user=user)
@@ -353,6 +364,18 @@ def test_list(self):
353364
patch_rsp = resp.data[0]
354365
self.assertSerialized(patch_obj, patch_rsp)
355366

367+
# test filtering by state
368+
resp = self.client.get(self.api_url(), {'state': 'under-review'})
369+
self.assertEqual([patch_obj.id], [x['id'] for x in resp.data])
370+
resp = self.client.get(self.api_url(), {'state': 'missing-state'})
371+
self.assertEqual(0, len(resp.data))
372+
373+
# test filtering by project
374+
resp = self.client.get(self.api_url(), {'project': 'myproject'})
375+
self.assertEqual([patch_obj.id], [x['id'] for x in resp.data])
376+
resp = self.client.get(self.api_url(), {'project': 'invalidproject'})
377+
self.assertEqual(0, len(resp.data))
378+
356379
def test_detail(self):
357380
"""Validate we can get a specific patch."""
358381
patch = create_patch(

0 commit comments

Comments
 (0)