summaryrefslogtreecommitdiff
path: root/tests/auth_tests/test_checks.py
diff options
context:
space:
mode:
authorHisham Mahmood <hishammahmood41@gmail.com>2024-05-05 11:21:28 +0500
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-05-22 08:51:17 +0200
commitc7fc9f20b49b5889a9a8f47de45165ac443c1a21 (patch)
tree113e55d5b047f479375638c1f17d9c127aedf618 /tests/auth_tests/test_checks.py
parent7857507c7fc43350701700d4215a37baea7655f0 (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/test_checks.py')
-rw-r--r--tests/auth_tests/test_checks.py110
1 files changed, 109 insertions, 1 deletions
diff --git a/tests/auth_tests/test_checks.py b/tests/auth_tests/test_checks.py
index 5757946f95..3d70451e9d 100644
--- a/tests/auth_tests/test_checks.py
+++ b/tests/auth_tests/test_checks.py
@@ -1,5 +1,14 @@
-from django.contrib.auth.checks import check_models_permissions, check_user_model
+from django.contrib.auth.checks import (
+ check_middleware,
+ check_models_permissions,
+ check_user_model,
+)
+from django.contrib.auth.middleware import (
+ AuthenticationMiddleware,
+ LoginRequiredMiddleware,
+)
from django.contrib.auth.models import AbstractBaseUser
+from django.contrib.sessions.middleware import SessionMiddleware
from django.core import checks
from django.db import models
from django.db.models import Q, UniqueConstraint
@@ -345,3 +354,102 @@ class ModelsPermissionsChecksTests(SimpleTestCase):
default_permissions = ()
self.assertEqual(checks.run_checks(self.apps.get_app_configs()), [])
+
+
+class LoginRequiredMiddlewareSubclass(LoginRequiredMiddleware):
+ redirect_field_name = "redirect_to"
+
+
+class AuthenticationMiddlewareSubclass(AuthenticationMiddleware):
+ pass
+
+
+class SessionMiddlewareSubclass(SessionMiddleware):
+ pass
+
+
+@override_system_checks([check_middleware])
+class MiddlewareChecksTests(SimpleTestCase):
+ @override_settings(
+ MIDDLEWARE=[
+ "auth_tests.test_checks.SessionMiddlewareSubclass",
+ "auth_tests.test_checks.AuthenticationMiddlewareSubclass",
+ "auth_tests.test_checks.LoginRequiredMiddlewareSubclass",
+ ]
+ )
+ def test_middleware_subclasses(self):
+ errors = checks.run_checks()
+ self.assertEqual(errors, [])
+
+ @override_settings(
+ MIDDLEWARE=[
+ "auth_tests.test_checks",
+ "auth_tests.test_checks.NotExist",
+ ]
+ )
+ def test_invalid_middleware_skipped(self):
+ errors = checks.run_checks()
+ self.assertEqual(errors, [])
+
+ @override_settings(
+ MIDDLEWARE=[
+ "django.contrib.does.not.Exist",
+ "django.contrib.sessions.middleware.SessionMiddleware",
+ "django.contrib.auth.middleware.AuthenticationMiddleware",
+ "django.contrib.auth.middleware.LoginRequiredMiddleware",
+ ]
+ )
+ def test_check_ignores_import_error_in_middleware(self):
+ errors = checks.run_checks()
+ self.assertEqual(errors, [])
+
+ @override_settings(
+ MIDDLEWARE=[
+ "django.contrib.sessions.middleware.SessionMiddleware",
+ "django.contrib.auth.middleware.AuthenticationMiddleware",
+ "django.contrib.auth.middleware.LoginRequiredMiddleware",
+ ]
+ )
+ def test_correct_order_with_login_required_middleware(self):
+ errors = checks.run_checks()
+ self.assertEqual(errors, [])
+
+ @override_settings(
+ MIDDLEWARE=[
+ "django.contrib.auth.middleware.LoginRequiredMiddleware",
+ "django.contrib.auth.middleware.AuthenticationMiddleware",
+ "django.contrib.sessions.middleware.SessionMiddleware",
+ ]
+ )
+ def test_incorrect_order_with_login_required_middleware(self):
+ errors = checks.run_checks()
+ self.assertEqual(
+ errors,
+ [
+ checks.Error(
+ "In order to use django.contrib.auth.middleware."
+ "LoginRequiredMiddleware, django.contrib.auth.middleware."
+ "AuthenticationMiddleware must be defined before it in MIDDLEWARE.",
+ id="auth.E013",
+ )
+ ],
+ )
+
+ @override_settings(
+ MIDDLEWARE=[
+ "django.contrib.auth.middleware.LoginRequiredMiddleware",
+ ]
+ )
+ def test_missing_authentication_with_login_required_middleware(self):
+ errors = checks.run_checks()
+ self.assertEqual(
+ errors,
+ [
+ checks.Error(
+ "In order to use django.contrib.auth.middleware."
+ "LoginRequiredMiddleware, django.contrib.auth.middleware."
+ "AuthenticationMiddleware must be defined before it in MIDDLEWARE.",
+ id="auth.E013",
+ )
+ ],
+ )