# -*- coding: utf-8 -*- from django.core.exceptions import ValidationError from django.test import SimpleTestCase from ..validators import PasswordScoreValidator, CharacterClassPasswordValidator class PasswordScoreValidatorTestCase(SimpleTestCase): def test_too_little_score(self): passwords = [ '', # score = 0 'abcdefghijklmnopq', # score = 17 'Abcdefghijklmnop', # score = 16 + 1 'ABcdefghijklmno', # score = 15 + 2 'AB1defghijklmn', # score = 14 + 2 + 1 'AB12efghijklm', # score = 13 + 2 + 2 'AB12+fghijkl', # score = 12 + 2 + 2 + 1 'AB12+-ghijk', # score = 11 + 2 + 2 + 2 'AB12+-*hij', # score = 10 + 2 + 2 + 3 'AB12+-*hi/', # score = 10 + 2 + 2 + 3 'AB12+-*hi3', # score = 10 + 2 + 2 + 3 'AB12+-*hiC', # score = 10 + 2 + 2 + 3 'abcbbbgbbjklmnopqr', # score = 17 ] validator = PasswordScoreValidator(min_classes=0) for password in passwords: try: validator.validate(password) except ValidationError as e: self.assertEqual('too_little_score', e.code) else: self.fail('%s: no validation error was raised' % password) def test_too_few_classes(self): passwords = [ '', # classes = 0 'abcdefgh', # classes = 1 'abcdef+-', # classes = 2 'abcd12gh', # classes = 2 'abcd12-+', # classes = 3 'abCDefgh', # classes = 2 'abCDef+-', # classes = 3 'abCD12gh', # classes = 3 'ABCD12-+', # classes = 3 ] validator = PasswordScoreValidator(min_score=0, min_classes=4) for password in passwords: try: validator.validate(password) except ValidationError as e: self.assertEqual('too_few_classes', e.code) else: self.fail('%s: no validation error was raised' % password) def test_valid(self): passwords = [ 'Abcdefghijklmnopq', 'ABcdefghijklmnop', 'AB1defghijklmno', 'AB12efghijklmn', 'AB12+fghijklm', 'AB12+-ghijkl', 'AB12+-*hijk', 'ab1defghijklmnopq', 'abcd+fghijklmnopq', 'AB1CDEFGHIJKLMNOPQ', ] validator = PasswordScoreValidator() for password in passwords: try: validator.validate(password) except ValidationError as e: self.fail(e) class CharacterClassPasswordValidatorTestCase(SimpleTestCase): def setUp(self): super(CharacterClassPasswordValidatorTestCase, self).setUp() self.validator = CharacterClassPasswordValidator() def test_invalid(self): invalid_passwords = [ ('', [ u'The password must contain at least 2 characters from a-z', u'The password must contain at least 2 characters from A-Z', u'The password must contain at least 2 digits from 0-9', u'The password must contain at least 2 non alpha numeric characters', ]), ('A+-', [ u'The password must contain at least 2 characters from a-z', u'The password must contain at least 2 characters from A-Z', u'The password must contain at least 2 digits from 0-9', ]), ('1234567890*', [ u'The password must contain at least 2 characters from a-z', u'The password must contain at least 2 characters from A-Z', u'The password must contain at least 2 non alpha numeric characters', ]), ('34*/()', [ u'The password must contain at least 2 characters from a-z', u'The password must contain at least 2 characters from A-Z', ]), ('AA', [ u'The password must contain at least 2 characters from a-z', u'The password must contain at least 2 digits from 0-9', u'The password must contain at least 2 non alpha numeric characters', ]), ('CD0.,', [ u'The password must contain at least 2 characters from a-z', u'The password must contain at least 2 digits from 0-9', ]), ('EF56', [ u'The password must contain at least 2 characters from a-z', u'The password must contain at least 2 non alpha numeric characters', ]), ('8GH?!8', [ u'The password must contain at least 2 characters from a-z', ]), ('bbX', [ u'The password must contain at least 2 characters from A-Z', u'The password must contain at least 2 digits from 0-9', u'The password must contain at least 2 non alpha numeric characters', ]), ('$cd%', [ u'The password must contain at least 2 characters from A-Z', u'The password must contain at least 2 digits from 0-9', ]), ('ef90', [ u'The password must contain at least 2 characters from A-Z', u'The password must contain at least 2 non alpha numeric characters', ]), ('1g=h3~', [ u'The password must contain at least 2 characters from A-Z', ]), ('Gi&jH', [ u'The password must contain at least 2 digits from 0-9', u'The password must contain at least 2 non alpha numeric characters', ]), ('IkK:i;', [ u'The password must contain at least 2 digits from 0-9', ]), ('mKn4L8', [ u'The password must contain at least 2 non alpha numeric characters', ]), ] for password, expected_errors in invalid_passwords: try: self.validator.validate(password) except ValidationError as e: errors = e.messages for expected_error in expected_errors: self.assertIn(expected_error, errors) for error in errors: self.assertIn(error, expected_errors) else: self.fail('%s: no validation error was raised' % password) def test_valid(self): valid_passwords = ['abCD12+-'] for password in valid_passwords: try: self.validator.validate(password) except ValidationError as e: self.fail(e)