Debian Codegraph @

tests/auth_tests/test_handlers.py

raw
1 from unittest import mock
2
3 from django.contrib.auth.handlers.modwsgi import check_password, groups_for_user
4 from django.contrib.auth.hashers import get_hasher
5 from django.contrib.auth.models import Group, User
6 from django.test import TransactionTestCase, override_settings
7
8 from .models import CustomUser
9
10
11 # This must be a TransactionTestCase because the WSGI auth handler performs
12 # its own transaction management.
13 class ModWsgiHandlerTestCase(TransactionTestCase):
14 """
15 Tests for the mod_wsgi authentication handler
16 """
17
18 available_apps = [
19 "django.contrib.auth",
20 "django.contrib.contenttypes",
21 "auth_tests",
22 ]
23
24 def test_check_password(self):
25 """
26 check_password() returns the correct values as per
27 https://modwsgi.readthedocs.io/en/develop/user-guides/access-control-mechanisms.html#apache-authentication-provider
28 """
29 User.objects.create_user("test", "[email protected]", "test")
30
31 # User not in database
32 self.assertIsNone(check_password({}, "unknown", ""))
33
34 # Valid user with correct password
35 self.assertTrue(check_password({}, "test", "test"))
36
37 # correct password, but user is inactive
38 User.objects.filter(username="test").update(is_active=False)
39 self.assertFalse(check_password({}, "test", "test"))
40
41 # Valid user with incorrect password
42 self.assertFalse(check_password({}, "test", "incorrect"))
43
44 @override_settings(AUTH_USER_MODEL="auth_tests.CustomUser")
45 def test_check_password_custom_user(self):
46 """
47 check_password() returns the correct values as per
48 https://modwsgi.readthedocs.io/en/develop/user-guides/access-control-mechanisms.html#apache-authentication-provider
49 with a custom user installed.
50 """
51 CustomUser._default_manager.create_user(
52 "[email protected]", "1990-01-01", "test"
53 )
54
55 # User not in database
56 self.assertIsNone(check_password({}, "unknown", ""))
57
58 # Valid user with correct password'
59 self.assertTrue(check_password({}, "[email protected]", "test"))
60
61 # Valid user with incorrect password
62 self.assertFalse(check_password({}, "[email protected]", "incorrect"))
63
64 def test_groups_for_user(self):
65 """
66 groups_for_user() returns correct values as per
67 https://modwsgi.readthedocs.io/en/develop/user-guides/access-control-mechanisms.html#apache-group-authorisation
68 """
69 user1 = User.objects.create_user("test", "[email protected]", "test")
70 User.objects.create_user("test1", "[email protected]", "test1")
71 group = Group.objects.create(name="test_group")
72 user1.groups.add(group)
73
74 # User not in database
75 self.assertEqual(groups_for_user({}, "unknown"), [])
76
77 self.assertEqual(groups_for_user({}, "test"), [b"test_group"])
78 self.assertEqual(groups_for_user({}, "test1"), [])
79
80 def test_check_password_fake_runtime(self):
81 """
82 Hasher is run once regardless of whether the user exists. Refs #20760.
83 """
84 User.objects.create_user("test", "[email protected]", "test")
85 User.objects.create_user("inactive", "[email protected]", "test", is_active=False)
86 User.objects.create_user("unusable", "[email protected]")
87
88 hasher = get_hasher()
89
90 for username, password in [
91 ("test", "test"),
92 ("test", "wrong"),
93 ("inactive", "test"),
94 ("inactive", "wrong"),
95 ("unusable", "test"),
96 ("doesnotexist", "test"),
97 ]:
98 with (
99 self.subTest(username=username, password=password),
100 mock.patch.object(hasher, "encode") as mock_make_password,
101 ):
102 check_password({}, username, password)
103 mock_make_password.assert_called_once()