From bceb3e15845da2666c57d1ec582683b3b4c1c0c7 Mon Sep 17 00:00:00 2001 From: upakarp Date: Sat, 27 Aug 2022 15:54:35 -0700 Subject: [PATCH] Check valid email and password challenge --- .../test_valid_email_password.py | 63 +++++++++++++++++++ .../valid_email_password.py | 41 ++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 challenges/valid_email_password/test_valid_email_password.py create mode 100644 challenges/valid_email_password/valid_email_password.py diff --git a/challenges/valid_email_password/test_valid_email_password.py b/challenges/valid_email_password/test_valid_email_password.py new file mode 100644 index 0000000..e1ab948 --- /dev/null +++ b/challenges/valid_email_password/test_valid_email_password.py @@ -0,0 +1,63 @@ +import unittest +from valid_email_password import ValidEmailAndPassword + +class TestValidEmailAndPassword(unittest.TestCase): + @classmethod + def setUpClass(self): + self.valid = ValidEmailAndPassword('', '') + + # Email validation test cases + def test_empty_email(self): + self.valid.set_email('') + self.assertFalse(self.valid.validEmail()) + + def test_email_with_no_username(self): + self.valid.set_email('@company.com') + self.assertFalse(self.valid.validEmail()) + + def test_email_with_no_company_name(self): + self.valid.set_email('username@.com') + self.assertFalse(self.valid.validEmail()) + + def test_email_with_no_domainname(self): + self.valid.set_email('username@company') + self.assertFalse(self.valid.validEmail()) + + def test_email_with_special_characters(self): + self.valid.set_email('!user@company.com') + self.assertFalse(self.valid.validEmail()) + + def test_email_with_domain_name_length_more_than_three(self): + self.valid.set_email('username@company.comcom') + self.assertFalse(self.valid.validEmail()) + + def test_valid_email(self): + self.valid.set_email('username@company.com') + self.assertTrue(self.valid.validEmail()) + + # Password validation test cases + def test_password_length_less_than_minimum(self): + self.valid.set_password('pass') + self.assertFalse(self.valid.validPassword()) + + def test_password_with_no_digit(self): + self.valid.set_password('password') + self.assertFalse(self.valid.validPassword()) + + def test_password_with_no_lowercase(self): + self.valid.set_password('P@55WORD') + self.assertFalse(self.valid.validPassword()) + + def test_password_with_no_uppercase(self): + self.valid.set_password('p@55word') + self.assertFalse(self.valid.validPassword()) + + def test_password_with_no_special_character(self): + self.valid.set_password('Pa55wrd') + self.assertFalse(self.valid.validPassword()) + + def test_valid_password(self): + self.valid.set_password('P@55word') + self.assertTrue(self.valid.validPassword()) + +unittest.main() diff --git a/challenges/valid_email_password/valid_email_password.py b/challenges/valid_email_password/valid_email_password.py new file mode 100644 index 0000000..ed4bced --- /dev/null +++ b/challenges/valid_email_password/valid_email_password.py @@ -0,0 +1,41 @@ +import re +class ValidEmailAndPassword: + def __init__(self, email, password): + self.email = email + self.password = password + + def set_email(self, email): + self.email = email + + def set_password(self, password): + self.password = password + + def validEmail(self): + # Email Regex is extracted from: https://www.tutorialspoint.com/python-program-to-validate-email-address + emailRegex = "^[a-zA-Z0-9-_]+@[a-zA-Z0-9]+\.[a-z]{1,3}$" + if re.match(emailRegex, self.email): + return True + return False + + def validPassword(self): + # Check minimum length + if len(self.password) <= 7: + return False + # Special characters list. More special characters can be added as needed + # Multiple ways to do this, can use regex, set/list. + specialChar = '!@#$%^&*()_-' + + # Check password contains digit, uppercase, lowercase and special character + containsDigit = any(char.isdigit() for char in self.password) + containsUpperCase = any(char.isupper() for char in self.password) + containsLowerCase = any(char.islower() for char in self.password) + + containsSpecialCharacter = any(char in specialChar for char in self.password) + if containsDigit and containsUpperCase and containsLowerCase and containsSpecialCharacter: + return True + return False + +if __name__ == "__main__": + validCheck = ValidEmailAndPassword("username@company.com", "P@55word") + print(validCheck.validEmail()) + print(validCheck.validPassword())