Adding initial SMTPCheckerTest tests

This commit is contained in:
László Károlyi 2021-03-01 18:09:43 +01:00
parent 5e84348d9b
commit 09e5e56d48
Signed by: karolyi
GPG Key ID: 2DCAF25E55735BFE
1 changed files with 39 additions and 3 deletions

View File

@ -1,3 +1,4 @@
from smtplib import SMTP, SMTPResponseException
from types import SimpleNamespace
from unittest.case import TestCase
from unittest.mock import Mock, patch
@ -6,8 +7,11 @@ 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_cleaned_mx_records, mx_check
from validate_email.exceptions import (
DNSTimeoutError, NoValidMXError, SMTPCommunicationError, SMTPMessage,
SMTPTemporaryError)
from validate_email.mx_check import (
_get_cleaned_mx_records, _SMTPChecker, mx_check)
class DnsNameStub(object):
@ -66,10 +70,42 @@ class GetMxRecordsTestCase(TestCase):
_get_cleaned_mx_records(domain='testdomain3', timeout=10)
self.assertTupleEqual(exc.exception.args, ())
@patch.object(target=mx_module._SMTPChecker, attribute='check')
@patch.object(target=_SMTPChecker, attribute='check')
def test_skip_smtp_argument(self, check_mx_records_mock):
'Check correct work of `skip_smtp` argument.'
self.assertTrue(mx_check(
EmailAddress('test@mail.ru'), debug=False, skip_smtp=True))
self.assertEqual(check_mx_records_mock.call_count, 0)
check_mx_records_mock.call_count
class SMTPCheckerTest(TestCase):
'Checking the `_SMTPChecker` class functions.'
@patch.object(target=SMTP, attribute='connect')
def test_connect_raises_serverdisconnected(self, mock_connect):
'Connect raises `SMTPServerDisconnected`.'
mock_connect.side_effect = OSError('test message')
checker = _SMTPChecker(
local_hostname='localhost', timeout=5, debug=False,
sender='test@example.com', recip='test@example.com')
with self.assertRaises(SMTPCommunicationError) as exc:
checker.check(hosts=['testhost'])
self.assertDictEqual(exc.exception.error_messages, {
'testhost': SMTPMessage(
command='connect', code=0, text='test message')
})
@patch.object(target=SMTP, attribute='connect')
def test_connect_with_error(self, mock_connect):
'Connect raises `SMTPServerDisconnected`.'
checker = _SMTPChecker(
local_hostname='localhost', timeout=5, debug=False,
sender='test@example.com', recip='test@example.com')
mock_connect.return_value = (400, b'test delay message')
with self.assertRaises(SMTPTemporaryError) as exc:
checker.check(hosts=['testhost'])
self.assertDictEqual(exc.exception.error_messages, {
'testhost': SMTPMessage(
command='connect', code=400, text='test delay message')
})