@@ -60,6 +60,21 @@ def setUpTestData(cls):
60
60
)
61
61
62
62
63
+ class ExtraValidationFormMixin :
64
+ def __init__ (self , * args , failing_fields = None , ** kwargs ):
65
+ super ().__init__ (* args , ** kwargs )
66
+ self .failing_fields = failing_fields or {}
67
+
68
+ def failing_helper (self , field_name ):
69
+ if field_name in self .failing_fields :
70
+ errors = [
71
+ ValidationError (error , code = "invalid" )
72
+ for error in self .failing_fields [field_name ]
73
+ ]
74
+ raise ValidationError (errors )
75
+ return self .cleaned_data [field_name ]
76
+
77
+
63
78
class BaseUserCreationFormTest (TestDataMixin , TestCase ):
64
79
def test_user_already_exists (self ):
65
80
data = {
@@ -324,6 +339,22 @@ def test_password_help_text(self):
324
339
"</li></ul>" ,
325
340
)
326
341
342
+ def test_password_extra_validations (self ):
343
+ class ExtraValidationForm (ExtraValidationFormMixin , BaseUserCreationForm ):
344
+ def clean_password1 (self ):
345
+ return self .failing_helper ("password1" )
346
+
347
+ def clean_password2 (self ):
348
+ return self .failing_helper ("password2" )
349
+
350
+ data = {"username" : "extra" , "password1" : "abc" , "password2" : "abc" }
351
+ for fields in (["password1" ], ["password2" ], ["password1" , "password2" ]):
352
+ with self .subTest (fields = fields ):
353
+ errors = {field : [f"Extra validation for { field } ." ] for field in fields }
354
+ form = ExtraValidationForm (data , failing_fields = errors )
355
+ self .assertIs (form .is_valid (), False )
356
+ self .assertDictEqual (form .errors , errors )
357
+
327
358
@override_settings (
328
359
AUTH_PASSWORD_VALIDATORS = [
329
360
{
@@ -865,6 +896,27 @@ def test_html_autocomplete_attributes(self):
865
896
form .fields [field_name ].widget .attrs ["autocomplete" ], autocomplete
866
897
)
867
898
899
+ def test_password_extra_validations (self ):
900
+ class ExtraValidationForm (ExtraValidationFormMixin , SetPasswordForm ):
901
+ def clean_new_password1 (self ):
902
+ return self .failing_helper ("new_password1" )
903
+
904
+ def clean_new_password2 (self ):
905
+ return self .failing_helper ("new_password2" )
906
+
907
+ user = User .objects .get (username = "testclient" )
908
+ data = {"new_password1" : "abc" , "new_password2" : "abc" }
909
+ for fields in (
910
+ ["new_password1" ],
911
+ ["new_password2" ],
912
+ ["new_password1" , "new_password2" ],
913
+ ):
914
+ with self .subTest (fields = fields ):
915
+ errors = {field : [f"Extra validation for { field } ." ] for field in fields }
916
+ form = ExtraValidationForm (user , data , failing_fields = errors )
917
+ self .assertIs (form .is_valid (), False )
918
+ self .assertDictEqual (form .errors , errors )
919
+
868
920
869
921
class PasswordChangeFormTest (TestDataMixin , TestCase ):
870
922
def test_incorrect_password (self ):
@@ -1456,6 +1508,23 @@ def test_password_whitespace_not_stripped(self):
1456
1508
self .assertEqual (form .cleaned_data ["password2" ], data ["password2" ])
1457
1509
self .assertEqual (form .changed_data , ["password" ])
1458
1510
1511
+ def test_password_extra_validations (self ):
1512
+ class ExtraValidationForm (ExtraValidationFormMixin , AdminPasswordChangeForm ):
1513
+ def clean_password1 (self ):
1514
+ return self .failing_helper ("password1" )
1515
+
1516
+ def clean_password2 (self ):
1517
+ return self .failing_helper ("password2" )
1518
+
1519
+ user = User .objects .get (username = "testclient" )
1520
+ data = {"username" : "extra" , "password1" : "abc" , "password2" : "abc" }
1521
+ for fields in (["password1" ], ["password2" ], ["password1" , "password2" ]):
1522
+ with self .subTest (fields = fields ):
1523
+ errors = {field : [f"Extra validation for { field } ." ] for field in fields }
1524
+ form = ExtraValidationForm (user , data , failing_fields = errors )
1525
+ self .assertIs (form .is_valid (), False )
1526
+ self .assertDictEqual (form .errors , errors )
1527
+
1459
1528
def test_non_matching_passwords (self ):
1460
1529
user = User .objects .get (username = "testclient" )
1461
1530
data = {"password1" : "password1" , "password2" : "password2" }
0 commit comments