Modify requirements (+travis), refactor mutual code

This commit is contained in:
László Károlyi 2019-11-24 11:59:28 +01:00
parent 193788fea3
commit 908fd4d74e
Signed by untrusted user: karolyi
GPG Key ID: 2DCAF25E55735BFE
7 changed files with 31 additions and 27 deletions

View File

@ -11,6 +11,7 @@ python:
# command to install dependencies
install:
- pip install -U pip wheel setuptools
- pip install -U isort flake8
- pip install -r requirements.txt
- pip install -e .

View File

@ -1,4 +1,2 @@
dnspython==1.16.0
idna==2.8
isort==4.3.21
flake8==3.7.8

View File

@ -41,7 +41,7 @@ class BlacklistCheckTestCase(TestCase):
dl.run()
domainlist_check._load_builtin_blacklist()
self.assertFalse(expr=domainlist_check(
email_address='pa2@mailinator.com'))
user_part='pa2', domain_part='mailinator.com'))
self.assertFalse(expr=validate_email(
email_address='pa2@mailinator.com', check_regex=False,
use_blacklist=True))
@ -52,9 +52,5 @@ class BlacklistCheckTestCase(TestCase):
def test_blacklist_negative(self):
'Allows a domain not in the blacklist.'
self.assertTrue(expr=domainlist_check(
email_address='pa2@some-random-domain-thats-not-blacklisted.com'))
def test_erroneous_email(self):
'Will reject emails in erroneous format.'
self.assertFalse(expr=domainlist_check(
email_address='pa2-mailinator.com'))
user_part='pa2',
domain_part='some-random-domain-thats-not-blacklisted.com'))

View File

@ -1,6 +1,7 @@
from unittest.case import TestCase
from validate_email.regex_check import regex_check
from validate_email.validate_email import validate_email
VALID_EXAMPLES = [
'email@domain.com', # basic valid email
@ -19,11 +20,9 @@ VALID_EXAMPLES = [
]
INVALID_EXAMPLES = [
'plainaddress', # missing @ sign and domain
'#@%^%#$@#$@#.com', # garbage
'@domain.com', # missing username
'Joe Smith <email@domain.com>', # encoded html within email is invalid
'email.domain.com', # missing @
'email@domain@domain.com', # two @ sign
'.email@domain.com', # leading dot in address is not allowed
'email.@domain.com', # trailing dot in address is not allowed
@ -35,17 +34,30 @@ INVALID_EXAMPLES = [
'email@domain..com', # multiple dot in the domain portion is invalid
]
UNPARSEABLE_EXAMPLES = [
'plainaddress', # missing @ sign and domain
'email.domain.com', # missing @
]
class RegexTest(TestCase):
def test_valid_email_structure_regex(self):
'Accepts an email with a valid structure.'
for address in VALID_EXAMPLES:
user_part, domain_part = address.rsplit('@', 1)
self.assertTrue(
expr=regex_check(address),
expr=regex_check(user_part=user_part, domain_part=domain_part),
msg=f'Check is not true with {address}')
def test_invalid_email_structure_regex(self):
'Rejects an email with an invalid structure.'
for address in INVALID_EXAMPLES:
user_part, domain_part = address.rsplit('@', 1)
self.assertFalse(
expr=regex_check(address),
expr=regex_check(user_part=user_part, domain_part=domain_part),
msg=f'Check is true with {address}')
def test_unparseable_email(self):
'Rejects an unparseable email.'
for address in UNPARSEABLE_EXAMPLES:
self.assertFalse(expr=validate_email(email_address=address))

View File

@ -29,13 +29,8 @@ class DomainListValidator(object):
self.domain_blacklist = \
set(x.strip().lower() for x in lines if x.strip())
def __call__(self, email_address: str) -> bool:
def __call__(self, user_part: str, domain_part: str) -> bool:
'Do the checking here.'
if not email_address or '@' not in email_address:
return False
user_part, domain_part = email_address.rsplit('@', 1)
if domain_part in self.domain_whitelist:
return True
if domain_part in self.domain_blacklist:

View File

@ -34,12 +34,9 @@ def _validate_ipv46_address(value: str) -> bool:
class RegexValidator(object):
'Slightly adjusted email regex checker from the Django project.'
def __call__(self, email_address: str, use_blacklist: bool = True) -> bool:
if not email_address or '@' not in email_address:
return False
user_part, domain_part = email_address.rsplit('@', 1)
def __call__(
self, user_part: str, domain_part: str,
use_blacklist: bool = True) -> bool:
if not USER_REGEX.match(user_part):
return False

View File

@ -16,9 +16,14 @@ def validate_email(
Return `None` if the result is ambigious.
"""
if check_regex and not regex_check(email_address=email_address):
if not email_address or '@' not in email_address:
return False
if use_blacklist and not domainlist_check(email_address=email_address):
user_part, domain_part = email_address.rsplit('@', 1)
if check_regex and \
not regex_check(user_part=user_part, domain_part=domain_part):
return False
if use_blacklist and \
not domainlist_check(user_part=user_part, domain_part=domain_part):
return False
if not check_mx:
return True