| 1 |
""" |
| 2 |
Test error class imports, arguments, and inheritance. |
| 3 |
""" |
| 4 |
|
| 5 |
import asyncio |
| 6 |
|
| 7 |
import pytest |
| 8 |
from hypothesis import given |
| 9 |
from hypothesis.strategies import integers, lists, text, tuples |
| 10 |
|
| 11 |
from aiosmtplib import ( |
| 12 |
SMTPAuthenticationError, |
| 13 |
SMTPConnectError, |
| 14 |
SMTPConnectTimeoutError, |
| 15 |
SMTPDataError, |
| 16 |
SMTPException, |
| 17 |
SMTPHeloError, |
| 18 |
SMTPNotSupported, |
| 19 |
SMTPReadTimeoutError, |
| 20 |
SMTPRecipientRefused, |
| 21 |
SMTPRecipientsRefused, |
| 22 |
SMTPResponseException, |
| 23 |
SMTPSenderRefused, |
| 24 |
SMTPServerDisconnected, |
| 25 |
SMTPTimeoutError, |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
@given(error_message=text()) |
| 30 |
def test_raise_smtp_exception(error_message: str) -> None: |
| 31 |
with pytest.raises(SMTPException) as excinfo: |
| 32 |
raise SMTPException(error_message) |
| 33 |
|
| 34 |
assert excinfo.value.message == error_message |
| 35 |
|
| 36 |
|
| 37 |
@given(code=integers(), error_message=text()) |
| 38 |
def test_raise_smtp_response_exception(code: int, error_message: str) -> None: |
| 39 |
with pytest.raises(SMTPResponseException) as excinfo: |
| 40 |
raise SMTPResponseException(code, error_message) |
| 41 |
|
| 42 |
assert issubclass(excinfo.type, SMTPException) |
| 43 |
assert excinfo.value.code == code |
| 44 |
assert excinfo.value.message == error_message |
| 45 |
|
| 46 |
|
| 47 |
@pytest.mark.parametrize( |
| 48 |
"error_class", (SMTPServerDisconnected, SMTPConnectError, SMTPConnectTimeoutError) |
| 49 |
) |
| 50 |
@given(error_message=text()) |
| 51 |
def test_connection_exceptions( |
| 52 |
error_message: str, error_class: type[SMTPException] |
| 53 |
) -> None: |
| 54 |
with pytest.raises(error_class) as excinfo: |
| 55 |
raise error_class(error_message) |
| 56 |
|
| 57 |
assert issubclass(excinfo.type, SMTPException) |
| 58 |
assert issubclass(excinfo.type, ConnectionError) |
| 59 |
assert excinfo.value.message == error_message |
| 60 |
|
| 61 |
|
| 62 |
@pytest.mark.parametrize( |
| 63 |
"error_class", (SMTPTimeoutError, SMTPConnectTimeoutError, SMTPReadTimeoutError) |
| 64 |
) |
| 65 |
@given(error_message=text()) |
| 66 |
def test_timeout_exceptions( |
| 67 |
error_message: str, error_class: type[SMTPException] |
| 68 |
) -> None: |
| 69 |
with pytest.raises(error_class) as excinfo: |
| 70 |
raise error_class(error_message) |
| 71 |
|
| 72 |
assert issubclass(excinfo.type, SMTPException) |
| 73 |
assert issubclass(excinfo.type, asyncio.TimeoutError) |
| 74 |
assert excinfo.value.message == error_message |
| 75 |
|
| 76 |
|
| 77 |
@pytest.mark.parametrize( |
| 78 |
"error_class", (SMTPHeloError, SMTPDataError, SMTPAuthenticationError) |
| 79 |
) |
| 80 |
@given(code=integers(), error_message=text()) |
| 81 |
def test_simple_response_exceptions( |
| 82 |
code: int, |
| 83 |
error_message: str, |
| 84 |
error_class: type[SMTPHeloError | SMTPDataError | SMTPAuthenticationError], |
| 85 |
) -> None: |
| 86 |
with pytest.raises(error_class) as excinfo: |
| 87 |
raise error_class(code, error_message) |
| 88 |
|
| 89 |
assert issubclass(excinfo.type, SMTPResponseException) |
| 90 |
assert excinfo.value.code == code |
| 91 |
assert excinfo.value.message == error_message |
| 92 |
|
| 93 |
|
| 94 |
@given(code=integers(), error_message=text(), sender=text()) |
| 95 |
def test_raise_smtp_sender_refused(code: int, error_message: str, sender: str) -> None: |
| 96 |
with pytest.raises(SMTPSenderRefused) as excinfo: |
| 97 |
raise SMTPSenderRefused(code, error_message, sender) |
| 98 |
|
| 99 |
assert issubclass(excinfo.type, SMTPResponseException) |
| 100 |
assert excinfo.value.code == code |
| 101 |
assert excinfo.value.message == error_message |
| 102 |
assert excinfo.value.sender == sender |
| 103 |
|
| 104 |
|
| 105 |
@given(code=integers(), error_message=text(), recipient=text()) |
| 106 |
def test_raise_smtp_recipient_refused( |
| 107 |
code: int, error_message: str, recipient: str |
| 108 |
) -> None: |
| 109 |
with pytest.raises(SMTPRecipientRefused) as excinfo: |
| 110 |
raise SMTPRecipientRefused(code, error_message, recipient) |
| 111 |
|
| 112 |
assert issubclass(excinfo.type, SMTPResponseException) |
| 113 |
assert excinfo.value.code == code |
| 114 |
assert excinfo.value.message == error_message |
| 115 |
assert excinfo.value.recipient == recipient |
| 116 |
|
| 117 |
|
| 118 |
@given(lists(elements=tuples(integers(), text(), text()))) |
| 119 |
def test_raise_smtp_recipients_refused(addresses: list[tuple[int, str, str]]) -> None: |
| 120 |
errors = [SMTPRecipientRefused(*address) for address in addresses] |
| 121 |
with pytest.raises(SMTPRecipientsRefused) as excinfo: |
| 122 |
raise SMTPRecipientsRefused(errors) |
| 123 |
|
| 124 |
assert issubclass(excinfo.type, SMTPException) |
| 125 |
assert excinfo.value.recipients == errors |
| 126 |
|
| 127 |
|
| 128 |
@given(error_message=text()) |
| 129 |
def test_raise_smtp_not_supported(error_message: str) -> None: |
| 130 |
with pytest.raises(SMTPNotSupported) as excinfo: |
| 131 |
raise SMTPNotSupported(error_message) |
| 132 |
|
| 133 |
assert issubclass(excinfo.type, SMTPException) |
| 134 |
assert excinfo.value.message == error_message |