Meta-Belege enthalten keine PDF ("Das ist keine Rechnung"), Rechnung nur im Billing Hub.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Wd9aeH8MKgesfbsaS3kpnx
38 lines
2.3 KiB
Python
38 lines
2.3 KiB
Python
import email
|
|
from backend.routers.emails import classify_mail
|
|
|
|
|
|
def _m(raw):
|
|
return email.message_from_string(raw)
|
|
|
|
|
|
def test_classify():
|
|
pdf = "Content-Type: multipart/mixed; boundary=b\n\n--b\nContent-Type: text/plain\n\nhi\n--b\nContent-Type: application/pdf\nContent-Disposition: attachment; filename=r.pdf\n\nx\n--b--\n"
|
|
assert classify_mail(_m(pdf)) == ("attachment", None)
|
|
link = "Content-Type: text/plain\n\nIhre Rechnung: https://pay.stripe.com/invoice/abc/pdf"
|
|
assert classify_mail(_m(link))[0] == "link"
|
|
portal = "Content-Type: text/plain\n\nIhre Rechnung ist verfügbar. Bitte in Ihrem Konto herunterladen."
|
|
assert classify_mail(_m(portal)) == ("portal", None)
|
|
assert classify_mail(_m("Content-Type: text/plain\n\nHallo"))[0] == "ignore"
|
|
# Login-/Konsolen-Links sind kein Direktdownload -> portal
|
|
aws = "Content-Type: text/plain\n\nYour bill: https://console.aws.amazon.com/billing/home#/bills"
|
|
assert classify_mail(_m(aws)) == ("portal", None)
|
|
assert classify_mail(_m("Content-Type: text/plain\n\nIhr Zahlungsbeleg liegt bereit."))[0] == "portal"
|
|
assert classify_mail(_m("Content-Type: text/html\n\n<p>Das ist keine Rechnung</p> Transaktions-ID 1"))[0] == "portal" # Meta
|
|
# http (kein TLS) wird nie direkt geladen
|
|
assert classify_mail(_m("Content-Type: text/plain\n\nRechnung http://x.example/r.pdf"))[0] == "portal"
|
|
# HTML-Entities im Link werden aufgelöst
|
|
h = 'Content-Type: text/html\n\n<a href="https://x.example/r.pdf?a=1&b=2">Rechnung</a>'
|
|
assert classify_mail(_m(h)) == ("link", "https://x.example/r.pdf?a=1&b=2")
|
|
|
|
|
|
def test_tracker_and_stripe():
|
|
from backend.fetchers.direct import pdf_url
|
|
h = ('Content-Type: text/html\n\n<a href="http://url1.email.openai.com/ls/click?x=1&y=2">Terms</a>'
|
|
'<a href="http://url1.email.openai.com/ls/click?inv=9">View invoice</a>')
|
|
assert classify_mail(_m(h)) == ("link", "http://url1.email.openai.com/ls/click?inv=9")
|
|
# Tracker fremder Absender wird nicht verfolgt
|
|
evil = 'Content-Type: text/html\n\n<a href="http://url1.email.evil.example/ls/click">View invoice</a>'
|
|
assert classify_mail(_m(evil))[0] == "portal"
|
|
assert pdf_url("https://invoice.stripe.com/i/acct_1A/live_X2?s=em") == "https://pay.stripe.com/invoice/acct_1A/live_X2/pdf?s=em"
|
|
assert pdf_url("https://x.example/r.pdf") == "https://x.example/r.pdf"
|