File: test_commands.py

package info (click to toggle)
aiosmtplib 4.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 572 kB
  • sloc: python: 5,516; makefile: 20; sh: 6
file content (446 lines) | stat: -rw-r--r-- 14,997 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
"""
Lower level SMTP command tests.
"""

from typing import Any

import pytest

from aiosmtplib import (
    SMTP,
    SMTPDataError,
    SMTPHeloError,
    SMTPNotSupported,
    SMTPResponseException,
    SMTPServerDisconnected,
    SMTPStatus,
)

from .smtpd import (
    RecordingHandler,
    mock_response_done,
    mock_response_bad_data,
    mock_response_ehlo_full,
    mock_response_expn,
    mock_response_gibberish,
    mock_response_unavailable,
    mock_response_unrecognized_command,
    mock_response_bad_command_sequence,
    mock_response_syntax_error,
    mock_response_syntax_error_and_cleanup,
)


async def test_helo_ok(smtp_client: SMTP) -> None:
    async with smtp_client:
        response = await smtp_client.helo()

        assert response.code == SMTPStatus.completed


async def test_helo_with_hostname(smtp_client: SMTP) -> None:
    async with smtp_client:
        response = await smtp_client.helo(hostname="example.com")

        assert response.code == SMTPStatus.completed


async def test_helo_with_hostname_unset_after_connect(smtp_client: SMTP) -> None:
    async with smtp_client:
        smtp_client.local_hostname = None
        response = await smtp_client.helo()

        assert response.code == SMTPStatus.completed


@pytest.mark.smtpd_mocks(smtp_HELO=mock_response_unrecognized_command)
async def test_helo_error(smtp_client: SMTP) -> None:
    async with smtp_client:
        with pytest.raises(SMTPHeloError) as exception_info:
            await smtp_client.helo()
        assert exception_info.value.code == SMTPStatus.unrecognized_command


async def test_ehlo_ok(smtp_client: SMTP) -> None:
    async with smtp_client:
        response = await smtp_client.ehlo()

        assert response.code == SMTPStatus.completed


async def test_ehlo_with_hostname(smtp_client: SMTP) -> None:
    async with smtp_client:
        response = await smtp_client.ehlo(hostname="example.com")

        assert response.code == SMTPStatus.completed


async def test_ehlo_with_hostname_unset_after_connect(smtp_client: SMTP) -> None:
    async with smtp_client:
        smtp_client.local_hostname = None
        response = await smtp_client.ehlo()

        assert response.code == SMTPStatus.completed


@pytest.mark.smtpd_mocks(smtp_EHLO=mock_response_unrecognized_command)
async def test_ehlo_error(smtp_client: SMTP) -> None:
    async with smtp_client:
        with pytest.raises(SMTPHeloError) as exception_info:
            await smtp_client.ehlo()
        assert exception_info.value.code == SMTPStatus.unrecognized_command


@pytest.mark.smtpd_mocks(smtp_EHLO=mock_response_ehlo_full)
async def test_ehlo_parses_esmtp_extensions(smtp_client: SMTP) -> None:
    async with smtp_client:
        await smtp_client.ehlo()

        assert smtp_client.supports_extension("8bitmime")
        assert smtp_client.supports_extension("size")
        assert smtp_client.supports_extension("pipelining")
        assert smtp_client.supports_extension("ENHANCEDSTATUSCODES")
        assert not smtp_client.supports_extension("notreal")


@pytest.mark.smtpd_mocks(smtp_EHLO=mock_response_done)
async def test_ehlo_with_no_extensions(smtp_client: SMTP) -> None:
    async with smtp_client:
        await smtp_client.ehlo()

        assert not smtp_client.supports_extension("size")


async def test_ehlo_or_helo_if_needed_ehlo_success(smtp_client: SMTP) -> None:
    async with smtp_client:
        assert smtp_client.is_ehlo_or_helo_needed is True

        await smtp_client._ehlo_or_helo_if_needed()

        assert smtp_client.is_ehlo_or_helo_needed is False


@pytest.mark.smtpd_mocks(smtp_EHLO=mock_response_unrecognized_command)
async def test_ehlo_or_helo_if_needed_helo_success(smtp_client: SMTP) -> None:
    async with smtp_client:
        assert smtp_client.is_ehlo_or_helo_needed is True

        await smtp_client._ehlo_or_helo_if_needed()

        assert smtp_client.is_ehlo_or_helo_needed is False


@pytest.mark.smtpd_mocks(
    smtp_HELO=mock_response_unrecognized_command,
    smtp_EHLO=mock_response_unrecognized_command,
)
async def test_ehlo_or_helo_if_needed_neither_succeeds(smtp_client: SMTP) -> None:
    async with smtp_client:
        assert smtp_client.is_ehlo_or_helo_needed is True

        with pytest.raises(SMTPHeloError) as exception_info:
            await smtp_client._ehlo_or_helo_if_needed()
        assert exception_info.value.code == SMTPStatus.unrecognized_command


@pytest.mark.smtpd_mocks(smtp_EHLO=mock_response_unavailable)
async def test_ehlo_or_helo_if_needed_disconnect_after_ehlo(smtp_client: SMTP) -> None:
    async with smtp_client:
        with pytest.raises(SMTPHeloError):
            await smtp_client._ehlo_or_helo_if_needed()


async def test_rset_ok(smtp_client: SMTP) -> None:
    async with smtp_client:
        response = await smtp_client.rset()

        assert response.code == SMTPStatus.completed
        assert response.message == "OK"


@pytest.mark.smtpd_mocks(smtp_RSET=mock_response_bad_command_sequence)
async def test_rset_error(smtp_client: SMTP) -> None:
    async with smtp_client:
        with pytest.raises(SMTPResponseException) as exception_info:
            await smtp_client.rset()
        assert exception_info.value.code == SMTPStatus.bad_command_sequence


async def test_noop_ok(smtp_client: SMTP) -> None:
    async with smtp_client:
        response = await smtp_client.noop()

        assert response.code == SMTPStatus.completed
        assert response.message == "OK"


@pytest.mark.smtpd_mocks(smtp_NOOP=mock_response_syntax_error)
async def test_noop_error(smtp_client: SMTP) -> None:
    async with smtp_client:
        with pytest.raises(SMTPResponseException) as exception_info:
            await smtp_client.noop()
        assert exception_info.value.code == SMTPStatus.syntax_error


async def test_vrfy_ok(smtp_client: SMTP) -> None:
    nice_address = "test@example.com"
    async with smtp_client:
        response = await smtp_client.vrfy(nice_address)

        assert response.code == SMTPStatus.cannot_vrfy


async def test_vrfy_with_blank_address(smtp_client: SMTP) -> None:
    bad_address = ""
    async with smtp_client:
        with pytest.raises(SMTPResponseException):
            await smtp_client.vrfy(bad_address)


@pytest.mark.smtpd_options(smtputf8=True)
async def test_vrfy_smtputf8_supported(smtp_client: SMTP) -> None:
    async with smtp_client:
        response = await smtp_client.vrfy("tést@exåmple.com", options=["SMTPUTF8"])

        assert response.code == SMTPStatus.cannot_vrfy


@pytest.mark.smtpd_options(smtputf8=False)
async def test_vrfy_smtputf8_not_supported(smtp_client: SMTP) -> None:
    async with smtp_client:
        with pytest.raises(SMTPNotSupported):
            await smtp_client.vrfy("tést@exåmple.com", options=["SMTPUTF8"])


@pytest.mark.smtpd_mocks(smtp_EXPN=mock_response_expn)
async def test_expn_ok(smtp_client: SMTP) -> None:
    async with smtp_client:
        response = await smtp_client.expn("listserv-members")
        assert response.code == SMTPStatus.completed


async def test_expn_error(smtp_client: SMTP) -> None:
    """
    Since EXPN isn't implemented by aiosmtpd, it raises an exception by default.
    """
    async with smtp_client:
        with pytest.raises(SMTPResponseException):
            await smtp_client.expn("a-list")


@pytest.mark.smtpd_options(smtputf8=True)
@pytest.mark.smtpd_mocks(smtp_EXPN=mock_response_expn)
async def test_expn_smtputf8_supported(smtp_client: SMTP) -> None:
    utf8_list = "tést-lïst"
    async with smtp_client:
        response = await smtp_client.expn(utf8_list, options=["SMTPUTF8"])

        assert response.code == SMTPStatus.completed


@pytest.mark.smtpd_options(smtputf8=False)
async def test_expn_smtputf8_not_supported(smtp_client: SMTP) -> None:
    utf8_list = "tést-lïst"
    async with smtp_client:
        with pytest.raises(SMTPNotSupported):
            await smtp_client.expn(utf8_list, options=["SMTPUTF8"])


async def test_help_ok(smtp_client: SMTP) -> None:
    async with smtp_client:
        help_message = await smtp_client.help()

        assert "Supported commands" in help_message


@pytest.mark.smtpd_mocks(smtp_HELP=mock_response_syntax_error)
async def test_help_error(smtp_client: SMTP) -> None:
    async with smtp_client:
        with pytest.raises(SMTPResponseException) as exception_info:
            await smtp_client.help()
        assert exception_info.value.code == SMTPStatus.syntax_error


@pytest.mark.smtpd_mocks(smtp_QUIT=mock_response_syntax_error_and_cleanup)
async def test_quit_error(smtp_client: SMTP) -> None:
    async with smtp_client:
        with pytest.raises(SMTPResponseException) as exception_info:
            await smtp_client.quit()
        assert exception_info.value.code == SMTPStatus.syntax_error


async def test_supported_methods(smtp_client: SMTP) -> None:
    async with smtp_client:
        response = await smtp_client.ehlo()

        assert response.code == SMTPStatus.completed
        assert smtp_client.supports_extension("size")
        assert smtp_client.supports_extension("help")
        assert not smtp_client.supports_extension("bogus")


async def test_mail_ok(smtp_client: SMTP) -> None:
    async with smtp_client:
        response = await smtp_client.mail("j@example.com")

        assert response.code == SMTPStatus.completed
        assert response.message == "OK"


@pytest.mark.smtpd_mocks(smtp_MAIL=mock_response_bad_command_sequence)
async def test_mail_error(smtp_client: SMTP) -> None:
    async with smtp_client:
        with pytest.raises(SMTPResponseException) as exception_info:
            await smtp_client.mail("test@example.com")
        assert exception_info.value.code == SMTPStatus.bad_command_sequence


async def test_mail_options_not_implemented(smtp_client: SMTP) -> None:
    async with smtp_client:
        with pytest.raises(SMTPResponseException):
            await smtp_client.mail("j@example.com", options=["OPT=1"])


@pytest.mark.smtpd_options(smtputf8=True)
async def test_mail_smtputf8(smtp_client: SMTP) -> None:
    async with smtp_client:
        response = await smtp_client.mail(
            "tést@exåmple.com", options=["SMTPUTF8"], encoding="utf-8"
        )

        assert response.code == SMTPStatus.completed


async def test_mail_default_encoding_utf8_encode_error(smtp_client: SMTP) -> None:
    async with smtp_client:
        with pytest.raises(UnicodeEncodeError):
            await smtp_client.mail("tést@exåmple.com", options=["SMTPUTF8"])


async def test_rcpt_ok(smtp_client: SMTP) -> None:
    async with smtp_client:
        await smtp_client.mail("j@example.com")

        response = await smtp_client.rcpt("test@example.com")

        assert response.code == SMTPStatus.completed
        assert response.message == "OK"


@pytest.mark.smtpd_mocks(smtp_RCPT=mock_response_done)
async def test_rcpt_options_ok(smtp_client: SMTP) -> None:
    async with smtp_client:
        await smtp_client.mail("j@example.com")

        response = await smtp_client.rcpt(
            "test@example.com", options=["NOTIFY=FAILURE,DELAY"]
        )

        assert response.code == SMTPStatus.completed


async def test_rcpt_options_not_implemented(smtp_client: SMTP) -> None:
    # RCPT options are not implemented in aiosmtpd, so any option will return 555
    async with smtp_client:
        await smtp_client.mail("j@example.com")

        with pytest.raises(SMTPResponseException) as err:
            await smtp_client.rcpt("test@example.com", options=["OPT=1"])
            assert err.value.code == SMTPStatus.syntax_error


@pytest.mark.smtpd_mocks(smtp_RCPT=mock_response_syntax_error)
async def test_rcpt_error(smtp_client: SMTP) -> None:
    async with smtp_client:
        await smtp_client.mail("j@example.com")

        with pytest.raises(SMTPResponseException) as exception_info:
            await smtp_client.rcpt("test@example.com")
        assert exception_info.value.code == SMTPStatus.syntax_error


@pytest.mark.smtpd_options(smtputf8=True)
async def test_rcpt_smtputf8(smtp_client: SMTP) -> None:
    async with smtp_client:
        await smtp_client.mail("j@example.com", options=["SMTPUTF8"])
        response = await smtp_client.rcpt("tést@exåmple.com", encoding="utf-8")

        assert response.code == SMTPStatus.completed


async def test_rcpt_default_encoding_utf8_encode_error(smtp_client: SMTP) -> None:
    async with smtp_client:
        await smtp_client.mail("j@example.com")

        with pytest.raises(UnicodeEncodeError):
            await smtp_client.rcpt("tést@exåmple.com", options=["SMTPUTF8"])


async def test_data_ok(smtp_client: SMTP) -> None:
    async with smtp_client:
        await smtp_client.mail("j@example.com")
        await smtp_client.rcpt("test@example.com")
        response = await smtp_client.data("HELLO WORLD")

        assert response.code == SMTPStatus.completed
        assert response.message == "OK"


@pytest.mark.smtpd_mocks(smtp_DATA=mock_response_bad_command_sequence)
async def test_data_error_on_start_input(smtp_client: SMTP) -> None:
    async with smtp_client:
        await smtp_client.mail("admin@example.com")
        await smtp_client.rcpt("test@example.com")
        with pytest.raises(SMTPDataError) as exception_info:
            await smtp_client.data("TEST MESSAGE")
        assert exception_info.value.code == SMTPStatus.bad_command_sequence


async def test_data_complete_error(
    smtp_client: SMTP,
    smtpd_handler: RecordingHandler,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    monkeypatch.setattr(smtpd_handler, "handle_DATA", mock_response_syntax_error)

    async with smtp_client:
        await smtp_client.mail("admin@example.com")
        await smtp_client.rcpt("test@example.com")
        with pytest.raises(SMTPDataError) as exception_info:
            await smtp_client.data("TEST MESSAGE")
        assert exception_info.value.code == SMTPStatus.syntax_error


async def test_data_error_when_disconnected() -> None:
    client = SMTP()

    with pytest.raises(SMTPServerDisconnected):
        await client.data("HELLO WORLD")


@pytest.mark.smtpd_mocks(smtp_NOOP=mock_response_gibberish)
async def test_gibberish_raises_exception(smtp_client: SMTP) -> None:
    async with smtp_client:
        with pytest.raises(SMTPResponseException):
            await smtp_client.noop()


@pytest.mark.smtpd_mocks(smtp_NOOP=mock_response_bad_data)
async def test_badly_encoded_text_response(smtp_client: SMTP) -> None:
    async with smtp_client:
        response = await smtp_client.noop()

    assert response.code == SMTPStatus.completed


async def test_header_injection(
    smtp_client: SMTP,
    received_commands: list[tuple[str, tuple[Any, ...]]],
) -> None:
    async with smtp_client:
        await smtp_client.mail("test@example.com\r\nX-Malicious-Header: bad stuff")

    assert len(received_commands) > 0
    for command in received_commands:
        for arg in command:
            assert "bad stuff" not in arg