| 1 |
""" |
| 2 |
Tests that cover asyncio usage. |
| 3 |
""" |
| 4 |
|
| 5 |
import asyncio |
| 6 |
import ssl |
| 7 |
from collections.abc import Awaitable |
| 8 |
from typing import Any |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from aiosmtplib import SMTP |
| 13 |
from aiosmtplib.response import SMTPResponse |
| 14 |
|
| 15 |
from .smtpd import mock_response_expn |
| 16 |
|
| 17 |
|
| 18 |
RECIPIENTS = [ |
| 19 |
"[email protected]", |
| 20 |
"[email protected]", |
| 21 |
"[email protected]", |
| 22 |
] |
| 23 |
|
| 24 |
|
| 25 |
async def test_sendmail_multiple_times_in_sequence( |
| 26 |
smtp_client: SMTP, |
| 27 |
smtpd_server: asyncio.AbstractServer, |
| 28 |
sender_str: str, |
| 29 |
message_str: str, |
| 30 |
) -> None: |
| 31 |
async with smtp_client: |
| 32 |
for recipient in RECIPIENTS: |
| 33 |
errors, response = await smtp_client.sendmail( |
| 34 |
sender_str, [recipient], message_str |
| 35 |
) |
| 36 |
|
| 37 |
assert not errors |
| 38 |
assert isinstance(errors, dict) |
| 39 |
assert response != "" |
| 40 |
|
| 41 |
|
| 42 |
async def test_sendmail_multiple_times_with_gather( |
| 43 |
smtp_client: SMTP, |
| 44 |
smtpd_server: asyncio.AbstractServer, |
| 45 |
sender_str: str, |
| 46 |
message_str: str, |
| 47 |
) -> None: |
| 48 |
async with smtp_client: |
| 49 |
tasks = [ |
| 50 |
smtp_client.sendmail(sender_str, [recipient], message_str) |
| 51 |
for recipient in RECIPIENTS |
| 52 |
] |
| 53 |
results = await asyncio.gather(*tasks) |
| 54 |
for errors, message in results: |
| 55 |
assert not errors |
| 56 |
assert isinstance(errors, dict) |
| 57 |
assert message != "" |
| 58 |
|
| 59 |
|
| 60 |
async def test_connect_and_sendmail_multiple_times_with_gather( |
| 61 |
hostname: str, |
| 62 |
smtpd_server_port: int, |
| 63 |
client_tls_context: ssl.SSLContext, |
| 64 |
sender_str: str, |
| 65 |
message_str: str, |
| 66 |
) -> None: |
| 67 |
async def connect_and_send( |
| 68 |
*args: Any, **kwargs: Any |
| 69 |
) -> tuple[dict[str, SMTPResponse], str]: |
| 70 |
async with SMTP( |
| 71 |
hostname=hostname, port=smtpd_server_port, tls_context=client_tls_context |
| 72 |
) as client: |
| 73 |
response = await client.sendmail(*args, **kwargs) |
| 74 |
|
| 75 |
return response |
| 76 |
|
| 77 |
tasks = [ |
| 78 |
connect_and_send(sender_str, [recipient], message_str) |
| 79 |
for recipient in RECIPIENTS |
| 80 |
] |
| 81 |
results = await asyncio.gather(*tasks) |
| 82 |
for errors, message in results: |
| 83 |
assert not errors |
| 84 |
assert isinstance(errors, dict) |
| 85 |
assert message != "" |
| 86 |
|
| 87 |
|
| 88 |
async def test_multiple_clients_with_gather( |
| 89 |
hostname: str, |
| 90 |
smtpd_server: asyncio.AbstractServer, |
| 91 |
smtpd_server_port: int, |
| 92 |
client_tls_context: ssl.SSLContext, |
| 93 |
sender_str: str, |
| 94 |
message_str: str, |
| 95 |
) -> None: |
| 96 |
async def connect_and_send( |
| 97 |
*args: Any, **kwargs: Any |
| 98 |
) -> tuple[dict[str, SMTPResponse], str]: |
| 99 |
client = SMTP( |
| 100 |
hostname=hostname, port=smtpd_server_port, tls_context=client_tls_context |
| 101 |
) |
| 102 |
async with client: |
| 103 |
response = await client.sendmail(*args, **kwargs) |
| 104 |
|
| 105 |
return response |
| 106 |
|
| 107 |
tasks = [ |
| 108 |
connect_and_send(sender_str, [recipient], message_str) |
| 109 |
for recipient in RECIPIENTS |
| 110 |
] |
| 111 |
results = await asyncio.gather(*tasks) |
| 112 |
for errors, message in results: |
| 113 |
assert not errors |
| 114 |
assert isinstance(errors, dict) |
| 115 |
assert message != "" |
| 116 |
|
| 117 |
|
| 118 |
async def test_multiple_actions_in_context_manager_with_gather( |
| 119 |
hostname: str, |
| 120 |
smtpd_server_port: int, |
| 121 |
client_tls_context: ssl.SSLContext, |
| 122 |
sender_str: str, |
| 123 |
message_str: str, |
| 124 |
) -> None: |
| 125 |
async def connect_and_run_commands(*args: Any, **kwargs: Any) -> SMTPResponse: |
| 126 |
async with SMTP( |
| 127 |
hostname=hostname, port=smtpd_server_port, tls_context=client_tls_context |
| 128 |
) as client: |
| 129 |
await client.ehlo() |
| 130 |
await client.help() |
| 131 |
response = await client.noop() |
| 132 |
|
| 133 |
return response |
| 134 |
|
| 135 |
tasks = [ |
| 136 |
connect_and_run_commands(sender_str, [recipient], message_str) |
| 137 |
for recipient in RECIPIENTS |
| 138 |
] |
| 139 |
responses = await asyncio.gather(*tasks) |
| 140 |
for response in responses: |
| 141 |
assert 200 <= response.code < 300 |
| 142 |
|
| 143 |
|
| 144 |
@pytest.mark.smtpd_mocks(smtp_EXPN=mock_response_expn) |
| 145 |
async def test_many_commands_with_gather(smtp_client: SMTP) -> None: |
| 146 |
""" |
| 147 |
Tests that appropriate locks are in place to prevent commands confusing each other. |
| 148 |
""" |
| 149 |
async with smtp_client: |
| 150 |
tasks: list[Awaitable] = [ |
| 151 |
smtp_client.ehlo(), |
| 152 |
smtp_client.helo(), |
| 153 |
smtp_client.noop(), |
| 154 |
smtp_client.vrfy("[email protected]"), |
| 155 |
smtp_client.expn("[email protected]"), |
| 156 |
smtp_client.mail("[email protected]"), |
| 157 |
smtp_client.help(), |
| 158 |
] |
| 159 |
results = await asyncio.gather(*tasks) |
| 160 |
|
| 161 |
for result in results[:-1]: |
| 162 |
assert 200 <= result.code < 300 |
| 163 |
|
| 164 |
# Help text is returned as a string, not a result tuple |
| 165 |
assert "Supported commands" in results[-1] |
| 166 |
|
| 167 |
|
| 168 |
async def test_close_works_on_closing_transport( |
| 169 |
hostname: str, |
| 170 |
smtpd_server_port: int, |
| 171 |
client_tls_context: ssl.SSLContext, |
| 172 |
) -> None: |
| 173 |
client = SMTP( |
| 174 |
hostname=hostname, port=smtpd_server_port, tls_context=client_tls_context |
| 175 |
) |
| 176 |
|
| 177 |
await client.connect() |
| 178 |
assert client.is_connected |
| 179 |
assert client.transport is not None |
| 180 |
|
| 181 |
# Ideally we'd test with a stopped event loop, but that breaks |
| 182 |
# pytest-asyncio's finalizer. Closing the client's transport is the |
| 183 |
# next best thing. |
| 184 |
client.transport.close() |
| 185 |
|
| 186 |
client.close() |
| 187 |
assert not client.is_connected |
| 188 |
|
| 189 |
|
| 190 |
async def test_context_manager_entry_multiple_times_with_gather( |
| 191 |
smtp_client: SMTP, sender_str: str, message_str: str |
| 192 |
) -> None: |
| 193 |
async def connect_and_send( |
| 194 |
*args: Any, **kwargs: Any |
| 195 |
) -> tuple[dict[str, SMTPResponse], str]: |
| 196 |
async with smtp_client: |
| 197 |
response = await smtp_client.sendmail(*args, **kwargs) |
| 198 |
|
| 199 |
return response |
| 200 |
|
| 201 |
tasks = [ |
| 202 |
asyncio.wait_for(connect_and_send(sender_str, [recipient], message_str), 2.0) |
| 203 |
for recipient in RECIPIENTS |
| 204 |
] |
| 205 |
results = await asyncio.gather(*tasks) |
| 206 |
for errors, message in results: |
| 207 |
assert not errors |
| 208 |
assert isinstance(errors, dict) |
| 209 |
assert message != "" |