Add worker service with e-mail delivery and dev Mailpit
- worker: sends email_outbox mail over SMTP (lettre), woken by LISTEN/NOTIFY, SKIP LOCKED claiming, retries with backoff, hu/en templates (text + HTML), hourly cleanup of expired data - migration 0002: next_attempt_at, failed_at, notify trigger - docker-compose.override.yml: Mailpit for development only - Dockerfiles: lock shared cargo cache mounts for parallel builds Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -0,0 +1,182 @@
|
||||
use serde_json::json;
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::email::{Delivery, Mailer, backoff};
|
||||
use crate::templates::{TemplateError, render};
|
||||
|
||||
#[test]
|
||||
fn templates_in_both_languages() {
|
||||
let payload = json!({"url": "https://example.com/verify-email?token=abc", "display_name": "Anna"});
|
||||
let hu = render("verify_email", "hu", &payload).unwrap();
|
||||
assert_eq!(hu.subject, "Erősítsd meg az e-mail-címed");
|
||||
assert!(hu.text.starts_with("Szia Anna!") && hu.text.contains("token=abc"));
|
||||
assert!(
|
||||
hu.html.contains(r#"<html lang="hu">"#)
|
||||
&& hu.html.contains(r#"href="https://example.com/verify-email?token=abc""#)
|
||||
);
|
||||
|
||||
let en = render("reset_password", "en", &payload).unwrap();
|
||||
assert_eq!(en.subject, "Set a new password");
|
||||
assert!(en.text.starts_with("Hi Anna,"));
|
||||
// Unknown locales fall back to English.
|
||||
assert_eq!(render("reset_password", "de", &payload).unwrap(), en);
|
||||
|
||||
let accepted = render("invite_accepted", "hu", &json!({"url": "u", "invitee_name": "Béla"})).unwrap();
|
||||
assert_eq!(accepted.subject, "Béla elfogadta a meghívásod");
|
||||
let anonymous = render("invite_accepted", "en", &json!({"url": "u", "invitee_name": null})).unwrap();
|
||||
assert_eq!(anonymous.subject, "Your invitation was accepted");
|
||||
assert!(anonymous.text.contains("Your invitee accepted"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn template_input_is_escaped_and_checked() {
|
||||
let mail = render("verify_email", "hu", &json!({"url": "u", "display_name": "<b>X</b>\r\nBcc: y"})).unwrap();
|
||||
assert!(mail.html.contains("Szia <b>X</b>Bcc: y!"));
|
||||
assert!(!mail.html.contains("<b>X"));
|
||||
assert_eq!(render("verify_email", "hu", &json!({})), Err(TemplateError::MissingField("url")));
|
||||
assert_eq!(render("nope", "hu", &json!({"url": "u"})), Err(TemplateError::UnknownTemplate("nope".into())));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn backoff_grows_and_is_capped() {
|
||||
assert_eq!(backoff(1).as_secs(), 60);
|
||||
assert_eq!(backoff(2).as_secs(), 120);
|
||||
assert_eq!(backoff(4).as_secs(), 480);
|
||||
assert_eq!(backoff(20).as_secs(), 6 * 3600);
|
||||
}
|
||||
|
||||
fn delivery(db: PgPool, fail: Vec<(&str, bool)>) -> Delivery {
|
||||
Delivery {
|
||||
db,
|
||||
mailer: Mailer::Memory {
|
||||
sent: Default::default(),
|
||||
fail: fail.into_iter().map(|(a, p)| (a.to_owned(), p)).collect(),
|
||||
},
|
||||
from: "Sorsfonalak <noreply@example.com>".parse().unwrap(),
|
||||
max_attempts: 3,
|
||||
}
|
||||
}
|
||||
|
||||
fn sent(d: &Delivery) -> Vec<(String, String)> {
|
||||
let Mailer::Memory { sent, .. } = &d.mailer else { unreachable!() };
|
||||
sent.lock().unwrap().iter().map(|(to, m)| (to.clone(), m.subject.clone())).collect()
|
||||
}
|
||||
|
||||
async fn queue(db: &PgPool, to: &str, template: &str) -> Uuid {
|
||||
let id = Uuid::now_v7();
|
||||
sqlx::query("INSERT INTO email_outbox (id, to_email, template, locale, payload) VALUES ($1, $2, $3, 'hu', $4)")
|
||||
.bind(id)
|
||||
.bind(to)
|
||||
.bind(template)
|
||||
.bind(json!({"url": "https://example.com/x"}))
|
||||
.execute(db)
|
||||
.await
|
||||
.unwrap();
|
||||
id
|
||||
}
|
||||
|
||||
type State = (i32, bool, bool, Option<String>, bool);
|
||||
|
||||
async fn state(db: &PgPool, id: Uuid) -> State {
|
||||
sqlx::query_as(
|
||||
"SELECT attempts, sent_at IS NOT NULL, failed_at IS NOT NULL, last_error, next_attempt_at > now() \
|
||||
FROM email_outbox WHERE id = $1",
|
||||
)
|
||||
.bind(id)
|
||||
.fetch_one(db)
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn delivers_retries_and_gives_up(db: PgPool) {
|
||||
let ok = queue(&db, "anna@example.com", "verify_email").await;
|
||||
let flaky = queue(&db, "flaky@example.com", "reset_password").await;
|
||||
let rejected = queue(&db, "rejected@example.com", "verify_email").await;
|
||||
let broken = queue(&db, "anna@example.com", "no_such_template").await;
|
||||
let d = delivery(db.clone(), vec![("flaky@example.com", false), ("rejected@example.com", true)]);
|
||||
|
||||
assert_eq!(d.deliver_due().await.unwrap(), 4);
|
||||
assert_eq!(sent(&d), vec![("anna@example.com".into(), "Erősítsd meg az e-mail-címed".into())]);
|
||||
assert_eq!(state(&db, ok).await, (1, true, false, None, false));
|
||||
// Temporary failure: retried later, not at once.
|
||||
assert_eq!(state(&db, flaky).await, (1, false, false, Some("test failure".into()), true));
|
||||
// Permanent failures are not retried.
|
||||
assert_eq!(state(&db, rejected).await, (1, false, true, Some("test failure".into()), false));
|
||||
let (_, _, failed, error, _) = state(&db, broken).await;
|
||||
assert!(failed && error.unwrap().contains("unknown e-mail template"));
|
||||
assert_eq!(d.deliver_due().await.unwrap(), 0);
|
||||
|
||||
// Retries until max_attempts (3), then gives up.
|
||||
for expected in [2, 3] {
|
||||
sqlx::query("UPDATE email_outbox SET next_attempt_at = now() WHERE id = $1")
|
||||
.bind(flaky)
|
||||
.execute(&db)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(d.deliver_due().await.unwrap(), 1);
|
||||
assert_eq!(state(&db, flaky).await.0, expected);
|
||||
}
|
||||
let (_, sent_ok, failed, _, _) = state(&db, flaky).await;
|
||||
assert!(!sent_ok && failed);
|
||||
assert_eq!(sent(&d).len(), 1);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn concurrent_workers_do_not_send_twice(db: PgPool) {
|
||||
for i in 0..15 {
|
||||
queue(&db, &format!("u{i}@example.com"), "verify_email").await;
|
||||
}
|
||||
let (a, b) = (delivery(db.clone(), vec![]), delivery(db.clone(), vec![]));
|
||||
let (na, nb) = tokio::join!(async { a.deliver_due().await.unwrap() }, async { b.deliver_due().await.unwrap() });
|
||||
let (ra, rb) = (a.deliver_due().await.unwrap(), b.deliver_due().await.unwrap());
|
||||
assert_eq!(na + nb + ra + rb, 15);
|
||||
let mut all: Vec<String> = sent(&a).into_iter().chain(sent(&b)).map(|(to, _)| to).collect();
|
||||
all.sort();
|
||||
all.dedup();
|
||||
assert_eq!(all.len(), 15);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn insert_notifies_the_worker(db: PgPool) {
|
||||
let mut listener = sqlx::postgres::PgListener::connect_with(&db).await.unwrap();
|
||||
listener.listen("email_outbox").await.unwrap();
|
||||
queue(&db, "anna@example.com", "verify_email").await;
|
||||
let n = tokio::time::timeout(std::time::Duration::from_secs(5), listener.recv()).await.unwrap().unwrap();
|
||||
assert_eq!(n.channel(), "email_outbox");
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn cleanup_removes_only_expired_data(db: PgPool) {
|
||||
let user = Uuid::now_v7();
|
||||
sqlx::query("INSERT INTO users (id, email) VALUES ($1, 'anna@example.com')").bind(user).execute(&db).await.unwrap();
|
||||
for (hash, expires) in
|
||||
[(b"live".as_slice(), "now() + interval '1 day'"), (b"dead".as_slice(), "now() - interval '1 second'")]
|
||||
{
|
||||
sqlx::query(&format!(
|
||||
"INSERT INTO sessions (id, user_id, token_hash, expires_at) VALUES ($1, $2, $3, {expires})"
|
||||
))
|
||||
.bind(Uuid::now_v7())
|
||||
.bind(user)
|
||||
.bind(hash)
|
||||
.execute(&db)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
let old = queue(&db, "anna@example.com", "verify_email").await;
|
||||
let recent = queue(&db, "anna@example.com", "verify_email").await;
|
||||
let unsent = queue(&db, "anna@example.com", "verify_email").await;
|
||||
sqlx::query("UPDATE email_outbox SET sent_at = now() - interval '8 days' WHERE id = $1")
|
||||
.bind(old)
|
||||
.execute(&db)
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query("UPDATE email_outbox SET sent_at = now() WHERE id = $1").bind(recent).execute(&db).await.unwrap();
|
||||
|
||||
let deleted = crate::cleanup::run_once(&db).await.unwrap();
|
||||
assert_eq!(deleted, vec![("sessions", 1), ("auth_tokens", 0), ("emails", 1), ("invites", 0)]);
|
||||
let (left,): (Vec<Uuid>,) =
|
||||
sqlx::query_as("SELECT array_agg(id ORDER BY id) FROM email_outbox").fetch_one(&db).await.unwrap();
|
||||
assert_eq!(left, vec![recent, unsent]);
|
||||
}
|
||||
Reference in New Issue
Block a user