From ec7d69035a408b357f1803ca05a7c991cc358cfa Mon Sep 17 00:00:00 2001 From: Ben Cail Date: Thu, 26 Sep 2024 10:11:41 -0400 Subject: Fixed #35782 -- Allowed overriding password validation error messages. --- tests/auth_tests/test_validators.py | 70 +++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'tests/auth_tests/test_validators.py') 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): -- cgit v1.3