diff options
| author | Hisham Mahmood <hishammahmood41@gmail.com> | 2024-05-05 11:21:28 +0500 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2024-05-22 08:51:17 +0200 |
| commit | c7fc9f20b49b5889a9a8f47de45165ac443c1a21 (patch) | |
| tree | 113e55d5b047f479375638c1f17d9c127aedf618 /tests/auth_tests/urls.py | |
| parent | 7857507c7fc43350701700d4215a37baea7655f0 (diff) | |
Fixed #31405 -- Added LoginRequiredMiddleware.
Co-authored-by: Adam Johnson <me@adamj.eu>
Co-authored-by: Mehmet İnce <mehmet@mehmetince.net>
Co-authored-by: Sarah Boyce <42296566+sarahboyce@users.noreply.github.com>
Diffstat (limited to 'tests/auth_tests/urls.py')
| -rw-r--r-- | tests/auth_tests/urls.py | 53 |
1 files changed, 51 insertions, 2 deletions
diff --git a/tests/auth_tests/urls.py b/tests/auth_tests/urls.py index 99fa22e4f4..cb6a0ed1cf 100644 --- a/tests/auth_tests/urls.py +++ b/tests/auth_tests/urls.py @@ -1,6 +1,10 @@ from django.contrib import admin from django.contrib.auth import views -from django.contrib.auth.decorators import login_required, permission_required +from django.contrib.auth.decorators import ( + login_not_required, + login_required, + permission_required, +) from django.contrib.auth.forms import AuthenticationForm from django.contrib.auth.urls import urlpatterns as auth_urlpatterns from django.contrib.auth.views import LoginView @@ -9,6 +13,8 @@ from django.http import HttpRequest, HttpResponse from django.shortcuts import render from django.template import RequestContext, Template from django.urls import path, re_path, reverse_lazy +from django.utils.decorators import method_decorator +from django.views import View from django.views.decorators.cache import never_cache from django.views.i18n import set_language @@ -88,6 +94,42 @@ class CustomDefaultRedirectURLLoginView(LoginView): return "/custom/" +class EmptyResponseBaseView(View): + def get(self, request, *args, **kwargs): + return HttpResponse() + + +@method_decorator(login_not_required, name="dispatch") +class PublicView(EmptyResponseBaseView): + pass + + +class ProtectedView(EmptyResponseBaseView): + pass + + +@method_decorator( + login_required(login_url="/custom_login/", redirect_field_name="step"), + name="dispatch", +) +class ProtectedViewWithCustomLoginRequired(EmptyResponseBaseView): + pass + + +@login_not_required +def public_view(request): + return HttpResponse() + + +def protected_view(request): + return HttpResponse() + + +@login_required(login_url="/custom_login/", redirect_field_name="step") +def protected_view_with_login_required_decorator(request): + return HttpResponse() + + # special urls for auth test cases urlpatterns = auth_urlpatterns + [ path( @@ -198,7 +240,14 @@ urlpatterns = auth_urlpatterns + [ "login_and_permission_required_exception/", login_and_permission_required_exception, ), + path("public_view/", PublicView.as_view()), + path("public_function_view/", public_view), + path("protected_view/", ProtectedView.as_view()), + path("protected_function_view/", protected_view), + path( + "login_required_decorator_view/", protected_view_with_login_required_decorator + ), + path("login_required_cbv_view/", ProtectedViewWithCustomLoginRequired.as_view()), path("setlang/", set_language, name="set_language"), - # This line is only required to render the password reset with is_admin=True path("admin/", admin.site.urls), ] |
