Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from testscenarios import TestWithScenarios
from unittest import TestCase
try:
from unittest.mock import patch
except ImportError:
from mock import patch
import dns.resolver
from pyisemail import is_email
from pyisemail.diagnosis import BaseDiagnosis, DNSDiagnosis
from tests.validators import create_diagnosis, get_scenarios
class IsEmailTest(TestWithScenarios):
scenarios = get_scenarios("tests.xml")
threshold = BaseDiagnosis.CATEGORIES['THRESHOLD']
def test_without_diagnosis(self):
result = is_email(self.address)
expected = create_diagnosis(self.diagnosis) < self.threshold
self.assertEqual(
result,
expected,
("%s (%s): Got %s, but expected %s."
% (self.id, self.address, result, expected))
)
def test_with_diagnosis(self):
result = is_email(self.address, diagnose=True)
expected = create_diagnosis(self.diagnosis)
def is_email(address, check_dns=False, diagnose=False):
"""Validate an email address.
Keyword arguments:
address --- the email address as a string
check_dns --- flag for whether to check the DNS status of the domain
diagnose --- flag for whether to return True/False or a Diagnosis
"""
threshold = BaseDiagnosis.CATEGORIES["THRESHOLD"]
d = ParserValidator().is_email(address, True)
if check_dns is True and d < BaseDiagnosis.CATEGORIES["DNSWARN"]:
threshold = BaseDiagnosis.CATEGORIES["VALID"]
d = max(d, DNSValidator().is_valid(address.split("@")[1], True))
return d if diagnose else d < threshold
"""Check that an address address conforms to RFCs 5321, 5322 and others.
More specifically, see the follow RFCs:
* http://tools.ietf.org/html/rfc5321
* http://tools.ietf.org/html/rfc5322
* http://tools.ietf.org/html/rfc4291#section-2.2
* http://tools.ietf.org/html/rfc1123#section-2.1
* http://tools.ietf.org/html/rfc3696) (guidance only)
Keyword arguments:
address -- address to check.
diagnose -- flag to report a diagnosis or a boolean (default False)
"""
threshold = BaseDiagnosis.CATEGORIES['VALID']
return_status = [ValidDiagnosis()]
parse_data = {}
# Parse the address into components, character by character
raw_length = len(address)
context = Context.LOCALPART # Where we are
context_stack = [context] # Where we've been
context_prior = Context.LOCALPART # Where we just came from
token = '' # The current character
token_prior = '' # The previous character
parse_data[Context.LOCALPART] = '' # The address' components
parse_data[Context.DOMAIN] = ''
atom_list = {
Context.LOCALPART: [''],
Context.DOMAIN: ['']
} # The address' dot-atoms
def validate_email(email_string):
d = is_email(email_string, diagnose=True)
if d > BaseDiagnosis.CATEGORIES["VALID"]:
raise SwaggerValidationError(f"{email_string} {d.message}")