laterite-web 0.9.17

Laterite CMF public web layer: themes, static-site generation, page metadata, canonical URLs, and sitemaps
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//! The Themes screen: gated by its permission, creating a theme and a
//! template, refusing a template that does not parse. Imports only the
//! public API.

use laterite_admin::axum::body::Body;
use laterite_admin::axum::http::{Request, StatusCode};
use laterite_admin::axum::Router;
use laterite_admin::{router, AdminConfig, Contributions};
use laterite_auth::{
    password, store as auth_store, AuthConfig, AuthService, NewOperator, RequestContext,
};
use laterite_core::{CatalogStore, Db, Module};
use laterite_web::theme::{self, store, Kind, ThemeModule};
use std::sync::Arc;
use tower::ServiceExt;

const SESSION_COOKIE: &str = "laterite_session";

async fn test_db() -> (Db, laterite_core::testing::TestGuard) {
    let mut migrations = laterite_admin::builtin_migrations();
    migrations.push(ThemeModule.migrations());
    laterite_core::testing::connect_test(&migrations).await
}

fn app(db: &Db) -> Router {
    let mut config = AdminConfig::default();
    config.origin = "https://acme.test".to_string();
    let mut contributions = Contributions::default();
    contributions.permissions.push(theme::admin::permission());
    contributions.screens.push(theme::admin::screen());
    contributions.settings.push(theme::admin::settings_item());
    router(
        AuthService::new(db.clone(), AuthConfig::default()),
        db.clone(),
        contributions,
        config,
        Arc::new(CatalogStore::default()),
    )
}

/// A superuser's session and a plain operator's, the first with a known
/// request token.
async fn sessions(db: &Db) -> (String, 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();
    let hash = password::hash_password("plainpw12345").unwrap();
    auth_store::create_user(db, "plain", "plain@acme.test", "Plain", None, &hash, false)
        .await
        .unwrap();
    let mut tokens = Vec::new();
    for (user, pw) in [("root", "rootpw12345"), ("plain", "plainpw12345")] {
        tokens.push(
            svc.authenticate(user, pw, &RequestContext::default())
                .await
                .unwrap()
                .token,
        );
    }
    svc.set_session_data(&tokens[0], r#"{"v":1,"csrf":"tok-123"}"#)
        .await
        .unwrap();
    (tokens.remove(0), tokens.remove(0))
}

async fn get(app: &Router, path: &str, session: &str) -> (StatusCode, String) {
    let req = Request::builder()
        .uri(path)
        .header("cookie", format!("{SESSION_COOKIE}={session}"))
        .body(Body::empty())
        .unwrap();
    let response = app.clone().oneshot(req).await.unwrap();
    let status = response.status();
    let bytes = laterite_admin::axum::body::to_bytes(response.into_body(), usize::MAX)
        .await
        .unwrap();
    (status, String::from_utf8_lossy(&bytes).to_string())
}

/// Posts a form as the root session and returns the status and the redirect.
async fn post(app: &Router, path: &str, session: &str, body: &str) -> (StatusCode, String) {
    let req = Request::builder()
        .method("POST")
        .uri(path)
        .header("cookie", format!("{SESSION_COOKIE}={session}"))
        .header("content-type", "application/x-www-form-urlencoded")
        .header("sec-fetch-site", "same-origin")
        .header("x-csrf-token", "tok-123")
        .body(Body::from(body.to_string()))
        .unwrap();
    let response = app.clone().oneshot(req).await.unwrap();
    let status = response.status();
    let location = response
        .headers()
        .get("location")
        .and_then(|v| v.to_str().ok())
        .unwrap_or_default()
        .to_string();
    (status, location)
}

#[tokio::test]
async fn the_screen_is_gated_and_lists_themes() {
    let (db, _g) = test_db().await;
    let app = app(&db);
    let (root, plain) = sessions(&db).await;
    store::create_theme(&db, "base", "name: Base\n")
        .await
        .unwrap();
    theme::set_active_theme(&db, "base").await.unwrap();

    let (status, html) = get(&app, "/admin/themes", &root).await;
    assert_eq!(status, StatusCode::OK);
    assert!(html.contains("Base") && html.contains(">base<"), "{html}");
    assert!(html.contains("Active"), "{html}");
    assert!(html.contains("New theme"), "{html}");

    let (status, _) = get(&app, "/admin/themes", &plain).await;
    assert_eq!(status, StatusCode::FORBIDDEN);
    // Themes is a settings entry, not a tab: the settings index lists it for
    // an operator who may manage themes and leaves it out for one who may not.
    let (_, html) = get(&app, "/admin/settings", &plain).await;
    assert!(
        !html.contains("/admin/themes"),
        "out of their settings: {html}"
    );
    let (_, html) = get(&app, "/admin/settings", &root).await;
    assert!(
        html.contains("/admin/themes") && html.contains("Website"),
        "in settings: {html}"
    );
    let (_, html) = get(&app, "/admin", &root).await;
    assert!(
        !html.contains(r#"class="lat-topnav__section" href="/admin/themes""#),
        "not a tab: {html}"
    );
}

#[tokio::test]
async fn a_theme_and_a_template_are_created_and_a_broken_template_is_refused() {
    let (db, _g) = test_db().await;
    let app = app(&db);
    let (root, _) = sessions(&db).await;

    let (status, location) = post(&app, "/admin/themes", &root, "code=acme&name=Acme+Site").await;
    assert_eq!(status, StatusCode::SEE_OTHER);
    assert_eq!(location, "/admin/themes/acme");
    let created = store::find_theme(&db, "acme").await.unwrap().unwrap();
    assert_eq!(created.config.name, "Acme Site");

    let (status, html) = get(&app, "/admin/themes/acme", &root).await;
    assert_eq!(status, StatusCode::OK);
    assert!(html.contains("Theme created."), "{html}");
    assert!(
        html.contains("Layouts") && html.contains("Edit properties"),
        "{html}"
    );

    let (status, location) = post(
        &app,
        "/admin/themes/acme/templates",
        &root,
        "kind=page&path=home.htm",
    )
    .await;
    assert_eq!(status, StatusCode::SEE_OTHER);
    let t = store::find_template(&db, created.id, Kind::Page, "home.htm")
        .await
        .unwrap()
        .unwrap();
    assert_eq!(location, format!("/admin/themes/acme/templates/{}", t.id));

    let editor = format!("/admin/themes/acme/templates/{}", t.id);
    let (status, _) = post(
        &app,
        &format!("{editor}/save"),
        &root,
        "url=%2F&markup=%7B%25+if+%25%7D",
    )
    .await;
    assert_eq!(status, StatusCode::SEE_OTHER);
    let (_, html) = get(&app, &editor, &root).await;
    assert!(html.contains("syntax error"), "{html}");
    assert_eq!(
        store::template(&db, t.id).await.unwrap().unwrap().markup,
        "",
        "a broken template is not saved"
    );

    let (status, _) = post(
        &app,
        &format!("{editor}/save"),
        &root,
        "url=%2F&markup=%3Ch1%3E%7B%7B+theme.name+%7D%7D%3C%2Fh1%3E",
    )
    .await;
    assert_eq!(status, StatusCode::SEE_OTHER);
    let saved = store::template(&db, t.id).await.unwrap().unwrap();
    assert_eq!(saved.markup, "<h1>{{ theme.name }}</h1>");
    assert_eq!(saved.settings["url"], "/");
    let (_, html) = get(&app, &editor, &root).await;
    assert!(
        html.contains("Template saved.") && html.contains("Restore"),
        "{html}"
    );

    // A template is reached only under its own theme.
    store::create_theme(&db, "other", "name: Other\n")
        .await
        .unwrap();
    let (status, location) = get(
        &app,
        &format!("/admin/themes/other/templates/{}", t.id),
        &root,
    )
    .await;
    assert_eq!(status, StatusCode::SEE_OTHER, "{location}");

    let (status, location) = post(&app, "/admin/themes/other/delete", &root, "").await;
    assert_eq!(
        (status, location.as_str()),
        (StatusCode::SEE_OTHER, "/admin/themes")
    );
    assert!(store::find_theme(&db, "other").await.unwrap().is_none());
}

#[tokio::test]
async fn the_theme_page_shows_and_saves_the_declared_settings() {
    let (db, _g) = test_db().await;
    let app = app(&db);
    let (root, _) = sessions(&db).await;
    store::create_theme(
        &db,
        "acme",
        "name: Acme\nform:\n  fields:\n    site_name:\n      label: Site name\n      type: text\n",
    )
    .await
    .unwrap();

    let (status, html) = get(&app, "/admin/themes/acme", &root).await;
    assert_eq!(status, StatusCode::OK);
    assert!(
        html.contains("Site name") && html.contains(r#"name="site_name""#),
        "{html}"
    );

    let (status, location) = post(
        &app,
        "/admin/themes/acme/settings",
        &root,
        "site_name=Acme+Site",
    )
    .await;
    assert_eq!(
        (status, location.as_str()),
        (StatusCode::SEE_OTHER, "/admin/themes/acme")
    );
    let theme = store::find_theme(&db, "acme").await.unwrap().unwrap();
    assert_eq!(
        theme.settings,
        serde_json::json!({"site_name": "Acme Site"})
    );
    let (_, html) = get(&app, "/admin/themes/acme", &root).await;
    assert!(
        html.contains("Theme settings saved.") && html.contains("Acme Site"),
        "{html}"
    );
}

const BOUNDARY: &str = "lat-boundary";

/// A multipart body: text fields, then one file field holding `file`.
fn multipart(fields: &[(&str, &str)], file: &[u8]) -> Vec<u8> {
    let mut out = Vec::new();
    for (name, value) in fields {
        out.extend_from_slice(
            format!("--{BOUNDARY}\r\nContent-Disposition: form-data; name=\"{name}\"\r\n\r\n{value}\r\n")
                .as_bytes(),
        );
    }
    out.extend_from_slice(
        format!("--{BOUNDARY}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"t.zip\"\r\nContent-Type: application/zip\r\n\r\n")
            .as_bytes(),
    );
    out.extend_from_slice(file);
    out.extend_from_slice(format!("\r\n--{BOUNDARY}--\r\n").as_bytes());
    out
}

#[tokio::test]
async fn a_theme_exports_as_a_zip_and_imports_as_a_new_one() {
    let (db, _g) = test_db().await;
    let app = app(&db);
    let (root, _) = sessions(&db).await;
    let base = store::create_theme(&db, "base", "name: Base\n")
        .await
        .unwrap();
    store::save_template(
        &db,
        base.id,
        Kind::Page,
        "home.htm",
        None,
        "<h1>hi</h1>",
        None,
    )
    .await
    .unwrap();

    let req = Request::builder()
        .uri("/admin/themes/base/export")
        .header("cookie", format!("{SESSION_COOKIE}={root}"))
        .body(Body::empty())
        .unwrap();
    let response = app.clone().oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(
        response
            .headers()
            .get("content-type")
            .and_then(|v| v.to_str().ok()),
        Some("application/zip")
    );
    assert!(response
        .headers()
        .get("content-disposition")
        .and_then(|v| v.to_str().ok())
        .unwrap_or_default()
        .contains("base.zip"));
    let bytes = laterite_admin::axum::body::to_bytes(response.into_body(), usize::MAX)
        .await
        .unwrap();

    let req = Request::builder()
        .method("POST")
        .uri("/admin/themes/import")
        .header("cookie", format!("{SESSION_COOKIE}={root}"))
        .header("sec-fetch-site", "same-origin")
        .header("x-csrf-token", "tok-123")
        .header(
            "content-type",
            format!("multipart/form-data; boundary={BOUNDARY}"),
        )
        .body(Body::from(multipart(&[("code", "copy")], &bytes)))
        .unwrap();
    let response = app.clone().oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::SEE_OTHER);
    assert_eq!(
        response
            .headers()
            .get("location")
            .and_then(|v| v.to_str().ok()),
        Some("/admin/themes/copy")
    );
    let copy = store::find_theme(&db, "copy").await.unwrap().unwrap();
    assert_eq!(copy.config.name, "Base");
    let home = store::find_template(&db, copy.id, Kind::Page, "home.htm")
        .await
        .unwrap()
        .unwrap();
    assert_eq!(home.markup, "<h1>hi</h1>");
    let (_, html) = get(&app, "/admin/themes/copy", &root).await;
    assert!(
        html.contains("Imported 1 template(s), skipped 0."),
        "{html}"
    );
}

#[tokio::test]
async fn a_page_save_checks_its_url_and_layout() {
    let (db, _g) = test_db().await;
    let app = app(&db);
    let (root, _) = sessions(&db).await;
    let theme = store::create_theme(&db, "acme", "name: Acme\n")
        .await
        .unwrap();
    let (_, location) = post(
        &app,
        "/admin/themes/acme/templates",
        &root,
        "kind=page&path=about.htm",
    )
    .await;
    let t = store::find_template(&db, theme.id, Kind::Page, "about.htm")
        .await
        .unwrap()
        .unwrap();
    assert_eq!(
        t.settings,
        serde_json::json!({"url": "/about"}),
        "a new page gets a url"
    );
    let editor = location;

    // The editor shows the page's settings as fields.
    let (_, html) = get(&app, &editor, &root).await;
    assert!(
        html.contains(r#"name="url""#) && html.contains(r#"name="hidden""#),
        "{html}"
    );
    assert!(!html.contains(r#"name="settings""#), "{html}");

    // A layout the theme lacks is refused; a bad pattern too.
    let (_, _) = post(
        &app,
        &format!("{editor}/save"),
        &root,
        "url=%2Fabout&layout=default&markup=x",
    )
    .await;
    let (_, html) = get(&app, &editor, &root).await;
    assert!(html.contains("no layout"), "{html}");
    assert_eq!(
        store::template(&db, t.id).await.unwrap().unwrap().markup,
        ""
    );

    let (_, _) = post(&app, &format!("{editor}/save"), &root, "url=about&markup=x").await;
    let (_, html) = get(&app, &editor, &root).await;
    assert!(html.contains("starts with a slash"), "{html}");

    store::save_template(
        &db,
        theme.id,
        Kind::Layout,
        "default.htm",
        None,
        "{% block page %}{% endblock %}",
        None,
    )
    .await
    .unwrap();
    let (_, html) = get(&app, &editor, &root).await;
    assert!(
        html.contains(r#"<option value="default""#),
        "the layout picker lists it: {html}"
    );
    let (_, _) = post(
        &app,
        &format!("{editor}/save"),
        &root,
        "url=%2Fabout&layout=default&title=About+us&hidden=1&markup=x",
    )
    .await;
    let saved = store::template(&db, t.id).await.unwrap().unwrap();
    assert_eq!(saved.markup, "x");
    assert_eq!(saved.settings["url"], "/about");
    assert_eq!(saved.settings["layout"], "default");
    assert_eq!(saved.settings["title"], "About us");
    assert_eq!(saved.settings["hidden"], true);
    let (_, html) = get(&app, "/admin/themes/acme", &root).await;
    assert!(
        html.contains(">/about</span>"),
        "the page's URL beside its path: {html}"
    );
}

/// The properties dialog rewrites the human fields of theme.yaml and keeps the
/// rest: the settings form the theme declares survives a rename.
#[tokio::test]
async fn the_properties_dialog_keeps_the_rest_of_the_config() {
    let (db, _guard) = test_db().await;
    let app = app(&db);
    let (root, _) = sessions(&db).await;
    store::create_theme(
        &db,
        "acme",
        "name: Acme\nform:\n  fields:\n    site_name:\n      label: Site name\n      type: text\n",
    )
    .await
    .unwrap();

    let (status, location) = post(
        &app,
        "/admin/themes/acme/properties",
        &root,
        "name=Acme+Clay&author=Acme+Studio&homepage=https%3A%2F%2Facme.test&description=Honest+clay&preview=https%3A%2F%2Facme.test%2Fpreview.png",
    )
    .await;
    assert_eq!(
        (status, location.as_str()),
        (StatusCode::SEE_OTHER, "/admin/themes/acme")
    );
    let theme = store::find_theme(&db, "acme").await.unwrap().unwrap();
    assert_eq!(theme.config.name, "Acme Clay");
    assert_eq!(theme.config.author, "Acme Studio");
    assert_eq!(
        theme.config.preview.as_deref(),
        Some("https://acme.test/preview.png")
    );
    assert!(theme.config.form.is_some(), "the settings form survives");

    let (_, html) = get(&app, "/admin/themes", &root).await;
    assert!(
        html.contains("https://acme.test/preview.png") && html.contains("Acme Clay"),
        "{html}"
    );
    let (status, _) = post(&app, "/admin/themes/acme/properties", &root, "name=").await;
    assert_eq!(status, StatusCode::SEE_OTHER);
    let (_, html) = get(&app, "/admin/themes/acme", &root).await;
    assert!(html.contains("A theme needs a name."), "{html}");
}