//! The Forms tab: the Immigration Bureau's forms filled in from the records, //! what each still lacks, the PDFs and their audit entries. use axum::http::StatusCode; use sqlx::PgPool; use visa::papers::values::{self, Application}; use crate::common::local; async fn erika(pool: &PgPool) -> i64 { sqlx::query_scalar("SELECT id FROM people WHERE surname = 'Mustermann'") .fetch_one(pool) .await .unwrap() } fn pages(pdf: &[u8]) -> usize { krilla::pdf::Pdf::new(pdf.to_vec()).unwrap().pages().len() } #[sqlx::test(migrator = "visa::db::MIGRATOR")] async fn the_values_come_from_the_records(pool: PgPool) { let today = visa::dates::today(); visa::seed::demo(&pool, today).await.unwrap(); let id = erika(&pool).await; let facts = values::load(&pool, id).await.unwrap().unwrap(); let app = Application::default().with_defaults(&facts, today); assert_eq!( app.reason, "To continue working for Demo Engineering Co., Ltd. as Electrical engineer" ); assert_eq!((app.days.as_str(), app.entries.as_str()), ("365", "single")); let v = values::values(&facts, &app, today); let plus = |days: i64| today + chrono::TimeDelta::days(days); let expect = [ ("family_name", "MUSTERMANN".to_string()), ("first_name", "ERIKA".to_string()), ("full_name", "ERIKA MUSTERMANN".to_string()), ("title", "Ms".to_string()), ("nationality", "GERMANY".to_string()), ("birth_place", "BERLIN".to_string()), ("birth_month", "AUGUST".to_string()), ("passport_no", "C01X00T47".to_string()), ("passport_issued_at", "STADT KOELN".to_string()), ("passport_expiry_year", "2031".to_string()), ("visa_type", "NON-IMMIGRANT B".to_string()), ("visa_kind", "non-immigrant".to_string()), ("visa_issued_at", "ROYAL THAI EMBASSY, BERLIN".to_string()), // the last arrival: back from a trip on the re-entry permit ("arrived_by", "TG111".to_string()), ("arrived_from", "SINGAPORE".to_string()), ( "arrival_port", "CHIANG MAI INTERNATIONAL AIRPORT".to_string(), ), ( "permitted_day", chrono::Datelike::day(&plus(60)).to_string(), ), ("position", "ELECTRICAL ENGINEER".to_string()), ("salary", "95,000".to_string()), ("company_name", "DEMO ENGINEERING CO., LTD.".to_string()), ("company_capital", "5,000,000".to_string()), ("revenue", "48,500,000".to_string()), ("figures_year", "2025".to_string()), ("thai_staff", "24".to_string()), ("coordinator", "WANIDA MEECHAI".to_string()), ("days", "365".to_string()), ( "reason_short", "WORKING AT DEMO ENGINEERING CO., LTD.".to_string(), ), ( "company_written_at", "DEMO ENGINEERING CO., LTD.".to_string(), ), ("road", "SOI 7, HUAY KAEW ROAD".to_string()), ("extension_nth", "2".to_string()), ]; for (key, want) in expect { assert_eq!(v.get(key), Some(&want), "{key}"); } // nothing is missing for an extension but the employer's certificate // date, which only the company's documents give for paper in visa::papers::EXTENSION { let lacks: Vec<&str> = values::missing(paper, &v, &facts) .iter() .map(|m| m.what) .collect(); if paper == visa::papers::Paper::Stm1 { assert_eq!( lacks, ["The employer's company certificate, uploaded with its date"], "{}", paper.code() ); } else { assert!(lacks.is_empty(), "{}: {lacks:?}", paper.code()); } } } #[sqlx::test(migrator = "visa::db::MIGRATOR")] async fn the_forms_tab_lists_the_forms_and_what_they_lack(pool: PgPool) { let today = visa::dates::today(); visa::seed::demo(&pool, today).await.unwrap(); let app = local(pool.clone()); let id = erika(&pool).await; let r = app.get(&format!("/people/{id}/forms")).await; assert_eq!(r.status, StatusCode::OK, "{}", r.body); let t = r.text(); for code in ["TM.7", "STM.2", "STM.9", "STM.11", "STM.1", "TM.47", "TM.8"] { assert!(t.contains(code), "{code}"); } assert!(t.contains( "value=\"To continue working for Demo Engineering Co., Ltd. as Electrical engineer\"" )); assert!( t.contains(&format!("/people/{id}/forms/tm7.pdf?date={today}")), "{t}" ); // the trip is still blank on TM.8, and the certificate's date on STM.1 let tm8 = &t[t.find("id=\"form-tm8\"").unwrap()..]; assert!(tm8.contains("The trip"), "{tm8}"); let stm1 = &t[t.find("id=\"form-stm1\"").unwrap()..t.find("id=\"form-tm47\"").unwrap()]; assert!(stm1.contains("company certificate"), "{stm1}"); assert!( stm1.contains("#documents"), "the link goes to the company's documents" ); // with the trip, TM.8 is complete and its link carries the trip let r = app .get(&format!( "/people/{id}/forms?to=Germany&leave=2027-01-05&return=2027-01-20&entries=multiple" )) .await; assert_eq!(r.status, StatusCode::OK); let t = r.text(); // the fields keep what was entered (they serialize as "return" and "for") assert!( t.contains("name=\"return\" type=\"date\" value=\"2027-01-20\""), "{t}" ); let tm8 = &t[t.find("id=\"form-tm8\"").unwrap()..]; assert!(tm8.contains("Complete"), "{tm8}"); assert!(tm8.contains("to=Germany"), "{tm8}"); // wrong values are marked on the tab; a PDF with them goes back there let r = app .get(&format!("/people/{id}/forms?date=202611-18&days=many")) .await; assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY); assert!( r.text().contains("Enter the number of days"), "{}", r.text() ); let r = app .get(&format!("/people/{id}/forms/tm7.pdf?days=many")) .await; assert_eq!(r.status, StatusCode::SEE_OTHER); assert!(r.location().starts_with(&format!("/people/{id}/forms?"))); } #[sqlx::test(migrator = "visa::db::MIGRATOR")] async fn a_form_is_the_official_pages_filled_in_and_audited(pool: PgPool) { let today = visa::dates::today(); visa::seed::demo(&pool, today).await.unwrap(); let app = local(pool.clone()); let id = erika(&pool).await; let r = app.get(&format!("/people/{id}/forms/tm7.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").starts_with("inline;")); assert!( r.header("content-disposition") .contains(&format!("MUSTERMANN Erika - TM.7 - {today}.pdf")), "{}", r.header("content-disposition") ); assert_eq!(pages(&r.bytes), 2, "TM.7 has two pages"); let r = app .get(&format!("/people/{id}/forms/extension.pdf?download=1")) .await; assert_eq!(r.status, StatusCode::OK); assert!(r.header("content-disposition").starts_with("attachment;")); assert_eq!(pages(&r.bytes), 6, "TM.7, STM.2, STM.9, STM.11 and STM.1"); let r = app.get(&format!("/people/{id}/forms/tm8.pdf")).await; assert_eq!(pages(&r.bytes), 2); // what was opened and saved, by whom; the form's values are not logged let rows: Vec<(String, Vec)> = sqlx::query_as( "SELECT action, fields FROM audit_log WHERE entity = 'form' AND person_id = $1 ORDER BY id", ) .bind(id) .fetch_all(&pool) .await .unwrap(); assert_eq!( rows, [ ("view".to_string(), vec!["TM.7".to_string()]), ( "download".to_string(), ["TM.7", "STM.2", "STM.9", "STM.11", "STM.1"] .map(String::from) .to_vec() ), ("view".to_string(), vec!["TM.8".to_string()]), ] ); let t = app.get(&format!("/activity?person={id}")).await.text(); assert!(t.contains("TM.7, STM.2, STM.9, STM.11, STM.1"), "{t}"); for bad in ["tm99.pdf", "tm7", "extension.txt"] { let r = app.get(&format!("/people/{id}/forms/{bad}")).await; assert_eq!(r.status, StatusCode::NOT_FOUND, "{bad}"); } let r = app.get("/people/999999/forms/tm7.pdf").await; assert_eq!(r.status, StatusCode::NOT_FOUND); }