diff options
| author | Ben Cail <bcail@crossway.org> | 2024-09-26 10:11:41 -0400 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2024-10-15 17:23:39 +0200 |
| commit | ec7d69035a408b357f1803ca05a7c991cc358cfa (patch) | |
| tree | 63b1146a137bcaf35901c39854ad8cd0af142760 /tests/auth_tests | |
| parent | 06bf06a911695c5c84f746742f764c040e237ece (diff) | |
Fixed #35782 -- Allowed overriding password validation error messages.
Diffstat (limited to 'tests/auth_tests')
| -rw-r--r-- | tests/auth_tests/test_validators.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/auth_tests/test_validators.py b/tests/auth_tests/test_validators.py index 506c85c0ae..d7e4968951 100644 --- a/tests/auth_tests/test_validators.py +++ b/tests/auth_tests/test_validators.py @@ -144,6 +144,20 @@ class MinimumLengthValidatorTest(SimpleTestCase): "Your password must contain at least 8 characters.", ) + def test_custom_error(self): + class CustomMinimumLengthValidator(MinimumLengthValidator): + def get_error_message(self): + return "Your password must be %d characters long" % self.min_length + + expected_error = "Your password must be %d characters long" + + with self.assertRaisesMessage(ValidationError, expected_error % 8) as cm: + CustomMinimumLengthValidator().validate("1234567") + self.assertEqual(cm.exception.error_list[0].code, "password_too_short") + + with self.assertRaisesMessage(ValidationError, expected_error % 3) as cm: + CustomMinimumLengthValidator(min_length=3).validate("12") + class UserAttributeSimilarityValidatorTest(TestCase): def test_validate(self): @@ -213,6 +227,42 @@ class UserAttributeSimilarityValidatorTest(TestCase): "Your password can’t be too similar to your other personal information.", ) + def test_custom_error(self): + class CustomUserAttributeSimilarityValidator(UserAttributeSimilarityValidator): + def get_error_message(self): + return "The password is too close to the %(verbose_name)s." + + user = User.objects.create_user( + username="testclient", + password="password", + email="testclient@example.com", + first_name="Test", + last_name="Client", + ) + + expected_error = "The password is too close to the %s." + + with self.assertRaisesMessage(ValidationError, expected_error % "username"): + CustomUserAttributeSimilarityValidator().validate("testclient", user=user) + + def test_custom_error_verbose_name_not_used(self): + class CustomUserAttributeSimilarityValidator(UserAttributeSimilarityValidator): + def get_error_message(self): + return "The password is too close to a user attribute." + + user = User.objects.create_user( + username="testclient", + password="password", + email="testclient@example.com", + first_name="Test", + last_name="Client", + ) + + expected_error = "The password is too close to a user attribute." + + with self.assertRaisesMessage(ValidationError, expected_error): + CustomUserAttributeSimilarityValidator().validate("testclient", user=user) + class CommonPasswordValidatorTest(SimpleTestCase): def test_validate(self): @@ -247,6 +297,16 @@ class CommonPasswordValidatorTest(SimpleTestCase): "Your password can’t be a commonly used password.", ) + def test_custom_error(self): + class CustomCommonPasswordValidator(CommonPasswordValidator): + def get_error_message(self): + return "This password has been used too much." + + expected_error = "This password has been used too much." + + with self.assertRaisesMessage(ValidationError, expected_error): + CustomCommonPasswordValidator().validate("godzilla") + class NumericPasswordValidatorTest(SimpleTestCase): def test_validate(self): @@ -264,6 +324,16 @@ class NumericPasswordValidatorTest(SimpleTestCase): "Your password can’t be entirely numeric.", ) + def test_custom_error(self): + class CustomNumericPasswordValidator(NumericPasswordValidator): + def get_error_message(self): + return "This password is all digits." + + expected_error = "This password is all digits." + + with self.assertRaisesMessage(ValidationError, expected_error): + CustomNumericPasswordValidator().validate("42424242") + class UsernameValidatorsTests(SimpleTestCase): def test_unicode_validator(self): |
