Skip to content

Commit cf569bc

Browse files
committedMar 1, 2025·
test: reduce message type test overlap
1 parent 2fd56b2 commit cf569bc

File tree

4 files changed

+79
-63
lines changed

4 files changed

+79
-63
lines changed
 

‎tests/conftest.py

+34-6
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ def message_str(recipient_str: str, sender_str: str) -> str:
143143
)
144144

145145

146+
@pytest.fixture(scope="session")
147+
def message_bytes(message_str: str) -> bytes:
148+
return message_str.encode("ascii")
149+
150+
146151
@pytest.fixture(scope="session")
147152
def smtpd_class() -> type[SMTPD]:
148153
return TestSMTPD
@@ -307,16 +312,39 @@ def mime_message(
307312
return message
308313

309314

310-
@pytest.fixture(scope="function", params=["mime_multipart", "compat32"])
315+
@pytest.fixture(scope="function")
316+
def email_message(recipient_str: str, sender_str: str) -> email.message.EmailMessage:
317+
message = email.message.EmailMessage()
318+
message["To"] = recipient_str
319+
message["From"] = sender_str
320+
message["Subject"] = "A message"
321+
message.set_payload("Hello World")
322+
323+
return message
324+
325+
326+
@pytest.fixture(scope="function")
311327
def message(
312-
request: ParamFixtureRequest,
328+
request: pytest.FixtureRequest,
329+
email_message: email.message.EmailMessage,
313330
compat32_message: email.message.Message,
314-
mime_message: email.message.EmailMessage,
315-
) -> Union[email.message.Message, email.message.EmailMessage]:
316-
if request.param == "compat32":
331+
mime_message: email.mime.multipart.MIMEMultipart,
332+
message_str: str,
333+
message_bytes: bytes,
334+
) -> Union[email.message.EmailMessage, email.message.Message, str, bytes]:
335+
if not hasattr(request, "param"):
336+
return email_message
337+
338+
if request.param == "compat32_message":
317339
return compat32_message
318-
else:
340+
elif request.param == "mime_message":
319341
return mime_message
342+
elif request.param == "str":
343+
return message_str
344+
elif request.param == "bytes":
345+
return message_bytes
346+
else:
347+
return email_message
320348

321349

322350
# Server helpers and factories #

‎tests/test_api.py

+24-43
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
from aiosmtplib import send
1616

1717

18+
@pytest.mark.parametrize(
19+
"message", ["message", "compat32_message", "mime_message"], indirect=True
20+
)
1821
async def test_send(
1922
hostname: str,
2023
smtpd_server_port: int,
21-
message: email.message.Message,
24+
message: Union[email.message.EmailMessage, email.message.Message],
2225
received_messages: list[email.message.EmailMessage],
2326
client_tls_context: ssl.SSLContext,
2427
) -> None:
25-
errors, response = await send(
28+
errors, _ = await send(
2629
message,
2730
hostname=hostname,
2831
port=smtpd_server_port,
@@ -33,37 +36,17 @@ async def test_send(
3336
assert len(received_messages) == 1
3437

3538

36-
async def test_send_with_str(
39+
@pytest.mark.parametrize("message", ["message_str", "message_bytes"], indirect=True)
40+
async def test_send_with_raw_message(
3741
hostname: str,
3842
smtpd_server_port: int,
3943
recipient_str: str,
4044
sender_str: str,
41-
message_str: str,
45+
message: Union[str, bytes],
4246
received_messages: list[email.message.EmailMessage],
4347
) -> None:
44-
errors, response = await send(
45-
message_str,
46-
hostname=hostname,
47-
port=smtpd_server_port,
48-
sender=sender_str,
49-
recipients=[recipient_str],
50-
start_tls=False,
51-
)
52-
53-
assert not errors
54-
assert len(received_messages) == 1
55-
56-
57-
async def test_send_with_bytes(
58-
hostname: str,
59-
smtpd_server_port: int,
60-
recipient_str: str,
61-
sender_str: str,
62-
message_str: str,
63-
received_messages: list[email.message.EmailMessage],
64-
) -> None:
65-
errors, response = await send(
66-
bytes(message_str, "ascii"),
48+
errors, _ = await send(
49+
message,
6750
hostname=hostname,
6851
port=smtpd_server_port,
6952
sender=sender_str,
@@ -80,10 +63,9 @@ async def test_send_without_sender(
8063
smtpd_server_port: int,
8164
recipient_str: str,
8265
message_str: str,
83-
received_messages: list[email.message.EmailMessage],
8466
) -> None:
8567
with pytest.raises(ValueError):
86-
errors, response = await send(
68+
await send(
8769
message_str,
8870
hostname=hostname,
8971
port=smtpd_server_port,
@@ -98,10 +80,9 @@ async def test_send_without_recipients(
9880
smtpd_server_port: int,
9981
sender_str: str,
10082
message_str: str,
101-
received_messages: list[email.message.EmailMessage],
10283
) -> None:
10384
with pytest.raises(ValueError):
104-
errors, response = await send(
85+
await send(
10586
message_str,
10687
hostname=hostname,
10788
port=smtpd_server_port,
@@ -115,11 +96,11 @@ async def test_send_with_start_tls(
11596
hostname: str,
11697
smtpd_server_port: int,
11798
client_tls_context: ssl.SSLContext,
118-
message: email.message.Message,
99+
message: email.message.EmailMessage,
119100
received_messages: list[email.message.EmailMessage],
120101
received_commands: list[tuple[str, tuple[Any, ...]]],
121102
) -> None:
122-
errors, response = await send(
103+
errors, _ = await send(
123104
message,
124105
hostname=hostname,
125106
port=smtpd_server_port,
@@ -135,14 +116,14 @@ async def test_send_with_start_tls(
135116
async def test_send_with_login(
136117
hostname: str,
137118
smtpd_server_port: int,
138-
message: email.message.Message,
119+
message: email.message.EmailMessage,
139120
received_messages: list[email.message.EmailMessage],
140121
received_commands: list[tuple[str, tuple[Any, ...]]],
141122
auth_username: str,
142123
auth_password: str,
143124
client_tls_context: ssl.SSLContext,
144125
) -> None:
145-
errors, response = await send(
126+
errors, _ = await send(
146127
message,
147128
hostname=hostname,
148129
port=smtpd_server_port,
@@ -160,14 +141,14 @@ async def test_send_with_login(
160141
async def test_send_via_socket(
161142
hostname: str,
162143
smtpd_server_port: int,
163-
mime_message: email.message.EmailMessage,
144+
message: email.message.EmailMessage,
164145
received_messages: list[email.message.EmailMessage],
165146
) -> None:
166147
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
167148
sock.connect((hostname, smtpd_server_port))
168149

169150
errors, _ = await send(
170-
mime_message,
151+
message,
171152
hostname=None,
172153
port=None,
173154
sock=sock,
@@ -183,14 +164,14 @@ async def test_send_via_socket_tls_and_hostname(
183164
hostname: str,
184165
client_tls_context: ssl.SSLContext,
185166
smtpd_server_port: int,
186-
mime_message: email.message.EmailMessage,
167+
message: email.message.EmailMessage,
187168
received_messages: list[email.message.EmailMessage],
188169
) -> None:
189170
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
190171
sock.connect((hostname, smtpd_server_port))
191172

192173
errors, _ = await send(
193-
mime_message,
174+
message,
194175
hostname=hostname,
195176
port=None,
196177
sock=sock,
@@ -211,11 +192,11 @@ async def test_send_via_socket_path(
211192
smtpd_server_socket_path: asyncio.AbstractServer,
212193
socket_path: pathlib.Path,
213194
socket_path_type: Union[type[str], type[pathlib.Path], type[bytes]],
214-
mime_message: email.message.EmailMessage,
195+
message: email.message.EmailMessage,
215196
received_messages: list[email.message.EmailMessage],
216197
) -> None:
217198
errors, _ = await send(
218-
mime_message,
199+
message,
219200
hostname=None,
220201
port=None,
221202
socket_path=socket_path_type(socket_path),
@@ -232,11 +213,11 @@ async def test_send_via_socket_path_with_tls(
232213
socket_path: pathlib.Path,
233214
hostname: str,
234215
client_tls_context: ssl.SSLContext,
235-
mime_message: email.message.EmailMessage,
216+
message: email.message.EmailMessage,
236217
received_messages: list[email.message.EmailMessage],
237218
) -> None:
238219
errors, _ = await send(
239-
mime_message,
220+
message,
240221
hostname=hostname,
241222
port=None,
242223
socket_path=socket_path,

‎tests/test_sendmail.py

+20-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import email.generator
77
import email.header
88
import email.message
9-
from typing import Any, Optional
9+
from typing import Any, Optional, Union
1010

1111
import pytest
1212

@@ -248,7 +248,7 @@ async def test_send_message(smtp_client: SMTP, message: email.message.Message) -
248248

249249
async def test_send_message_with_sender_and_recipient_args(
250250
smtp_client: SMTP,
251-
message: email.message.Message,
251+
message: email.message.EmailMessage,
252252
received_messages: list[email.message.EmailMessage],
253253
) -> None:
254254
sender = "sender2@example.com"
@@ -270,7 +270,7 @@ async def test_send_message_with_sender_and_recipient_args(
270270
async def test_send_message_with_cc_recipients(
271271
smtp_client: SMTP,
272272
recipient_str: str,
273-
message: email.message.Message,
273+
message: email.message.EmailMessage,
274274
received_messages: list[email.message.EmailMessage],
275275
received_commands: list[tuple[str, tuple[Any, ...]]],
276276
) -> None:
@@ -299,7 +299,7 @@ async def test_send_message_with_cc_recipients(
299299
async def test_send_message_with_bcc_recipients(
300300
smtp_client: SMTP,
301301
recipient_str: str,
302-
message: email.message.Message,
302+
message: email.message.EmailMessage,
303303
received_messages: list[email.message.EmailMessage],
304304
received_commands: list[tuple[str, tuple[Any, ...]]],
305305
) -> None:
@@ -324,7 +324,7 @@ async def test_send_message_with_bcc_recipients(
324324
async def test_send_message_with_cc_and_bcc_recipients(
325325
smtp_client: SMTP,
326326
recipient_str: str,
327-
message: email.message.Message,
327+
message: email.message.EmailMessage,
328328
received_messages: list[email.message.EmailMessage],
329329
received_commands: list[tuple[str, tuple[Any, ...]]],
330330
) -> None:
@@ -354,7 +354,7 @@ async def test_send_message_with_cc_and_bcc_recipients(
354354

355355
async def test_send_message_recipient_str(
356356
smtp_client: SMTP,
357-
message: email.message.Message,
357+
message: email.message.EmailMessage,
358358
received_commands: list[tuple[str, tuple[Any, ...]]],
359359
) -> None:
360360
recipient_str = "1234@example.org"
@@ -371,7 +371,7 @@ async def test_send_message_recipient_str(
371371

372372
async def test_send_message_mail_options(
373373
smtp_client: SMTP,
374-
message: email.message.Message,
374+
message: email.message.EmailMessage,
375375
) -> None:
376376
async with smtp_client:
377377
errors, response = await smtp_client.send_message(
@@ -384,7 +384,7 @@ async def test_send_message_mail_options(
384384

385385

386386
async def test_send_multiple_messages_in_sequence(
387-
smtp_client: SMTP, message: email.message.Message
387+
smtp_client: SMTP, message: email.message.EmailMessage
388388
) -> None:
389389
message1 = copy.copy(message)
390390

@@ -407,7 +407,7 @@ async def test_send_multiple_messages_in_sequence(
407407

408408

409409
async def test_send_message_without_recipients(
410-
smtp_client: SMTP, message: email.message.Message
410+
smtp_client: SMTP, message: email.message.EmailMessage
411411
) -> None:
412412
del message["To"]
413413

@@ -417,7 +417,7 @@ async def test_send_message_without_recipients(
417417

418418

419419
async def test_send_message_without_sender(
420-
smtp_client: SMTP, message: email.message.Message
420+
smtp_client: SMTP, message: email.message.EmailMessage
421421
) -> None:
422422
del message["From"]
423423

@@ -426,10 +426,13 @@ async def test_send_message_without_sender(
426426
await smtp_client.send_message(message)
427427

428428

429+
@pytest.mark.parametrize(
430+
"message", ["message", "compat32_message", "mime_message"], indirect=True
431+
)
429432
@pytest.mark.smtpd_options(smtputf8=True)
430433
async def test_send_message_smtputf8_sender(
431434
smtp_client: SMTP,
432-
message: email.message.Message,
435+
message: Union[email.message.EmailMessage, email.message.Message],
433436
received_commands: list[tuple[str, tuple[Any, ...]]],
434437
received_messages: list[email.message.EmailMessage],
435438
) -> None:
@@ -510,8 +513,9 @@ async def test_send_compat32_message_smtputf8_recipient(
510513

511514
@pytest.mark.smtpd_options(smtputf8=False)
512515
async def test_send_message_smtputf8_not_supported(
513-
smtp_client: SMTP, message: email.message.Message
516+
smtp_client: SMTP, message: email.message.EmailMessage
514517
) -> None:
518+
del message["To"]
515519
message["To"] = "reçipïént2@exåmple.com"
516520

517521
async with smtp_client:
@@ -581,10 +585,13 @@ async def test_send_mime_message_utf8_text_without_smtputf8(
581585
]
582586

583587

588+
@pytest.mark.parametrize(
589+
"message", ["message", "compat32_message", "mime_message"], indirect=True
590+
)
584591
@pytest.mark.smtpd_options(**{"smtputf8": False, "7bit": True})
585592
async def test_send_message_7bit(
586593
smtp_client: SMTP,
587-
message: email.message.Message,
594+
message: Union[email.message.EmailMessage, email.message.Message],
588595
received_commands: list[tuple[str, tuple[Any, ...]]],
589596
) -> None:
590597
async with smtp_client:

‎tests/test_sync.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_sendmail_sync(
2424

2525
def test_send_message_sync(
2626
smtp_client_threaded: SMTP,
27-
message: email.message.Message,
27+
message: email.message.EmailMessage,
2828
) -> None:
2929
errors, response = smtp_client_threaded.send_message_sync(message)
3030

0 commit comments

Comments
 (0)
Please sign in to comment.