summaryrefslogtreecommitdiff
path: root/tests/auth_tests
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2015-06-08 13:27:47 -0400
committerTim Graham <timograham@gmail.com>2015-06-10 07:41:01 -0400
commit55b3bd84681a87266f6bef72480aaef48a7c295f (patch)
tree4413b39f348f0a0d017db07c182ec4e0af8e6f24 /tests/auth_tests
parenta0047c6242fd48068eb444e0a58f7a5d2bc1bcd3 (diff)
downloaddjango-55b3bd84681a87266f6bef72480aaef48a7c295f.tar.gz
Refs #16860 -- Minor edits and fixes to password validation.
Diffstat (limited to 'tests/auth_tests')
-rw-r--r--tests/auth_tests/test_forms.py7
-rw-r--r--tests/auth_tests/test_validators.py18
2 files changed, 19 insertions, 6 deletions
diff --git a/tests/auth_tests/test_forms.py b/tests/auth_tests/test_forms.py
index c469201408..2771c09111 100644
--- a/tests/auth_tests/test_forms.py
+++ b/tests/auth_tests/test_forms.py
@@ -278,8 +278,11 @@ class SetPasswordFormTest(TestDataMixin, TestCase):
form = SetPasswordForm(user, data)
self.assertFalse(form.is_valid())
self.assertEqual(len(form["new_password2"].errors), 2)
- self.assertTrue('The password is too similar to the username.' in form["new_password2"].errors)
- self.assertTrue('This password is too short. It must contain at least 12 characters.' in form["new_password2"].errors)
+ self.assertIn('The password is too similar to the username.', form["new_password2"].errors)
+ self.assertIn(
+ 'This password is too short. It must contain at least 12 characters.',
+ form["new_password2"].errors
+ )
@override_settings(USE_TZ=False, PASSWORD_HASHERS=['django.contrib.auth.hashers.SHA1PasswordHasher'])
diff --git a/tests/auth_tests/test_validators.py b/tests/auth_tests/test_validators.py
index 543e520e8f..3822c9c675 100644
--- a/tests/auth_tests/test_validators.py
+++ b/tests/auth_tests/test_validators.py
@@ -12,6 +12,7 @@ from django.contrib.auth.password_validation import (
)
from django.core.exceptions import ValidationError
from django.test import TestCase, override_settings
+from django.utils._os import upath
@override_settings(AUTH_PASSWORD_VALIDATORS=[
@@ -43,10 +44,12 @@ class PasswordValidationTest(TestCase):
with self.assertRaises(ValidationError, args=['This password is too short.']) as cm:
validate_password('django4242')
self.assertEqual(cm.exception.messages, [msg_too_short])
+ self.assertEqual(cm.exception.error_list[0].code, 'password_too_short')
with self.assertRaises(ValidationError) as cm:
validate_password('password')
self.assertEqual(cm.exception.messages, ['This password is too common.', msg_too_short])
+ self.assertEqual(cm.exception.error_list[0].code, 'password_too_common')
self.assertIsNone(validate_password('password', password_validators=[]))
@@ -56,14 +59,14 @@ class PasswordValidationTest(TestCase):
def test_password_validators_help_texts(self):
help_texts = password_validators_help_texts()
self.assertEqual(len(help_texts), 2)
- self.assertTrue('12 characters' in help_texts[1])
+ self.assertIn('12 characters', help_texts[1])
self.assertEqual(password_validators_help_texts(password_validators=[]), [])
def test_password_validators_help_text_html(self):
help_text = password_validators_help_text_html()
self.assertEqual(help_text.count('<li>'), 2)
- self.assertTrue('12 characters' in help_text)
+ self.assertIn('12 characters', help_text)
class MinimumLengthValidatorTest(TestCase):
@@ -75,6 +78,7 @@ class MinimumLengthValidatorTest(TestCase):
with self.assertRaises(ValidationError) as cm:
MinimumLengthValidator().validate('1234567')
self.assertEqual(cm.exception.messages, [expected_error % 8])
+ self.assertEqual(cm.exception.error_list[0].code, 'password_too_short')
with self.assertRaises(ValidationError) as cm:
MinimumLengthValidator(min_length=3).validate('12')
@@ -100,13 +104,17 @@ class UserAttributeSimilarityValidatorTest(TestCase):
with self.assertRaises(ValidationError) as cm:
UserAttributeSimilarityValidator().validate('testclient', user=user),
self.assertEqual(cm.exception.messages, [expected_error % "username"])
+ self.assertEqual(cm.exception.error_list[0].code, 'password_too_similar')
with self.assertRaises(ValidationError) as cm:
UserAttributeSimilarityValidator().validate('example.com', user=user),
self.assertEqual(cm.exception.messages, [expected_error % "email address"])
with self.assertRaises(ValidationError) as cm:
- UserAttributeSimilarityValidator(user_attributes=['first_name'], max_similarity=0.3).validate('testclient', user=user),
+ UserAttributeSimilarityValidator(
+ user_attributes=['first_name'],
+ max_similarity=0.3,
+ ).validate('testclient', user=user)
self.assertEqual(cm.exception.messages, [expected_error % "first name"])
self.assertIsNone(
@@ -130,7 +138,7 @@ class CommonPasswordValidatorTest(TestCase):
self.assertEqual(cm.exception.messages, [expected_error])
def test_validate_custom_list(self):
- path = os.path.dirname(os.path.realpath(__file__)) + '/common-passwords-custom.txt'
+ path = os.path.join(os.path.dirname(os.path.realpath(upath(__file__))), 'common-passwords-custom.txt')
validator = CommonPasswordValidator(password_list_path=path)
expected_error = "This password is too common."
self.assertIsNone(validator.validate('a-safe-password'))
@@ -138,6 +146,7 @@ class CommonPasswordValidatorTest(TestCase):
with self.assertRaises(ValidationError) as cm:
validator.validate('from-my-custom-list')
self.assertEqual(cm.exception.messages, [expected_error])
+ self.assertEqual(cm.exception.error_list[0].code, 'password_too_common')
def test_help_text(self):
self.assertEqual(
@@ -154,6 +163,7 @@ class NumericPasswordValidatorTest(TestCase):
with self.assertRaises(ValidationError) as cm:
NumericPasswordValidator().validate('42424242')
self.assertEqual(cm.exception.messages, [expected_error])
+ self.assertEqual(cm.exception.error_list[0].code, 'password_entirely_numeric')
def test_help_text(self):
self.assertEqual(