-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkpass_test.py
76 lines (60 loc) · 2.67 KB
/
checkpass_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
import hashlib
import unittest
from checkpass import read_file, request_api_data, get_password_leaks_count, check_pwned_api
class CheckPassTest(unittest.TestCase):
def test_read_file_correct_path(self):
path = 'passwords.txt'
with open(path, 'w') as file:
file.write("password1\njasdh1234\ntest12\n")
result = read_file(path)
expected_passwords = ['password1', 'jasdh1234', 'test12']
self.assertEqual(result, expected_passwords)
def test_read_file_empty(self):
path = 'empty.txt'
with open(path, 'w') as file:
file.write("")
result = read_file(path)
self.assertTrue(len(result) == 0)
def test_read_file_invalid_path(self):
with self.assertRaises(FileNotFoundError) as err:
read_file("non_existing_file.txt")
self.assertTrue("Check the file path." in str(err.exception))
def test_request_api_data_hashed(self):
hash_code = '8B673'
expected_code = 200
actual = request_api_data(hash_code)
self.assertEqual(actual.status_code, expected_code)
def test_request_api_data_wrong_input(self):
input_data = 'abc'
with self.assertRaises(RuntimeError) as err:
request_api_data(input_data)
def test_get_password_leaks_count_weak_pass(self):
pass_to_check = 'hello'
sha1_password = hashlib.sha1(pass_to_check.encode('utf-8')).hexdigest().upper()
first5_char, tail = sha1_password[:5], sha1_password[5:]
response = request_api_data(first5_char)
count = int(get_password_leaks_count(response, tail))
self.assertTrue(count > 0)
def test_get_password_leaks_count_strong_pass(self):
pass_to_check = '612*&&%%BC012?#'
sha1_password = hashlib.sha1(pass_to_check.encode('utf-8')).hexdigest().upper()
first5_char, tail = sha1_password[:5], sha1_password[5:]
response = request_api_data(first5_char)
count = int(get_password_leaks_count(response, tail))
self.assertTrue(count == 0)
def test_check_pwned_api_weak_pass(self):
pass_to_check = 'test123'
count = int(check_pwned_api(pass_to_check))
self.assertTrue(count > 0)
def test_check_pwned_api_strong_pass(self):
pass_to_check = '&%&12(^t3$T)'
count = int(check_pwned_api(pass_to_check))
self.assertEqual(count, 0)
def test_check_pwned_api_empty_pass(self):
with self.assertRaises(AttributeError) as err:
check_pwned_api('')
def test_check_pwned_api_None_pass(self):
with self.assertRaises(AttributeError) as err:
check_pwned_api(None)
if __name__ == '__main__':
unittest.main()