forked from django/django
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #35782 -- Allowed overriding password validation error messages.
- Loading branch information
1 parent
06bf06a
commit ec7d690
Showing
4 changed files
with
131 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,6 +144,20 @@ def test_help_text(self): | |
"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 @@ def test_help_text(self): | |
"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="[email protected]", | ||
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="[email protected]", | ||
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 @@ def test_help_text(self): | |
"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 @@ def test_help_text(self): | |
"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): | ||
|