Skip to content

Commit

Permalink
Merge pull request #67 from upakarp/valid_email_password
Browse files Browse the repository at this point in the history
Check valid email and password challenge
  • Loading branch information
ademclk authored Aug 28, 2022
2 parents 82603c7 + bceb3e1 commit 24a4993
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
63 changes: 63 additions & 0 deletions challenges/valid_email_password/test_valid_email_password.py
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()
41 changes: 41 additions & 0 deletions challenges/valid_email_password/valid_email_password.py
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())

0 comments on commit 24a4993

Please sign in to comment.