|
| 1 | +from unittest import mock |
| 2 | + |
1 | 3 | from django.contrib.auth.handlers.modwsgi import check_password, groups_for_user |
| 4 | +from django.contrib.auth.hashers import get_hasher |
2 | 5 | from django.contrib.auth.models import Group, User |
3 | 6 | from django.test import TransactionTestCase, override_settings |
4 | 7 |
|
@@ -73,3 +76,28 @@ def test_groups_for_user(self): |
73 | 76 |
|
74 | 77 | self.assertEqual(groups_for_user({}, "test"), [b"test_group"]) |
75 | 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", "test@example.com", "test") |
| 85 | + User.objects.create_user("inactive", "test@nono.com", "test", is_active=False) |
| 86 | + User.objects.create_user("unusable", "test@nono.com") |
| 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() |
0 commit comments