| 1 |
""" |
| 2 |
Tests for ESMTP extension parsing. |
| 3 |
""" |
| 4 |
|
| 5 |
from aiosmtplib.esmtp import parse_esmtp_extensions |
| 6 |
|
| 7 |
|
| 8 |
def test_basic_extension_parsing() -> None: |
| 9 |
response = """size.does.matter.af.MIL offers FIFTEEN extensions: |
| 10 |
8BITMIME |
| 11 |
PIPELINING |
| 12 |
DSN |
| 13 |
ENHANCEDSTATUSCODES |
| 14 |
EXPN |
| 15 |
HELP |
| 16 |
SAML |
| 17 |
SEND |
| 18 |
SOML |
| 19 |
TURN |
| 20 |
XADR |
| 21 |
XSTA |
| 22 |
ETRN |
| 23 |
XGEN |
| 24 |
SIZE 51200000 |
| 25 |
""" |
| 26 |
extensions, auth_types = parse_esmtp_extensions(response) |
| 27 |
|
| 28 |
assert "size" in extensions |
| 29 |
assert extensions["size"] == "51200000" |
| 30 |
assert "saml" in extensions |
| 31 |
assert "size.does.matter.af.mil" not in extensions |
| 32 |
assert auth_types == [] |
| 33 |
|
| 34 |
|
| 35 |
def test_no_extension_parsing() -> None: |
| 36 |
response = """size.does.matter.af.MIL offers ZERO extensions: |
| 37 |
""" |
| 38 |
extensions, auth_types = parse_esmtp_extensions(response) |
| 39 |
|
| 40 |
assert extensions == {} |
| 41 |
assert auth_types == [] |
| 42 |
|
| 43 |
|
| 44 |
def test_auth_type_parsing() -> None: |
| 45 |
response = """mail.example.com Hello [127.0.0.1] |
| 46 |
AUTH FOO BAR |
| 47 |
""" |
| 48 |
_, auth_types = parse_esmtp_extensions(response) |
| 49 |
|
| 50 |
assert "foo" in auth_types |
| 51 |
assert "bar" in auth_types |
| 52 |
assert "bogus" not in auth_types |
| 53 |
|
| 54 |
|
| 55 |
def test_old_school_auth_type_parsing() -> None: |
| 56 |
response = """mail.example.com Hello [127.0.0.1] |
| 57 |
AUTH=PLAIN |
| 58 |
""" |
| 59 |
_, auth_types = parse_esmtp_extensions(response) |
| 60 |
|
| 61 |
assert "plain" in auth_types |
| 62 |
assert "cram-md5" not in auth_types |
| 63 |
|
| 64 |
|
| 65 |
def test_mixed_auth_type_parsing() -> None: |
| 66 |
response = """mail.example.com Hello [127.0.0.1] |
| 67 |
AUTH=PLAIN |
| 68 |
AUTH CRAM-MD5 |
| 69 |
""" |
| 70 |
_, auth_types = parse_esmtp_extensions(response) |
| 71 |
|
| 72 |
assert "plain" in auth_types |
| 73 |
assert "cram-md5" in auth_types |
| 74 |
|
| 75 |
|
| 76 |
def test_old_school_multiple_auth_type_parsing() -> None: |
| 77 |
response = """mail.example.com Hello [127.0.0.1] |
| 78 |
AUTH=PLAIN LOGIN |
| 79 |
""" |
| 80 |
extensions, auth_types = parse_esmtp_extensions(response) |
| 81 |
|
| 82 |
# Every space-separated method in the advertisement is kept. |
| 83 |
assert auth_types == ["plain", "login"] |
| 84 |
# login() requires the "auth" extension to be registered for old-style |
| 85 |
# advertisements, so supports_extension("auth") stays accurate. |
| 86 |
assert "auth" in extensions |
| 87 |
|
| 88 |
|
| 89 |
def test_old_school_only_auth_no_junk() -> None: |
| 90 |
response = """mail.example.com Hello [127.0.0.1] |
| 91 |
AUTH=CRAM-MD5 |
| 92 |
""" |
| 93 |
extensions, auth_types = parse_esmtp_extensions(response) |
| 94 |
|
| 95 |
assert "auth" in extensions |
| 96 |
assert auth_types == ["cram-md5"] |
| 97 |
|
| 98 |
|
| 99 |
def test_leading_whitespace_extension_parsing() -> None: |
| 100 |
response = "mail.example.com Hello [127.0.0.1]\n SIZE 1000" |
| 101 |
extensions, _ = parse_esmtp_extensions(response) |
| 102 |
|
| 103 |
assert extensions["size"] == "1000" |
| 104 |
|
| 105 |
|
| 106 |
def test_blank_line_no_empty_keyword() -> None: |
| 107 |
response = "mail.example.com Hello [127.0.0.1]\n\n8BITMIME\n" |
| 108 |
extensions, _ = parse_esmtp_extensions(response) |
| 109 |
|
| 110 |
assert "" not in extensions |
| 111 |
assert "8bitmime" in extensions |