- Klassifikator: Direktlink nur Allowlist/https-PDF, Login-/Konsolen-Links -> portal - grabber.py: INBOX/Archive/Trash readonly, Dedupe Message-ID + SHA-256 - Stichtag GRAB_CUTOFF: Rückblick = pending_review, neu = queued - mailer.py: SMTP an ACCOUNTABLE_INBOX (ohne Adresse kein Versand) - /outbound: Liste/xlsx, PDF-Vorschau, approve, run Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
34 lines
1.9 KiB
Python
34 lines
1.9 KiB
Python
import email
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from backend import grabber
|
|
from backend.models import Base, Outbound
|
|
|
|
LINK = ("Message-ID: <{mid}>\nSubject: Ihre Rechnung\nFrom: Shop <billing@shop.example>\n"
|
|
"Date: {date}\nContent-Type: text/plain\n\nDownload: https://pay.stripe.com/invoice/x/pdf")
|
|
PORTAL = ("Message-ID: <p1>\nSubject: Rechnung verfügbar\nFrom: AWS <no@aws.example>\n"
|
|
"Date: Mon, 3 Nov 2025 10:00:00 +0000\nContent-Type: text/plain\n\nSee https://console.aws.amazon.com/billing")
|
|
|
|
|
|
def test_process_message(tmp_path, monkeypatch):
|
|
db = sessionmaker(bind=create_engine("sqlite://"))()
|
|
Base.metadata.create_all(db.get_bind())
|
|
monkeypatch.setattr(grabber, "fetch_pdf", lambda url: b"%PDF-1.4 same")
|
|
monkeypatch.setenv("GRAB_CUTOFF", "2026-11-13")
|
|
run = lambda raw: grabber.process_message(db, email.message_from_string(raw), "INBOX", str(tmp_path))
|
|
|
|
assert run(LINK.format(mid="a", date="Mon, 3 Nov 2025 10:00:00 +0000")) == "pending_review" # Rückblick
|
|
assert run(LINK.format(mid="a", date="Mon, 3 Nov 2025 10:00:00 +0000")) is None # gleiche Message-ID
|
|
assert run(LINK.format(mid="b", date="Mon, 3 Nov 2025 10:00:00 +0000")) == "duplicate" # gleiche PDF
|
|
monkeypatch.setattr(grabber, "fetch_pdf", lambda url: b"%PDF-1.4 neu")
|
|
assert run(LINK.format(mid="c", date="Mon, 16 Nov 2026 10:00:00 +0000")) == "queued" # nach Stichtag
|
|
assert run(PORTAL) == "manual"
|
|
assert run("Subject: Newsletter\n\nhallo") is None
|
|
assert db.query(Outbound).count() == 4
|
|
assert (tmp_path / "picker" / "2025" / "11").is_dir()
|
|
|
|
monkeypatch.delenv("GRAB_CUTOFF") # ohne Stichtag nie automatisch senden
|
|
monkeypatch.setattr(grabber, "fetch_pdf", lambda url: b"%PDF-1.4 dritte")
|
|
assert run(LINK.format(mid="d", date="Mon, 16 Nov 2026 10:00:00 +0000")) == "pending_review"
|