Validating a password with a regular expression¶
A valid password here must contain at least one lowercase letter, one uppercase letter, one digit and one special character, and be 7-20 characters long. Each rule is a separate lookahead.
In [1]:
Copied!
import re
reg = r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!#%*?&]{7,20}$"
def validate_pw(password):
"""Return True if the password satisfies every rule above."""
return re.search(re.compile(reg), password) is not None
import re reg = r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!#%*?&]{7,20}$" def validate_pw(password): """Return True if the password satisfies every rule above.""" return re.search(re.compile(reg), password) is not None
Each test below pins down one rule.
In [2]:
Copied!
import unittest
class MyTestClass(unittest.TestCase):
def test_valid_password(self):
self.assertTrue(validate_pw("Abc123@"))
def test_too_short_password(self):
self.assertFalse(validate_pw("Abc12@"))
def test_too_long_password(self):
self.assertFalse(validate_pw("Abc123@Abc123@Abc123@"))
def test_no_lowercase_character(self):
self.assertFalse(validate_pw("ABC123@"))
def test_no_uppercase_character(self):
self.assertFalse(validate_pw("abc123@"))
def test_no_digit(self):
self.assertFalse(validate_pw("Abcdef@"))
def test_no_special_character(self):
self.assertFalse(validate_pw("Abc1234"))
def test_empty_string(self):
self.assertFalse(validate_pw(""))
loader = unittest.TestLoader()
test_suite = loader.loadTestsFromTestCase(MyTestClass)
result = unittest.TextTestRunner(verbosity=2).run(test_suite)
assert result.wasSuccessful(), "password validation tests failed"
import unittest class MyTestClass(unittest.TestCase): def test_valid_password(self): self.assertTrue(validate_pw("Abc123@")) def test_too_short_password(self): self.assertFalse(validate_pw("Abc12@")) def test_too_long_password(self): self.assertFalse(validate_pw("Abc123@Abc123@Abc123@")) def test_no_lowercase_character(self): self.assertFalse(validate_pw("ABC123@")) def test_no_uppercase_character(self): self.assertFalse(validate_pw("abc123@")) def test_no_digit(self): self.assertFalse(validate_pw("Abcdef@")) def test_no_special_character(self): self.assertFalse(validate_pw("Abc1234")) def test_empty_string(self): self.assertFalse(validate_pw("")) loader = unittest.TestLoader() test_suite = loader.loadTestsFromTestCase(MyTestClass) result = unittest.TextTestRunner(verbosity=2).run(test_suite) assert result.wasSuccessful(), "password validation tests failed"
test_empty_string (__main__.MyTestClass.test_empty_string) ...
ok
test_no_digit (__main__.MyTestClass.test_no_digit) ...
ok
test_no_lowercase_character (__main__.MyTestClass.test_no_lowercase_character) ...
ok
test_no_special_character (__main__.MyTestClass.test_no_special_character) ...
ok
test_no_uppercase_character (__main__.MyTestClass.test_no_uppercase_character) ...
ok
test_too_long_password (__main__.MyTestClass.test_too_long_password) ...
ok
test_too_short_password (__main__.MyTestClass.test_too_short_password) ...
ok
test_valid_password (__main__.MyTestClass.test_valid_password) ...
ok
---------------------------------------------------------------------- Ran 8 tests in 0.010s OK