Files
visa/tests/companies.rs
T
grabowski 83bc481952
CI / secret scan (push) Successful in 4s
CI / fmt, clippy, tests (push) Successful in 22s
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.
2026-11-17 18:20:14 +07:00

266 lines
8.1 KiB
Rust

//! 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"]);
}