| 1 |
import os |
| 2 |
from unittest import mock |
| 3 |
|
| 4 |
from django.contrib.auth import validators |
| 5 |
from django.contrib.auth.models import User |
| 6 |
from django.contrib.auth.password_validation import ( |
| 7 |
CommonPasswordValidator, |
| 8 |
MinimumLengthValidator, |
| 9 |
NumericPasswordValidator, |
| 10 |
UserAttributeSimilarityValidator, |
| 11 |
get_default_password_validators, |
| 12 |
get_password_validators, |
| 13 |
password_changed, |
| 14 |
password_validators_help_text_html, |
| 15 |
password_validators_help_texts, |
| 16 |
validate_password, |
| 17 |
) |
| 18 |
from django.core.exceptions import ImproperlyConfigured, ValidationError |
| 19 |
from django.db import models |
| 20 |
from django.test import SimpleTestCase, TestCase, override_settings |
| 21 |
from django.test.utils import isolate_apps |
| 22 |
from django.utils.html import conditional_escape |
| 23 |
|
| 24 |
|
| 25 |
@override_settings( |
| 26 |
AUTH_PASSWORD_VALIDATORS=[ |
| 27 |
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"}, |
| 28 |
{ |
| 29 |
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", |
| 30 |
"OPTIONS": { |
| 31 |
"min_length": 12, |
| 32 |
}, |
| 33 |
}, |
| 34 |
] |
| 35 |
) |
| 36 |
class PasswordValidationTest(SimpleTestCase): |
| 37 |
def test_get_default_password_validators(self): |
| 38 |
validators = get_default_password_validators() |
| 39 |
self.assertEqual(len(validators), 2) |
| 40 |
self.assertEqual(validators[0].__class__.__name__, "CommonPasswordValidator") |
| 41 |
self.assertEqual(validators[1].__class__.__name__, "MinimumLengthValidator") |
| 42 |
self.assertEqual(validators[1].min_length, 12) |
| 43 |
|
| 44 |
def test_get_password_validators_custom(self): |
| 45 |
validator_config = [ |
| 46 |
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"} |
| 47 |
] |
| 48 |
validators = get_password_validators(validator_config) |
| 49 |
self.assertEqual(len(validators), 1) |
| 50 |
self.assertEqual(validators[0].__class__.__name__, "CommonPasswordValidator") |
| 51 |
|
| 52 |
self.assertEqual(get_password_validators([]), []) |
| 53 |
|
| 54 |
def test_get_password_validators_custom_invalid(self): |
| 55 |
validator_config = [{"NAME": "json.tool"}] |
| 56 |
msg = ( |
| 57 |
"The module in NAME could not be imported: json.tool. " |
| 58 |
"Check your AUTH_PASSWORD_VALIDATORS setting." |
| 59 |
) |
| 60 |
with self.assertRaisesMessage(ImproperlyConfigured, msg): |
| 61 |
get_password_validators(validator_config) |
| 62 |
|
| 63 |
def test_validate_password(self): |
| 64 |
self.assertIsNone(validate_password("sufficiently-long")) |
| 65 |
msg_too_short = ( |
| 66 |
"This password is too short. It must contain at least 12 characters." |
| 67 |
) |
| 68 |
|
| 69 |
with self.assertRaises(ValidationError) as cm: |
| 70 |
validate_password("django4242") |
| 71 |
self.assertEqual(cm.exception.messages, [msg_too_short]) |
| 72 |
self.assertEqual(cm.exception.error_list[0].code, "password_too_short") |
| 73 |
|
| 74 |
with self.assertRaises(ValidationError) as cm: |
| 75 |
validate_password("password") |
| 76 |
self.assertEqual( |
| 77 |
cm.exception.messages, ["This password is too common.", msg_too_short] |
| 78 |
) |
| 79 |
self.assertEqual(cm.exception.error_list[0].code, "password_too_common") |
| 80 |
|
| 81 |
self.assertIsNone(validate_password("password", password_validators=[])) |
| 82 |
|
| 83 |
def test_password_changed(self): |
| 84 |
self.assertIsNone(password_changed("password")) |
| 85 |
|
| 86 |
def test_password_changed_with_custom_validator(self): |
| 87 |
class Validator: |
| 88 |
def password_changed(self, password, user): |
| 89 |
self.password = password |
| 90 |
self.user = user |
| 91 |
|
| 92 |
user = object() |
| 93 |
validator = Validator() |
| 94 |
password_changed("password", user=user, password_validators=(validator,)) |
| 95 |
self.assertIs(validator.user, user) |
| 96 |
self.assertEqual(validator.password, "password") |
| 97 |
|
| 98 |
def test_password_validators_help_texts(self): |
| 99 |
help_texts = password_validators_help_texts() |
| 100 |
self.assertEqual(len(help_texts), 2) |
| 101 |
self.assertIn("12 characters", help_texts[1]) |
| 102 |
|
| 103 |
self.assertEqual(password_validators_help_texts(password_validators=[]), []) |
| 104 |
|
| 105 |
def test_password_validators_help_text_html(self): |
| 106 |
help_text = password_validators_help_text_html() |
| 107 |
self.assertEqual(help_text.count("<li>"), 2) |
| 108 |
self.assertIn("12 characters", help_text) |
| 109 |
|
| 110 |
def test_password_validators_help_text_html_escaping(self): |
| 111 |
class AmpersandValidator: |
| 112 |
def get_help_text(self): |
| 113 |
return "Must contain &" |
| 114 |
|
| 115 |
help_text = password_validators_help_text_html([AmpersandValidator()]) |
| 116 |
self.assertEqual(help_text, "<ul><li>Must contain &</li></ul>") |
| 117 |
# help_text is marked safe and therefore unchanged by conditional_escape(). |
| 118 |
self.assertEqual(help_text, conditional_escape(help_text)) |
| 119 |
|
| 120 |
@override_settings(AUTH_PASSWORD_VALIDATORS=[]) |
| 121 |
def test_empty_password_validator_help_text_html(self): |
| 122 |
self.assertEqual(password_validators_help_text_html(), "") |
| 123 |
|
| 124 |
|
| 125 |
class MinimumLengthValidatorTest(SimpleTestCase): |
| 126 |
def test_validate(self): |
| 127 |
expected_error = ( |
| 128 |
"This password is too short. It must contain at least %d characters." |
| 129 |
) |
| 130 |
self.assertIsNone(MinimumLengthValidator().validate("12345678")) |
| 131 |
self.assertIsNone(MinimumLengthValidator(min_length=3).validate("123")) |
| 132 |
|
| 133 |
with self.assertRaises(ValidationError) as cm: |
| 134 |
MinimumLengthValidator().validate("1234567") |
| 135 |
self.assertEqual(cm.exception.messages, [expected_error % 8]) |
| 136 |
error = cm.exception.error_list[0] |
| 137 |
self.assertEqual(error.code, "password_too_short") |
| 138 |
self.assertEqual(error.params, {"min_length": 8}) |
| 139 |
|
| 140 |
with self.assertRaises(ValidationError) as cm: |
| 141 |
MinimumLengthValidator(min_length=3).validate("12") |
| 142 |
self.assertEqual(cm.exception.messages, [expected_error % 3]) |
| 143 |
error = cm.exception.error_list[0] |
| 144 |
self.assertEqual(error.code, "password_too_short") |
| 145 |
self.assertEqual(error.params, {"min_length": 3}) |
| 146 |
|
| 147 |
def test_help_text(self): |
| 148 |
self.assertEqual( |
| 149 |
MinimumLengthValidator().get_help_text(), |
| 150 |
"Your password must contain at least 8 characters.", |
| 151 |
) |
| 152 |
|
| 153 |
@mock.patch("django.contrib.auth.password_validation.ngettext") |
| 154 |
def test_l10n(self, mock_ngettext): |
| 155 |
with self.subTest("get_error_message"): |
| 156 |
MinimumLengthValidator().get_error_message() |
| 157 |
mock_ngettext.assert_called_with( |
| 158 |
"This password is too short. It must contain at least %d character.", |
| 159 |
"This password is too short. It must contain at least %d characters.", |
| 160 |
8, |
| 161 |
) |
| 162 |
mock_ngettext.reset() |
| 163 |
with self.subTest("get_help_text"): |
| 164 |
MinimumLengthValidator().get_help_text() |
| 165 |
mock_ngettext.assert_called_with( |
| 166 |
"Your password must contain at least %(min_length)d " "character.", |
| 167 |
"Your password must contain at least %(min_length)d " "characters.", |
| 168 |
8, |
| 169 |
) |
| 170 |
|
| 171 |
def test_custom_error(self): |
| 172 |
class CustomMinimumLengthValidator(MinimumLengthValidator): |
| 173 |
def get_error_message(self): |
| 174 |
return "Your password must be %d characters long" % self.min_length |
| 175 |
|
| 176 |
expected_error = "Your password must be %d characters long" |
| 177 |
|
| 178 |
with self.assertRaisesMessage(ValidationError, expected_error % 8) as cm: |
| 179 |
CustomMinimumLengthValidator().validate("1234567") |
| 180 |
self.assertEqual(cm.exception.error_list[0].code, "password_too_short") |
| 181 |
|
| 182 |
with self.assertRaisesMessage(ValidationError, expected_error % 3) as cm: |
| 183 |
CustomMinimumLengthValidator(min_length=3).validate("12") |
| 184 |
|
| 185 |
|
| 186 |
class UserAttributeSimilarityValidatorTest(TestCase): |
| 187 |
def test_validate(self): |
| 188 |
user = User.objects.create_user( |
| 189 |
username="testclient", |
| 190 |
password="password", |
| 191 |
email="[email protected]", |
| 192 |
first_name="Test", |
| 193 |
last_name="Client", |
| 194 |
) |
| 195 |
expected_error = "The password is too similar to the %s." |
| 196 |
|
| 197 |
self.assertIsNone(UserAttributeSimilarityValidator().validate("testclient")) |
| 198 |
|
| 199 |
with self.assertRaises(ValidationError) as cm: |
| 200 |
UserAttributeSimilarityValidator().validate("testclient", user=user) |
| 201 |
self.assertEqual(cm.exception.messages, [expected_error % "username"]) |
| 202 |
self.assertEqual(cm.exception.error_list[0].code, "password_too_similar") |
| 203 |
|
| 204 |
with self.assertRaises(ValidationError) as cm: |
| 205 |
UserAttributeSimilarityValidator().validate("example.com", user=user) |
| 206 |
self.assertEqual(cm.exception.messages, [expected_error % "email address"]) |
| 207 |
|
| 208 |
with self.assertRaises(ValidationError) as cm: |
| 209 |
UserAttributeSimilarityValidator( |
| 210 |
user_attributes=["first_name"], |
| 211 |
max_similarity=0.3, |
| 212 |
).validate("testclient", user=user) |
| 213 |
self.assertEqual(cm.exception.messages, [expected_error % "first name"]) |
| 214 |
# max_similarity=1 doesn't allow passwords that are identical to the |
| 215 |
# attribute's value. |
| 216 |
with self.assertRaises(ValidationError) as cm: |
| 217 |
UserAttributeSimilarityValidator( |
| 218 |
user_attributes=["first_name"], |
| 219 |
max_similarity=1, |
| 220 |
).validate(user.first_name, user=user) |
| 221 |
self.assertEqual(cm.exception.messages, [expected_error % "first name"]) |
| 222 |
# Very low max_similarity is rejected. |
| 223 |
msg = "max_similarity must be at least 0.1" |
| 224 |
with self.assertRaisesMessage(ValueError, msg): |
| 225 |
UserAttributeSimilarityValidator(max_similarity=0.09) |
| 226 |
# Passes validation. |
| 227 |
self.assertIsNone( |
| 228 |
UserAttributeSimilarityValidator(user_attributes=["first_name"]).validate( |
| 229 |
"testclient", user=user |
| 230 |
) |
| 231 |
) |
| 232 |
|
| 233 |
@isolate_apps("auth_tests") |
| 234 |
def test_validate_property(self): |
| 235 |
class TestUser(models.Model): |
| 236 |
pass |
| 237 |
|
| 238 |
@property |
| 239 |
def username(self): |
| 240 |
return "foobar" |
| 241 |
|
| 242 |
with self.assertRaises(ValidationError) as cm: |
| 243 |
UserAttributeSimilarityValidator().validate("foobar", user=TestUser()) |
| 244 |
self.assertEqual( |
| 245 |
cm.exception.messages, ["The password is too similar to the username."] |
| 246 |
) |
| 247 |
|
| 248 |
def test_help_text(self): |
| 249 |
self.assertEqual( |
| 250 |
UserAttributeSimilarityValidator().get_help_text(), |
| 251 |
"Your password can’t be too similar to your other personal information.", |
| 252 |
) |
| 253 |
|
| 254 |
def test_custom_error(self): |
| 255 |
class CustomUserAttributeSimilarityValidator(UserAttributeSimilarityValidator): |
| 256 |
def get_error_message(self): |
| 257 |
return "The password is too close to the %(verbose_name)s." |
| 258 |
|
| 259 |
user = User.objects.create_user( |
| 260 |
username="testclient", |
| 261 |
password="password", |
| 262 |
email="[email protected]", |
| 263 |
first_name="Test", |
| 264 |
last_name="Client", |
| 265 |
) |
| 266 |
|
| 267 |
expected_error = "The password is too close to the %s." |
| 268 |
|
| 269 |
with self.assertRaisesMessage(ValidationError, expected_error % "username"): |
| 270 |
CustomUserAttributeSimilarityValidator().validate("testclient", user=user) |
| 271 |
|
| 272 |
def test_custom_error_verbose_name_not_used(self): |
| 273 |
class CustomUserAttributeSimilarityValidator(UserAttributeSimilarityValidator): |
| 274 |
def get_error_message(self): |
| 275 |
return "The password is too close to a user attribute." |
| 276 |
|
| 277 |
user = User.objects.create_user( |
| 278 |
username="testclient", |
| 279 |
password="password", |
| 280 |
email="[email protected]", |
| 281 |
first_name="Test", |
| 282 |
last_name="Client", |
| 283 |
) |
| 284 |
|
| 285 |
expected_error = "The password is too close to a user attribute." |
| 286 |
|
| 287 |
with self.assertRaisesMessage(ValidationError, expected_error): |
| 288 |
CustomUserAttributeSimilarityValidator().validate("testclient", user=user) |
| 289 |
|
| 290 |
|
| 291 |
class CommonPasswordValidatorTest(SimpleTestCase): |
| 292 |
def test_validate(self): |
| 293 |
expected_error = "This password is too common." |
| 294 |
self.assertIsNone(CommonPasswordValidator().validate("a-safe-password")) |
| 295 |
|
| 296 |
with self.assertRaises(ValidationError) as cm: |
| 297 |
CommonPasswordValidator().validate("godzilla") |
| 298 |
self.assertEqual(cm.exception.messages, [expected_error]) |
| 299 |
|
| 300 |
def test_common_hexed_codes(self): |
| 301 |
expected_error = "This password is too common." |
| 302 |
common_hexed_passwords = ["asdfjkl:", "ठ:"] |
| 303 |
for password in common_hexed_passwords: |
| 304 |
with self.subTest(password=password): |
| 305 |
with self.assertRaises(ValidationError) as cm: |
| 306 |
CommonPasswordValidator().validate(password) |
| 307 |
self.assertEqual(cm.exception.messages, [expected_error]) |
| 308 |
|
| 309 |
def test_validate_custom_list(self): |
| 310 |
path = os.path.join( |
| 311 |
os.path.dirname(os.path.realpath(__file__)), "common-passwords-custom.txt" |
| 312 |
) |
| 313 |
validator = CommonPasswordValidator(password_list_path=path) |
| 314 |
expected_error = "This password is too common." |
| 315 |
self.assertIsNone(validator.validate("a-safe-password")) |
| 316 |
|
| 317 |
with self.assertRaises(ValidationError) as cm: |
| 318 |
validator.validate("from-my-custom-list") |
| 319 |
self.assertEqual(cm.exception.messages, [expected_error]) |
| 320 |
self.assertEqual(cm.exception.error_list[0].code, "password_too_common") |
| 321 |
|
| 322 |
def test_validate_django_supplied_file(self): |
| 323 |
validator = CommonPasswordValidator() |
| 324 |
for password in validator.passwords: |
| 325 |
self.assertEqual(password, password.lower()) |
| 326 |
|
| 327 |
def test_help_text(self): |
| 328 |
self.assertEqual( |
| 329 |
CommonPasswordValidator().get_help_text(), |
| 330 |
"Your password can’t be a commonly used password.", |
| 331 |
) |
| 332 |
|
| 333 |
def test_custom_error(self): |
| 334 |
class CustomCommonPasswordValidator(CommonPasswordValidator): |
| 335 |
def get_error_message(self): |
| 336 |
return "This password has been used too much." |
| 337 |
|
| 338 |
expected_error = "This password has been used too much." |
| 339 |
|
| 340 |
with self.assertRaisesMessage(ValidationError, expected_error): |
| 341 |
CustomCommonPasswordValidator().validate("godzilla") |
| 342 |
|
| 343 |
|
| 344 |
class NumericPasswordValidatorTest(SimpleTestCase): |
| 345 |
def test_validate(self): |
| 346 |
expected_error = "This password is entirely numeric." |
| 347 |
self.assertIsNone(NumericPasswordValidator().validate("a-safe-password")) |
| 348 |
|
| 349 |
with self.assertRaises(ValidationError) as cm: |
| 350 |
NumericPasswordValidator().validate("42424242") |
| 351 |
self.assertEqual(cm.exception.messages, [expected_error]) |
| 352 |
self.assertEqual(cm.exception.error_list[0].code, "password_entirely_numeric") |
| 353 |
|
| 354 |
def test_help_text(self): |
| 355 |
self.assertEqual( |
| 356 |
NumericPasswordValidator().get_help_text(), |
| 357 |
"Your password can’t be entirely numeric.", |
| 358 |
) |
| 359 |
|
| 360 |
def test_custom_error(self): |
| 361 |
class CustomNumericPasswordValidator(NumericPasswordValidator): |
| 362 |
def get_error_message(self): |
| 363 |
return "This password is all digits." |
| 364 |
|
| 365 |
expected_error = "This password is all digits." |
| 366 |
|
| 367 |
with self.assertRaisesMessage(ValidationError, expected_error): |
| 368 |
CustomNumericPasswordValidator().validate("42424242") |
| 369 |
|
| 370 |
|
| 371 |
class UsernameValidatorsTests(SimpleTestCase): |
| 372 |
def test_unicode_validator(self): |
| 373 |
valid_usernames = ["joe", "René", "ᴮᴵᴳᴮᴵᴿᴰ", "أحمد"] |
| 374 |
invalid_usernames = [ |
| 375 |
"o'connell", |
| 376 |
"عبد ال", |
| 377 |
"zerowidth\u200bspace", |
| 378 |
"nonbreaking\u00a0space", |
| 379 |
"en\u2013dash", |
| 380 |
"trailingnewline\u000a", |
| 381 |
] |
| 382 |
v = validators.UnicodeUsernameValidator() |
| 383 |
for valid in valid_usernames: |
| 384 |
with self.subTest(valid=valid): |
| 385 |
v(valid) |
| 386 |
for invalid in invalid_usernames: |
| 387 |
with self.subTest(invalid=invalid): |
| 388 |
with self.assertRaises(ValidationError): |
| 389 |
v(invalid) |
| 390 |
|
| 391 |
def test_ascii_validator(self): |
| 392 |
valid_usernames = ["glenn", "GLEnN", "jean-marc"] |
| 393 |
invalid_usernames = [ |
| 394 |
"o'connell", |
| 395 |
"Éric", |
| 396 |
"jean marc", |
| 397 |
"أحمد", |
| 398 |
"trailingnewline\n", |
| 399 |
] |
| 400 |
v = validators.ASCIIUsernameValidator() |
| 401 |
for valid in valid_usernames: |
| 402 |
with self.subTest(valid=valid): |
| 403 |
v(valid) |
| 404 |
for invalid in invalid_usernames: |
| 405 |
with self.subTest(invalid=invalid): |
| 406 |
with self.assertRaises(ValidationError): |
| 407 |
v(invalid) |