tablo-core 0.2.0

The core toolkit types for Tablo, a server-rendered admin toolkit on Topcoat and Toasty.
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
use toasty::Db;
use topcoat::router::Body;

use super::*;
use crate::{
    ResourceDef, lens,
    panel::test_support::{Subscriber, Tagged, TaggedResource, mount, panel_for, response_html},
};

fn messages<'a>(errors: &'a FieldErrors, key: &str) -> Vec<&'a str> {
    errors
        .iter()
        .filter(|error| error.key == key)
        .map(|error| error.message.as_str())
        .collect()
}

#[tokio::test]
async fn unique_check_flags_duplicates_for_marked_fields() {
    use topcoat::context::CxTestBuilder;

    use crate::schema::{Field, Schema};

    struct SubscriberResource;
    impl Resource for SubscriberResource {
        type Model = Subscriber;
        type Form = crate::NoForm<Self::Model>;

        fn declare() -> ResourceDef<Self> {
            ResourceDef::new().table(crate::table::Table::new(crate::table::TextColumn::new(
                lens!(Subscriber.email),
            )))
        }
    }

    let mut db = Db::builder()
        .models(toasty::models!(Subscriber))
        .connect("sqlite::memory:")
        .await
        .unwrap();
    db.push_schema().await.unwrap();
    toasty::create!(Subscriber { email: "a@b.c" })
        .exec(&mut db)
        .await
        .unwrap();
    let cx = CxTestBuilder::new().app_context(db).build();
    let mut ex = crate::db::db(&cx);

    let schema = Schema::new(Field::text(Subscriber::fields().email()).unique());
    let mut values = HashMap::new();
    values.insert("email".to_string(), "a@b.c".to_string());

    let errors = check_unique(
        &cx,
        &crate::resource::require_mounted::<SubscriberResource>(&cx).unwrap(),
        &schema,
        &values,
        &HashMap::new(),
        &mut ex,
    )
    .await
    .unwrap();
    assert_eq!(
        messages(&errors, "email"),
        ["Email has already been taken"],
        "duplicate must be flagged, got {errors:?}"
    );

    let mut fresh = HashMap::new();
    fresh.insert("email".to_string(), "other@b.c".to_string());
    let errors = check_unique(
        &cx,
        &crate::resource::require_mounted::<SubscriberResource>(&cx).unwrap(),
        &schema,
        &fresh,
        &HashMap::new(),
        &mut ex,
    )
    .await
    .unwrap();
    assert!(errors.is_empty(), "fresh value must pass, got {errors:?}");

    let mut current = HashMap::new();
    current.insert("email".to_string(), "a@b.c".to_string());
    let errors = check_unique(
        &cx,
        &crate::resource::require_mounted::<SubscriberResource>(&cx).unwrap(),
        &schema,
        &values,
        &current,
        &mut ex,
    )
    .await
    .unwrap();
    assert!(
        errors.is_empty(),
        "own unchanged value must be skipped, got {errors:?}"
    );

    let mut changed_current = HashMap::new();
    changed_current.insert("email".to_string(), "old@b.c".to_string());
    let errors = check_unique(
        &cx,
        &crate::resource::require_mounted::<SubscriberResource>(&cx).unwrap(),
        &schema,
        &values,
        &changed_current,
        &mut ex,
    )
    .await
    .unwrap();
    assert_eq!(
        messages(&errors, "email"),
        ["Email has already been taken"],
        "changed-to-duplicate must be flagged, got {errors:?}"
    );

    let mut empty = HashMap::new();
    empty.insert("email".to_string(), "   ".to_string());
    let optional_schema = Schema::new(
        Field::text(Subscriber::fields().email())
            .optional()
            .unique(),
    );
    let errors = check_unique(
        &cx,
        &crate::resource::require_mounted::<SubscriberResource>(&cx).unwrap(),
        &optional_schema,
        &empty,
        &HashMap::new(),
        &mut ex,
    )
    .await
    .unwrap();
    assert!(
        errors.is_empty(),
        "an empty unique submit must not be probed, got {errors:?}"
    );
}

/// An explicitly `unique()` field stays required even when marked `.optional()`.
#[tokio::test]
async fn unique_field_is_required_however_it_is_marked() {
    use topcoat::context::CxTestBuilder;

    use crate::schema::{Field, Schema};

    struct SubscriberResource;
    impl Resource for SubscriberResource {
        type Model = Subscriber;
        type Form = crate::NoForm<Self::Model>;

        fn declare() -> ResourceDef<Self> {
            ResourceDef::new().table(crate::table::Table::new(crate::table::TextColumn::new(
                lens!(Subscriber.email),
            )))
        }
    }

    let db = Db::builder()
        .models(toasty::models!(Subscriber))
        .connect("sqlite::memory:")
        .await
        .unwrap();
    db.push_schema().await.unwrap();
    let cx = CxTestBuilder::new().app_context(db.clone()).build();
    let mut ex = crate::db::db(&cx);

    let schema = Schema::new(
        Field::text(Subscriber::fields().email())
            .unique()
            .optional(),
    );
    let mut first = HashMap::new();
    first.insert("email".to_string(), "   ".to_string());
    assert_eq!(
        messages(&schema.validate(&first), "email"),
        ["Email is required"],
        "an empty unique field must fail validation as required"
    );

    let errors = check_unique(
        &cx,
        &crate::resource::require_mounted::<SubscriberResource>(&cx).unwrap(),
        &schema,
        &first,
        &HashMap::new(),
        &mut ex,
    )
    .await
    .unwrap();
    assert!(
        errors.is_empty(),
        "an empty unique submit must not be probed, got {errors:?}"
    );

    let mut db_check = db;
    let stored = Subscriber::all().exec(&mut db_check).await.unwrap();
    assert!(
        stored.is_empty(),
        "an empty unique submit must not write, got {} rows",
        stored.len()
    );
}

/// Lens-derived `#[unique]` fields are required without a `.unique()` call.
#[tokio::test]
async fn lens_derived_unique_is_required_without_a_unique_call() {
    use topcoat::context::CxTestBuilder;

    use crate::schema::{Field, Schema};

    struct SubscriberResource;
    impl Resource for SubscriberResource {
        type Model = Subscriber;
        type Form = crate::NoForm<Self::Model>;

        fn declare() -> ResourceDef<Self> {
            ResourceDef::new().table(crate::table::Table::new(crate::table::TextColumn::new(
                lens!(Subscriber.email),
            )))
        }
    }

    let db = Db::builder()
        .models(toasty::models!(Subscriber))
        .connect("sqlite::memory:")
        .await
        .unwrap();
    db.push_schema().await.unwrap();
    let cx = CxTestBuilder::new().app_context(db).build();
    let mut ex = crate::db::db(&cx);

    let input = Field::text(Subscriber::fields().email());
    assert!(
        input.is_unique(),
        "the index must be recognized without a `.unique()` call"
    );
    assert!(input.is_required(), "derived uniqueness implies presence");

    let schema = Schema::new(input);
    let mut empty = HashMap::new();
    empty.insert("email".to_string(), "".to_string());
    assert_eq!(
        messages(&schema.validate(&empty), "email"),
        ["Email is required"],
        "an empty submit must be refused inline, not probed"
    );
    let errors = check_unique(
        &cx,
        &crate::resource::require_mounted::<SubscriberResource>(&cx).unwrap(),
        &schema,
        &empty,
        &HashMap::new(),
        &mut ex,
    )
    .await
    .unwrap();
    assert!(
        errors.is_empty(),
        "validation owns the empty case; the probe must add nothing, got {errors:?}"
    );
}

#[tokio::test]
async fn unique_check_propagates_probe_errors() {
    use topcoat::context::CxTestBuilder;

    use crate::schema::{Field, Schema};

    #[derive(Debug, toasty::Model, Clone)]
    struct Probe {
        #[key]
        #[auto]
        id: uuid::Uuid,
        #[unique]
        email: String,
    }
    struct ProbeResource;
    impl Resource for ProbeResource {
        type Model = Probe;
        type Form = crate::NoForm<Self::Model>;

        fn declare() -> ResourceDef<Self> {
            ResourceDef::new().table(crate::table::Table::new(crate::table::TextColumn::new(
                lens!(Probe.email),
            )))
        }
    }

    // Schema never pushed, so the probe cannot run.
    let db = Db::builder()
        .models(toasty::models!(Probe))
        .connect("sqlite::memory:")
        .await
        .unwrap();
    let cx = CxTestBuilder::new().app_context(db).build();
    let mut ex = crate::db::db(&cx);

    let schema = Schema::new(Field::text(Probe::fields().email()).unique());
    let mut values = HashMap::new();
    values.insert("email".to_string(), "a@b.c".to_string());
    let result = check_unique(
        &cx,
        &crate::resource::require_mounted::<ProbeResource>(&cx).unwrap(),
        &schema,
        &values,
        &HashMap::new(),
        &mut ex,
    )
    .await;
    assert!(
        result.is_err(),
        "a failing probe must fail the submit, got {result:?}"
    );
}

#[tokio::test]
async fn unique_check_ignores_absent_repeater_groups() {
    use topcoat::context::CxTestBuilder;

    use crate::schema::{Field, Repeater, Schema};

    #[derive(Debug, toasty::Model, Clone)]
    struct Nicknamed {
        #[key]
        #[auto]
        id: uuid::Uuid,
        #[unique]
        nickname: String,
    }
    struct TaggedResource;
    impl Resource for TaggedResource {
        type Model = Nicknamed;
        type Form = crate::NoForm<Self::Model>;

        fn declare() -> ResourceDef<Self> {
            ResourceDef::new().table(crate::table::Table::new(crate::table::TextColumn::new(
                lens!(Nicknamed.nickname),
            )))
        }
    }

    let mut db = Db::builder()
        .models(toasty::models!(Nicknamed))
        .connect("sqlite::memory:")
        .await
        .unwrap();
    db.push_schema().await.unwrap();
    toasty::create!(Nicknamed {
        nickname: "".to_string()
    })
    .exec(&mut db)
    .await
    .unwrap();
    let cx = CxTestBuilder::new().app_context(db).build();
    let mut ex = crate::db::db(&cx);

    let schema = Schema::new(
        Repeater::new("Tags").schema(
            Field::text(Nicknamed::fields().nickname())
                .unique()
                .optional(),
        ),
    );

    // An absent group validates clean, so the unique check agrees.
    let mut absent = HashMap::new();
    absent.insert("nickname".to_string(), "".to_string());
    assert!(
        schema.validate(&absent).is_empty(),
        "absent group must validate clean"
    );
    let errors = check_unique(
        &cx,
        &crate::resource::require_mounted::<TaggedResource>(&cx).unwrap(),
        &schema,
        &absent,
        &HashMap::new(),
        &mut ex,
    )
    .await
    .unwrap();
    assert!(
        errors.is_empty(),
        "absent group must not be unique-checked, got {errors:?}"
    );

    let mut present = HashMap::new();
    present.insert("nickname".to_string(), "taken".to_string());
    toasty::create!(Nicknamed {
        nickname: "taken".to_string()
    })
    .exec(&mut ex)
    .await
    .unwrap();
    let errors = check_unique(
        &cx,
        &crate::resource::require_mounted::<TaggedResource>(&cx).unwrap(),
        &schema,
        &present,
        &HashMap::new(),
        &mut ex,
    )
    .await
    .unwrap();
    assert_eq!(
        errors.first("nickname").map(|error| error.message.as_str()),
        Some("Nickname has already been taken"),
        "present group must still be unique-checked, got {errors:?}"
    );
}

/// A typed unique field probes the declared type, not its text spelling.
#[tokio::test]
async fn a_typed_unique_field_probes_the_declared_type() {
    const TOKEN: &str = "0f8fad5b-d9cb-469f-a165-70867728950e";

    let db = Db::builder()
        .models(toasty::models!(Tagged))
        .connect("sqlite::memory:")
        .await
        .unwrap();
    db.push_schema().await.unwrap();
    let mut db_q = db.clone();
    toasty::create!(Tagged {
        name: "one".to_string(),
        token: uuid::Uuid::parse_str(TOKEN).unwrap(),
    })
    .exec(&mut db_q)
    .await
    .unwrap();
    let router = mount(db.clone(), panel_for::<TaggedResource>()).expect("panel builds");

    let csrf = uuid::Uuid::new_v4().to_string();
    let request = |body: String| {
        http::Request::builder()
            .method(http::Method::POST)
            .uri("/admin/tagged/create")
            .header(
                http::header::CONTENT_TYPE,
                "application/x-www-form-urlencoded",
            )
            .header(
                http::header::COOKIE,
                format!("{}={csrf}", crate::csrf::COOKIE_NAME),
            )
            .body(Body::from(body))
            .unwrap()
    };

    let response = router
        .handle(request(format!(
            "name=two&token={}&csrf_token={csrf}",
            TOKEN.to_uppercase()
        )))
        .await;
    assert_eq!(
        response.status(),
        200,
        "the duplicate must re-render, not create"
    );
    let html = response_html(response).await;
    assert!(
        html.contains("Token has already been taken"),
        "the typed probe must see the duplicate, got {html}"
    );
    let mut db_q = db.clone();
    assert_eq!(
        Tagged::all().exec(&mut db_q).await.unwrap().len(),
        1,
        "a refused create writes nothing"
    );

    let response = router
        .handle(request(format!(
            "name=two&token=3f8fad5b-d9cb-469f-a165-70867728950e&csrf_token={csrf}"
        )))
        .await;
    assert_eq!(
        response.status(),
        303,
        "a distinct value must create, not flag a duplicate"
    );
    let mut db_q = db.clone();
    assert_eq!(
        Tagged::all().exec(&mut db_q).await.unwrap().len(),
        2,
        "the accepted create writes its row"
    );
}

/// An edit re-spelling the record's own typed value still saves.
#[tokio::test]
async fn a_typed_unique_field_skips_the_records_own_value_on_edit() {
    const MINE: &str = "0f8fad5b-d9cb-469f-a165-70867728950e";
    const THEIRS: &str = "3f8fad5b-d9cb-469f-a165-70867728950e";

    let db = Db::builder()
        .models(toasty::models!(Tagged))
        .connect("sqlite::memory:")
        .await
        .unwrap();
    db.push_schema().await.unwrap();
    let mut db_q = db.clone();
    let mine = toasty::create!(Tagged {
        name: "mine".to_string(),
        token: uuid::Uuid::parse_str(MINE).unwrap(),
    })
    .exec(&mut db_q)
    .await
    .unwrap();
    toasty::create!(Tagged {
        name: "theirs".to_string(),
        token: uuid::Uuid::parse_str(THEIRS).unwrap(),
    })
    .exec(&mut db_q)
    .await
    .unwrap();
    let router = mount(db.clone(), panel_for::<TaggedResource>()).expect("panel builds");

    let csrf = uuid::Uuid::new_v4().to_string();
    let url = format!("/admin/tagged/{}/edit", mine.id);
    let edit = |token: &str| {
        http::Request::builder()
            .method(http::Method::POST)
            .uri(&url)
            .header(
                http::header::CONTENT_TYPE,
                "application/x-www-form-urlencoded",
            )
            .header(
                http::header::COOKIE,
                format!("{}={csrf}", crate::csrf::COOKIE_NAME),
            )
            .body(Body::from(format!(
                "name=mine&token={token}&csrf_token={csrf}"
            )))
            .unwrap()
    };

    let response = router.handle(edit(&MINE.to_uppercase())).await;
    assert!(
        response.status().is_redirection(),
        "re-spelling the record's own value must save, got {} {}",
        response.status(),
        response_html(response).await
    );
    let mut db_q = db.clone();
    let saved = Tagged::filter(Tagged::fields().id().eq(mine.id))
        .first()
        .exec(&mut db_q)
        .await
        .unwrap()
        .expect("the edited record");
    assert_eq!(
        saved.token,
        uuid::Uuid::parse_str(MINE).unwrap(),
        "the re-spelled value is stored canonically"
    );

    let response = router.handle(edit(THEIRS)).await;
    assert_eq!(
        response.status(),
        200,
        "another record's value must refuse the edit"
    );
    let html = response_html(response).await;
    assert!(
        html.contains("Token has already been taken"),
        "the typed probe must see the other record, got {html}"
    );
    let mut db_q = db.clone();
    let unchanged = Tagged::filter(Tagged::fields().id().eq(mine.id))
        .first()
        .exec(&mut db_q)
        .await
        .unwrap()
        .expect("the refused record");
    assert_eq!(
        unchanged.token,
        uuid::Uuid::parse_str(MINE).unwrap(),
        "a refused edit writes nothing"
    );
}