renox 1.0.0

A batteries-included web framework for Rust, inspired by Laravel: Axum, HTMX and Alpine.js, SQLite or PostgreSQL, deployed as one binary.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! Webhooks: verified, stored once per event, processed in the queue, and
//! still received in maintenance mode.

use std::sync::atomic::{AtomicBool, Ordering};

use renox::prelude::*;
use renox::testing::TestApp;
use renox::webhook;
use serde::Deserialize;

const SECRET: &str = "whsec_test";

/// A provider that signs the body with HMAC-SHA256 in `X-Signature`.
struct Pay;

#[derive(Deserialize)]
struct Event {
    id: String,
    order: String,
}

/// Lets a test make processing fail, then succeed.
static BROKEN: AtomicBool = AtomicBool::new(false);

impl Webhook for Pay {
    const PROVIDER: &'static str = "pay";

    fn verify(request: &WebhookRequest, _: &AppState) -> Result {
        let signature = request.header("x-signature").unwrap_or_default();
        webhook::ensure(webhook::verify_hmac_sha256(
            SECRET,
            &request.body,
            signature,
        ))
    }

    fn event_id(request: &WebhookRequest) -> Result<String> {
        Ok(request.json::<Event>()?.id)
    }

    async fn handle(call: WebhookCall, ctx: JobContext) -> Result {
        let event: Event = call.json()?;
        if event.order == "broken" && BROKEN.load(Ordering::SeqCst) {
            return Err(Error::BadRequest("the shop is closed".into()));
        }
        ctx.state
            .cache
            .put(&format!("paid:{}", event.order), &true, None)
            .await
    }
}

struct Shop;

impl Module for Shop {
    fn name(&self) -> &'static str {
        "shop"
    }

    fn routes(&self) -> Routes {
        Routes::new()
            .get("/", || async { "home" })
            .name("home")
            .webhook::<Pay>("/webhooks/pay")
    }

    fn register(&self, app: &mut Registry) {
        app.webhook::<Pay>();
    }
}

async fn app() -> TestApp {
    TestApp::new(App::new().module(Shop)).await
}

fn body(id: &str, order: &str) -> String {
    format!(r#"{{"id":"{id}","order":"{order}"}}"#)
}

async fn send(app: &TestApp, body: &str, signature: &str) -> renox::testing::TestResponse {
    app.request()
        .without_csrf()
        .header("x-signature", signature)
        .post_body("/webhooks/pay", "application/json", body)
        .await
}

async fn paid(app: &TestApp, order: &str) -> bool {
    app.state()
        .cache
        .get::<bool>(&format!("paid:{order}"))
        .await
        .unwrap()
        .unwrap_or(false)
}

#[renox::test]
async fn verified_calls_are_stored_once_and_processed_in_the_queue() {
    let app = app().await;
    let call = body("evt_1", "A-1");
    let signature = webhook::hmac_sha256_hex(SECRET, &call);

    send(&app, &call, &signature)
        .await
        .assert_ok()
        .assert_see("ok");
    app.assert_database_has(
        "webhook_calls",
        &[
            ("provider", &"pay"),
            ("event_id", &"evt_1"),
            ("status", &"received"),
        ],
    )
    .await;
    assert!(!paid(&app, "A-1").await, "processed later, by a worker");
    assert_eq!(app.queued_jobs().await, ["renox:webhook"]);

    // The provider retries: answered 200, not stored or queued again.
    send(&app, &call, &signature)
        .await
        .assert_ok()
        .assert_see("already received");
    app.assert_database_count("webhook_calls", 1).await;
    assert_eq!(app.queued_jobs().await.len(), 1);

    app.run_jobs().await;
    assert!(paid(&app, "A-1").await);
    app.assert_database_has(
        "webhook_calls",
        &[("event_id", &"evt_1"), ("status", &"processed")],
    )
    .await;
}

#[renox::test]
async fn forged_or_incomplete_calls_are_refused() {
    let app = app().await;
    let call = body("evt_2", "A-2");
    send(&app, &call, &webhook::hmac_sha256_hex("guess", &call))
        .await
        .assert_status(401);
    send(&app, &call, "").await.assert_status(401);
    let no_id = r#"{"order":"A-2"}"#;
    send(&app, no_id, &webhook::hmac_sha256_hex(SECRET, no_id))
        .await
        .assert_status(400);
    app.assert_database_count("webhook_calls", 0).await;
}

#[renox::test]
async fn failed_calls_can_be_retried() {
    let app = app().await;
    let call = body("evt_3", "broken");
    BROKEN.store(true, Ordering::SeqCst);
    send(&app, &call, &webhook::hmac_sha256_hex(SECRET, &call))
        .await
        .assert_ok();
    app.run_jobs().await;
    let failed = webhook::WebhookCall::failed(app.db()).await.unwrap();
    assert_eq!(failed.len(), 1);
    assert!(
        failed[0]
            .error
            .as_deref()
            .unwrap()
            .contains("the shop is closed")
    );

    BROKEN.store(false, Ordering::SeqCst);
    assert!(webhook::retry(app.state(), failed[0].id).await.unwrap());
    // The first job is still waiting for its retry backoff; the new one runs now.
    app.run_jobs().await;
    assert!(paid(&app, "broken").await);
    assert!(
        webhook::WebhookCall::failed(app.db())
            .await
            .unwrap()
            .is_empty()
    );
    assert!(!webhook::retry(app.state(), 999).await.unwrap());
}

#[renox::test]
async fn webhooks_work_in_maintenance_mode_and_show_in_route_list() {
    let app = app().await;
    renox::maintenance::down(
        &app.state().config.storage_path,
        renox::maintenance::DownOptions::new(),
    )
    .unwrap();
    app.get("/").await.assert_status(503);
    let call = body("evt_4", "A-4");
    send(&app, &call, &webhook::hmac_sha256_hex(SECRET, &call))
        .await
        .assert_ok();

    let route = app
        .kernel()
        .routes()
        .iter()
        .find(|r| r.path == "/webhooks/pay")
        .unwrap();
    assert_eq!(route.name.as_deref(), Some("webhooks.pay"));
    assert_eq!(route.middleware, ["no-csrf", "webhook:pay"]);
}

#[renox::test]
async fn a_webhook_route_needs_its_registration() {
    struct Forgetful;

    impl Module for Forgetful {
        fn name(&self) -> &'static str {
            "forgetful"
        }

        fn routes(&self) -> Routes {
            Routes::new().webhook::<Pay>("/webhooks/pay")
        }
    }

    let err = App::with_config(Config::default())
        .module(Forgetful)
        .boot()
        .await
        .err()
        .unwrap();
    assert!(
        format!("{err:?}").contains("add `app.webhook::<…>()`"),
        "{err:?}"
    );
}

/// #256: a provider without an event id in the body, and the processing job
/// meeting a call whose provider is gone.
#[renox::test]
async fn calls_without_an_id_and_calls_for_a_provider_thats_gone() {
    let app = app().await;
    let body = r#"{"id": "  ", "order": "A-1"}"#;
    send(&app, body, &webhook::hmac_sha256_hex(SECRET, body))
        .await
        .assert_status(400)
        .assert_see("the webhook has no event id");

    // A stored call whose provider this app no longer registers.
    let body = r#"{"id": "evt_gone", "order": "A-2"}"#;
    send(&app, body, &webhook::hmac_sha256_hex(SECRET, body))
        .await
        .assert_ok();
    renox::db::sql("UPDATE webhook_calls SET provider = ?")
        .bind("gone")
        .execute(app.db())
        .await
        .unwrap();
    app.run_jobs().await;
    let id: i64 = renox::db::sql("SELECT id FROM webhook_calls")
        .scalar(app.db())
        .await
        .unwrap();
    let call = WebhookCall::find(app.db(), id).await.unwrap().unwrap();
    assert_eq!(call.status, webhook::WebhookStatus::Failed);
    assert_eq!(
        call.error.as_deref(),
        Some("no webhook `gone` is registered")
    );
    assert_eq!(call.text().unwrap(), body);
    assert!(call.form::<Vec<(String, String)>>().is_ok());

    // A missing secret names the variable.
    let err = webhook::secret(app.state(), "PAY_SECRET").unwrap_err();
    assert!(
        format!("{err:?}").contains("set PAY_SECRET in .env"),
        "{err:?}"
    );
}

/// A provider that posts a form, read with `WebhookRequest::form`.
struct FormPay;

impl Webhook for FormPay {
    const PROVIDER: &'static str = "form-pay";

    fn verify(_: &WebhookRequest, _: &AppState) -> Result {
        Ok(())
    }

    fn event_id(request: &WebhookRequest) -> Result<String> {
        Ok(request.form::<Event>()?.id)
    }

    async fn handle(_call: WebhookCall, _ctx: JobContext) -> Result {
        Ok(())
    }
}

struct FormShop;

impl Module for FormShop {
    fn name(&self) -> &'static str {
        "form-shop"
    }

    fn routes(&self) -> Routes {
        Routes::new().webhook::<FormPay>("/webhooks/form-pay")
    }

    fn register(&self, app: &mut Registry) {
        app.webhook::<FormPay>();
    }
}

/// #256: form bodies, a store that can't be written (the provider sends
/// again), and the job meeting a call that is gone or already processed.
#[renox::test]
async fn form_calls_failed_stores_and_calls_done_meanwhile() {
    let app = TestApp::new(App::new().module(Shop).module(FormShop)).await;
    let post_form = |body: &'static str| {
        let app = &app;
        async move {
            app.request()
                .without_csrf()
                .post_body(
                    "/webhooks/form-pay",
                    "application/x-www-form-urlencoded",
                    body,
                )
                .await
        }
    };
    post_form("id=evt_f1&order=F-1")
        .await
        .assert_ok()
        .assert_see("ok");
    // Not a form with an `id`: refused.
    post_form("order=F-2").await.assert_status(400);

    // Gone before its job ran, and processed before its job ran: both jobs
    // end quietly.
    let gone = body("evt_gone", "G-1");
    send(&app, &gone, &webhook::hmac_sha256_hex(SECRET, &gone))
        .await
        .assert_ok();
    let done = body("evt_done", "D-1");
    send(&app, &done, &webhook::hmac_sha256_hex(SECRET, &done))
        .await
        .assert_ok();
    renox::db::sql("DELETE FROM webhook_calls WHERE event_id LIKE ?")
        .bind("%evt_gone")
        .execute(app.db())
        .await
        .unwrap();
    renox::db::sql("UPDATE webhook_calls SET status = 'processed' WHERE event_id LIKE ?")
        .bind("%evt_done")
        .execute(app.db())
        .await
        .unwrap();
    app.run_jobs().await;
    assert!(!paid(&app, "G-1").await && !paid(&app, "D-1").await);
    app.assert_database_count("failed_jobs", 0).await;

    // A call that failed before (its first try) is handled again by its job
    // and ends processed.
    let again = body("evt_again", "A-1");
    send(&app, &again, &webhook::hmac_sha256_hex(SECRET, &again))
        .await
        .assert_ok();
    renox::db::sql(
        "UPDATE webhook_calls SET status = 'failed', error = 'earlier' WHERE event_id LIKE ?",
    )
    .bind("%evt_again")
    .execute(app.db())
    .await
    .unwrap();
    app.run_jobs().await;
    assert!(paid(&app, "A-1").await);
    let status: String = renox::db::sql("SELECT status FROM webhook_calls WHERE event_id LIKE ?")
        .bind("%evt_again")
        .scalar(app.db())
        .await
        .unwrap();
    assert_eq!(status, "processed");

    // The table can't be written: a 500, so the provider sends it again.
    renox::db::sql("ALTER TABLE webhook_calls RENAME TO webhook_calls_away")
        .execute(app.db())
        .await
        .unwrap();
    let later = body("evt_later", "L-1");
    send(&app, &later, &webhook::hmac_sha256_hex(SECRET, &later))
        .await
        .assert_status(500);
}

#[renox::test]
async fn a_webhook_registered_twice_stops_the_boot() {
    struct Twice;

    impl Module for Twice {
        fn name(&self) -> &'static str {
            "twice"
        }

        fn register(&self, app: &mut Registry) {
            app.webhook::<Pay>();
            app.webhook::<Pay>();
        }
    }

    let err = App::with_config(Config::default())
        .module(Twice)
        .boot()
        .await
        .err()
        .unwrap();
    assert!(
        format!("{err:?}").contains("`pay` is registered twice"),
        "{err:?}"
    );
}

/// The routes only: the app itself registers the webhook.
struct PayRoutes;

impl Module for PayRoutes {
    fn name(&self) -> &'static str {
        "pay-routes"
    }

    fn routes(&self) -> Routes {
        Routes::new().webhook::<Pay>("/webhooks/pay")
    }
}

/// `App::webhook` registers a webhook without a module's `register`.
#[renox::test]
async fn the_app_can_register_a_webhook_itself() {
    let app = TestApp::new(App::new().webhook::<Pay>().module(PayRoutes)).await;
    let call = body("evt_app", "P-1");
    send(&app, &call, &webhook::hmac_sha256_hex(SECRET, &call))
        .await
        .assert_ok();
    app.run_jobs().await;
    assert!(paid(&app, "P-1").await);
}