Debian Codegraph @

tests/test_config.py

raw
1 """
2 Tests covering SMTP configuration options.
3 """
4
5 import socket
6 import ssl
7 from typing import Any
8
9 import pytest
10
11 from aiosmtplib import SMTP, SMTPConnectError, SMTPConnectTimeoutError
12
13
14 async def test_tls_context_and_cert_raises(client_tls_context: ssl.SSLContext) -> None:
15 with pytest.raises(ValueError):
16 SMTP(use_tls=True, client_cert="foo.crt", tls_context=client_tls_context)
17
18
19 async def test_tls_context_and_cert_to_connect_raises(
20 client_tls_context: ssl.SSLContext,
21 ) -> None:
22 client = SMTP(use_tls=True, tls_context=client_tls_context)
23
24 with pytest.raises(ValueError):
25 await client.connect(client_cert="foo.crt")
26
27
28 async def test_tls_context_and_cert_to_starttls_raises(
29 smtp_client: SMTP, client_tls_context: ssl.SSLContext
30 ) -> None:
31 async with smtp_client:
32 with pytest.raises(ValueError):
33 await smtp_client.starttls(
34 client_cert="test.cert", tls_context=client_tls_context
35 )
36
37
38 async def test_use_tls_and_start_tls_raises() -> None:
39 with pytest.raises(ValueError):
40 SMTP(use_tls=True, start_tls=True)
41
42
43 async def test_use_tls_and_start_tls_to_connect_raises() -> None:
44 client = SMTP(use_tls=True)
45
46 with pytest.raises(ValueError):
47 await client.connect(start_tls=True)
48
49
50 async def test_socket_and_port_raises() -> None:
51 with pytest.raises(ValueError):
52 SMTP(port=1, sock=socket.socket(socket.AF_INET))
53
54
55 async def test_socket_and_socket_path_raises() -> None:
56 with pytest.raises(ValueError):
57 SMTP(socket_path="/tmp/test", sock=socket.socket(socket.AF_INET)) # nosec
58
59
60 async def test_tls_socket_with_no_hostname_raises() -> None:
61 with pytest.raises(ValueError):
62 SMTP(hostname=None, sock=socket.socket(socket.AF_INET), use_tls=True) # nosec
63
64
65 async def test_tls_socket_path_with_no_hostname_raises() -> None:
66 with pytest.raises(ValueError):
67 SMTP(hostname=None, socket_path="/tmp/test", use_tls=True) # nosec
68
69
70 async def test_port_and_socket_path_raises() -> None:
71 with pytest.raises(ValueError):
72 SMTP(port=1, socket_path="/tmp/test") # nosec
73
74
75 async def test_config_via_connect_kwargs(
76 bind_address: str, unused_tcp_port: int, hostname: str, smtpd_server_port: int
77 ) -> None:
78 client = SMTP(
79 hostname="",
80 use_tls=True,
81 start_tls=None,
82 port=smtpd_server_port + 1,
83 local_hostname="example.com",
84 )
85
86 local_hostname = "smtp.example.com"
87 source_address = (bind_address, unused_tcp_port)
88 await client.connect(
89 hostname=hostname,
90 port=smtpd_server_port,
91 use_tls=False,
92 start_tls=False,
93 local_hostname=local_hostname,
94 source_address=source_address,
95 )
96 assert client.is_connected
97
98 assert client.hostname == hostname
99 assert client.port == smtpd_server_port
100 assert client.use_tls is False
101 assert client.local_hostname == local_hostname
102 assert client.source_address == source_address
103 assert client._start_tls_on_connect is False
104
105 await client.quit()
106
107
108 @pytest.mark.parametrize(
109 "use_tls,start_tls,expected_port",
110 [(False, False, 25), (True, False, 465), (False, True, 587)],
111 ids=["plaintext", "tls", "starttls"],
112 )
113 async def test_default_port_on_connect(
114 bind_address: str,
115 use_tls: bool,
116 start_tls: bool,
117 expected_port: int,
118 ) -> None:
119 client = SMTP()
120
121 # Nothing listens on the default ports; the error names the port tried.
122 with pytest.raises(
123 (SMTPConnectError, SMTPConnectTimeoutError), match=f"port {expected_port}"
124 ):
125 await client.connect(
126 hostname=bind_address, use_tls=use_tls, start_tls=start_tls, timeout=0.5
127 )
128
129 assert client.port is None
130
131
132 async def test_default_hostname_on_connect() -> None:
133 client = SMTP()
134
135 # No hostname configured; the client defaults to localhost for the attempt.
136 with pytest.raises((SMTPConnectError, SMTPConnectTimeoutError), match="localhost"):
137 await client.connect(timeout=0.5)
138
139 assert client.hostname is None
140
141
142 async def test_connect_hostname_takes_precedence(
143 hostname: str, smtpd_server_port: int
144 ) -> None:
145 client = SMTP(hostname="example.com", port=smtpd_server_port, start_tls=False)
146 await client.connect(hostname=hostname)
147
148 assert client.hostname == hostname
149
150 await client.quit()
151
152
153 async def test_connect_port_takes_precedence(
154 hostname: str, smtpd_server_port: int
155 ) -> None:
156 client = SMTP(hostname=hostname, port=17, start_tls=False)
157 await client.connect(port=smtpd_server_port)
158
159 assert client.port == smtpd_server_port
160
161 await client.quit()
162
163
164 async def test_connect_timeout_is_reverted(
165 hostname: str, smtpd_server_port: int
166 ) -> None:
167 client = SMTP(
168 hostname=hostname, port=smtpd_server_port, timeout=0.66, start_tls=False
169 )
170 await client.connect(timeout=0.99)
171
172 assert client.timeout == 0.66
173
174 await client.quit()
175
176
177 async def test_connect_source_address_takes_precedence(
178 bind_address: str,
179 unused_tcp_port: int,
180 hostname: str,
181 smtpd_server_port: int,
182 ) -> None:
183 client = SMTP(
184 hostname=hostname,
185 port=smtpd_server_port,
186 start_tls=False,
187 source_address=("example.com", 444),
188 )
189 await client.connect(source_address=(bind_address, unused_tcp_port))
190
191 assert client.source_address == (bind_address, unused_tcp_port)
192
193 await client.quit()
194
195
196 async def test_connect_local_hostname_takes_precedence(
197 hostname: str,
198 smtpd_server_port: int,
199 ) -> None:
200 client = SMTP(
201 hostname=hostname,
202 port=smtpd_server_port,
203 start_tls=False,
204 local_hostname="foo.com",
205 )
206 await client.connect(local_hostname="example.com")
207
208 assert client.local_hostname == "example.com"
209
210 await client.quit()
211
212
213 async def test_connect_use_tls_takes_precedence(
214 hostname: str,
215 smtpd_server_port: int,
216 client_tls_context: ssl.SSLContext,
217 ) -> None:
218 client = SMTP(
219 hostname=hostname,
220 port=smtpd_server_port,
221 use_tls=True,
222 tls_context=client_tls_context,
223 )
224
225 await client.connect(use_tls=False)
226
227 assert client.use_tls is False
228
229 await client.quit()
230
231
232 async def test_connect_validate_certs_takes_precedence(
233 hostname: str,
234 smtpd_server_port: int,
235 client_tls_context: ssl.SSLContext,
236 ) -> None:
237 client = SMTP(
238 hostname=hostname,
239 port=smtpd_server_port,
240 validate_certs=False,
241 tls_context=client_tls_context,
242 )
243
244 await client.connect(validate_certs=True)
245
246 assert client.validate_certs is True
247
248 await client.quit()
249
250
251 async def test_connect_certificate_options_take_precedence(
252 hostname: str, smtpd_server_port: int
253 ) -> None:
254 client = SMTP(
255 hostname=hostname,
256 port=smtpd_server_port,
257 client_cert="test",
258 client_key="test",
259 cert_bundle="test",
260 start_tls=False,
261 )
262
263 await client.connect(client_cert=None, client_key=None, cert_bundle=None)
264
265 assert client.client_cert is None
266 assert client.client_key is None
267 assert client.cert_bundle is None
268
269 await client.quit()
270
271
272 async def test_connect_tls_context_option_takes_precedence(
273 hostname: str,
274 smtpd_server_port: int,
275 client_tls_context: ssl.SSLContext,
276 server_tls_context: ssl.SSLContext,
277 ) -> None:
278 client = SMTP(
279 hostname=hostname, port=smtpd_server_port, tls_context=server_tls_context
280 )
281
282 await client.connect(tls_context=client_tls_context)
283
284 assert client.tls_context is client_tls_context
285
286 await client.quit()
287
288
289 async def test_starttls_certificate_options_take_precedence(
290 hostname: str,
291 smtpd_server_port: int,
292 ca_cert_path: str,
293 valid_cert_path: str,
294 valid_key_path: str,
295 ) -> None:
296 client = SMTP(
297 hostname=hostname,
298 port=smtpd_server_port,
299 start_tls=False,
300 validate_certs=False,
301 client_cert="test1",
302 client_key="test1",
303 cert_bundle="test1",
304 )
305
306 await client.connect(
307 validate_certs=False,
308 client_cert="test2",
309 client_key="test2",
310 cert_bundle="test2",
311 )
312
313 await client.starttls(
314 client_cert=valid_cert_path,
315 client_key=valid_key_path,
316 cert_bundle=ca_cert_path,
317 validate_certs=True,
318 )
319
320 assert client.client_cert == valid_cert_path
321 assert client.client_key == valid_key_path
322 assert client.cert_bundle == ca_cert_path
323 assert client.validate_certs is True
324
325 await client.quit()
326
327
328 async def test_hostname_newline_raises_error() -> None:
329 with pytest.raises(ValueError):
330 SMTP(hostname="localhost\r\n")
331
332
333 async def test_local_hostname_newline_raises_error() -> None:
334 with pytest.raises(ValueError):
335 SMTP(
336 hostname="localhost",
337 local_hostname="localhost\r\nRCPT TO: <[email protected]>",
338 )
339
340
341 @pytest.mark.parametrize(
342 "local_hostname",
343 [
344 "me.example.com XCLIENT ADDR=1.2.3.4",
345 "me.example.com\tXCLIENT ADDR=1.2.3.4",
346 "me.example.com\x00",
347 "",
348 " ",
349 ],
350 ids=["space", "tab", "nul", "empty", "whitespace_only"],
351 )
352 async def test_local_hostname_whitespace_raises_error(local_hostname: str) -> None:
353 with pytest.raises(ValueError):
354 SMTP(hostname="localhost", local_hostname=local_hostname)
355
356
357 async def test_local_hostname_surrounding_whitespace_is_stripped() -> None:
358 client = SMTP(hostname="localhost", local_hostname=" me.example.com\t")
359
360 assert client.local_hostname == "me.example.com"
361
362
363 async def test_connect_local_hostname_whitespace_raises_error(
364 hostname: str, smtpd_server_port: int
365 ) -> None:
366 client = SMTP(hostname=hostname, port=smtpd_server_port)
367
368 with pytest.raises(ValueError):
369 await client.connect(local_hostname="me.example.com XCLIENT ADDR=1.2.3.4")
370
371
372 async def test_starttls_invalid_config_raises_before_ehlo(
373 smtp_client: SMTP,
374 client_tls_context: ssl.SSLContext,
375 received_commands: list[tuple[str, tuple[Any, ...]]],
376 ) -> None:
377 async with smtp_client:
378 with pytest.raises(ValueError):
379 await smtp_client.starttls(
380 client_cert="test.cert", tls_context=client_tls_context
381 )
382
383 assert smtp_client.is_ehlo_or_helo_needed
384 assert not any(command == "EHLO" for command, _ in received_commands)