Stop relaying the original RFC822 (SendGrid headers, Fwd: subject, etc.). Send a new message with PDF/XML/ZIP attachments and minimal metadata body.
67 lines
2.2 KiB
Python
67 lines
2.2 KiB
Python
"""Outbound forward sends a clean message with invoice attachments only."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from email.message import EmailMessage
|
|
|
|
from forwarder.email.smtp_client import build_forward_message
|
|
|
|
|
|
def test_forward_sends_clean_message_with_pdf_only():
|
|
original = EmailMessage()
|
|
original["From"] = "[email protected]"
|
|
original["To"] = "[email protected]"
|
|
original["Subject"] = "Invoice & Ticket Delivery"
|
|
original["X-SG-EID"] = "spam-marker"
|
|
original.set_content("marketing body")
|
|
original.add_attachment(
|
|
b"%PDF-1.4",
|
|
maintype="application",
|
|
subtype="pdf",
|
|
filename="fatura.pdf",
|
|
)
|
|
|
|
outbound = build_forward_message(
|
|
original,
|
|
from_addr="[email protected]",
|
|
destination_email="[email protected]",
|
|
)
|
|
|
|
assert outbound["From"] == "[email protected]"
|
|
assert outbound["To"] == "[email protected]"
|
|
assert outbound["Subject"] == "Invoice & Ticket Delivery"
|
|
assert not outbound["Subject"].lower().startswith("fwd:")
|
|
assert outbound.get("Cc") is None
|
|
assert outbound.get("X-SG-EID") is None
|
|
|
|
attachments = list(outbound.iter_attachments())
|
|
assert len(attachments) == 1
|
|
assert attachments[0].get_filename() == "fatura.pdf"
|
|
assert attachments[0].get_content_type() == "application/pdf"
|
|
|
|
text_parts = [
|
|
p.get_content()
|
|
for p in outbound.walk()
|
|
if not p.is_multipart() and p.get_content_type() == "text/plain"
|
|
]
|
|
assert text_parts
|
|
assert "Remetente original" in text_parts[0]
|
|
assert "marketing body" not in text_parts[0]
|
|
|
|
|
|
def test_forward_includes_multiple_invoice_types():
|
|
original = EmailMessage()
|
|
original["Subject"] = "Docs"
|
|
original.set_content("x")
|
|
original.add_attachment(b"%PDF", maintype="application", subtype="pdf", filename="a.pdf")
|
|
original.add_attachment(b"<x/>", maintype="text", subtype="xml", filename="b.xml")
|
|
|
|
outbound = build_forward_message(
|
|
original,
|
|
from_addr="[email protected]",
|
|
destination_email="[email protected]",
|
|
)
|
|
|
|
names = {p.get_filename() for p in outbound.iter_attachments()}
|
|
assert names == {"a.pdf", "b.xml"}
|