|
| 1 | +import unittest |
| 2 | + |
| 3 | +import django |
1 | 4 | from django.contrib.auth.models import User
|
2 | 5 | from django.http import HttpRequest
|
3 | 6 | from django.test import override_settings
|
4 |
| -from django.urls import path |
| 7 | +from django.urls import include, path |
5 | 8 |
|
| 9 | +from rest_framework import status |
6 | 10 | from rest_framework.authentication import TokenAuthentication
|
7 | 11 | from rest_framework.authtoken.models import Token
|
| 12 | +from rest_framework.decorators import action, api_view |
8 | 13 | from rest_framework.request import is_form_media_type
|
9 | 14 | from rest_framework.response import Response
|
| 15 | +from rest_framework.routers import SimpleRouter |
10 | 16 | from rest_framework.test import APITestCase
|
11 | 17 | from rest_framework.views import APIView
|
| 18 | +from rest_framework.viewsets import GenericViewSet |
12 | 19 |
|
13 | 20 |
|
14 | 21 | class PostView(APIView):
|
15 | 22 | def post(self, request):
|
16 | 23 | return Response(data=request.data, status=200)
|
17 | 24 |
|
18 | 25 |
|
| 26 | +class GetAPIView(APIView): |
| 27 | + def get(self, request): |
| 28 | + return Response(data="OK", status=200) |
| 29 | + |
| 30 | + |
| 31 | +@api_view(['GET']) |
| 32 | +def get_func_view(request): |
| 33 | + return Response(data="OK", status=200) |
| 34 | + |
| 35 | + |
| 36 | +class ListViewSet(GenericViewSet): |
| 37 | + |
| 38 | + def list(self, request, *args, **kwargs): |
| 39 | + response = Response() |
| 40 | + response.view = self |
| 41 | + return response |
| 42 | + |
| 43 | + @action(detail=False, url_path='list-action') |
| 44 | + def list_action(self, request, *args, **kwargs): |
| 45 | + response = Response() |
| 46 | + response.view = self |
| 47 | + return response |
| 48 | + |
| 49 | + |
| 50 | +router = SimpleRouter() |
| 51 | +router.register(r'view-set', ListViewSet, basename='view_set') |
| 52 | + |
19 | 53 | urlpatterns = [
|
20 | 54 | path('auth', APIView.as_view(authentication_classes=(TokenAuthentication,))),
|
21 | 55 | path('post', PostView.as_view()),
|
| 56 | + path('get', GetAPIView.as_view()), |
| 57 | + path('get-func', get_func_view), |
| 58 | + path('api/', include(router.urls)), |
22 | 59 | ]
|
23 | 60 |
|
24 | 61 |
|
@@ -74,3 +111,38 @@ def test_middleware_can_access_request_post_when_processing_response(self):
|
74 | 111 |
|
75 | 112 | response = self.client.post('/post', {'foo': 'bar'}, format='json')
|
76 | 113 | assert response.status_code == 200
|
| 114 | + |
| 115 | + |
| 116 | +@unittest.skipUnless(django.VERSION >= (5, 1), 'Only for Django 5.1+') |
| 117 | +@override_settings( |
| 118 | + ROOT_URLCONF='tests.test_middleware', |
| 119 | + MIDDLEWARE=( |
| 120 | + # Needed for AuthenticationMiddleware |
| 121 | + 'django.contrib.sessions.middleware.SessionMiddleware', |
| 122 | + # Needed for LoginRequiredMiddleware |
| 123 | + 'django.contrib.auth.middleware.AuthenticationMiddleware', |
| 124 | + 'django.contrib.auth.middleware.LoginRequiredMiddleware', |
| 125 | + ), |
| 126 | +) |
| 127 | +class TestLoginRequiredMiddlewareCompat(APITestCase): |
| 128 | + """ |
| 129 | + Django's 5.1+ LoginRequiredMiddleware should NOT apply to DRF views. |
| 130 | +
|
| 131 | + Instead, users should put IsAuthenticated in their |
| 132 | + DEFAULT_PERMISSION_CLASSES setting. |
| 133 | + """ |
| 134 | + def test_class_based_view(self): |
| 135 | + response = self.client.get('/get') |
| 136 | + assert response.status_code == status.HTTP_200_OK |
| 137 | + |
| 138 | + def test_function_based_view(self): |
| 139 | + response = self.client.get('/get-func') |
| 140 | + assert response.status_code == status.HTTP_200_OK |
| 141 | + |
| 142 | + def test_viewset_list(self): |
| 143 | + response = self.client.get('/api/view-set/') |
| 144 | + assert response.status_code == status.HTTP_200_OK |
| 145 | + |
| 146 | + def test_viewset_list_action(self): |
| 147 | + response = self.client.get('/api/view-set/list-action/') |
| 148 | + assert response.status_code == status.HTTP_200_OK |
0 commit comments