Skip to content

Commit ee7334f

Browse files
author
Matthew Shirtliffe
committed
password strength indicator with tests
1 parent 9e1709f commit ee7334f

File tree

6 files changed

+133
-0
lines changed

6 files changed

+133
-0
lines changed

password_strength_indicator/app.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import re
2+
3+
def print_header():
4+
""" Print header
5+
"""
6+
header_text = ''
7+
TEXT = f' Password Strength Indicator \n'
8+
line = '-' * len(TEXT)
9+
line += '\n'
10+
11+
header_text += line
12+
header_text += TEXT
13+
header_text += line
14+
print(header_text)
15+
16+
17+
def get_password():
18+
19+
while True:
20+
password = input('Enter password: ')
21+
if len(password) >=1:
22+
return password
23+
else:
24+
print('A valid input is required')
25+
26+
def password_validator(password):
27+
28+
special_characters_regex = re.compile('[@_!#$%^&*()<>?/\|}{~:]')
29+
if len(password) < 8 and password.isdigit():
30+
print(f'The password \'{password}\' is a very weak password')
31+
elif len(password) < 8 and password.isalpha():
32+
print(f'The password \'{password}\' is a weak password')
33+
elif len(password) >= 8 and (any(char.isdigit() for char in password) and special_characters_regex.search(password) is None):
34+
print(f'The password \'{password}\' is a strong password')
35+
elif len(password) >= 8 and special_characters_regex.search(password):
36+
print(f'The password \'{password}\' is a very strong password')
37+
38+
def prompt_get_strings():
39+
print('Enter two strings and I\'ll tell you if they\nare anagrams: ')
40+
41+
42+
def get_first_string():
43+
44+
while True:
45+
first_string = input('Enter the first string: ')
46+
47+
if len(first_string) >=3:
48+
return first_string
49+
else:
50+
print('A valid input is required')
51+
52+
53+
def get_second_string():
54+
55+
while True:
56+
second_string = input('Enter the second string: ')
57+
58+
if len(second_string) >=3:
59+
return second_string
60+
else:
61+
print('A valid input is required')
62+
63+
64+
def is_anagram(first_string, second_string):
65+
66+
return sorted(first_string) == sorted(second_string)
67+
68+
69+
def print_is_anagram(first_string, second_string):
70+
if(is_anagram(first_string, second_string)):
71+
print(f'{first_string} and {second_string} are anagrams.')
72+
73+
if __name__ == "__main__":
74+
75+
print_header()
76+
prompt_get_strings()
77+
first_string = get_first_string()
78+
second_string = get_second_string()
79+
print_is_anagram(first_string, second_string)

password_strength_indicator/tests/__init__.py

Whitespace-only changes.

password_strength_indicator/tests/system/__init__.py

Whitespace-only changes.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from unittest import TestCase
2+
from unittest.mock import patch
3+
import app
4+
5+
class AppTest(TestCase):
6+
7+
def test_print_header(self):
8+
9+
expected = '------------------------------\n Password Strength Indicator \n------------------------------\n'
10+
with patch('builtins.print') as mocked_print:
11+
app.print_header()
12+
mocked_print.assert_called_with(expected)
13+
14+
def test_get_first_string(self):
15+
16+
expected = 'Enter password: '
17+
with patch('builtins.input') as mocked_input:
18+
with patch('builtins.print') as mocked_print:
19+
mocked_input.side_effect = ('' , '12345')
20+
password = app.get_password()
21+
mocked_print.assert_called_with('A valid input is required')
22+
mocked_input.assert_called_with(expected)
23+
self.assertEqual(password, '12345')
24+
25+
def test_password_validator(self):
26+
27+
with patch('builtins.print') as mocked_print:
28+
password = '12345'
29+
expected = f'The password \'{password}\' is a very weak password'
30+
app.password_validator(password)
31+
mocked_print.assert_called_with(expected)
32+
33+
password = 'abcdef'
34+
expected = f'The password \'{password}\' is a weak password'
35+
app.password_validator(password)
36+
mocked_print.assert_called_with(expected)
37+
38+
password = 'abcd123xyz'
39+
expected = f'The password \'{password}\' is a strong password'
40+
app.password_validator(password)
41+
mocked_print.assert_called_with(expected)
42+
43+
password = '1337h@xor!'
44+
expected = f'The password \'{password}\' is a very strong password'
45+
app.password_validator(password)
46+
mocked_print.assert_called_with(expected)

password_strength_indicator/tests/unit/__init__.py

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from unittest import TestCase
2+
import app
3+
4+
class PasswordStrengthIndicatorTest(TestCase):
5+
pass
6+
# def test_is_anagram(self):
7+
# is_anagram = app.is_anagram('tone', 'note')
8+
# self.assertTrue(is_anagram)

0 commit comments

Comments
 (0)