Files
forwarder/tests/test_smtp_forward.py
T
1sonhador f19886d78e fix: strip Cc/Bcc on forward to prevent mailbox loop
Re-sending the original MIME kept Cc/Bcc (e.g. faturas@) so copies
returned to the monitored inbox. Drop extra recipient headers and
send SMTP RCPT only to the BizDocs destination.
2026-07-01 09:51:00 +01:00

82 lines
2.5 KiB
Python

"""Forward re-sends the original message — no .eml wrapper."""
from __future__ import annotations
from email.message import EmailMessage
from forwarder.email.smtp_client import build_forward_message
def test_forward_resends_original_with_updated_headers():
original = EmailMessage()
original["From"] = "[email protected]"
original["To"] = "[email protected]"
original["Subject"] = "Fatura 123"
original.set_content("corpo original")
original.add_attachment(
b"%PDF-1.4",
maintype="application",
subtype="pdf",
filename="fatura.pdf",
)
fwd = build_forward_message(
original,
from_addr="[email protected]",
destination_email="[email protected]",
)
assert fwd["From"] == "[email protected]"
assert fwd["To"] == "[email protected]"
assert fwd["Subject"] == "Fwd: Fatura 123"
attachments = list(fwd.iter_attachments())
assert len(attachments) == 1
assert attachments[0].get_filename() == "fatura.pdf"
assert attachments[0].get_content_type() == "application/pdf"
assert not any((p.get_filename() or "").endswith(".eml") for p in attachments)
# Body preserved from original.
text_parts = [
p.get_payload(decode=True)
for p in fwd.walk()
if not p.is_multipart() and p.get_content_type() == "text/plain"
]
assert any(b"corpo original" in (part or b"") for part in text_parts)
def test_forward_does_not_double_prefix_subject():
original = EmailMessage()
original["Subject"] = "Fwd: Already forwarded"
original.set_content("x")
fwd = build_forward_message(
original,
from_addr="[email protected]",
destination_email="[email protected]",
)
assert fwd["Subject"] == "Fwd: Already forwarded"
def test_forward_strips_cc_bcc_to_prevent_loop():
original = EmailMessage()
original["From"] = "[email protected]"
original["To"] = "[email protected]"
original["Cc"] = "[email protected], [email protected]"
original["Bcc"] = "[email protected]"
original["Reply-To"] = "[email protected]"
original["Subject"] = "Fatura loop"
original.set_content("test")
fwd = build_forward_message(
original,
from_addr="[email protected]",
destination_email="[email protected]",
)
assert fwd["To"] == "[email protected]"
assert fwd.get("Cc") is None
assert fwd.get("Bcc") is None
assert fwd.get("Reply-To") is None