Created
June 6, 2018 10:35
-
-
Save suspectpart/df13c65330aa3e77680897b85149962b to your computer and use it in GitHub Desktop.
Python Password Validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import unittest | |
"""Valid passwords | |
- are between 8 and 20 characters long | |
- contain at least one number | |
- have at least one uppercase and one lowercase character | |
- have a special char of !"§$%&/()=? | |
""" | |
def validate(password): | |
length_ok = len(password) in range(8, 21) | |
has_number = any(c.isdigit() for c in password) | |
has_lower = bool(set(password) & set(password.lower())) | |
has_upper = bool(set(password) & set(password.upper())) | |
has_special = bool(set(password) & set('!"§$%&/()=?')) | |
return length_ok and has_number and has_lower and has_upper and has_special | |
class ValidateTests(unittest.TestCase): | |
def test_empty(self): | |
self.assertFalse(validate('')) | |
def test_too_short(self): | |
self.assertFalse(validate('aAbB1!?')) | |
def test_too_long(self): | |
self.assertFalse(validate('aA1!?' * 4 + 'a')) | |
def test_no_number(self): | |
self.assertFalse(validate("aAbBcC!?")) | |
def test_no_upper(self): | |
self.assertFalse(validate("aabbzz()")) | |
def test_no_lower(self): | |
self.assertFalse(validate("%&AABBCC")) | |
def test_no_special(self): | |
self.assertFalse(validate("aAbmMC19")) | |
def test_valid(self): | |
self.assertTrue(validate("a1?mzuF4")) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment