# -*- coding: utf-8 -*- from django.core.exceptions import ValidationError from django.test import SimpleTestCase from ..validators import PasswordScoreValidator, CustomWordlistPasswordValidator, CharacterClassPasswordValidator class PasswordScoreValidatorTestCase(SimpleTestCase): def test_too_little_score(self): passwords = [ u'', # score = 0 u'abcdefghijklmnopq', # score = 17 u'Abcdefghijklmnop', # score = 16 + 1 u'ABcdefghijklmno', # score = 15 + 2 u'AB1defghijklmn', # score = 14 + 2 + 1 u'AB12efghijklm', # score = 13 + 2 + 2 u'AB12+fghijkl', # score = 12 + 2 + 2 + 1 u'AB12+-ghijk', # score = 11 + 2 + 2 + 2 u'AB12+-*hij', # score = 10 + 2 + 2 + 3 u'AB12+-*hi/', # score = 10 + 2 + 2 + 3 u'AB12+-*hi3', # score = 10 + 2 + 2 + 3 u'AB12+-*hiC', # score = 10 + 2 + 2 + 3 u'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(u'%s: no validation error was raised' % password) def test_too_few_classes(self): passwords = [ u'', # classes = 0 u'abcdefgh', # classes = 1 u'abcdef+-', # classes = 2 u'abcd12gh', # classes = 2 u'abcd12-+', # classes = 3 u'abCDefgh', # classes = 2 u'abCDef+-', # classes = 3 u'abCD12gh', # classes = 3 u'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(u'%s: no validation error was raised' % password) def test_valid(self): passwords = [ u'Abcdefghijklmnopq', u'ABcdefghijklmnop', u'AB1defghijklmno', u'AB12efghijklmn', u'AB12+fghijklm', u'AB12+-ghijkl', u'AB12+-*hijk', u'ab1defghijklmnopq', u'abcd+fghijklmnopq', u'AB1CDEFGHIJKLMNOPQ', ] validator = PasswordScoreValidator() for password in passwords: try: validator.validate(password) except ValidationError as e: self.fail(e) class CustomWordlistPasswordValidatorTestCase(SimpleTestCase): def test_invalid(self): invalid_passwords = [ (u'passwort', [ u'The password must not contain the word \'passwort\'', ]), (u'abcdDaVefgh', [ u'The password must not contain the word \'dav\'', ]), (u'abcdsektIonefgh', [ u'The password must not contain the word \'sektion\'', ]), (u'alpen12verein34KArlsruhE berge', [ u'The password must not contain the word \'karlsruhe\'', u'The password must not contain the word \'berge\'', ]), (u'heinzel@alpenverein-karlsruhe.de', [ u'The password must not contain the word \'heinzel\'', u'The password must not contain the word \'alpenverein\'', u'The password must not contain the word \'karlsruhe\'', ]), ] validator = CustomWordlistPasswordValidator() for password, expected_errors in invalid_passwords: try: 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(u'%s: no validation error was raised' % password) def test_valid(self): passwords = [ u'', u'password', u'münchen', ] validator = CustomWordlistPasswordValidator() 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'', [ 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', ]), (u'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', ]), (u'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', ]), (u'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', ]), (u'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', ]), (u'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', ]), (u'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', ]), (u'8GH?!8', [ u'The password must contain at least 2 characters from a-z', ]), (u'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', ]), (u'$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', ]), (u'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', ]), (u'1g=h3~', [ u'The password must contain at least 2 characters from A-Z', ]), (u'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', ]), (u'IkK:i;', [ u'The password must contain at least 2 digits from 0-9', ]), (u'mKn4L8', [ u'The password must contain at least 2 non alpha numeric characters', ]), ] validator = self.validator for password, expected_errors in invalid_passwords: try: 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(u'%s: no validation error was raised' % password) def test_valid(self): valid_passwords = [u'abCD12+-'] validator = self.validator for password in valid_passwords: try: validator.validate(password) except ValidationError as e: self.fail(e)