Skip to content

Commit 3b12675

Browse files
committed
REST: Ensure submission exists for comment listing
Signed-off-by: Stephen Finucane <[email protected]> Closes: #225
1 parent 93ce847 commit 3b12675

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

patchwork/api/comment.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55

66
import email.parser
77

8+
from django.http import Http404
89
from rest_framework.generics import ListAPIView
910
from rest_framework.serializers import SerializerMethodField
1011

1112
from patchwork.api.base import BaseHyperlinkedModelSerializer
1213
from patchwork.api.base import PatchworkPermission
1314
from patchwork.api.embedded import PersonSerializer
1415
from patchwork.models import Comment
16+
from patchwork.models import Submission
1517

1618

1719
class CommentListSerializer(BaseHyperlinkedModelSerializer):
@@ -64,6 +66,9 @@ class CommentList(ListAPIView):
6466
lookup_url_kwarg = 'pk'
6567

6668
def get_queryset(self):
69+
if not Submission.objects.filter(pk=self.kwargs['pk']).exists():
70+
raise Http404
71+
6772
return Comment.objects.filter(
6873
submission=self.kwargs['pk']
6974
).select_related('submitter')

patchwork/tests/api/test_comment.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ def test_list(self):
6161
with self.assertRaises(NoReverseMatch):
6262
self.client.get(self.api_url(cover_obj, version='1.0'))
6363

64+
def test_list_invalid_cover(self):
65+
"""Ensure we get a 404 for a non-existent cover letter."""
66+
resp = self.client.get(
67+
reverse('api-cover-comment-list', kwargs={'pk': '99999'}))
68+
self.assertEqual(status.HTTP_404_NOT_FOUND, resp.status_code)
69+
6470

6571
@unittest.skipUnless(settings.ENABLE_REST_API, 'requires ENABLE_REST_API')
6672
class TestPatchComments(APITestCase):
@@ -99,3 +105,9 @@ def test_list(self):
99105
# check we can't access comments using the old version of the API
100106
with self.assertRaises(NoReverseMatch):
101107
self.client.get(self.api_url(patch_obj, version='1.0'))
108+
109+
def test_list_invalid_patch(self):
110+
"""Ensure we get a 404 for a non-existent patch."""
111+
resp = self.client.get(
112+
reverse('api-patch-comment-list', kwargs={'pk': '99999'}))
113+
self.assertEqual(status.HTTP_404_NOT_FOUND, resp.status_code)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
Showing comments for a non-existant patch or cover letter was returning an
5+
empty response instead of a HTTP 404. This issue is resolved for both
6+
resources.

0 commit comments

Comments
 (0)