Phase 2: companies, stay records, deadlines
Companies become records of their own, and the stay in Thailand is recorded in full; from these records the app works out each person's deadlines (docs/PLAN.md, phase 2). - Companies: list, page, form, archive; Thai name, the 13-digit registration number with its check digit verified, address and the director who signs. The employer is chosen from them on the person form and on work permits; a company's page adds people to it; the people list and the Deadlines page filter by employer. Migration 0002 turns the free-text employers into companies. - Stay records, each one `Kind` in src/records.rs with one form, one set of routes and the same audit entries: arrivals and departures with the TDAC, extensions of stay (approved, under consideration, refused), 90-day reports with the receipt's next date, visas, re-entry permits and work permits. Passports, visas and companies are chosen from the person's own records; the server checks the choice. - The deadline engine (src/deadlines.rs, a pure function with a unit test per rule): the 90-day report from day 1 of each arrival, reset by a report or an extension, with the in-person and online windows and the in-person rule after a new passport or an extension; the extension window from 30 days before to the last working day; the decision to collect; overstay with its fine; TM.30 after each arrival; passport and work permit expiry; re-entry permits; visas still to be used. - The Deadlines page at /, the person's status and deadlines on the Overview and the new "Visa and stay" tab, the person's Activity tab, and the next deadline in the people list. - The online 90-day window ends 7 days before the due date (the plan said 8; the Immigration Bureau's TM.47 manual says 7). - 69 tests: 45 unit tests and 24 integration tests.
This commit is contained in:
1 parent
2e009700a9
commit
83bc481952
37 files changed
+5515
-142
No files matched your search
@@ -0,0 +1,265 @@
|
||||
//! Companies and the selections that use them: the employer on the person
|
||||
//! form, employees added from the company page, the company filter.
|
||||
|
||||
use axum::http::StatusCode;
|
||||
use sqlx::PgPool;
|
||||
|
||||
use crate::common::{id_from, local};
|
||||
|
||||
fn company_from(location: &str) -> i64 {
|
||||
location
|
||||
.trim_start_matches("/companies/")
|
||||
.parse()
|
||||
.unwrap_or_else(|_| panic!("no company id in {location:?}"))
|
||||
}
|
||||
|
||||
async fn audit_fields(pool: &PgPool, entity: &str, id: i64, action: &str) -> Vec<Vec<String>> {
|
||||
sqlx::query_scalar(
|
||||
"SELECT fields FROM audit_log WHERE entity = $1 AND entity_id = $2 AND action = $3 \
|
||||
ORDER BY id",
|
||||
)
|
||||
.bind(entity)
|
||||
.bind(id)
|
||||
.bind(action)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
|
||||
async fn companies_are_checked_and_edited(pool: PgPool) {
|
||||
let app = local(pool.clone());
|
||||
let r = app
|
||||
.post(
|
||||
"/companies",
|
||||
&[
|
||||
("name", "Test Works Co., Ltd."),
|
||||
("registration_no", "0999999000005"),
|
||||
],
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
assert!(r.text().contains("check digit"), "{}", r.text());
|
||||
assert!(
|
||||
r.body.contains("value=\"Test Works Co., Ltd.\""),
|
||||
"typed values stay"
|
||||
);
|
||||
|
||||
let fields = [
|
||||
("name", "Test Works Co., Ltd."),
|
||||
("registration_no", "0-9999-99000-00-4"),
|
||||
("district", "Mueang Chiang Mai"),
|
||||
("province", "Chiang Mai"),
|
||||
("postal_code", "50000"),
|
||||
("signatory", "Malee Jaidee"),
|
||||
];
|
||||
let r = app.post("/companies", &fields).await;
|
||||
assert_eq!(r.status, StatusCode::SEE_OTHER, "{}", r.body);
|
||||
let id = company_from(r.location());
|
||||
let page = app.get(&format!("/companies/{id}")).await.text();
|
||||
assert!(page.contains("0999999000004"));
|
||||
assert!(
|
||||
page.contains("Mueang Chiang Mai, Chiang Mai 50000"),
|
||||
"{page}"
|
||||
);
|
||||
assert!(
|
||||
app.get("/companies")
|
||||
.await
|
||||
.text()
|
||||
.contains("Test Works Co., Ltd.")
|
||||
);
|
||||
|
||||
// the same number twice
|
||||
let r = app
|
||||
.post(
|
||||
"/companies",
|
||||
&[
|
||||
("name", "Other Co., Ltd."),
|
||||
("registration_no", "0999999000004"),
|
||||
],
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
assert!(
|
||||
r.text()
|
||||
.contains("Another company has this registration number")
|
||||
);
|
||||
|
||||
let mut changed = fields.to_vec();
|
||||
changed.push(("phone", "+66 53 000 000"));
|
||||
let r = app.post(&format!("/companies/{id}"), &changed).await;
|
||||
assert_eq!(r.status, StatusCode::SEE_OTHER);
|
||||
assert_eq!(
|
||||
audit_fields(&pool, "company", id, "update").await,
|
||||
vec![vec!["phone"]]
|
||||
);
|
||||
// nothing changed: no entry
|
||||
app.post(&format!("/companies/{id}"), &changed).await;
|
||||
assert_eq!(audit_fields(&pool, "company", id, "update").await.len(), 1);
|
||||
let log = app.get("/activity").await.text();
|
||||
assert!(
|
||||
log.contains("Test Works Co., Ltd."),
|
||||
"the log links the company"
|
||||
);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
|
||||
async fn the_employer_is_chosen_from_the_companies(pool: PgPool) {
|
||||
let app = local(pool.clone());
|
||||
let r = app
|
||||
.post("/companies", &[("name", "Test Works Co., Ltd.")])
|
||||
.await;
|
||||
let c = company_from(r.location());
|
||||
let cs = c.to_string();
|
||||
|
||||
let form = app.get("/people/new").await.body;
|
||||
assert!(form.contains(&format!(
|
||||
"<option value=\"{c}\">Test Works Co., Ltd.</option>"
|
||||
)));
|
||||
|
||||
let r = app
|
||||
.post(
|
||||
"/people",
|
||||
&[
|
||||
("surname", "Doe"),
|
||||
("first_name", "Jane"),
|
||||
("employer", &cs),
|
||||
],
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::SEE_OTHER, "{}", r.body);
|
||||
let jane = id_from(r.location());
|
||||
let page = app.get(&format!("/people/{jane}")).await.body;
|
||||
assert!(page.contains(&format!(
|
||||
"<a href=\"/companies/{c}\">Test Works Co., Ltd.</a>"
|
||||
)));
|
||||
|
||||
// an id that is no company
|
||||
let r = app
|
||||
.post(
|
||||
"/people",
|
||||
&[
|
||||
("surname", "Roe"),
|
||||
("first_name", "Rita"),
|
||||
("employer", "999999"),
|
||||
],
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
assert!(r.text().contains("Choose a company from the list."));
|
||||
|
||||
// someone else, added from the company's page
|
||||
let r = app
|
||||
.post("/people", &[("surname", "Novak"), ("first_name", "Tomas")])
|
||||
.await;
|
||||
let tomas = id_from(r.location());
|
||||
let page = app.get(&format!("/companies/{c}")).await.text();
|
||||
assert!(page.contains("DOE, Jane Marie") || page.contains("DOE, Jane"));
|
||||
assert!(page.contains(&format!("<option value=\"{tomas}\">NOVAK, Tomas</option>")));
|
||||
let r = app
|
||||
.post(
|
||||
&format!("/companies/{c}/employees"),
|
||||
&[("person", &tomas.to_string())],
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::SEE_OTHER);
|
||||
let employer: Option<i64> = sqlx::query_scalar("SELECT employer_id FROM people WHERE id = $1")
|
||||
.bind(tomas)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(employer, Some(c));
|
||||
assert_eq!(
|
||||
audit_fields(&pool, "person", tomas, "update").await,
|
||||
vec![vec!["employer"]]
|
||||
);
|
||||
|
||||
// the company filter on the people list
|
||||
let list = app.get(&format!("/people?company={c}")).await.text();
|
||||
assert!(list.contains("People at Test Works Co., Ltd."));
|
||||
assert!(list.contains("NOVAK, Tomas") && list.contains("DOE, Jane"));
|
||||
|
||||
let r = app
|
||||
.post(&format!("/companies/{c}/employees/{tomas}/remove"), &[])
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::SEE_OTHER);
|
||||
let list = app.get(&format!("/people?company={c}")).await.text();
|
||||
assert!(!list.contains("NOVAK, Tomas"));
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
|
||||
async fn an_archived_company_is_no_longer_offered(pool: PgPool) {
|
||||
let app = local(pool.clone());
|
||||
let r = app
|
||||
.post("/companies", &[("name", "Old Works Co., Ltd.")])
|
||||
.await;
|
||||
let c = company_from(r.location());
|
||||
let cs = c.to_string();
|
||||
let r = app
|
||||
.post(
|
||||
"/people",
|
||||
&[
|
||||
("surname", "Doe"),
|
||||
("first_name", "Jane"),
|
||||
("employer", &cs),
|
||||
],
|
||||
)
|
||||
.await;
|
||||
let jane = id_from(r.location());
|
||||
|
||||
app.post(&format!("/companies/{c}/archive"), &[]).await;
|
||||
assert!(
|
||||
app.get("/companies?archived=1")
|
||||
.await
|
||||
.text()
|
||||
.contains("Old Works")
|
||||
);
|
||||
assert!(
|
||||
!app.get("/people/new")
|
||||
.await
|
||||
.body
|
||||
.contains(&format!("value=\"{c}\""))
|
||||
);
|
||||
// a new person cannot choose it any more
|
||||
let r = app
|
||||
.post(
|
||||
"/people",
|
||||
&[
|
||||
("surname", "Roe"),
|
||||
("first_name", "Rita"),
|
||||
("employer", &cs),
|
||||
],
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
// the person who works there keeps it, and can be saved unchanged
|
||||
let edit = app.get(&format!("/people/{jane}/edit")).await.body;
|
||||
assert!(edit.contains(&format!(
|
||||
"<option value=\"{c}\" selected>Old Works Co., Ltd.</option>"
|
||||
)));
|
||||
let r = app
|
||||
.post(
|
||||
&format!("/people/{jane}"),
|
||||
&[
|
||||
("surname", "Doe"),
|
||||
("first_name", "Jane"),
|
||||
("employer", &cs),
|
||||
],
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::SEE_OTHER, "{}", r.body);
|
||||
|
||||
app.post(&format!("/companies/{c}/restore"), &[]).await;
|
||||
assert!(
|
||||
app.get("/people/new")
|
||||
.await
|
||||
.body
|
||||
.contains(&format!("value=\"{c}\""))
|
||||
);
|
||||
let actions: Vec<String> =
|
||||
sqlx::query_scalar("SELECT action FROM audit_log WHERE entity = 'company' ORDER BY id")
|
||||
.fetch_all(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(actions, vec!["create", "archive", "restore"]);
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
//! The deadlines of the demo people, on the Deadlines page, the people list
|
||||
//! and the person's pages.
|
||||
|
||||
use axum::http::StatusCode;
|
||||
use sqlx::PgPool;
|
||||
|
||||
use crate::common::local;
|
||||
|
||||
async fn person(pool: &PgPool, surname: &str) -> i64 {
|
||||
sqlx::query_scalar("SELECT id FROM people WHERE surname = $1")
|
||||
.bind(surname)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
|
||||
async fn the_demo_people_show_every_kind_of_deadline(pool: PgPool) {
|
||||
visa::seed::demo(&pool, visa::dates::today()).await.unwrap();
|
||||
let app = local(pool.clone());
|
||||
|
||||
let r = app.get("/").await;
|
||||
assert_eq!(r.status, StatusCode::OK);
|
||||
let page = r.text();
|
||||
for expect in [
|
||||
// Tomas: the report was due 11 days ago, a week of grace is over
|
||||
"Late",
|
||||
// Jane: 60 days of visa exemption, 20 left
|
||||
"Extension of stay",
|
||||
"Under 6 months",
|
||||
"TM.30 for the address",
|
||||
// Erika: back from a trip 79 days ago
|
||||
"Window open",
|
||||
"Work permit expiry",
|
||||
// Sofia: still abroad with her visa
|
||||
"Visa valid for entry",
|
||||
] {
|
||||
assert!(page.contains(expect), "the dashboard lacks {expect:?}");
|
||||
}
|
||||
assert!(
|
||||
!page.contains("SAMPLE"),
|
||||
"archived people have no deadlines"
|
||||
);
|
||||
// the most urgent first: Tomas's late report
|
||||
let late = page.find("Late").unwrap();
|
||||
let open = page.find("Window open").unwrap();
|
||||
assert!(late < open);
|
||||
|
||||
let tomas = person(&pool, "Novak").await;
|
||||
let p = app.get(&format!("/people/{tomas}")).await.text();
|
||||
assert!(
|
||||
p.contains("Basis: extension of stay for marriage to a Thai national."),
|
||||
"{p}"
|
||||
);
|
||||
assert!(p.contains("Report in person and pay the 2,000 THB fine"));
|
||||
let erika = person(&pool, "Mustermann").await;
|
||||
let s = app.get(&format!("/people/{erika}/stay")).await.text();
|
||||
assert!(
|
||||
s.contains("Basis: extension of stay for work, kept by a re-entry permit."),
|
||||
"{s}"
|
||||
);
|
||||
assert!(s.contains("Online until"), "Erika may still report online");
|
||||
assert!(
|
||||
s.contains("Demo Engineering Co., Ltd."),
|
||||
"the work permit names the employer"
|
||||
);
|
||||
|
||||
// the people list shows each person's next deadline
|
||||
let list = app.get("/people").await.text();
|
||||
assert!(list.contains("90-day report,"));
|
||||
// the company filter on the dashboard
|
||||
let company: i64 =
|
||||
sqlx::query_scalar("SELECT id FROM companies WHERE name = 'Demo Accounting Partnership'")
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let only = app.get(&format!("/?company={company}")).await.text();
|
||||
assert!(only.contains("NOVAK, Tomas") && !only.contains("MUSTERMANN"));
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
|
||||
async fn a_new_arrival_brings_its_deadlines(pool: PgPool) {
|
||||
let app = local(pool.clone());
|
||||
let r = app
|
||||
.post("/people", &[("surname", "Doe"), ("first_name", "Jane")])
|
||||
.await;
|
||||
let id = crate::common::id_from(r.location());
|
||||
let page = app.get(&format!("/people/{id}")).await.text();
|
||||
assert!(page.contains("No arrival is recorded yet."));
|
||||
|
||||
let today = visa::dates::today();
|
||||
let arrived = (today - chrono::TimeDelta::days(30)).to_string();
|
||||
let until = (today + chrono::TimeDelta::days(150)).to_string();
|
||||
let r = app
|
||||
.post(
|
||||
&format!("/people/{id}/arrivals"),
|
||||
&[
|
||||
("arrived_on", arrived.as_str()),
|
||||
("admission", "visa"),
|
||||
("permitted_until", until.as_str()),
|
||||
],
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::SEE_OTHER, "{}", r.text());
|
||||
let page = app.get(&format!("/people/{id}")).await.text();
|
||||
assert!(page.contains("In Thailand since"));
|
||||
assert!(page.contains("90-day report"));
|
||||
assert!(page.contains("Calculated as the arrival plus 89 days"));
|
||||
assert!(page.contains("No current address is recorded."));
|
||||
let due = visa::dates::fmt_date(today + chrono::TimeDelta::days(59));
|
||||
assert!(page.contains(&due), "the report is due on day 90: {due}");
|
||||
}
|
||||
@@ -4,8 +4,11 @@
|
||||
|
||||
mod auth;
|
||||
mod common;
|
||||
mod companies;
|
||||
mod deadlines;
|
||||
mod pages;
|
||||
mod people;
|
||||
mod records;
|
||||
|
||||
#[test]
|
||||
fn every_test_file_is_a_module() {
|
||||
|
||||
+70
-4
@@ -31,7 +31,69 @@ async fn every_page_renders_with_demo_data(pool: PgPool) {
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let pages = [
|
||||
let company: i64 =
|
||||
sqlx::query_scalar("SELECT id FROM companies WHERE name = 'Demo Engineering Co., Ltd.'")
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let arrival: i64 = sqlx::query_scalar(
|
||||
"SELECT id FROM arrivals WHERE person_id = $1 ORDER BY arrived_on DESC LIMIT 1",
|
||||
)
|
||||
.bind(erika)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let permit: i64 = sqlx::query_scalar("SELECT id FROM work_permits WHERE person_id = $1")
|
||||
.bind(erika)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let mut pages = vec![
|
||||
("/".to_string(), "90-day report".to_string()),
|
||||
("/?all=1".to_string(), "Only the next 90 days".to_string()),
|
||||
(
|
||||
format!("/?company={company}"),
|
||||
"MUSTERMANN, Erika".to_string(),
|
||||
),
|
||||
(
|
||||
format!("/people/{erika}/stay"),
|
||||
"Arrivals and departures".to_string(),
|
||||
),
|
||||
(format!("/people/{erika}/activity"), "seed".to_string()),
|
||||
(
|
||||
format!("/people/{erika}/arrivals/{arrival}/edit"),
|
||||
"Edit the arrival".to_string(),
|
||||
),
|
||||
(
|
||||
format!("/people/{erika}/work-permits/{permit}/edit"),
|
||||
"WP-DEMO-0001".to_string(),
|
||||
),
|
||||
(
|
||||
format!("/people?company={company}"),
|
||||
"People at Demo Engineering Co., Ltd.".to_string(),
|
||||
),
|
||||
(
|
||||
"/companies".to_string(),
|
||||
"Demo Accounting Partnership".to_string(),
|
||||
),
|
||||
("/companies/new".to_string(), "New company".to_string()),
|
||||
(
|
||||
format!("/companies/{company}"),
|
||||
"People employed".to_string(),
|
||||
),
|
||||
(
|
||||
format!("/companies/{company}/edit"),
|
||||
"0999999000004".to_string(),
|
||||
),
|
||||
];
|
||||
for kind in visa::records::STAY {
|
||||
pages.push((
|
||||
format!("/people/{erika}/{}/new", kind.slug),
|
||||
format!("New {}", kind.one),
|
||||
));
|
||||
}
|
||||
let older = [
|
||||
("/people".to_string(), "MUSTERMANN, Erika"),
|
||||
("/people?archived=1".to_string(), "SAMPLE, Oliver"),
|
||||
("/people/new".to_string(), "Read the passport"),
|
||||
@@ -53,10 +115,14 @@ async fn every_page_renders_with_demo_data(pool: PgPool) {
|
||||
("/activity".to_string(), "seed"),
|
||||
(format!("/activity?person={erika}"), "MUSTERMANN, Erika"),
|
||||
];
|
||||
pages.extend(older.into_iter().map(|(p, e)| (p, e.to_string())));
|
||||
for (path, expect) in &pages {
|
||||
let r = app.get(path).await;
|
||||
assert_eq!(r.status, StatusCode::OK, "{path}: {}", r.body);
|
||||
assert!(r.text().contains(expect), "{path} lacks {expect:?}");
|
||||
assert!(
|
||||
r.text().contains(expect.as_str()),
|
||||
"{path} lacks {expect:?}"
|
||||
);
|
||||
assert_unique_ids(&r.body, path);
|
||||
assert_eq!(r.headers["cache-control"], "no-store", "{path}");
|
||||
}
|
||||
@@ -103,8 +169,8 @@ async fn open_paths_headers_and_not_found(pool: PgPool) {
|
||||
);
|
||||
|
||||
let r = app.get("/").await;
|
||||
assert_eq!(r.status, StatusCode::SEE_OTHER);
|
||||
assert_eq!(r.location(), "/people");
|
||||
assert_eq!(r.status, StatusCode::OK);
|
||||
assert!(r.text().contains("No people yet"));
|
||||
|
||||
let r = app.get("/people/999999").await;
|
||||
assert_eq!(r.status, StatusCode::NOT_FOUND);
|
||||
|
||||
@@ -0,0 +1,262 @@
|
||||
//! The records of a stay through their forms: every kind is added, edited
|
||||
//! and deleted, with the audit log; references stay within the person.
|
||||
|
||||
use axum::http::StatusCode;
|
||||
use sqlx::PgPool;
|
||||
|
||||
use crate::common::{id_from, local};
|
||||
|
||||
async fn last_created(pool: &PgPool, person: i64) -> (i64, String) {
|
||||
sqlx::query_as(
|
||||
"SELECT entity_id, entity FROM audit_log WHERE person_id = $1 AND action = 'create' \
|
||||
ORDER BY id DESC LIMIT 1",
|
||||
)
|
||||
.bind(person)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// A kind's slug and audit entity, the fields of a new record, and a field
|
||||
/// to change: its name, the value typed and the value shown.
|
||||
type Case<'a> = (
|
||||
&'a str,
|
||||
&'a str,
|
||||
Vec<(&'a str, &'a str)>,
|
||||
(&'a str, &'a str, &'a str),
|
||||
);
|
||||
|
||||
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
|
||||
async fn every_kind_is_added_edited_and_deleted(pool: PgPool) {
|
||||
let app = local(pool.clone());
|
||||
let r = app
|
||||
.post("/companies", &[("name", "Test Works Co., Ltd.")])
|
||||
.await;
|
||||
let company = r.location().trim_start_matches("/companies/").to_string();
|
||||
let r = app
|
||||
.post("/people", &[("surname", "Doe"), ("first_name", "Jane")])
|
||||
.await;
|
||||
let id = id_from(r.location());
|
||||
|
||||
// (slug, entity, the fields of a new record, a field to change)
|
||||
let cases: Vec<Case> = vec![
|
||||
(
|
||||
"visas",
|
||||
"visa",
|
||||
vec![("category", "non-b"), ("valid_until", "2027-01-01")],
|
||||
("number", "v 123", "V123"),
|
||||
),
|
||||
(
|
||||
"arrivals",
|
||||
"arrival",
|
||||
vec![
|
||||
("arrived_on", "2026-09-01"),
|
||||
("admission", "exempt"),
|
||||
("permitted_until", "2026-10-30"),
|
||||
],
|
||||
("tdac_no", "tdac 77", "TDAC77"),
|
||||
),
|
||||
(
|
||||
"extensions",
|
||||
"extension",
|
||||
vec![
|
||||
("applied_on", "2026-10-20"),
|
||||
("basis", "tourism"),
|
||||
("decision", "approved"),
|
||||
("permitted_until", "2026-11-29"),
|
||||
],
|
||||
("fee_thb", "1,900", "1,900 THB"),
|
||||
),
|
||||
(
|
||||
"reports",
|
||||
"report",
|
||||
vec![("reported_on", "2026-09-10"), ("method", "online")],
|
||||
("next_due_on", "2026-12-09", "Dec 9, 2026"),
|
||||
),
|
||||
(
|
||||
"reentry-permits",
|
||||
"reentry-permit",
|
||||
vec![
|
||||
("kind", "single"),
|
||||
("issued_on", "2026-09-10"),
|
||||
("valid_until", "2026-10-30"),
|
||||
],
|
||||
(
|
||||
"office",
|
||||
"Chiang Mai Immigration Office",
|
||||
"Chiang Mai Immigration Office",
|
||||
),
|
||||
),
|
||||
(
|
||||
"work-permits",
|
||||
"work-permit",
|
||||
vec![
|
||||
("company_id", company.as_str()),
|
||||
("valid_until", "2027-09-01"),
|
||||
],
|
||||
("position", "Designer", "Designer"),
|
||||
),
|
||||
];
|
||||
for (slug, entity, fields, (field, value, shown)) in cases {
|
||||
let r = app.post(&format!("/people/{id}/{slug}"), &fields).await;
|
||||
assert_eq!(r.status, StatusCode::SEE_OTHER, "{slug}: {}", r.text());
|
||||
assert_eq!(r.location(), format!("/people/{id}/stay#{slug}"));
|
||||
let (rid, e) = last_created(&pool, id).await;
|
||||
assert_eq!(e, entity);
|
||||
|
||||
let edit = app.get(&format!("/people/{id}/{slug}/{rid}/edit")).await;
|
||||
assert_eq!(edit.status, StatusCode::OK, "{slug}");
|
||||
let mut changed = fields.clone();
|
||||
changed.push((field, value));
|
||||
let r = app
|
||||
.post(&format!("/people/{id}/{slug}/{rid}"), &changed)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::SEE_OTHER, "{slug}: {}", r.text());
|
||||
let logged: Vec<String> = sqlx::query_scalar(
|
||||
"SELECT fields FROM audit_log WHERE entity = $1 AND entity_id = $2 \
|
||||
AND action = 'update'",
|
||||
)
|
||||
.bind(entity)
|
||||
.bind(rid)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(logged, vec![field.to_string()], "{slug}");
|
||||
let page = app.get(&format!("/people/{id}/stay")).await.text();
|
||||
assert!(page.contains(shown), "{slug}: {shown} not shown");
|
||||
|
||||
// another person's path finds nothing
|
||||
assert_eq!(
|
||||
app.get(&format!("/people/{}/{slug}/{rid}/edit", id + 1000))
|
||||
.await
|
||||
.status,
|
||||
StatusCode::NOT_FOUND
|
||||
);
|
||||
let r = app
|
||||
.post(&format!("/people/{id}/{slug}/{rid}/delete"), &[])
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::SEE_OTHER, "{slug}");
|
||||
}
|
||||
let page = app.get(&format!("/people/{id}/stay")).await.text();
|
||||
assert!(page.contains("No arrival recorded yet."));
|
||||
let deleted: i64 = sqlx::query_scalar("SELECT count(*) FROM audit_log WHERE action = 'delete'")
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(deleted, 6);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
|
||||
async fn forms_say_what_is_missing_or_wrong(pool: PgPool) {
|
||||
let app = local(pool.clone());
|
||||
let r = app
|
||||
.post("/people", &[("surname", "Doe"), ("first_name", "Jane")])
|
||||
.await;
|
||||
let id = id_from(r.location());
|
||||
let r = app.post(&format!("/people/{id}/arrivals"), &[]).await;
|
||||
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
let t = r.text();
|
||||
assert!(
|
||||
t.contains("Enter the arrival date.") && t.contains("Enter the date in the arrival stamp.")
|
||||
);
|
||||
|
||||
let r = app
|
||||
.post(
|
||||
&format!("/people/{id}/arrivals"),
|
||||
&[
|
||||
("arrived_on", "2026-09-01"),
|
||||
("admission", "visa"),
|
||||
("permitted_until", "2026-08-01"),
|
||||
("departed_on", "2026-08-15"),
|
||||
],
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
let t = r.text();
|
||||
assert!(t.contains("The permitted stay cannot end before the arrival."));
|
||||
assert!(t.contains("The departure cannot come before the arrival."));
|
||||
assert!(r.body.contains("value=\"2026-09-01\""), "typed values stay");
|
||||
|
||||
let r = app
|
||||
.post(
|
||||
&format!("/people/{id}/extensions"),
|
||||
&[
|
||||
("applied_on", "2026-10-20"),
|
||||
("basis", "work"),
|
||||
("decision", "consideration"),
|
||||
],
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
assert!(r.text().contains("Enter the date in the stamp."));
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
|
||||
async fn passports_and_visas_are_chosen_from_the_persons_own(pool: PgPool) {
|
||||
let app = local(pool.clone());
|
||||
let mut people = Vec::new();
|
||||
for (surname, number) in [("Doe", "Z00000042"), ("Roe", "Z00000043")] {
|
||||
let r = app
|
||||
.post("/people", &[("surname", surname), ("first_name", "Test")])
|
||||
.await;
|
||||
let id = id_from(r.location());
|
||||
let r = app
|
||||
.post(
|
||||
&format!("/people/{id}/passports"),
|
||||
&[
|
||||
("number", number),
|
||||
("issuing_country", "USA"),
|
||||
("expires_on", "2031-01-01"),
|
||||
],
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::SEE_OTHER, "{}", r.text());
|
||||
let pp: i64 = sqlx::query_scalar("SELECT id FROM passports WHERE number = $1")
|
||||
.bind(number)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
people.push((id, pp));
|
||||
}
|
||||
let (doe, doe_pp) = people[0];
|
||||
let (_, roe_pp) = people[1];
|
||||
|
||||
// the new arrival's form offers Doe's passport, already chosen
|
||||
let form = app.get(&format!("/people/{doe}/arrivals/new")).await.body;
|
||||
assert!(form.contains(&format!(
|
||||
"<option value=\"{doe_pp}\" selected>Z00000042 (United States)</option>"
|
||||
)));
|
||||
assert!(!form.contains("Z00000043"));
|
||||
|
||||
let arrival = |pp: i64| {
|
||||
vec![
|
||||
("arrived_on", "2026-09-01".to_string()),
|
||||
("admission", "exempt".to_string()),
|
||||
("permitted_until", "2026-10-30".to_string()),
|
||||
("passport_id", pp.to_string()),
|
||||
]
|
||||
};
|
||||
let other = arrival(roe_pp);
|
||||
let other: Vec<(&str, &str)> = other.iter().map(|(k, v)| (*k, v.as_str())).collect();
|
||||
let r = app.post(&format!("/people/{doe}/arrivals"), &other).await;
|
||||
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
assert!(r.text().contains("Choose from the list."));
|
||||
|
||||
let own = arrival(doe_pp);
|
||||
let own: Vec<(&str, &str)> = own.iter().map(|(k, v)| (*k, v.as_str())).collect();
|
||||
let r = app.post(&format!("/people/{doe}/arrivals"), &own).await;
|
||||
assert_eq!(r.status, StatusCode::SEE_OTHER, "{}", r.text());
|
||||
|
||||
// deleting the passport leaves the arrival without one
|
||||
let r = app
|
||||
.post(&format!("/people/{doe}/passports/{doe_pp}/delete"), &[])
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::SEE_OTHER);
|
||||
let left: Option<i64> =
|
||||
sqlx::query_scalar("SELECT passport_id FROM arrivals WHERE person_id = $1")
|
||||
.bind(doe)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(left, None);
|
||||
}
|
||||
Reference in new issue
Block a user