|
3 | 3 |
|
4 | 4 | from django.test import RequestFactory
|
5 | 5 | from django.utils import timezone
|
| 6 | +from rest_framework import status |
| 7 | +from rest_framework.decorators import action |
6 | 8 | from rest_framework.exceptions import NotFound
|
7 | 9 | from rest_framework.request import Request
|
| 10 | +from rest_framework.response import Response |
8 | 11 | from rest_framework.reverse import reverse
|
9 | 12 | from rest_framework.test import APIRequestFactory, APITestCase, force_authenticate
|
10 | 13 |
|
| 14 | +from rest_framework_json_api import serializers, views |
11 | 15 | from rest_framework_json_api.utils import format_resource_type
|
12 | 16 |
|
13 | 17 | from example.factories import AuthorFactory, CommentFactory, EntryFactory
|
@@ -634,3 +638,67 @@ def test_get_object_gives_correct_entry(self):
|
634 | 638 | }
|
635 | 639 | got = resp.json()
|
636 | 640 | self.assertEqual(got, expected)
|
| 641 | + |
| 642 | + |
| 643 | +class BasicAuthorSerializer(serializers.ModelSerializer): |
| 644 | + class Meta: |
| 645 | + model = Author |
| 646 | + fields = ('name',) |
| 647 | + |
| 648 | + |
| 649 | +class ReadOnlyViewSetWithCustomActions(views.ReadOnlyModelViewSet): |
| 650 | + queryset = Author.objects.all() |
| 651 | + serializer_class = BasicAuthorSerializer |
| 652 | + |
| 653 | + @action(detail=False, methods=['post', 'patch', 'delete']) |
| 654 | + def group_action(self, request): |
| 655 | + return Response(status=status.HTTP_204_NO_CONTENT) |
| 656 | + |
| 657 | + @action(detail=True, methods=['post', 'patch', 'delete']) |
| 658 | + def item_action(self, request, pk): |
| 659 | + return Response(status=status.HTTP_204_NO_CONTENT) |
| 660 | + |
| 661 | + |
| 662 | +class TestReadonlyModelViewSet(TestBase): |
| 663 | + """ |
| 664 | + Test if ReadOnlyModelViewSet allows to have custom actions with POST, PATCH, DELETE methods |
| 665 | + """ |
| 666 | + factory = RequestFactory() |
| 667 | + viewset_class = ReadOnlyViewSetWithCustomActions |
| 668 | + media_type = 'application/vnd.api+json' |
| 669 | + |
| 670 | + def test_group_action_allows_post(self): |
| 671 | + view = self.viewset_class.as_view({'post': 'group_action'}) |
| 672 | + request = self.factory.post('/', '{}', content_type=self.media_type) |
| 673 | + response = view(request) |
| 674 | + self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code) |
| 675 | + |
| 676 | + def test_group_action_allows_patch(self): |
| 677 | + view = self.viewset_class.as_view({'patch': 'group_action'}) |
| 678 | + request = self.factory.patch('/', '{}', content_type=self.media_type) |
| 679 | + response = view(request) |
| 680 | + self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code) |
| 681 | + |
| 682 | + def test_group_action_allows_delete(self): |
| 683 | + view = self.viewset_class.as_view({'delete': 'group_action'}) |
| 684 | + request = self.factory.delete('/', '{}', content_type=self.media_type) |
| 685 | + response = view(request) |
| 686 | + self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code) |
| 687 | + |
| 688 | + def test_item_action_allows_post(self): |
| 689 | + view = self.viewset_class.as_view({'post': 'item_action'}) |
| 690 | + request = self.factory.post('/', '{}', content_type=self.media_type) |
| 691 | + response = view(request, pk='1') |
| 692 | + self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code) |
| 693 | + |
| 694 | + def test_item_action_allows_patch(self): |
| 695 | + view = self.viewset_class.as_view({'patch': 'item_action'}) |
| 696 | + request = self.factory.patch('/', '{}', content_type=self.media_type) |
| 697 | + response = view(request, pk='1') |
| 698 | + self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code) |
| 699 | + |
| 700 | + def test_item_action_allows_delete(self): |
| 701 | + view = self.viewset_class.as_view({'delete': 'item_action'}) |
| 702 | + request = self.factory.delete('/', '{}', content_type=self.media_type) |
| 703 | + response = view(request, pk='1') |
| 704 | + self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code) |
0 commit comments