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"
# 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\nRechnung'
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\nTerms'
'View invoice')
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\nView invoice'
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"