okami.mom/albedo
git clone https://git.nkp.pet/okami.mom/albedo
b585313
main
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 { 19let assert Ok ( db ) = 20store . start ( 21":memory:" , 22"PRAGMA foreign_keys=ON; CREATE TABLE sessions(id TEXT PRIMARY KEY); INSERT INTO sessions VALUES('infra'),('other');" , 23) 24let assert Ok ( Nil ) = webhooks . initialise ( db ) 25db 26} 27 28pub fn full_inbox_refuses_new_delivery_test () -> Nil { 29let db = database () 30let assert Ok ( webhooks . Provisioned ( hook , key )) = 31webhooks . create ( db , webhooks . Human , "infra" , "outage" , None ) 32store . query ( db , fn ( connection ) { 33sqlight . 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" , 35connection , 36[], 37decode . dynamic , 38) 39}) 40|> should . be_ok 41let body = bit_array . from_string ( "outage" ) 42webhooks . accept ( db , hook . id , headers ( sign ( body , key )), body , None ) 43|> should . equal ( Error ( webhooks . Overloaded )) 44// The refused delivery left no half behind. 45store . query ( db , fn ( connection ) { 46sqlight . query ( 47"SELECT count(*) FROM webhook_deliveries" , 48connection , 49[], 50decode . field ( 0 , decode . int , decode . success ), 51) 52}) 53|> should . equal ( Ok ([ 0 ])) 54store . close ( db ) 55}