Add optional API upload alongside SMTP, Nextcloud/WebDAV as a document source with the same dedup/retry flow as IMAP, stricter invoice file checks for images, and admin UI for source and delivery modes.
145 lines
4.0 KiB
Python
145 lines
4.0 KiB
Python
"""Invoice attachment validation tests."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import struct
|
|
from email.message import EmailMessage
|
|
|
|
from forwarder.email.invoice_validation import is_plausible_invoice_file
|
|
from forwarder.email.parsing import (
|
|
DEFAULT_ALLOWED_EXTENSIONS,
|
|
collect_matching_attachments,
|
|
has_matching_attachment,
|
|
)
|
|
|
|
|
|
def _fake_png(width: int, height: int, *, padding: int = 9000) -> bytes:
|
|
signature = b"\x89PNG\r\n\x1a\n"
|
|
ihdr_data = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)
|
|
ihdr = struct.pack(">I", 13) + b"IHDR" + ihdr_data + struct.pack(">I", 0)
|
|
iend = struct.pack(">I", 0) + b"IEND" + struct.pack(">I", 0xAE426082)
|
|
body = signature + ihdr + iend
|
|
if len(body) >= padding:
|
|
return body
|
|
return body + b"\x00" * (padding - len(body))
|
|
|
|
|
|
def _fake_jpeg(width: int, height: int, *, padding: int = 9000) -> bytes:
|
|
sof_payload = struct.pack(">BHH", 8, height, width) + b"\x03\x01\x11\x00"
|
|
sof = b"\xff\xc0" + struct.pack(">H", len(sof_payload) + 2) + sof_payload
|
|
body = b"\xff\xd8" + sof + b"\xff\xd9"
|
|
if len(body) >= padding:
|
|
return body
|
|
return body + b"\x00" * (padding - len(body))
|
|
|
|
|
|
def _msg_with_part(
|
|
*,
|
|
data: bytes,
|
|
filename: str,
|
|
content_type: str,
|
|
disposition: str = "attachment",
|
|
) -> EmailMessage:
|
|
msg = EmailMessage()
|
|
msg["Subject"] = "Test"
|
|
msg.set_content("body")
|
|
maintype, subtype = content_type.split("/", 1)
|
|
msg.add_attachment(
|
|
data,
|
|
maintype=maintype,
|
|
subtype=subtype,
|
|
filename=filename,
|
|
disposition=disposition,
|
|
)
|
|
return msg
|
|
|
|
|
|
def test_rejects_inline_embedded_image():
|
|
msg = _msg_with_part(
|
|
data=_fake_png(400, 400),
|
|
filename="logo.png",
|
|
content_type="image/png",
|
|
disposition="inline",
|
|
)
|
|
assert not has_matching_attachment(msg, allowed_extensions=DEFAULT_ALLOWED_EXTENSIONS)
|
|
assert collect_matching_attachments(msg) == []
|
|
|
|
|
|
def test_rejects_tiny_tracking_pixel():
|
|
msg = _msg_with_part(
|
|
data=b"\x89PNG" + b"\x00" * 100,
|
|
filename="pixel.png",
|
|
content_type="image/png",
|
|
disposition="attachment",
|
|
)
|
|
assert not has_matching_attachment(msg)
|
|
|
|
|
|
def test_rejects_small_icon_by_filename():
|
|
msg = _msg_with_part(
|
|
data=_fake_png(400, 400, padding=5000),
|
|
filename="company-logo.png",
|
|
content_type="image/png",
|
|
)
|
|
assert not is_plausible_invoice_file(
|
|
_fake_png(400, 400, padding=5000),
|
|
filename="company-logo.png",
|
|
content_type="image/png",
|
|
)
|
|
|
|
|
|
def test_accepts_large_invoice_scan_jpeg():
|
|
data = _fake_jpeg(800, 1200)
|
|
msg = _msg_with_part(
|
|
data=data,
|
|
filename="fatura_123.jpg",
|
|
content_type="image/jpeg",
|
|
)
|
|
assert has_matching_attachment(msg)
|
|
attachments = collect_matching_attachments(msg)
|
|
assert len(attachments) == 1
|
|
assert attachments[0].filename == "fatura_123.jpg"
|
|
|
|
|
|
def test_accepts_valid_pdf_attachment():
|
|
pdf = b"%PDF-1.4\n" + (b"% invoice body\n" * 80)
|
|
msg = _msg_with_part(
|
|
data=pdf,
|
|
filename="FT2024.pdf",
|
|
content_type="application/pdf",
|
|
)
|
|
assert has_matching_attachment(msg)
|
|
assert len(collect_matching_attachments(msg)) == 1
|
|
|
|
|
|
def test_rejects_truncated_pdf():
|
|
msg = _msg_with_part(
|
|
data=b"%PDF",
|
|
filename="x.pdf",
|
|
content_type="application/pdf",
|
|
)
|
|
assert not has_matching_attachment(msg)
|
|
|
|
|
|
def test_rejects_low_resolution_image():
|
|
data = _fake_png(80, 80, padding=9000)
|
|
msg = _msg_with_part(
|
|
data=data,
|
|
filename="scan.png",
|
|
content_type="image/png",
|
|
)
|
|
assert not has_matching_attachment(msg)
|
|
|
|
|
|
def test_accepts_valid_xml():
|
|
xml = (
|
|
b'<?xml version="1.0" encoding="UTF-8"?>'
|
|
b"<Invoice><Id>1</Id><Note>test document</Note></Invoice>"
|
|
)
|
|
msg = _msg_with_part(
|
|
data=xml,
|
|
filename="doc.xml",
|
|
content_type="application/xml",
|
|
)
|
|
assert has_matching_attachment(msg)
|