-
Notifications
You must be signed in to change notification settings - Fork 0
/
password_validator.py
67 lines (51 loc) · 1.76 KB
/
password_validator.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
"""
Write a Python Program to generate a logic for Password validator
1. Using Classes and Objects
2. Log all the Events
3. Write Positive and Negative TestCases
"""
#%% password.py
from getpass import getpass
import logging
import re
class PasswdValidate:
def __init__(self, passwd):
self.passwd = passwd
def minchar(self, min=8):
return(len(self.passwd) >= min)
def alphanum(self):
if re.search('[\d]+', self.passwd):
if re.search('[A-Z,a-z]', self.passwd):
return True
return False
def special(self):
if re.search('[\W]', self.passwd):
return True
return False
if __name__ == '__main__':
logging.basicConfig(filename='passwd.debug', level=logging.DEBUG)
password = getpass()
_password=PasswdValidate(password)
if not _password.minchar():
logging.info("Password is of less than 8 character")
if not _password.alphanum():
logging.info("Password does not have alpha numeric character")
if not _password.special():
logging.info("Password does not have special character")
#%% testunit
import unittest
from password import PasswdValidate
good_password=PasswdValidate("test@123$%")
bad_password=PasswdValidate("abcxyz")
class PasswdValidateTest(unittest.TestCase):
def test_minchar(self):
self.assertEqual(True, good_password.minchar())
self.assertEqual(False, bad_password.minchar())
def test_alphanum(self):
self.assertEqual(True, good_password.alphanum())
self.assertEqual(False, bad_password.alphanum())
def test_special(self):
self.assertEqual(True, good_password.special())
self.assertEqual(False, bad_password.special())
if __name__ == '__main__':
unittest.main()