"""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"] = "bounces+ticket@sendgrid.net" original["To"] = "faturas@bitreport.pt" original["Subject"] = "Invoice & Ticket Delivery" original["X-SG-EID"] = "spam-marker" original.set_content("marketing body") original.add_attachment( b"%PDF-1.4\n" + (b"x" * 1200), maintype="application", subtype="pdf", filename="fatura.pdf", ) outbound = build_forward_message( original, from_addr="faturas@bitreport.pt", destination_email="inbox.arquivodigital@bizdocs.mobi", ) assert outbound["From"] == "faturas@bitreport.pt" assert outbound["To"] == "inbox.arquivodigital@bizdocs.mobi" 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-1.4\n" + (b"x" * 1200), maintype="application", subtype="pdf", filename="a.pdf", ) original.add_attachment( b'' b"1item description10.00", maintype="text", subtype="xml", filename="b.xml", ) outbound = build_forward_message( original, from_addr="a@b.pt", destination_email="inbox.arquivodigital@bizdocs.mobi", ) names = {p.get_filename() for p in outbound.iter_attachments()} assert names == {"a.pdf", "b.xml"}