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 | |
| parent | 4e73d8c04d15f9cbae067249c7ff39dec9d66eb1 (diff) | |
Fixed #34565 -- Added support for async checking of user passwords.
Diffstat (limited to 'tests/auth_tests')
| -rw-r--r-- | tests/auth_tests/test_hashers.py | 10 | ||||
| -rw-r--r-- | tests/auth_tests/test_models.py | 25 |
2 files changed, 35 insertions, 0 deletions
diff --git a/tests/auth_tests/test_hashers.py b/tests/auth_tests/test_hashers.py index d1c26cfaa8..e10992b25c 100644 --- a/tests/auth_tests/test_hashers.py +++ b/tests/auth_tests/test_hashers.py @@ -11,6 +11,7 @@ from django.contrib.auth.hashers import ( PBKDF2PasswordHasher, PBKDF2SHA1PasswordHasher, ScryptPasswordHasher, + acheck_password, check_password, get_hasher, identify_hasher, @@ -59,6 +60,15 @@ class TestUtilsHashPass(SimpleTestCase): self.assertTrue(check_password("", blank_encoded)) self.assertFalse(check_password(" ", blank_encoded)) + async def test_acheck_password(self): + encoded = make_password("lètmein") + self.assertIs(await acheck_password("lètmein", encoded), True) + self.assertIs(await acheck_password("lètmeinz", encoded), False) + # Blank passwords. + blank_encoded = make_password("") + self.assertIs(await acheck_password("", blank_encoded), True) + self.assertIs(await acheck_password(" ", blank_encoded), False) + def test_bytes(self): encoded = make_password(b"bytes_password") self.assertTrue(encoded.startswith("pbkdf2_sha256$")) 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( |
