#
# SPDX-License-Identifier: GPL-2.0-or-later
-from django.http import Http404
from django.http.request import QueryDict
-from django.shortcuts import get_object_or_404
import rest_framework
from rest_framework.exceptions import PermissionDenied
+from rest_framework.generics import get_object_or_404
from rest_framework.generics import ListCreateAPIView
from rest_framework.generics import RetrieveAPIView
from rest_framework.serializers import CurrentUserDefault
def get_queryset(self):
patch_id = self.kwargs['patch_id']
- if not Patch.objects.filter(pk=self.kwargs['patch_id']).exists():
- raise Http404
+ # ensure the patch exists
+ get_object_or_404(Patch, id=self.kwargs['patch_id'])
return Check.objects.prefetch_related('user').filter(patch=patch_id)
import unittest
from django.conf import settings
+from django.urls import NoReverseMatch
from django.urls import reverse
from patchwork.models import Bundle
self.assertIn('url', resp.data)
self.assertNotIn('web_url', resp.data)
+ def test_detail_non_existent(self):
+ """Ensure we get a 404 for a non-existent bundle."""
+ resp = self.client.get(self.api_url('999999'))
+ self.assertEqual(status.HTTP_404_NOT_FOUND, resp.status_code)
+
+ def test_detail_invalid(self):
+ """Ensure we get a 404 for an invalid bundle ID."""
+ with self.assertRaises(NoReverseMatch):
+ self.client.get(self.api_url('foo'))
+
def _test_create_update(self, authenticate=True):
user = create_user()
project = create_project()
@unittest.skipUnless(settings.ENABLE_REST_API, 'requires ENABLE_REST_API')
class TestCoverComments(utils.APITestCase):
+
@staticmethod
def api_url(cover, version=None):
kwargs = {}
with self.assertRaises(NoReverseMatch):
self.client.get(self.api_url(cover, version='1.0'))
- def test_list_invalid_cover(self):
+ def test_list_non_existent_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)
+ def test_list_invalid_cover(self):
+ """Ensure we get a 404 for an invalid cover letter ID."""
+ with self.assertRaises(NoReverseMatch):
+ self.client.get(
+ reverse('api-cover-comment-list', kwargs={'pk': 'foo'}),
+ )
+
@unittest.skipUnless(settings.ENABLE_REST_API, 'requires ENABLE_REST_API')
class TestPatchComments(utils.APITestCase):
with self.assertRaises(NoReverseMatch):
self.client.get(self.api_url(patch, version='1.0'))
- def test_list_invalid_patch(self):
+ def test_list_non_existent_patch(self):
"""Ensure we get a 404 for a non-existent patch."""
resp = self.client.get(
reverse('api-patch-comment-list', kwargs={'patch_id': '99999'}))
self.assertEqual(status.HTTP_404_NOT_FOUND, resp.status_code)
+
+ def test_list_invalid_patch(self):
+ """Ensure we get a 404 for an invalid patch ID."""
+ with self.assertRaises(NoReverseMatch):
+ self.client.get(
+ reverse('api-patch-comment-list', kwargs={'patch_id': 'foo'}),
+ )
import unittest
from django.conf import settings
+from django.urls import NoReverseMatch
from django.urls import reverse
from patchwork.tests.api import utils
self.assertNotIn('web_url', resp.data)
self.assertNotIn('comments', resp.data)
+ def test_detail_non_existent(self):
+ """Ensure we get a 404 for a non-existent cover."""
+ resp = self.client.get(self.api_url('999999'))
+ self.assertEqual(status.HTTP_404_NOT_FOUND, resp.status_code)
+
+ def test_detail_invalid(self):
+ """Ensure we get a 404 for an invalid cover ID."""
+ with self.assertRaises(NoReverseMatch):
+ self.client.get(self.api_url('foo'))
+
def test_create_update_delete(self):
user = create_maintainer()
user.is_superuser = True
import unittest
from django.conf import settings
+from django.urls import NoReverseMatch
from django.urls import reverse
from patchwork.models import Patch
self.assertNotIn('web_url', resp.data)
self.assertNotIn('comments', resp.data)
+ def test_detail_non_existent(self):
+ """Ensure we get a 404 for a non-existent patch."""
+ resp = self.client.get(self.api_url('999999'))
+ self.assertEqual(status.HTTP_404_NOT_FOUND, resp.status_code)
+
+ def test_detail_invalid(self):
+ """Ensure we get a 404 for an invalid patch ID."""
+ with self.assertRaises(NoReverseMatch):
+ self.client.get(self.api_url('foo'))
+
def test_create(self):
"""Ensure creations are rejected."""
project = create_project()
import unittest
from django.conf import settings
+from django.urls import NoReverseMatch
from django.urls import reverse
from patchwork.tests.api import utils
self.assertEqual(status.HTTP_200_OK, resp.status_code)
self.assertSerialized(person, resp.data, has_user=True)
+ def test_detail_non_existent(self):
+ """Ensure we get a 404 for a non-existent person."""
+ user = create_user(link_person=True)
+ self.client.force_authenticate(user=user)
+
+ resp = self.client.get(self.api_url('999999'))
+ self.assertEqual(status.HTTP_404_NOT_FOUND, resp.status_code)
+
+ def test_detail_invalid(self):
+ """Ensure we get a 404 for an invalid person ID."""
+ with self.assertRaises(NoReverseMatch):
+ self.client.get(self.api_url('foo'))
+
def test_create_update_delete(self):
"""Ensure creates, updates and deletes aren't allowed"""
user = create_maintainer()
self.assertIn('name', resp.data)
self.assertNotIn('subject_match', resp.data)
+ def test_detail_non_existent(self):
+ """Ensure we get a 404 for a non-existent project."""
+ resp = self.client.get(self.api_url('999999'))
+ self.assertEqual(status.HTTP_404_NOT_FOUND, resp.status_code)
+
+ resp = self.client.get(self.api_url('foo'))
+ self.assertEqual(status.HTTP_404_NOT_FOUND, resp.status_code)
+
def test_create(self):
"""Ensure creations are rejected."""
project = create_project()
import unittest
from django.conf import settings
+from django.urls import NoReverseMatch
from django.urls import reverse
from patchwork.tests.api import utils
self.assertNotIn('mbox', resp.data['cover_letter'])
self.assertNotIn('web_url', resp.data['patches'][0])
+ def test_detail_non_existent(self):
+ """Ensure we get a 404 for a non-existent series."""
+ resp = self.client.get(self.api_url('999999'))
+ self.assertEqual(status.HTTP_404_NOT_FOUND, resp.status_code)
+
+ def test_detail_invalid(self):
+ """Ensure we get a 404 for an invalid series ID."""
+ with self.assertRaises(NoReverseMatch):
+ self.client.get(self.api_url('foo'))
+
def test_create_update_delete(self):
"""Ensure creates, updates and deletes aren't allowed"""
user = create_maintainer()
import unittest
from django.conf import settings
+from django.urls import NoReverseMatch
from django.urls import reverse
from patchwork.tests.api import utils
self.assertEqual(status.HTTP_200_OK, resp.status_code)
self.assertSerialized(user, resp.data, has_settings=True)
+ def test_detail_non_existent(self):
+ """Ensure we get a 404 for a non-existent user."""
+ user = create_user()
+
+ self.client.force_authenticate(user=user)
+ resp = self.client.get(self.api_url('999999'))
+ self.assertEqual(status.HTTP_404_NOT_FOUND, resp.status_code)
+
+ def test_detail_invalid(self):
+ """Ensure we get a 404 for an invalid user ID."""
+ with self.assertRaises(NoReverseMatch):
+ self.client.get(self.api_url('foo'))
+
@utils.store_samples('users-update-error-forbidden')
def test_update_anonymous(self):
"""Update user as anonymous user."""
name='api-user-list',
),
path(
- 'users/<pk>/',
+ 'users/<int:pk>/',
api_user_views.UserDetail.as_view(),
name='api-user-detail',
),
name='api-person-list',
),
path(
- 'people/<pk>/',
+ 'people/<int:pk>/',
api_person_views.PersonDetail.as_view(),
name='api-person-detail',
),
name='api-cover-list',
),
path(
- 'covers/<pk>/',
+ 'covers/<int:pk>/',
api_cover_views.CoverDetail.as_view(),
name='api-cover-detail',
),
name='api-patch-list',
),
path(
- 'patches/<pk>/',
+ 'patches/<int:pk>/',
api_patch_views.PatchDetail.as_view(),
name='api-patch-detail',
),
path(
- 'patches/<patch_id>/checks/',
+ 'patches/<int:patch_id>/checks/',
api_check_views.CheckListCreate.as_view(),
name='api-check-list',
),
path(
- 'patches/<patch_id>/checks/<check_id>/',
+ 'patches/<int:patch_id>/checks/<int:check_id>/',
api_check_views.CheckDetail.as_view(),
name='api-check-detail',
),
name='api-series-list',
),
path(
- 'series/<pk>/',
+ 'series/<int:pk>/',
api_series_views.SeriesDetail.as_view(),
name='api-series-detail',
),
name='api-bundle-list',
),
path(
- 'bundles/<pk>/',
+ 'bundles/<int:pk>/',
api_bundle_views.BundleDetail.as_view(),
name='api-bundle-detail',
),
api_1_1_patterns = [
path(
- 'patches/<patch_id>/comments/',
+ 'patches/<int:patch_id>/comments/',
api_comment_views.PatchCommentList.as_view(),
name='api-patch-comment-list',
),
path(
- 'covers/<pk>/comments/',
+ 'covers/<int:pk>/comments/',
api_comment_views.CoverCommentList.as_view(),
name='api-cover-comment-list',
),