| 1 |
""" |
| 2 |
send coroutine testing. |
| 3 |
""" |
| 4 |
|
| 5 |
import asyncio |
| 6 |
import email |
| 7 |
import email.message |
| 8 |
import pathlib |
| 9 |
import socket |
| 10 |
import ssl |
| 11 |
from typing import Any |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
from aiosmtplib import send |
| 16 |
|
| 17 |
|
| 18 |
@pytest.mark.parametrize( |
| 19 |
"message", ["message", "compat32_message", "mime_message"], indirect=True |
| 20 |
) |
| 21 |
async def test_send( |
| 22 |
hostname: str, |
| 23 |
smtpd_server_port: int, |
| 24 |
message: email.message.EmailMessage | email.message.Message, |
| 25 |
received_messages: list[email.message.EmailMessage], |
| 26 |
client_tls_context: ssl.SSLContext, |
| 27 |
) -> None: |
| 28 |
errors, _ = await send( |
| 29 |
message, |
| 30 |
hostname=hostname, |
| 31 |
port=smtpd_server_port, |
| 32 |
tls_context=client_tls_context, |
| 33 |
) |
| 34 |
|
| 35 |
assert not errors |
| 36 |
assert len(received_messages) == 1 |
| 37 |
|
| 38 |
|
| 39 |
@pytest.mark.parametrize("message", ["message_str", "message_bytes"], indirect=True) |
| 40 |
async def test_send_with_raw_message( |
| 41 |
hostname: str, |
| 42 |
smtpd_server_port: int, |
| 43 |
recipient_str: str, |
| 44 |
sender_str: str, |
| 45 |
message: str | bytes, |
| 46 |
received_messages: list[email.message.EmailMessage], |
| 47 |
) -> None: |
| 48 |
errors, _ = await send( |
| 49 |
message, |
| 50 |
hostname=hostname, |
| 51 |
port=smtpd_server_port, |
| 52 |
sender=sender_str, |
| 53 |
recipients=[recipient_str], |
| 54 |
start_tls=False, |
| 55 |
) |
| 56 |
|
| 57 |
assert not errors |
| 58 |
assert len(received_messages) == 1 |
| 59 |
|
| 60 |
|
| 61 |
async def test_send_without_sender( |
| 62 |
hostname: str, |
| 63 |
smtpd_server_port: int, |
| 64 |
recipient_str: str, |
| 65 |
message_str: str, |
| 66 |
) -> None: |
| 67 |
with pytest.raises(ValueError): |
| 68 |
await send( |
| 69 |
message_str, |
| 70 |
hostname=hostname, |
| 71 |
port=smtpd_server_port, |
| 72 |
sender=None, |
| 73 |
recipients=[recipient_str], |
| 74 |
start_tls=False, |
| 75 |
) |
| 76 |
|
| 77 |
|
| 78 |
async def test_send_without_recipients( |
| 79 |
hostname: str, |
| 80 |
smtpd_server_port: int, |
| 81 |
sender_str: str, |
| 82 |
message_str: str, |
| 83 |
) -> None: |
| 84 |
with pytest.raises(ValueError): |
| 85 |
await send( |
| 86 |
message_str, |
| 87 |
hostname=hostname, |
| 88 |
port=smtpd_server_port, |
| 89 |
sender=sender_str, |
| 90 |
recipients=[], |
| 91 |
start_tls=False, |
| 92 |
) |
| 93 |
|
| 94 |
|
| 95 |
async def test_send_with_start_tls( |
| 96 |
hostname: str, |
| 97 |
smtpd_server_port: int, |
| 98 |
client_tls_context: ssl.SSLContext, |
| 99 |
message: email.message.EmailMessage, |
| 100 |
received_messages: list[email.message.EmailMessage], |
| 101 |
received_commands: list[tuple[str, tuple[Any, ...]]], |
| 102 |
) -> None: |
| 103 |
errors, _ = await send( |
| 104 |
message, |
| 105 |
hostname=hostname, |
| 106 |
port=smtpd_server_port, |
| 107 |
start_tls=True, |
| 108 |
tls_context=client_tls_context, |
| 109 |
) |
| 110 |
|
| 111 |
assert not errors |
| 112 |
assert "STARTTLS" in [command[0] for command in received_commands] |
| 113 |
assert len(received_messages) == 1 |
| 114 |
|
| 115 |
|
| 116 |
async def test_send_with_login( |
| 117 |
hostname: str, |
| 118 |
smtpd_server_port: int, |
| 119 |
message: email.message.EmailMessage, |
| 120 |
received_messages: list[email.message.EmailMessage], |
| 121 |
received_commands: list[tuple[str, tuple[Any, ...]]], |
| 122 |
auth_username: str, |
| 123 |
auth_password: str, |
| 124 |
client_tls_context: ssl.SSLContext, |
| 125 |
) -> None: |
| 126 |
errors, _ = await send( |
| 127 |
message, |
| 128 |
hostname=hostname, |
| 129 |
port=smtpd_server_port, |
| 130 |
start_tls=True, |
| 131 |
tls_context=client_tls_context, |
| 132 |
username=auth_username, |
| 133 |
password=auth_password, |
| 134 |
) |
| 135 |
|
| 136 |
assert not errors |
| 137 |
assert "AUTH" in [command[0] for command in received_commands] |
| 138 |
assert len(received_messages) == 1 |
| 139 |
|
| 140 |
|
| 141 |
async def test_send_via_socket( |
| 142 |
hostname: str, |
| 143 |
smtpd_server_port: int, |
| 144 |
message: email.message.EmailMessage, |
| 145 |
received_messages: list[email.message.EmailMessage], |
| 146 |
) -> None: |
| 147 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: |
| 148 |
sock.connect((hostname, smtpd_server_port)) |
| 149 |
|
| 150 |
errors, _ = await send( |
| 151 |
message, |
| 152 |
hostname=None, |
| 153 |
port=None, |
| 154 |
sock=sock, |
| 155 |
start_tls=False, |
| 156 |
) |
| 157 |
|
| 158 |
assert not errors |
| 159 |
assert len(received_messages) == 1 |
| 160 |
|
| 161 |
|
| 162 |
@pytest.mark.smtpd_options(tls=True) |
| 163 |
async def test_send_via_socket_tls_and_hostname( |
| 164 |
hostname: str, |
| 165 |
client_tls_context: ssl.SSLContext, |
| 166 |
smtpd_server_port: int, |
| 167 |
message: email.message.EmailMessage, |
| 168 |
received_messages: list[email.message.EmailMessage], |
| 169 |
) -> None: |
| 170 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: |
| 171 |
sock.connect((hostname, smtpd_server_port)) |
| 172 |
|
| 173 |
errors, _ = await send( |
| 174 |
message, |
| 175 |
hostname=hostname, |
| 176 |
port=None, |
| 177 |
sock=sock, |
| 178 |
tls_context=client_tls_context, |
| 179 |
use_tls=True, |
| 180 |
) |
| 181 |
|
| 182 |
assert not errors |
| 183 |
assert len(received_messages) == 1 |
| 184 |
|
| 185 |
|
| 186 |
@pytest.mark.parametrize( |
| 187 |
"socket_path_type", |
| 188 |
[str, pathlib.Path, bytes], |
| 189 |
ids=["str", "Path", "bytes"], |
| 190 |
) |
| 191 |
async def test_send_via_socket_path( |
| 192 |
smtpd_server_socket_path: asyncio.AbstractServer, |
| 193 |
socket_path: pathlib.Path, |
| 194 |
socket_path_type: type[str] | type[pathlib.Path] | type[bytes], |
| 195 |
message: email.message.EmailMessage, |
| 196 |
received_messages: list[email.message.EmailMessage], |
| 197 |
) -> None: |
| 198 |
errors, _ = await send( |
| 199 |
message, |
| 200 |
hostname=None, |
| 201 |
port=None, |
| 202 |
socket_path=socket_path_type(socket_path), |
| 203 |
start_tls=False, |
| 204 |
) |
| 205 |
|
| 206 |
assert not errors |
| 207 |
assert len(received_messages) == 1 |
| 208 |
|
| 209 |
|
| 210 |
@pytest.mark.smtpd_options(tls=True) |
| 211 |
async def test_send_via_socket_path_with_tls( |
| 212 |
smtpd_server_socket_path: asyncio.AbstractServer, |
| 213 |
socket_path: pathlib.Path, |
| 214 |
hostname: str, |
| 215 |
client_tls_context: ssl.SSLContext, |
| 216 |
message: email.message.EmailMessage, |
| 217 |
received_messages: list[email.message.EmailMessage], |
| 218 |
) -> None: |
| 219 |
errors, _ = await send( |
| 220 |
message, |
| 221 |
hostname=hostname, |
| 222 |
port=None, |
| 223 |
socket_path=socket_path, |
| 224 |
use_tls=True, |
| 225 |
tls_context=client_tls_context, |
| 226 |
) |
| 227 |
|
| 228 |
assert not errors |
| 229 |
assert len(received_messages) == 1 |
| 230 |
|
| 231 |
|
| 232 |
async def test_send_with_mail_options( |
| 233 |
hostname: str, |
| 234 |
smtpd_server_port: int, |
| 235 |
recipient_str: str, |
| 236 |
sender_str: str, |
| 237 |
message_str: str, |
| 238 |
received_messages: list[email.message.EmailMessage], |
| 239 |
) -> None: |
| 240 |
errors, _ = await send( |
| 241 |
message_str, |
| 242 |
hostname=hostname, |
| 243 |
port=smtpd_server_port, |
| 244 |
sender=sender_str, |
| 245 |
recipients=[recipient_str], |
| 246 |
mail_options=["BODY=8BITMIME"], |
| 247 |
start_tls=False, |
| 248 |
) |
| 249 |
|
| 250 |
assert not errors |
| 251 |
assert len(received_messages) == 1 |
| 252 |
|
| 253 |
|
| 254 |
async def test_send_with_rcpt_options( |
| 255 |
hostname: str, |
| 256 |
smtpd_server_port: int, |
| 257 |
recipient_str: str, |
| 258 |
sender_str: str, |
| 259 |
message_str: str, |
| 260 |
received_messages: list[email.message.EmailMessage], |
| 261 |
) -> None: |
| 262 |
errors, _ = await send( |
| 263 |
message_str, |
| 264 |
hostname=hostname, |
| 265 |
port=smtpd_server_port, |
| 266 |
sender=sender_str, |
| 267 |
recipients=[recipient_str], |
| 268 |
# RCPT params are not supported by the server; just check that the kwarg works |
| 269 |
rcpt_options=[], |
| 270 |
start_tls=False, |
| 271 |
) |
| 272 |
|
| 273 |
assert not errors |
| 274 |
assert len(received_messages) == 1 |