-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword_validator_test.py
153 lines (133 loc) · 6.83 KB
/
password_validator_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# coding=utf-8
import unittest
import os.path
import password_validator
class TestPasswordValidator(unittest.TestCase):
"""
Run unit test for password_validator script
"""
def test_constant(self):
"""
Test that constant value are correct
"""
self.assertEqual(password_validator.MIN_PASSWORD_LENGTH, 8)
self.assertEqual(password_validator.MAX_PASSWORD_LENGTH, 64)
self.assertEqual(
password_validator.ERROR_INVALID_INPUT,
('Please check that your input is in the correct format. \n' +
'python password_validator.py input_password.txt commonly_used_password_file.txt')
)
self.assertEqual(password_validator.ERROR_FILE_NOT_FOUND, 'File {} is not found.')
self.assertEqual(password_validator.ERROR_PASSWORD_IS_TOO_SHORT, 'Too Short')
self.assertEqual(password_validator.ERROR_PASSWORD_IS_TOO_LONG, 'Too Long')
self.assertEqual(password_validator.ERROR_PASSWORD_IS_NOT_ASCII, 'Invalid Characters')
self.assertEqual(password_validator.ERROR_PASSWORD_IS_TOO_COMMON, 'Too Common')
self.assertEqual(password_validator.NOTICE_READING_WEAK_PASSWORDS_VALIDATION_FILE, '--- Attempting to read in weak password validation file ---')
self.assertEqual(password_validator.NOTICE_CHECKING_INPUT_PASSWORDS_FILE, '--- Validating input password ---')
self.assertEqual(password_validator.NOTICE_SUMMARY, '--- Printing summary ---')
self.assertEqual(password_validator.NOTICE_COUNT_BAD_PASSWORD, 'Number of bad password: {}')
self.assertEqual(password_validator.NOTICE_COUNT_GOOD_PASSWORD, 'Number of good password: {}')
self.assertEqual(password_validator.NOTICE_COUNT_TOTAL_PASSWORD, 'Number of password found: {}')
def test_validate_input_from_command_line_when_valid_argument_is_passed(self):
"""
Test that validate_input_from_command_line
return True when there are valid number of arguments
"""
is_argument_correct = password_validator.validate_input_from_command_line({'command', 'input.txt', 'weakpass.txt'});
self.assertTrue(is_argument_correct)
def test_validate_input_from_command_line_when_invalid_argument_is_passed(self):
"""
Test that validate_input_from_command_line
return False when there are invalid number of arguments
"""
is_argument_correct = password_validator.validate_input_from_command_line({'command', 'input.txt'});
self.assertFalse(is_argument_correct)
def test_validate_file_exist_when_one_file_does_not_exist(self):
"""
Test that validate_files_exist
return error when one of the file does not exist
"""
os.path.isfile = lambda path: path == 'file2'
is_file_exist = password_validator.validate_files_exist('file1', 'file2')
self.assertEqual(is_file_exist, password_validator.ERROR_FILE_NOT_FOUND.format('file1'))
def test_validate_file_exist_when_both_files_does_not_exist(self):
"""
Test that validate_files_exist
return error when both files does not exist
"""
is_file_exist = password_validator.validate_files_exist('file1', 'file2')
self.assertEqual(
is_file_exist,
password_validator.ERROR_FILE_NOT_FOUND.format('file1') +
'\n' + password_validator.ERROR_FILE_NOT_FOUND.format('file2')
)
def test_validate_password_when_password_is_valid(self):
"""
Test that validate_password
can return true when password is valid
"""
password = 'balloon is wanted'
result = password_validator.validate_password(password, {'balloon'}, True)
self.assertTrue(result)
def test_validate_password_when_password_less_than_minimum(self):
"""
Test that validate_password
can return correct error when password is less than minimum length
"""
password = 'balloon'
result = password_validator.validate_password(password, {}, True)
self.assertEqual(result, password + ' -> Error: ' + password_validator.ERROR_PASSWORD_IS_TOO_SHORT)
def test_validate_password_when_password_greater_than_maximum(self):
"""
Test that validate_password
can return correct error when password is greater than maximum length
"""
password = ('thisisahugerepeatedstringthisisahugerepeatedstringthisisahugerepeatedstringthisisahugerepeatedstr' +
'ingthisisahugerepeatedstringthisisgerepeatedstringthisisahugerepeatedstringthisisahugerepeatedstringthisisahu' +
'gerepeatedstringthisisahugerepeatedstringthisisahugerepeatedstringthisisahugerepeatedstringthisisahugerepeatedstring')
result = password_validator.validate_password(password, {}, True)
self.assertEqual(result, password + ' -> Error: ' + password_validator.ERROR_PASSWORD_IS_TOO_LONG)
def test_validate_password_when_password_is_not_ascii(self):
"""
Test that validate_password
can return correct error when password is not ascii
"""
password = '的中文翻譯 | 英漢字典'
result = password_validator.validate_password(password, {}, True)
self.assertEqual(result, password + ' -> Error: ' + password_validator.ERROR_PASSWORD_IS_NOT_ASCII)
def test_validate_password_when_password_is_common(self):
"""
Test that validate_password
can return correct error when password is common
"""
password = 'this is a common password'
result = password_validator.validate_password(password, {password}, True)
self.assertEqual(result, password + ' -> Error: ' + password_validator.ERROR_PASSWORD_IS_TOO_COMMON)
def test_validate_password_when_there_are_more_than_one_error(self):
"""
Test that validate_password
can return correct errors when there are more than one error
"""
password = '12345'
result = password_validator.validate_password(password, {password}, True)
self.assertEqual(result,
password + ' -> Error: ' + password_validator.ERROR_PASSWORD_IS_TOO_SHORT + ', '
+ password_validator.ERROR_PASSWORD_IS_TOO_COMMON)
def test_validate_ascii_when_password_validate_ascii(self):
"""
Test that validate_ascii
can return True when password is ascii
"""
password = 'this is ascii'
result = password_validator.validate_ascii(password)
self.assertTrue(result)
def test_validate_ascii_when_password_is_not_ascii(self):
"""
Test that validate_ascii
can return False when password is not ascii
"""
password = '英漢字典'
result = password_validator.validate_ascii(password)
self.assertFalse(result)
if __name__ == '__main__':
unittest.main()