okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

Aly Raffaufrefactor(gleam): clean up code and enforce focused lint checksb585313

main
1.7 KiB55 linesraw
1//// A full inbox must reject new webhook deliveries rather than silently lose mail.
2
3import albedo/daemon/store
4import albedo/harness/extensions/webhooks/ledger as webhooks
5import gleam/bit_array
6import gleam/dynamic/decode
7import gleam/option.{None}
8import gleeunit/should
9import sqlight
10
11@external(erlang, "albedo_webhooks_test_support", "sign")
12fn sign(body: BitArray, secret: String) -> String
13
14fn headers(signature: String) -> List(#(String, String)) {
15  [#("x-albedo-signature", signature)]
16}
17
18fn database() -> store.Store {
19  let assert Ok(db) =
20    store.start(
21      ":memory:",
22      "PRAGMA foreign_keys=ON; CREATE TABLE sessions(id TEXT PRIMARY KEY); INSERT INTO sessions VALUES('infra'),('other');",
23    )
24  let assert Ok(Nil) = webhooks.initialise(db)
25  db
26}
27
28pub fn full_inbox_refuses_new_delivery_test() -> Nil {
29  let db = database()
30  let assert Ok(webhooks.Provisioned(hook, key)) =
31    webhooks.create(db, webhooks.Human, "infra", "outage", None)
32  store.query(db, fn(connection) {
33    sqlight.query(
34      "WITH RECURSIVE nums(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM nums WHERE n<1000) INSERT INTO mail(id,recipient,sender_name,kind,body,created_at) SELECT 'seed-'||n,'infra','outage','webhook','x',0 FROM nums",
35      connection,
36      [],
37      decode.dynamic,
38    )
39  })
40  |> should.be_ok
41  let body = bit_array.from_string("outage")
42  webhooks.accept(db, hook.id, headers(sign(body, key)), body, None)
43  |> should.equal(Error(webhooks.Overloaded))
44  // The refused delivery left no half behind.
45  store.query(db, fn(connection) {
46    sqlight.query(
47      "SELECT count(*) FROM webhook_deliveries",
48      connection,
49      [],
50      decode.field(0, decode.int, decode.success),
51    )
52  })
53  |> should.equal(Ok([0]))
54  store.close(db)
55}