py3-validate-email/tests/test_mx_check.py

107 lines
3.9 KiB
Python
Raw Normal View History

2019-04-03 21:49:22 +02:00
from types import SimpleNamespace
from unittest.case import TestCase
from unittest.mock import Mock, patch
2019-05-25 13:54:20 +02:00
2019-05-25 13:51:02 +02:00
from dns.exception import Timeout
2019-03-01 23:29:01 +01:00
from validate_email import mx_check as mx_module
2020-04-09 00:35:51 +02:00
from validate_email.exceptions import (
AddressFormatError, DNSTimeoutError, NoValidMXError)
2019-04-03 21:49:22 +02:00
from validate_email.mx_check import (
2019-05-03 18:44:44 +02:00
_dissect_email, _get_idna_address, _get_mx_records)
2018-06-01 14:29:44 +02:00
DOMAINS = {
2019-03-01 21:49:39 +01:00
'email@domain.com': 'domain.com',
'email@subdomain.domain.com': 'subdomain.domain.com',
'email@123.123.123.123': '123.123.123.123',
'email@[123.123.123.123]': '123.123.123.123',
'email@domain-one.com': 'domain-one.com',
'email@domain.co.jp': 'domain.co.jp',
2018-06-01 14:29:44 +02:00
}
class DnsNameStub(object):
'Stub for `dns.name.Name`.'
def __init__(self, value: str):
self.value = value
def to_text(self) -> str:
return self.value
TEST_QUERY = Mock()
class DomainTestCase(TestCase):
def test_domain_from_email_address(self):
for address, domain in DOMAINS.items():
2019-05-03 18:44:44 +02:00
_user, domain_from_function = _dissect_email(email_address=address)
self.assertEqual(domain_from_function, domain)
2019-05-03 18:44:44 +02:00
class IdnaTestCase(TestCase):
'Testing IDNA converting.'
def test_resolves_idna_domains(self):
'Resolves email@motörhéád.com.'
self.assertEqual(
first=_get_idna_address(email_address='email@motörhéád.com'),
second='email@xn--motrhd-tta7d3f.com')
def test_resolves_conventional_domains(self):
'Resolves email@address.com.'
self.assertEqual(
first=_get_idna_address(email_address='email@address.com'),
second='email@address.com')
2019-04-03 21:49:22 +02:00
class GetMxRecordsTestCase(TestCase):
'Testing `_get_mx_records`.'
@patch.object(target=mx_module, attribute='query', new=TEST_QUERY)
def test_fails_with_invalid_hostnames(self):
'Fails when an MX hostname is "."'
TEST_QUERY.return_value = [
SimpleNamespace(exchange=DnsNameStub(value='.'))]
with self.assertRaises(NoValidMXError) as exc:
2019-05-25 13:51:02 +02:00
_get_mx_records(domain='testdomain1', timeout=10)
self.assertTupleEqual(exc.exception.args, ())
@patch.object(target=mx_module, attribute='query', new=TEST_QUERY)
def test_fails_with_null_hostnames(self):
'Fails when an MX hostname is invalid.'
TEST_QUERY.return_value = [
SimpleNamespace(exchange=DnsNameStub(value='asdqwe'))]
with self.assertRaises(NoValidMXError) as exc:
2019-05-25 13:51:02 +02:00
_get_mx_records(domain='testdomain2', timeout=10)
self.assertTupleEqual(exc.exception.args, ())
@patch.object(target=mx_module, attribute='query', new=TEST_QUERY)
def test_filters_out_invalid_hostnames(self):
'Returns only the valid hostnames.'
TEST_QUERY.return_value = [
SimpleNamespace(exchange=DnsNameStub(value='asdqwe.')),
SimpleNamespace(exchange=DnsNameStub(value='.')),
SimpleNamespace(exchange=DnsNameStub(value='valid.host.')),
SimpleNamespace(exchange=DnsNameStub(value='valid2.host.')),
]
2019-05-25 13:51:02 +02:00
result = _get_mx_records(domain='testdomain3', timeout=10)
self.assertListEqual(result, ['valid.host.', 'valid2.host.'])
2019-05-25 13:51:02 +02:00
@patch.object(target=mx_module, attribute='query', new=TEST_QUERY)
def test_raises_exception_on_dns_timeout(self):
'Raises exception on DNS timeout.'
2019-05-25 13:51:02 +02:00
TEST_QUERY.side_effect = Timeout()
with self.assertRaises(DNSTimeoutError) as exc:
2019-05-25 13:51:02 +02:00
_get_mx_records(domain='testdomain3', timeout=10)
self.assertTupleEqual(exc.exception.args, ())
2019-06-26 14:31:52 +02:00
def test_returns_false_on_idna_failure(self):
'Returns `False` on IDNA failure.'
with self.assertRaises(AddressFormatError) as exc:
2020-04-09 00:35:51 +02:00
mx_module.mx_check(
email_address='test@♥web.de', from_address='mail@example.com')
self.assertTupleEqual(exc.exception.args, ())