Files
visa/tests/pack.rs
T
grabowski 1f526f17f9
CI / secret scan (push) Successful in 6s
CI / fmt, clippy, tests (push) Successful in 43s
CI / release (push) Skipped
Pack print: A4 upright pages, small pictures at their size, card sides on one sheet
Every page of the pack is now A4 upright, so it prints as one stack. A
PDF page or a whole-page picture that lies wider than tall is turned
onto the page (a small one that fits as it is stays as it is), and a
PDF page is only made smaller, never larger.

A picture never prints larger than its resolution allows (the JFIF or
PNG dpi, else 150 dpi), nor larger than its kind's box: passport pages,
visas and stamps 180 x 128 mm, cards 130 x 90 mm, and a phone's
screenshot (the TDAC, or any picture about twice as tall as wide)
150 x 75 mm. These print at the top of the page.

Cards: a new kind "ID card", and the work permit, take two pictures in
one upload as front and back (migration 0008 documents.back_of). The
Documents tab marks them front/back, lists them together and offers
"Print both sides" (.../documents/{id}/sides.pdf); the pack prints both
on one sheet. The client link takes ID cards too. The demo seeds a TDAC
screenshot and an ID card's two sides.

Checks: fmt, clippy, 108 unit and 57 integration tests (print sizes,
dpi reading, the back following its front, the paired upload, its
audit and the sides PDF); the demo pack's pages checked as images.
The Documents tab's actions stack on a phone, so none is cut off.
2026-11-21 18:30:43 +07:00

186 lines
7.1 KiB
Rust

//! The Pack tab: the extension pack's checklist with each item's state, the
//! employer's part on the company's page, and the whole pack as one PDF with
//! its audit entries.
use axum::http::StatusCode;
use sqlx::PgPool;
use crate::common::{App, local};
async fn seeded(pool: &PgPool) -> (App, i64, i64) {
let today = visa::dates::today();
visa::seed::demo(pool, today).await.unwrap();
let app = local(pool.clone());
let store = app.st.files.as_ref().unwrap();
visa::seed::demo_documents(pool, store, today)
.await
.unwrap();
let erika: i64 = sqlx::query_scalar("SELECT id FROM people WHERE surname = 'Mustermann'")
.fetch_one(pool)
.await
.unwrap();
let company: i64 =
sqlx::query_scalar("SELECT id FROM companies WHERE name = 'Demo Engineering Co., Ltd.'")
.fetch_one(pool)
.await
.unwrap();
(app, erika, company)
}
/// The item `key` of the tab, up to the next one.
fn item<'a>(page: &'a str, key: &str) -> &'a str {
let at = page
.find(&format!("id=\"item-{key}\""))
.unwrap_or_else(|| panic!("no item {key}"));
let rest = &page[at..];
let end = rest[1..]
.find("<li class=\"ci\"")
.map_or(rest.len(), |e| e + 1);
&rest[..end]
}
fn pages(pdf: &[u8]) -> Vec<(f32, f32)> {
krilla::pdf::Pdf::new(pdf.to_vec())
.unwrap()
.pages()
.iter()
.map(|p| p.render_dimensions())
.collect()
}
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
async fn the_tab_shows_each_item_with_its_state(pool: PgPool) {
let (app, id, company) = seeded(&pool).await;
let r = app.get(&format!("/people/{id}/pack")).await;
assert_eq!(r.status, StatusCode::OK, "{}", r.body);
let t = r.text();
assert!(t.contains("Extension pack, work (Non-B)"), "{t}");
assert!(t.contains("aria-current=\"page\">Pack</a>"), "{t}");
// the forms come from the records; the copies from the documents
assert!(item(&t, "tm7").contains("Generated"));
assert!(item(&t, "tm7").contains(&format!("/people/{id}/forms/tm7.pdf?date=")));
for key in [
"passport",
"visa",
"arrival",
"reentry",
"extensions",
"tdac",
"work-permit",
] {
assert!(
item(&t, key).contains("Uploaded"),
"{key}: {}",
item(&t, key)
);
}
// the employer's documents, each judged by its date
assert!(item(&t, "company-certificate").contains("counts until"));
assert!(item(&t, "shareholders").contains("Out of date"));
assert!(item(&t, "pnd1").contains("3 files"));
assert!(item(&t, "sps1-10").contains("No date"));
assert!(item(&t, "office-photo").contains("1 of 2"));
let pp30 = item(&t, "pp30");
assert!(pp30.contains("Missing"), "{pp30}");
assert!(
pp30.contains(&format!("/companies/{company}?kind=pp30#upload")),
"{pp30}"
);
assert!(t.contains(&format!("/people/{id}/pack.pdf?date=")));
// the Forms tab and the Pack tab carry the application to each other
let r = app.get(&format!("/people/{id}/pack?days=180")).await;
assert!(r.text().contains(&format!("/people/{id}/forms?date=")));
assert!(r.text().contains("days=180"));
// a wrong value is marked, and its PDF goes back to the tab
let r = app.get(&format!("/people/{id}/pack?date=202611-20")).await;
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
let r = app.get(&format!("/people/{id}/pack.pdf?days=many")).await;
assert_eq!(r.status, StatusCode::SEE_OTHER);
assert!(r.location().starts_with(&format!("/people/{id}/pack?")));
assert_eq!(
app.get("/people/999999/pack").await.status,
StatusCode::NOT_FOUND
);
// the fixes open the upload with the kind chosen
let r = app.get(&format!("/people/{id}/documents?kind=pnd91")).await;
assert!(r.text().contains("<option value=\"pnd91\" selected>"));
let r = app.get(&format!("/companies/{company}?kind=pp30")).await;
let t = r.text();
assert!(t.contains("<option value=\"pp30\" selected>"), "{t}");
// the company's page shows its part of every pack
assert!(t.contains("For the extension packs"));
assert!(item(&t, "shareholders").contains("Out of date"));
// the deadline of the extension leads to the pack
let t = app.get(&format!("/people/{id}")).await.text();
assert!(
t.contains(&format!("href=\"/people/{id}/pack\">Prepare the pack")),
"{t}"
);
}
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
async fn the_pdf_is_the_cover_the_forms_and_the_copies_and_it_is_audited(pool: PgPool) {
let (app, id, company) = seeded(&pool).await;
let r = app.get(&format!("/people/{id}/pack.pdf")).await;
assert_eq!(r.status, StatusCode::OK, "{}", r.body);
assert_eq!(r.header("content-type"), "application/pdf");
assert_eq!(r.header("cache-control"), "private, no-store");
assert!(
r.header("content-disposition")
.contains("MUSTERMANN Erika - Extension pack - "),
"{}",
r.header("content-disposition")
);
let sizes = pages(&r.bytes);
// the cover, six pages of forms, nine copies of Erika's and eight of the
// employer's (the old BOJ.5 stays out)
assert_eq!(sizes.len(), 1 + 6 + 9 + 8, "{sizes:?}");
// every page is A4 upright, so the pack prints as one stack: the
// passport's photo page, wider than high, lies on the page as it is
assert!(
sizes
.iter()
.all(|(w, h)| (w - 595.0).abs() < 2.5 && (h - 842.0).abs() < 2.5),
"{sizes:?}"
);
let rows: Vec<(String, String, Option<i64>, Option<i64>)> = sqlx::query_as(
"SELECT action, entity, person_id, company_id FROM audit_log \
WHERE entity IN ('form', 'document') AND actor <> 'seed' ORDER BY id",
)
.fetch_all(&pool)
.await
.unwrap();
assert_eq!(rows.len(), 1 + 17, "{rows:?}");
assert!(rows.iter().all(|r| r.0 == "view" && r.2 == Some(id)));
let of_company = rows.iter().filter(|r| r.3 == Some(company)).count();
assert_eq!(of_company, 8);
let codes: Vec<String> =
sqlx::query_scalar("SELECT unnest(fields) FROM audit_log WHERE entity = 'form'")
.fetch_all(&pool)
.await
.unwrap();
assert_eq!(codes, ["TM.7", "STM.2", "STM.9", "STM.11", "STM.1"]);
// a file that cannot be read gets a page of its own, and no audit row
let tdac: i64 =
sqlx::query_scalar("SELECT id FROM documents WHERE person_id = $1 AND kind = 'tdac'")
.bind(id)
.fetch_one(&pool)
.await
.unwrap();
app.st.files.as_ref().unwrap().remove(tdac).await.unwrap();
let r = app.get(&format!("/people/{id}/pack.pdf?download=1")).await;
assert_eq!(r.status, StatusCode::OK, "{}", r.body);
assert!(r.header("content-disposition").starts_with("attachment;"));
assert_eq!(pages(&r.bytes).len(), 24);
let downloads: i64 = sqlx::query_scalar(
"SELECT count(*) FROM audit_log WHERE action = 'download' AND entity = 'document'",
)
.fetch_one(&pool)
.await
.unwrap();
assert_eq!(downloads, 16);
}