|
| 1 | +from django.core.exceptions import ValidationError |
| 2 | + |
| 3 | +from nav.web.auth.password_validation import CompositionValidator |
| 4 | + |
| 5 | + |
| 6 | +def test_init_with_no_args_builds_default_check_mapping(): |
| 7 | + default_check_mapping = { |
| 8 | + 'min_numeric': { |
| 9 | + 'required': 1, |
| 10 | + 'help_text': '1 digit', |
| 11 | + }, |
| 12 | + 'min_upper': { |
| 13 | + 'required': 1, |
| 14 | + 'help_text': '1 uppercase letter', |
| 15 | + }, |
| 16 | + 'min_lower': { |
| 17 | + 'required': 1, |
| 18 | + 'help_text': '1 lowercase letter', |
| 19 | + }, |
| 20 | + 'min_special': { |
| 21 | + 'required': 1, |
| 22 | + 'help_text': ( |
| 23 | + '1 special character from the following: %s' |
| 24 | + % CompositionValidator.DEFAULT_SPECIAL_CHARACTERS |
| 25 | + ), |
| 26 | + }, |
| 27 | + } |
| 28 | + cv = CompositionValidator() |
| 29 | + assert cv.check_mapping == default_check_mapping, 'Check mapping was built wrong' |
| 30 | + |
| 31 | + |
| 32 | +def test_init_with_int_args_as_zero_builds_empty_check_mapping(): |
| 33 | + cv = CompositionValidator(min_numeric=0, min_upper=0, min_lower=0, min_special=0) |
| 34 | + assert cv.check_mapping == {}, 'Check mapping is not empty' |
| 35 | + |
| 36 | + |
| 37 | +def test_init_with_empty_special_characters_menas_nop_special_check(): |
| 38 | + cv = CompositionValidator(special_characters="") |
| 39 | + assert ( |
| 40 | + 'min_special' not in cv.check_mapping |
| 41 | + ), "Check mapping was built wrong, special check should not have been included" |
| 42 | + |
| 43 | + |
| 44 | +def test_get_help_text_with_one_required_check_does_not_contain_and_or_comma(): |
| 45 | + cv = CompositionValidator(min_upper=0, min_lower=0, min_special=0) |
| 46 | + help_text = cv.get_help_text() |
| 47 | + assert 'and' not in help_text, 'Help text for a single check is wrong, has "and"' |
| 48 | + assert ',' not in help_text, 'Help text for a single check is wrong, has comma' |
| 49 | + |
| 50 | + |
| 51 | +def test_get_help_text_with_two_or_more_required_check_always_contains_and_and_may_contain_comma(): |
| 52 | + cv = CompositionValidator(min_lower=0, min_special=0) |
| 53 | + help_text = cv.get_help_text() |
| 54 | + assert 'and' in help_text, 'Help text for two checks is wrongi, lacks "and"' |
| 55 | + cv = CompositionValidator(min_special=0) |
| 56 | + help_text = cv.get_help_text() |
| 57 | + assert 'and' in help_text, 'Help text for three checks is wrong, lacks "and"' |
| 58 | + assert ',' in help_text, 'Help text for three checks is wrong, lacks comma' |
| 59 | + cv = CompositionValidator() |
| 60 | + help_text = cv.get_help_text() |
| 61 | + assert 'and' in help_text, 'Help text for four checks is wrong' |
| 62 | + assert ',' in help_text, 'Help text for four checks is wrong, lacks comma' |
| 63 | + |
| 64 | + |
| 65 | +def test_validate_with_correct_password_returns_None(): |
| 66 | + cv = CompositionValidator(min_upper=0, min_lower=0, min_special=0) |
| 67 | + result = cv.validate("42") |
| 68 | + assert result is None, "The password did not validate but should" |
| 69 | + |
| 70 | + |
| 71 | +def test_validate_with_incorrect_password_returns_ValidationError_with_error_message(): |
| 72 | + cv = CompositionValidator(min_upper=0, min_lower=0, min_special=0) |
| 73 | + try: |
| 74 | + cv.validate("") |
| 75 | + except ValidationError as e: |
| 76 | + expected_error = 'Invalid password, must have at least 1 digit' |
| 77 | + assert ( |
| 78 | + e.message == expected_error |
| 79 | + ), "Error message of incorrect password was wrong" |
| 80 | + else: |
| 81 | + assert False, "Incorrect password did not raise ValidationError" |
0 commit comments