order confirmation
All checks were successful
Deploy / build-deploy (push) Successful in 6m14s

This commit is contained in:
Jorijn van der Graaf 2026-08-09 00:14:09 +02:00
commit 2fa6e70af1
10 changed files with 619 additions and 20 deletions

View file

@ -56,6 +56,20 @@ gpg --batch --passphrase '' --quick-gen-key 'Catcrafts e2e <invoices@e2e.invalid
|| { echo "e2e: could not create a GPG key (is gnupg installed?)" >&2; exit 1; }
export INVOICE_GPG_KEY='invoices@e2e.invalid'
# A fake sendmail, so the mailer's REAL path — build the MIME message, attach
# the signed invoice, shell out — runs with zero network. Each accepted
# message lands as its own mail-<n>.eml; the mailer sends sequentially from
# one thread, so the count-up cannot race itself.
cat > "$WORK/sendmail" <<EOF
#!/bin/sh
n=1
while [ -e "$WORK/mail-\$n.eml" ]; do n=\$((n + 1)); done
cat > "$WORK/mail-\$n.eml"
EOF
chmod +x "$WORK/sendmail"
export MAIL_COMMAND="$WORK/sendmail"
export MAIL_FROM='Catcrafts <info@catcrafts.net>'
"$SERVER" --serve "$PORT" --orders="$ORDERS" --rail=fake >"$WORK/server.log" 2>&1 &
SRV_PID=$!
@ -714,6 +728,71 @@ else
bad "customer series" "every invoice got its own customer uuid — series not shared"
fi
echo "== the confirmation email =="
# Every paid order gets exactly one confirmation with the signed invoice
# attached. Four orders were paid above; the mailer sweeps the ledger every
# 2 s, so all four messages should exist within a few sweeps.
i=0
until [ "$(ls "$WORK"/mail-*.eml 2>/dev/null | wc -l)" -ge 4 ]; do
i=$((i + 1))
if [ "$i" -gt 60 ]; then break; fi
sleep 0.25
done
n_mail=$(ls "$WORK"/mail-*.eml 2>/dev/null | wc -l)
if [ "$n_mail" -eq 4 ]; then
ok "one confirmation email per paid order ($n_mail sent)"
else
bad "confirmation email count" "expected 4, got $n_mail"
fi
# The NL order's message, found by its own order link (the same email address
# placed two orders, so the address alone would be ambiguous).
MAIL=$(grep -l "/order/$TOKEN" "$WORK"/mail-*.eml 2>/dev/null | head -n1)
if [ -n "$MAIL" ]; then
for probe in 'To: e2e@example.org' 'Subject: Catcrafts order CC-' \
'From: Catcrafts <info@catcrafts.net>' 'MIME-Version: 1.0' \
'€578.30' 'incl. 21% NL VAT' 'KVK 78437059' \
'BEGIN PGP SIGNED MESSAGE' 'filename="catcrafts-invoice-'; do
if grep -qF -- "$probe" "$MAIL"; then
ok "email has $probe"
else
bad "email content" "missing: $probe"
fi
done
# The attached invoice must verify offline exactly like the download.
sed -n '/BEGIN PGP SIGNED MESSAGE/,/END PGP SIGNATURE/p' "$MAIL" > "$WORK/mail-invoice.asc"
if gpg --verify "$WORK/mail-invoice.asc" >/dev/null 2>&1; then
ok "emailed invoice signature verifies with gpg"
else
bad "emailed invoice signature" "gpg --verify failed"
fi
else
bad "confirmation email" "no message links /order/$TOKEN"
fi
# The export order's message states the VAT treatment its invoice carries.
MAIL_CA=$(grep -l "/order/$TOKEN_CA" "$WORK"/mail-*.eml 2>/dev/null | head -n1)
if [ -n "$MAIL_CA" ] && grep -qF 'zero-rated export' "$MAIL_CA"; then
ok "export confirmation states the zero-rated treatment"
else
bad "export confirmation" "no message for the CA order, or no VAT note in it"
fi
# Idempotency comes from the ledger's notified event, not from luck in
# timing — sit out two more sweeps and expect no fifth message.
sleep 5
n_after=$(ls "$WORK"/mail-*.eml 2>/dev/null | wc -l)
if [ "$n_after" = "$n_mail" ]; then
ok "no order was emailed twice"
else
bad "email idempotency" "message count grew from $n_mail to $n_after"
fi
if grep -q '"type":"notified"' "$ORDERS"; then
ok "notified events recorded in the ledger"
else
bad "notified event" "no notified event in $ORDERS"
fi
else
echo "== checkout (coming soon) =="
# A perfectly valid order must be refused while the shop is closed: after