2019-03-01 23:01:21 +01:00
|
|
|
from unittest.case import TestCase
|
2019-03-01 20:37:52 +01:00
|
|
|
|
2020-04-08 23:56:49 +02:00
|
|
|
from validate_email.exceptions import AddressFormatError
|
2019-03-01 23:29:01 +01:00
|
|
|
from validate_email.regex_check import regex_check
|
2019-11-24 11:59:28 +01:00
|
|
|
from validate_email.validate_email import validate_email
|
2019-03-01 23:29:01 +01:00
|
|
|
|
2019-03-01 23:01:21 +01:00
|
|
|
VALID_EXAMPLES = [
|
2019-03-01 20:37:52 +01:00
|
|
|
'email@domain.com', # basic valid email
|
|
|
|
'firstname.lastname@domain.com', # dot in address field
|
|
|
|
'email@subdomain.domain.com', # dot in subdomain
|
|
|
|
'firstname+lastname@domain.com', # + in address field
|
|
|
|
'email@123.123.123.123', # domain address is IP address
|
|
|
|
'email@[123.123.123.123]', # square brackets around IP address
|
|
|
|
'\'email\'@domain.com', # quote marks in address fields
|
|
|
|
'1234567890@domain.com', # numbers in address field
|
|
|
|
'email@domain-one.com', # dash in subdomain
|
|
|
|
'_______@domain.com', # underscore in address field
|
|
|
|
'email@domain.name', # .name top level domain name
|
|
|
|
'email@domain.co.jp', # dot in top level domain
|
|
|
|
'firstname-lastname@domain.com' # dash in address field
|
|
|
|
]
|
|
|
|
|
2019-03-01 21:49:39 +01:00
|
|
|
INVALID_EXAMPLES = [
|
2019-03-01 20:37:52 +01:00
|
|
|
'#@%^%#$@#$@#.com', # garbage
|
|
|
|
'@domain.com', # missing username
|
|
|
|
'Joe Smith <email@domain.com>', # encoded html within email is invalid
|
|
|
|
'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
|
|
|
|
'email..email@domain.com', # multiple dots
|
|
|
|
'あいうえお@domain.com', # unicode char as address
|
|
|
|
'email@domain.com (Joe Smith)', # text followed email is not allowed
|
|
|
|
'email@domain', # missing top level domain (.com/.net/.org/etc)
|
|
|
|
'email@-domain.com', # leading dash in front of domain is invalid
|
|
|
|
'email@domain..com', # multiple dot in the domain portion is invalid
|
|
|
|
]
|
|
|
|
|
2019-11-24 11:59:28 +01:00
|
|
|
UNPARSEABLE_EXAMPLES = [
|
|
|
|
'plainaddress', # missing @ sign and domain
|
|
|
|
'email.domain.com', # missing @
|
|
|
|
]
|
2019-03-01 20:37:52 +01:00
|
|
|
|
2019-11-24 12:01:58 +01:00
|
|
|
|
|
|
|
class FormatValidity(TestCase):
|
|
|
|
'Testing regex validation + format validity.'
|
2019-03-01 20:37:52 +01:00
|
|
|
|
2019-03-01 23:01:21 +01:00
|
|
|
def test_valid_email_structure_regex(self):
|
2019-11-24 11:59:28 +01:00
|
|
|
'Accepts an email with a valid structure.'
|
2019-03-01 23:01:21 +01:00
|
|
|
for address in VALID_EXAMPLES:
|
2019-11-24 11:59:28 +01:00
|
|
|
user_part, domain_part = address.rsplit('@', 1)
|
2019-03-01 23:01:21 +01:00
|
|
|
self.assertTrue(
|
2019-11-24 11:59:28 +01:00
|
|
|
expr=regex_check(user_part=user_part, domain_part=domain_part),
|
2019-03-01 23:01:21 +01:00
|
|
|
msg=f'Check is not true with {address}')
|
2019-03-01 20:37:52 +01:00
|
|
|
|
2019-03-01 23:01:21 +01:00
|
|
|
def test_invalid_email_structure_regex(self):
|
2019-11-24 11:59:28 +01:00
|
|
|
'Rejects an email with an invalid structure.'
|
2019-03-01 23:01:21 +01:00
|
|
|
for address in INVALID_EXAMPLES:
|
2019-11-24 11:59:28 +01:00
|
|
|
user_part, domain_part = address.rsplit('@', 1)
|
2020-04-08 23:56:49 +02:00
|
|
|
with self.assertRaises(
|
2020-04-10 12:53:10 +02:00
|
|
|
expected_exception=AddressFormatError,
|
|
|
|
msg=f'Test failed for {address}'):
|
2020-04-08 23:56:49 +02:00
|
|
|
regex_check(user_part=user_part, domain_part=domain_part),
|
2019-11-24 11:59:28 +01:00
|
|
|
|
|
|
|
def test_unparseable_email(self):
|
|
|
|
'Rejects an unparseable email.'
|
|
|
|
for address in UNPARSEABLE_EXAMPLES:
|
|
|
|
self.assertFalse(expr=validate_email(email_address=address))
|