-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathtest_email_utils.py
More file actions
103 lines (70 loc) · 3.61 KB
/
test_email_utils.py
File metadata and controls
103 lines (70 loc) · 3.61 KB
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
"""
Tests for email utility functions — no network calls needed.
"""
import base64
import email as email_lib
import pytest
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email_client.reader import _decode_header_value, _extract_text_body
from email_client.sender import _build_mime
# ── _decode_header_value ─────────────────────────────────────────────────────
def test_decode_plain_header():
assert _decode_header_value("Hello World") == "Hello World"
def test_decode_none_header():
assert _decode_header_value(None) == ""
def test_decode_encoded_header():
# RFC 2047 encoded header (UTF-8 base64)
encoded = "=?utf-8?b?SGVsbG8gV29ybGQ=?="
assert _decode_header_value(encoded) == "Hello World"
def test_decode_quoted_printable_header():
encoded = "=?utf-8?q?Hello_World?="
assert _decode_header_value(encoded) == "Hello World"
# ── _extract_text_body ────────────────────────────────────────────────────────
def _make_simple_message(body: str, ctype: str = "text/plain") -> email_lib.message.Message:
msg = MIMEMultipart("alternative")
msg.attach(MIMEText(body, ctype.split("/")[1]))
return msg
def test_extract_plain_text():
msg = _make_simple_message("Hello from the body", "text/plain")
assert "Hello from the body" in _extract_text_body(msg)
def test_extract_html_body():
html = "<html><body><p>Hello HTML</p></body></html>"
msg = MIMEMultipart("alternative")
msg.attach(MIMEText(html, "html"))
body = _extract_text_body(msg)
assert "Hello HTML" in body
def test_plain_preferred_over_html():
msg = MIMEMultipart("alternative")
msg.attach(MIMEText("Plain text version", "plain"))
msg.attach(MIMEText("<p>HTML version</p>", "html"))
body = _extract_text_body(msg)
assert "Plain text version" in body
def test_empty_message():
msg = MIMEMultipart("alternative")
assert _extract_text_body(msg) == ""
# ── _build_mime ────────────────────────────────────────────────────────────────
def test_build_mime_headers():
msg = _build_mime("from@test.com", "to@test.com", "Test Subject", "Hello body")
assert msg["From"] == "from@test.com"
assert msg["To"] == "to@test.com"
assert msg["Subject"] == "Test Subject"
def test_build_mime_body():
msg = _build_mime("f@t.com", "t@t.com", "Subj", "My message body")
payload = msg.get_payload()
body_text = payload[0].get_payload()
assert "My message body" in body_text
def test_build_mime_is_multipart():
msg = _build_mime("f@t.com", "t@t.com", "S", "B")
assert msg.is_multipart()
# ── sender reply subject ──────────────────────────────────────────────────────
def test_reply_subject_adds_re():
from email_client.sender import _build_mime
subject = "Original Subject"
reply_subject = subject if subject.lower().startswith("re:") else f"Re: {subject}"
assert reply_subject == "Re: Original Subject"
def test_reply_subject_no_double_re():
subject = "Re: Already a reply"
reply_subject = subject if subject.lower().startswith("re:") else f"Re: {subject}"
assert reply_subject == "Re: Already a reply"
assert not reply_subject.startswith("Re: Re:")