//! The passport photo reader: its button on the forms, the policy that lets
//! its WebAssembly run there and nowhere else, its files, and the lines it
//! sends read like typed ones.
use axum::http::StatusCode;
use sqlx::PgPool;
use crate::common::local;
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
async fn the_forms_offer_a_photo_and_allow_its_reader(pool: PgPool) {
let app = local(pool);
let r = app.get("/people/new").await;
assert_eq!(r.status, StatusCode::OK);
let t = r.text();
assert!(t.contains("data-mrz-camera>Take a photo"), "{t}");
assert!(
t.contains(r#""#));
assert!(t.contains("/static/mrz-photo.js?v="));
assert!(
r.header("content-security-policy")
.contains("'wasm-unsafe-eval'")
);
assert!(r.header("permissions-policy").contains("camera=(self)"));
// other pages keep the strict policy and no camera
let list = app.get("/people").await;
assert!(!list.header("content-security-policy").contains("wasm"));
assert!(list.header("permissions-policy").contains("camera=()"));
assert!(!list.text().contains("mrz-photo.js"));
}
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
async fn the_reader_files_are_served_with_their_policy(pool: PgPool) {
let app = local(pool);
for (file, kind) in [
("mrz-photo.js", "text/javascript; charset=utf-8"),
(
"vendor/tesseract/tesseract.min.js",
"text/javascript; charset=utf-8",
),
(
"vendor/tesseract/worker.min.js",
"text/javascript; charset=utf-8",
),
(
"vendor/tesseract/tesseract-core-simd-lstm.js",
"text/javascript; charset=utf-8",
),
(
"vendor/tesseract/tesseract-core-simd-lstm.wasm",
"application/wasm",
),
(
"vendor/tesseract/tesseract-core-lstm.wasm",
"application/wasm",
),
("vendor/tesseract/mrz.traineddata.gz", "application/gzip"),
] {
let r = app.get(&format!("/static/{file}")).await;
assert_eq!(r.status, StatusCode::OK, "{file}");
assert_eq!(r.header("content-type"), kind, "{file}");
let csp = r.header("content-security-policy");
assert_eq!(
csp.contains("'wasm-unsafe-eval'"),
file.starts_with("vendor/"),
"{file}"
);
}
}
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
async fn lines_from_a_photo_fill_in_the_form(pool: PgPool) {
let app = local(pool);
// as the reader sends them: the closer read (a 3 misread), then the
// first look, out of order; O for 0 in a date
let read = "3334013720GBR5802140M27O1013<<<<<<<<<<<<<<06\n\
P