Add no_smtp argument that allows us to validate only mx records without SMTP request

This commit is contained in:
Sergey Shevchenko 2021-02-11 17:41:21 +04:00
parent c92bd0c5c0
commit fea879a809
No known key found for this signature in database
GPG Key ID: 356B0EBCAA35DFF7
3 changed files with 19 additions and 6 deletions

View File

@ -5,8 +5,9 @@ from unittest.mock import Mock, patch
from dns.exception import Timeout
from validate_email import mx_check as mx_module
from validate_email.email_address import EmailAddress
from validate_email.exceptions import DNSTimeoutError, NoValidMXError
from validate_email.mx_check import _get_mx_records
from validate_email.mx_check import _get_mx_records, mx_check
class DnsNameStub(object):
@ -62,3 +63,12 @@ class GetMxRecordsTestCase(TestCase):
with self.assertRaises(DNSTimeoutError) as exc:
_get_mx_records(domain='testdomain3', timeout=10)
self.assertTupleEqual(exc.exception.args, ())
@patch.object(target=mx_module, attribute='_check_mx_records')
def test_no_smtp_argument(self, check_mx_records_mock):
'Check correct work of no_smtp argument.'
self.assertTrue(
mx_check(EmailAddress('test@mail.ru'), debug=False, no_smtp=True)
)
self.assertEqual(check_mx_records_mock.call_count, 0)

View File

@ -167,7 +167,7 @@ def mx_check(
email_address: EmailAddress, debug: bool,
from_address: Optional[EmailAddress] = None,
helo_host: Optional[str] = None, smtp_timeout: int = 10,
dns_timeout: int = 10
dns_timeout: int = 10, no_smtp: bool = False
) -> Optional[bool]:
"""
Return `True` if the host responds with a deliverable response code,
@ -181,6 +181,8 @@ def mx_check(
else:
mx_records = _get_mx_records(
domain=email_address.domain, timeout=dns_timeout)
if no_smtp:
return True
return _check_mx_records(
mx_records=mx_records, smtp_timeout=smtp_timeout, helo_host=host,
from_address=from_address, email_address=email_address, debug=debug)

View File

@ -13,9 +13,10 @@ LOGGER = getLogger(name=__name__)
def validate_email_or_fail(
email_address: str, check_regex: bool = True, check_mx: bool = True,
from_address: Optional[str] = None, helo_host: Optional[str] = None,
smtp_timeout: int = 10, dns_timeout: int = 10,
use_blacklist: bool = True, debug: bool = False) -> Optional[bool]:
no_smtp: bool = False, from_address: Optional[str] = None,
helo_host: Optional[str] = None, smtp_timeout: int = 10,
dns_timeout: int = 10, use_blacklist: bool = True, debug: bool = False
) -> Optional[bool]:
"""
Return `True` if the email address validation is successful, `None` if the
validation result is ambigious, and raise an exception if the validation
@ -37,7 +38,7 @@ def validate_email_or_fail(
return mx_check(
email_address=email_address, from_address=from_address,
helo_host=helo_host, smtp_timeout=smtp_timeout,
dns_timeout=dns_timeout, debug=debug)
dns_timeout=dns_timeout, no_smtp=no_smtp, debug=debug)
def validate_email(