Skip to content

Commit

Permalink
REST: Ensure submission exists for comment listing
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Finucane <[email protected]>
Closes: #225
  • Loading branch information
stephenfin committed Dec 22, 2018
1 parent 93ce847 commit 3b12675
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions patchwork/api/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

import email.parser

from django.http import Http404
from rest_framework.generics import ListAPIView
from rest_framework.serializers import SerializerMethodField

from patchwork.api.base import BaseHyperlinkedModelSerializer
from patchwork.api.base import PatchworkPermission
from patchwork.api.embedded import PersonSerializer
from patchwork.models import Comment
from patchwork.models import Submission


class CommentListSerializer(BaseHyperlinkedModelSerializer):
Expand Down Expand Up @@ -64,6 +66,9 @@ class CommentList(ListAPIView):
lookup_url_kwarg = 'pk'

def get_queryset(self):
if not Submission.objects.filter(pk=self.kwargs['pk']).exists():
raise Http404

return Comment.objects.filter(
submission=self.kwargs['pk']
).select_related('submitter')
12 changes: 12 additions & 0 deletions patchwork/tests/api/test_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ def test_list(self):
with self.assertRaises(NoReverseMatch):
self.client.get(self.api_url(cover_obj, version='1.0'))

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


@unittest.skipUnless(settings.ENABLE_REST_API, 'requires ENABLE_REST_API')
class TestPatchComments(APITestCase):
Expand Down Expand Up @@ -99,3 +105,9 @@ def test_list(self):
# check we can't access comments using the old version of the API
with self.assertRaises(NoReverseMatch):
self.client.get(self.api_url(patch_obj, version='1.0'))

def test_list_invalid_patch(self):
"""Ensure we get a 404 for a non-existent patch."""
resp = self.client.get(
reverse('api-patch-comment-list', kwargs={'pk': '99999'}))
self.assertEqual(status.HTTP_404_NOT_FOUND, resp.status_code)
6 changes: 6 additions & 0 deletions releasenotes/notes/issue-225-94215600c1b23f6e.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Showing comments for a non-existant patch or cover letter was returning an
empty response instead of a HTTP 404. This issue is resolved for both
resources.

0 comments on commit 3b12675

Please sign in to comment.