laterite-admin 0.9.21

Laterite CMF admin surface: the Axum router, session middleware, and login shell
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
//! A module mounting its own routes: an admin screen and a public endpoint.
//! Imports only the public API.

use axum::body::Body;
use axum::http::{Request, StatusCode};
use axum::routing::get;
use axum::Router;
use laterite_admin::routes::{PublicRoute, PublicRouteReg, RouteCtx, Screen, ScreenReg};
use laterite_admin::{router, AdminConfig, Contributions};
use laterite_auth::{password, store, AuthConfig, AuthService, NewOperator, RequestContext};
use laterite_core::{CatalogStore, Db};
use std::sync::Arc;
use tower::ServiceExt;

const SESSION_COOKIE: &str = "laterite_session";
const PERMISSION: &str = "acme.import";

async fn test_db() -> (Db, laterite_core::testing::TestGuard) {
    laterite_core::testing::connect_test(&laterite_admin::builtin_migrations()).await
}

/// A screen with two routes, one of which reports the base it mounted at, so the
/// test can assert a self-link is built from the resolved path.
struct Importer;

impl Screen for Importer {
    fn mount(&self, ctx: &RouteCtx) -> Router {
        let here = ctx.url("/step2");
        Router::new()
            .route("/", get(|| async { "import" }))
            .route("/where", get(move || async move { here }))
    }
}

fn app_in_menu(db: Db) -> Router {
    let auth = AuthService::new(db.clone(), AuthConfig::default());
    router(
        auth,
        db,
        Contributions {
            screens: vec![
                ScreenReg::new("/import", PERMISSION, Arc::new(Importer)).in_menu("Import places"),
                ScreenReg::new("/hidden", PERMISSION, Arc::new(Importer)),
            ],
            ..Default::default()
        },
        AdminConfig::default(),
        Arc::new(CatalogStore::default()),
    )
}

fn app(db: Db, base: &str) -> Router {
    let auth = AuthService::new(db.clone(), AuthConfig::default());
    router(
        auth,
        db,
        Contributions {
            screens: vec![ScreenReg::new(base, PERMISSION, Arc::new(Importer))],
            ..Default::default()
        },
        AdminConfig::default(),
        Arc::new(CatalogStore::default()),
    )
}

async fn body(router: Router, path: &str, token: &str) -> (StatusCode, String) {
    let resp = router
        .oneshot(
            Request::builder()
                .method("GET")
                .uri(path)
                .header("cookie", format!("{SESSION_COOKIE}={token}"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    let status = resp.status();
    let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
        .await
        .unwrap();
    (status, String::from_utf8(bytes.to_vec()).unwrap())
}

/// A superuser (who holds every permission) and their session token.
async fn superuser(db: &Db) -> String {
    let svc = AuthService::new(db.clone(), AuthConfig::default());
    svc.create_superuser(NewOperator {
        username: "root",
        email: "root@acme.test",
        first_name: "Root",
        last_name: None,
        password: "rootpw12345",
        timezone: None,
    })
    .await
    .unwrap();
    svc.authenticate("root", "rootpw12345", &RequestContext::default())
        .await
        .unwrap()
        .token
}

#[tokio::test]
async fn a_contributed_screen_serves_under_the_admin_mount() {
    let (db, _guard) = test_db().await;
    let token = superuser(&db).await;
    let (status, body) = body(app(db, "/import"), "/admin/import", &token).await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(body, "import");
}

#[tokio::test]
async fn a_screen_links_to_itself_through_its_resolved_base() {
    let (db, _guard) = test_db().await;
    let token = superuser(&db).await;
    // The screen never sees a literal path: it asks the context where it is.
    let (_, body) = body(app(db, "/import"), "/admin/import/where", &token).await;
    assert_eq!(body, "/admin/import/step2");
}

#[tokio::test]
async fn the_framework_enforces_the_screens_permission() {
    let (db, _guard) = test_db().await;
    let svc = AuthService::new(db.clone(), AuthConfig::default());
    // A superuser exists so the panel is past first-run setup.
    superuser(&db).await;
    let hash = password::hash_password("editorpw12345").unwrap();
    store::create_user(
        &db,
        "editor",
        "editor@acme.test",
        "Editor",
        None,
        &hash,
        false,
    )
    .await
    .unwrap();
    let token = svc
        .authenticate("editor", "editorpw12345", &RequestContext::default())
        .await
        .unwrap()
        .token;

    // The screen's own handler never checks anything; the guard runs first.
    let (status, _) = body(app(db, "/import"), "/admin/import", &token).await;
    assert_eq!(status, StatusCode::FORBIDDEN);
}

#[tokio::test]
async fn an_unauthenticated_request_never_reaches_the_screen() {
    let (db, _guard) = test_db().await;
    superuser(&db).await;
    let resp = app(db, "/import")
        .oneshot(
            Request::builder()
                .method("GET")
                .uri("/admin/import")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(
        resp.status(),
        StatusCode::SEE_OTHER,
        "redirected to sign in"
    );
}

/// A module's own extension point: a type the framework knows nothing about,
/// which other modules contribute and this module's route reads.
struct FeedReg {
    name: &'static str,
}

/// A public endpoint that reads what other modules contributed to it, and the
/// site's own origin, which is what a sitemap or a feed needs.
struct Feeds;

impl PublicRoute for Feeds {
    fn mount(&self, ctx: &RouteCtx) -> Router {
        let mut names: Vec<&str> = ctx
            .contributions::<FeedReg>()
            .iter()
            .map(|r| r.name)
            .collect();
        names.sort_unstable();
        let listed = names.join(",");
        let origin = ctx.base_url().to_string();
        Router::new()
            .route("/", get(move || async move { listed }))
            .route("/origin", get(move || async move { origin }))
    }
}

fn app_with_plugin_defined(db: Db) -> Router {
    app_with_origin(db, "")
}

fn app_with_origin(db: Db, origin: &str) -> Router {
    let auth = AuthService::new(db.clone(), AuthConfig::default());
    // AdminConfig is non-exhaustive, so it is built rather than literalled.
    let mut config = AdminConfig::default();
    config.origin = origin.to_string();
    // Two modules contribute a type only they know about; a third reads it.
    let mut registry = laterite_core::Registry::new();
    registry.set_owner(laterite_core::ModuleId::new("acme.blog"));
    registry.add(FeedReg { name: "posts" });
    registry.set_owner(laterite_core::ModuleId::new("acme.shop"));
    registry.add(FeedReg { name: "products" });

    router(
        auth,
        db,
        Contributions {
            public_routes: vec![PublicRouteReg::new("/feeds", Arc::new(Feeds))],
            plugin_defined: Arc::new(registry),
            ..Default::default()
        },
        config,
        Arc::new(CatalogStore::default()),
    )
}

#[tokio::test]
async fn a_route_reads_contributions_the_framework_never_learns_about() {
    let (db, _guard) = test_db().await;
    let app = app_with_plugin_defined(db);

    let resp = app
        .clone()
        .oneshot(
            Request::builder()
                .uri("/feeds")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
        .await
        .unwrap();
    // Both modules' contributions reached the reading module, whatever order
    // they registered in: everything is collected before any route runs.
    assert_eq!(String::from_utf8(body.to_vec()).unwrap(), "posts,products");
}

#[tokio::test]
async fn a_route_is_told_the_sites_own_origin() {
    let (db, _guard) = test_db().await;
    // Configured with a trailing slash, which the admin trims: a `<loc>` built
    // from this must not end up with a doubled separator.
    let resp = app_with_origin(db, "https://acme.example/")
        .oneshot(
            Request::builder()
                .uri("/feeds/origin")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
        .await
        .unwrap();
    // Absolute URLs come from configuration, never the request Host, which a
    // proxy controls.
    assert_eq!(
        String::from_utf8(body.to_vec()).unwrap(),
        "https://acme.example"
    );
}

/// A public endpoint: no session, no permission, a literal path.
struct Robots;

impl PublicRoute for Robots {
    fn mount(&self, ctx: &RouteCtx) -> Router {
        // A module asks where the panel is rather than assuming /admin.
        let panel = ctx.admin_path().to_string();
        Router::new()
            .route("/", get(|| async { "User-agent: *\nAllow: /\n" }))
            .route("/panel", get(move || async move { panel }))
    }
}

fn app_with_public(db: Db) -> Router {
    let auth = AuthService::new(db.clone(), AuthConfig::default());
    router(
        auth,
        db,
        Contributions {
            public_routes: vec![PublicRouteReg::new("/robots.txt", Arc::new(Robots))],
            ..Default::default()
        },
        AdminConfig::default(),
        Arc::new(CatalogStore::default()),
    )
}

#[tokio::test]
async fn a_public_route_answers_without_a_session() {
    let (db, _guard) = test_db().await;
    superuser(&db).await;
    let resp = app_with_public(db)
        .oneshot(
            Request::builder()
                .method("GET")
                .uri("/robots.txt")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
        .await
        .unwrap();
    assert!(String::from_utf8(bytes.to_vec())
        .unwrap()
        .contains("User-agent"));
}

#[tokio::test]
async fn a_public_route_does_not_disturb_the_admin() {
    let (db, _guard) = test_db().await;
    superuser(&db).await;
    // An unmatched URL still renders the styled 404 rather than the public route.
    let resp = app_with_public(db)
        .oneshot(
            Request::builder()
                .method("GET")
                .uri("/nothing-here")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn a_module_can_ask_where_the_panel_is() {
    let (db, _guard) = test_db().await;
    superuser(&db).await;
    let resp = app_with_public(db)
        .oneshot(
            Request::builder()
                .method("GET")
                .uri("/robots.txt/panel")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
        .await
        .unwrap();
    assert_eq!(String::from_utf8(bytes.to_vec()).unwrap(), "/admin");
}

#[tokio::test]
async fn a_screen_appears_in_the_menu_only_when_it_asks() {
    let (db, _guard) = test_db().await;
    let token = superuser(&db).await;
    let (status, html) = body(app_in_menu(db), "/admin", &token).await;
    assert_eq!(status, StatusCode::OK);
    assert!(
        html.contains("Import places"),
        "the labelled screen is listed"
    );
    assert!(html.contains("/admin/import"), "and links to itself");
    assert!(
        !html.contains("/admin/hidden"),
        "a screen with no label stays out of the menu"
    );
}

/// A public route is nested at its literal path, so a request *below* that path
/// enters the nested router and matches nothing there. What it must not do is
/// escape into axum's bare 404: the deployment's styled error page is the whole
/// point of routing these through the framework rather than beside it. Pinned
/// because fallback inheritance through `nest_service` is a behaviour, not a
/// guarantee we control.
#[tokio::test]
async fn a_path_below_a_public_route_still_gets_the_framework_404() {
    let (db, _guard) = test_db().await;
    superuser(&db).await;
    let resp = app_with_public(db)
        .oneshot(
            Request::builder()
                .method("GET")
                .uri("/robots.txt/nonsense")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
        .await
        .unwrap();
    let body = String::from_utf8(bytes.to_vec()).unwrap();
    assert!(
        body.contains("<!DOCTYPE html>") || body.contains("<html"),
        "expected the styled error page, got: {body:?}"
    );
}

/// A plugin has to be able to exercise its own routes without booting the
/// framework, which is what the public builder is for. It also defaults the admin
/// path to `/admin`, so a route that excludes the panel must be tested against a
/// different one to prove it reads the value rather than hardcoding it.
#[tokio::test]
async fn a_route_can_be_mounted_from_a_hand_built_context() {
    let (db, _guard) = test_db().await;
    let ctx = RouteCtx::builder(db)
        .admin_path("/backoffice")
        .base_url("https://acme.example")
        .build();

    assert_eq!(ctx.admin_path(), "/backoffice");
    assert_eq!(ctx.base_url(), "https://acme.example");

    let resp = Robots
        .mount(&ctx)
        .oneshot(
            Request::builder()
                .method("GET")
                .uri("/panel")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
        .await
        .unwrap();
    assert_eq!(String::from_utf8(bytes.to_vec()).unwrap(), "/backoffice");
}