Phase 11, step 2: the journal's daily anchor by mail
Every day from 07:00 Chiang Mai time the app mails the journal's last entry number and its hash to the addresses set on the Notifications page (src/anchor.rs, migration 0012: settings, journal_anchors), when anything was added since the last anchor that went out. A day without news is noted without a mail; a failed send is tried again after an hour; a transaction-level advisory lock keeps two runs from both sending. "Send the anchor now" sends at once. Each mail says whether the chain checked whole (in capitals when it did not) and links to /journal/anchor, which compares a kept entry number and hash with the journal now: the same hash, another one, or no such entry. Changing the recipients is itself a journal entry. Every anchor mail is in the notifications log. Tests: the daily rule (before seven, once a day, an hour after a failure), recipients checked and journaled, one mail per recipient with the head hash, unchanged days, by hand, the check page's three answers, and a chain broken around the app said in the mail. 126 unit, 67 integration.
This commit is contained in:
1 parent
94ad25b6fc
commit
72a4faf71a
14 files changed
+990
-22
No files matched your search
@@ -188,3 +188,220 @@ async fn without_a_mail_server_the_page_says_so(pool: PgPool) {
|
||||
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
assert!(r.text().contains("Mail is not set up on this server"));
|
||||
}
|
||||
|
||||
/// A mail's text as the reader sees it: quoted-printable decoded (lettre
|
||||
/// encodes long lines so).
|
||||
fn readable(data: &str) -> String {
|
||||
let joined = data.replace("=\n", "");
|
||||
let b = joined.as_bytes();
|
||||
let mut out = Vec::new();
|
||||
let mut i = 0;
|
||||
while i < b.len() {
|
||||
if b[i] == b'='
|
||||
&& i + 2 < b.len()
|
||||
&& let Ok(v) = u8::from_str_radix(&joined[i + 1..i + 3], 16)
|
||||
{
|
||||
out.push(v);
|
||||
i += 3;
|
||||
continue;
|
||||
}
|
||||
out.push(b[i]);
|
||||
i += 1;
|
||||
}
|
||||
String::from_utf8_lossy(&out).into_owned()
|
||||
}
|
||||
|
||||
/// Chiang Mai time on Nov 22, 2026 plus `days`.
|
||||
fn cm(days: i64, h: u32, m: u32) -> chrono::DateTime<chrono::Utc> {
|
||||
use chrono::TimeZone;
|
||||
visa::dates::bangkok()
|
||||
.with_ymd_and_hms(2026, 11, 22, h, m, 0)
|
||||
.unwrap()
|
||||
.with_timezone(&chrono::Utc)
|
||||
+ chrono::Duration::days(days)
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
|
||||
async fn the_journal_anchor_goes_out_daily_and_checks_later(pool: PgPool) {
|
||||
let (port, got) = fake_smtp().await;
|
||||
let cfg = with_mail(port);
|
||||
let app = with_config(pool.clone(), cfg.clone());
|
||||
let r = app
|
||||
.post(
|
||||
"/lawyers",
|
||||
&[("name", "Malee Rakdee"), ("status", "active")],
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::SEE_OTHER);
|
||||
|
||||
// nobody to send to yet: the day's run does nothing
|
||||
let out = visa::anchor::run_daily(&cfg, &pool, cm(0, 7, 5))
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(
|
||||
matches!(out, Some(visa::anchor::Outcome::Nothing(_))),
|
||||
"{out:?}"
|
||||
);
|
||||
sqlx::query("DELETE FROM journal_anchors")
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// the recipients: checked, saved, and the change is in the journal
|
||||
let r = app
|
||||
.post(
|
||||
"/notifications/anchor-to",
|
||||
&[("anchor_to", "lead@example.com, nope")],
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
assert!(
|
||||
r.text().contains("Not an email address: nope."),
|
||||
"{}",
|
||||
r.text()
|
||||
);
|
||||
let r = app
|
||||
.post(
|
||||
"/notifications/anchor-to",
|
||||
&[("anchor_to", "lead@example.com, archive@example.org")],
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::SEE_OTHER);
|
||||
let said: String =
|
||||
sqlx::query_scalar("SELECT detail FROM journal WHERE event = 'anchor recipients changed'")
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
said,
|
||||
"The journal's daily anchor goes to lead@example.com, archive@example.org (before: nobody)."
|
||||
);
|
||||
let page = app.get("/notifications").await.text();
|
||||
assert!(
|
||||
page.contains("value=\"lead@example.com, archive@example.org\""),
|
||||
"{page}"
|
||||
);
|
||||
|
||||
// before seven nothing goes; at seven one mail to each, with the head
|
||||
assert!(
|
||||
visa::anchor::run_daily(&cfg, &pool, cm(0, 6, 50))
|
||||
.await
|
||||
.unwrap()
|
||||
.is_none()
|
||||
);
|
||||
let out = visa::anchor::run_daily(&cfg, &pool, cm(0, 7, 5))
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(
|
||||
matches!(
|
||||
out,
|
||||
Some(visa::anchor::Outcome::Sent {
|
||||
failed: 0,
|
||||
to: 2,
|
||||
..
|
||||
})
|
||||
),
|
||||
"{out:?}"
|
||||
);
|
||||
let check = visa::journal::verify(&pool).await.unwrap();
|
||||
let mails = got.lock().unwrap().clone();
|
||||
assert_eq!(mails.len(), 2);
|
||||
assert_eq!(mails[1].to, ["<archive@example.org>"]);
|
||||
let m = &readable(&mails[0].data);
|
||||
assert!(m.contains(&format!("Its hash: {}", check.head)), "{m}");
|
||||
assert!(m.contains("Last entry: number 2,"), "{m}");
|
||||
assert!(m.contains("The chain checked whole"), "{m}");
|
||||
assert!(
|
||||
m.contains(&format!("/journal/anchor?seq=2&hash={}", check.head)),
|
||||
"{m}"
|
||||
);
|
||||
// once a day; the next day with nothing new: no mail, noted
|
||||
assert!(
|
||||
visa::anchor::run_daily(&cfg, &pool, cm(0, 9, 0))
|
||||
.await
|
||||
.unwrap()
|
||||
.is_none()
|
||||
);
|
||||
let out = visa::anchor::run_daily(&cfg, &pool, cm(1, 7, 1))
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(
|
||||
matches!(out, Some(visa::anchor::Outcome::Unchanged)),
|
||||
"{out:?}"
|
||||
);
|
||||
assert_eq!(got.lock().unwrap().len(), 2);
|
||||
// by hand it goes at once
|
||||
let r = app.post("/notifications/anchor-now", &[]).await;
|
||||
assert_eq!(r.status, StatusCode::SEE_OTHER);
|
||||
assert_eq!(r.location(), "/notifications?anchor=sent#anchor");
|
||||
assert_eq!(got.lock().unwrap().len(), 4);
|
||||
let logged: i64 =
|
||||
sqlx::query_scalar("SELECT count(*) FROM notifications WHERE purpose = 'anchor'")
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(logged, 4);
|
||||
let page = app.get("/notifications").await.text();
|
||||
assert!(page.contains("id=\"anchor-log\""), "{page}");
|
||||
assert!(page.contains(">unchanged<"), "{page}");
|
||||
|
||||
// the check: the kept hash still matches ...
|
||||
let t = app
|
||||
.get(&format!("/journal/anchor?seq=2&hash={}", check.head))
|
||||
.await
|
||||
.text();
|
||||
assert!(t.contains("Entry 2 still has this hash."), "{t}");
|
||||
// ... another one does not, and an entry taken out shows
|
||||
let t = app
|
||||
.get(&format!("/journal/anchor?seq=2&hash={}", "ab".repeat(32)))
|
||||
.await
|
||||
.text();
|
||||
assert!(t.contains("Entry 2 has a different hash now."), "{t}");
|
||||
let t = app
|
||||
.get(&format!("/journal/anchor?seq=99&hash={}", check.head))
|
||||
.await
|
||||
.text();
|
||||
assert!(t.contains("There is no entry 99."), "{t}");
|
||||
let r = app.get("/journal/anchor?seq=x&hash=short").await;
|
||||
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
|
||||
async fn a_broken_chain_is_said_in_the_anchor_mail(pool: PgPool) {
|
||||
let (port, got) = fake_smtp().await;
|
||||
let cfg = with_mail(port);
|
||||
let app = with_config(pool.clone(), cfg.clone());
|
||||
app.post(
|
||||
"/lawyers",
|
||||
&[("name", "Malee Rakdee"), ("status", "active")],
|
||||
)
|
||||
.await;
|
||||
app.post(
|
||||
"/notifications/anchor-to",
|
||||
&[("anchor_to", "lead@example.com")],
|
||||
)
|
||||
.await;
|
||||
// a change made around the app
|
||||
sqlx::query("ALTER TABLE journal DISABLE TRIGGER journal_no_change")
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query("UPDATE journal SET detail = 'something else' WHERE seq = 1")
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let out = visa::anchor::run_daily(&cfg, &pool, cm(0, 7, 5))
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(
|
||||
matches!(out, Some(visa::anchor::Outcome::Sent { .. })),
|
||||
"{out:?}"
|
||||
);
|
||||
let m = readable(&got.lock().unwrap()[0].data);
|
||||
assert!(m.contains("WARNING: THE CHAIN DID NOT CHECK WHOLE"), "{m}");
|
||||
let whole: bool = sqlx::query_scalar("SELECT whole FROM journal_anchors")
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(!whole);
|
||||
}
|
||||
Reference in new issue
Block a user