Files
visa/tests/originals.rs
T
grabowski ff716eb9d3
CI / secret scan (push) Successful in 8s
CI / fmt, clippy, tests (push) Successful in 1m23s
CI / release (push) Skipped
Originals in our care (phase 12, step 1)
What clients hand Lanna Desk for a while (a passport for an extension, a
signed contract, a title deed) gets a code and a label (OR-2026-0001), and
a record of where it is: held at a place in the office, out with someone
such as Chiang Mai Immigration until a date, or returned.

- Migration 0013: table originals, owned by a client or a person; the
  database refuses a delete and a change of what it is, whose it is and
  how it came.
- Every intake and move is a journal entry starting with the code; the
  item's page reads its history from there. The audit log has the new
  entity "original", with a view when a person's original is shown.
- Pages: Legal, Originals (in our care, out, held, returned, all; a find
  box for a typed or scanned code), the item's page with the move form,
  intake from a client's or a person's page, and the label with a QR code
  to the item's page. Cards on the client's and the person's pages.
- A field can show only for some choices of a selection
  (data-shown-when), so the move form asks "Where it lies", "With whom"
  or "Returned to" as fits.
- Demo data: a passport out at Immigration, a title deed and a house
  registration held, a company certificate returned.
- PLAN: phase 12, with step 2 (the inbox that files a pile of scans) and
  step 3 planned.
2026-11-23 14:06:50 +07:00

289 lines
9.7 KiB
Rust

//! Originals in our care (phase 12): taken in from a client's or a person's
//! page, moved out and back, returned; every step in the journal, nothing
//! deleted or rewritten.
use axum::http::StatusCode;
use sqlx::PgPool;
use crate::common::local;
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
async fn an_original_is_taken_in_moved_and_returned_on_record(pool: PgPool) {
let app = local(pool.clone());
let today = visa::dates::today();
visa::seed::demo(&pool, today).await.unwrap();
let person: i64 = sqlx::query_scalar("SELECT min(id) FROM people")
.fetch_one(&pool)
.await
.unwrap();
// the form starts from the person's page and names them
let t = app
.get(&format!("/originals/new?person={person}"))
.await
.text();
assert!(t.contains("id=\"original-form\""), "{t}");
assert!(t.contains(&format!("value=\"person:{person}\"")));
assert_eq!(
app.get("/originals/new").await.status,
StatusCode::BAD_REQUEST
);
// what it is, from whom and where are needed
let owner = format!("person:{person}");
let r = app
.post(
"/originals",
&[
("owner", &owner),
("kind", "passport"),
("what", ""),
("received_from", "x"),
("location", ""),
],
)
.await;
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
let t = r.text();
assert!(t.contains("Say what it is"), "{t}");
assert!(t.contains("Say where it lies now"));
let r = app
.post(
"/originals",
&[
("owner", &owner),
("kind", "passport"),
("what", "Passport C01X00T47, Germany"),
("received_from", "Erika Mustermann"),
("location", "Office, safe, tray 1"),
],
)
.await;
assert_eq!(r.status, StatusCode::SEE_OTHER, "{}", r.text());
let code = r.location().trim_start_matches("/originals/").to_string();
assert!(
code.starts_with("OR-20") && code.ends_with("-0001"),
"{code}"
);
let id: i64 = sqlx::query_scalar("SELECT id FROM originals WHERE code = $1")
.bind(&code)
.fetch_one(&pool)
.await
.unwrap();
// out with Immigration: a holder is needed
let move_to = format!("/originals/{id}/move");
let r = app
.post(&move_to, &[("state", "out"), ("holder", "")])
.await;
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
assert!(r.text().contains("Say who has it"));
let back = (today + chrono::Duration::days(5)).to_string();
let r = app
.post(
&move_to,
&[
("state", "out"),
("holder", "Chiang Mai Immigration"),
("due_on", &back),
("note", "With the extension."),
],
)
.await;
assert_eq!(r.status, StatusCode::SEE_OTHER, "{}", r.text());
// the same again changes nothing
app.post(
&move_to,
&[
("state", "out"),
("holder", "Chiang Mai Immigration"),
("due_on", &back),
],
)
.await;
// back in the safe, then returned to her
app.post(
&move_to,
&[("state", "held"), ("location", "Office, safe, tray 1")],
)
.await;
let r = app
.post(
&move_to,
&[
("state", "returned"),
("holder", "Chiang Mai Immigration"),
("returned_to", "Erika Mustermann"),
],
)
.await;
assert_eq!(r.status, StatusCode::SEE_OTHER);
let events: Vec<(String, String)> = sqlx::query_as(
"SELECT event, detail FROM journal WHERE starts_with(detail, $1) ORDER BY seq",
)
.bind(format!("{code}:"))
.fetch_all(&pool)
.await
.unwrap();
let names: Vec<&str> = events.iter().map(|e| e.0.as_str()).collect();
assert_eq!(
names,
[
"original received",
"original out",
"original moved",
"original returned"
]
);
assert!(
events[1]
.1
.contains("out with Chiang Mai Immigration, back by"),
"{:?}",
events[1]
);
assert!(events[1].1.ends_with("With the extension."));
assert!(events[3].1.contains("returned to Erika Mustermann"));
assert!(
visa::journal::verify(&pool).await.unwrap().whole(),
"the chain is whole"
);
// the page shows it all, also by a scanned lower-case code
let r = app
.get(&format!("/originals/{}", code.to_lowercase()))
.await;
assert_eq!(r.status, StatusCode::SEE_OTHER);
let t = app.get(&format!("/originals/{code}")).await.text();
assert!(t.contains("Returned") && t.contains("id=\"history\""));
assert!(t.contains("original out"));
let r = app
.get(&format!("/originals?code=%20{}%0A", code.to_lowercase()))
.await;
assert_eq!(r.location(), format!("/originals/{code}"));
assert_eq!(
app.get("/originals?code=OR-1999-9999").await.status,
StatusCode::NOT_FOUND
);
// a returned one leaves the list of those in our care
let row = format!("id=\"original-{id}\"");
assert!(!app.get("/originals").await.text().contains(&row));
assert!(
app.get("/originals?show=returned")
.await
.text()
.contains(&row)
);
// the person's page lists it, and the label prints
let t = app.get(&format!("/people/{person}")).await.text();
assert!(t.contains("id=\"originals\"") && t.contains(&code));
let t = app.get(&format!("/originals/{id}/label")).await.text();
assert!(t.contains(&format!("/originals/{code}")) && t.contains("<svg"));
// viewing a person's original is in the audit log
let views: i64 = sqlx::query_scalar(
"SELECT count(*) FROM audit_log WHERE entity = 'original' AND action = 'view' AND person_id = $1",
)
.bind(person)
.fetch_one(&pool)
.await
.unwrap();
assert!(views >= 2, "{views}");
// the database keeps it: no delete, no new owner or description
assert!(
sqlx::query("DELETE FROM originals WHERE id = $1")
.bind(id)
.execute(&pool)
.await
.is_err()
);
assert!(
sqlx::query("UPDATE originals SET what = 'something else' WHERE id = $1")
.bind(id)
.execute(&pool)
.await
.is_err()
);
// a held original needs its place, one out its holder
assert!(
sqlx::query("UPDATE originals SET state = 'out', holder = '' WHERE id = $1")
.bind(id)
.execute(&pool)
.await
.is_err()
);
}
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
async fn a_clients_original_can_belong_to_their_contract(pool: PgPool) {
let app = local(pool.clone());
let today = visa::dates::today();
visa::seed::demo(&pool, today).await.unwrap();
visa::seed::legal::demo(&pool, None, today).await.unwrap();
// the demo has some in our care, one out at Immigration first
let t = app.get("/originals").await.text();
assert!(t.contains("Chiang Mai Immigration"), "{t}");
let first = t.find("Chiang Mai Immigration").unwrap();
assert!(
first < t.find("Chanote no. 12345").unwrap(),
"what is out comes first"
);
let (client, contract): (i64, i64) =
sqlx::query_as("SELECT client_id, id FROM contracts ORDER BY id LIMIT 1")
.fetch_one(&pool)
.await
.unwrap();
let other: i64 = sqlx::query_scalar("SELECT id FROM contracts WHERE client_id <> $1 LIMIT 1")
.bind(client)
.fetch_one(&pool)
.await
.unwrap();
let owner = format!("client:{client}");
let t = app
.get(&format!("/originals/new?client={client}"))
.await
.text();
assert!(t.contains("Belongs to the contract"));
let form = |c: String| {
vec![
("owner".to_string(), owner.clone()),
("kind".to_string(), "contract".to_string()),
("what".to_string(), "Signed original, 12 pages".to_string()),
("received_from".to_string(), "Khun Ploy".to_string()),
("location".to_string(), "Office, cabinet B".to_string()),
("contract".to_string(), c),
]
};
fn pairs(v: &[(String, String)]) -> Vec<(&str, &str)> {
v.iter().map(|(a, b)| (a.as_str(), b.as_str())).collect()
}
// another client's contract is refused
let bad = form(other.to_string());
let r = app.post("/originals", &pairs(&bad)).await;
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
assert!(r.text().contains("Choose one of this client's contracts"));
let good = form(contract.to_string());
let r = app.post("/originals", &pairs(&good)).await;
assert_eq!(r.status, StatusCode::SEE_OTHER, "{}", r.text());
let (cid, kid): (Option<i64>, Option<i64>) = sqlx::query_as(
"SELECT client_id, contract_id FROM originals WHERE what = 'Signed original, 12 pages'",
)
.fetch_one(&pool)
.await
.unwrap();
assert_eq!((cid, kid), (Some(client), Some(contract)));
// its journal entry belongs to the client and the contract
let (jc, jk): (Option<i64>, Option<i64>) = sqlx::query_as(
"SELECT client_id, contract_id FROM journal WHERE event = 'original received' ORDER BY seq DESC LIMIT 1",
)
.fetch_one(&pool)
.await
.unwrap();
assert_eq!((jc, jk), (Some(client), Some(contract)));
let t = app.get(&format!("/clients/{client}")).await.text();
assert!(t.contains("Signed original, 12 pages"));
}