-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from upakarp/valid_email_password
Check valid email and password challenge
- Loading branch information
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
challenges/valid_email_password/test_valid_email_password.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('[email protected]') | ||
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('[email protected]') | ||
self.assertFalse(self.valid.validEmail()) | ||
|
||
def test_email_with_domain_name_length_more_than_three(self): | ||
self.valid.set_email('[email protected]') | ||
self.assertFalse(self.valid.validEmail()) | ||
|
||
def test_valid_email(self): | ||
self.valid.set_email('[email protected]') | ||
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() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("[email protected]", "P@55word") | ||
print(validCheck.validEmail()) | ||
print(validCheck.validPassword()) |