diff options
| author | HappyDingning <ssdyny@foxmail.com> | 2023-05-15 00:12:22 +0800 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-05-18 09:39:04 +0200 |
| commit | 674c23999cb6982a9d447fedec4d72e135201fee (patch) | |
| tree | ee9a368e355adca083a2a3583b06ba8a07c935fb /tests/auth_tests/test_models.py | |
| parent | 4e73d8c04d15f9cbae067249c7ff39dec9d66eb1 (diff) | |
Fixed #34565 -- Added support for async checking of user passwords.
Diffstat (limited to 'tests/auth_tests/test_models.py')
| -rw-r--r-- | tests/auth_tests/test_models.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/tests/auth_tests/test_models.py b/tests/auth_tests/test_models.py index 41f822fdc7..19454ade65 100644 --- a/tests/auth_tests/test_models.py +++ b/tests/auth_tests/test_models.py @@ -1,5 +1,7 @@ from unittest import mock +from asgiref.sync import sync_to_async + from django.conf.global_settings import PASSWORD_HASHERS from django.contrib.auth import get_user_model from django.contrib.auth.backends import ModelBackend @@ -312,6 +314,29 @@ class AbstractUserTestCase(TestCase): finally: hasher.iterations = old_iterations + @override_settings(PASSWORD_HASHERS=PASSWORD_HASHERS) + async def test_acheck_password_upgrade(self): + user = await sync_to_async(User.objects.create_user)( + username="user", password="foo" + ) + initial_password = user.password + self.assertIs(await user.acheck_password("foo"), True) + hasher = get_hasher("default") + self.assertEqual("pbkdf2_sha256", hasher.algorithm) + + old_iterations = hasher.iterations + try: + # Upgrade the password iterations. + hasher.iterations = old_iterations + 1 + with mock.patch( + "django.contrib.auth.password_validation.password_changed" + ) as pw_changed: + self.assertIs(await user.acheck_password("foo"), True) + self.assertEqual(pw_changed.call_count, 0) + self.assertNotEqual(initial_password, user.password) + finally: + hasher.iterations = old_iterations + class CustomModelBackend(ModelBackend): def with_perm( |
