Files
visa/tests/companies.rs
T
grabowski 2a6e972b68
CI / secret scan (push) Successful in 4s
CI / fmt, clippy, tests (push) Successful in 22s
A company's office: its GPS position and a photo
The company form has a field for the office's GPS position (migration
0004, column companies.office_gps). It takes what people have at hand:
the numbers Google Maps copies, a link to the place on Google Maps or
OpenStreetMap, a geo: URI, or degrees, minutes and seconds. It keeps
decimal degrees with six decimals and refuses a position outside Thailand,
which catches a swapped pair; a short maps.app.goo.gl link is refused with
a hint, as it holds no position (src/gps.rs, with unit tests).

The company's page has an Office card: the position with links that open
it on OpenStreetMap and Google Maps, the newest photo of the office, and a
strip that uploads a photo (kind "Photo of the office") and returns to the
card. The photos are documents like any other: encrypted on the server and
audited. A refused photo shows its errors on the card only; office photos
that are PDFs are counted under Company documents.

The demo company has a position. Tests: the position read from a link,
from degrees, minutes and seconds, the three refusals, the audit entry and
its removal; a photo uploaded from the card, shown, and its errors placed.
2026-11-18 11:56:26 +07:00

339 lines
11 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"]);
}
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
async fn the_office_position_is_read_from_what_people_paste(pool: PgPool) {
let app = local(pool.clone());
let name = ("name", "Test Works Co., Ltd.");
for (pasted, message) in [
("99.0082, 18.7965", "outside Thailand"),
(
"https://maps.app.goo.gl/AbCdEf123",
"A short link holds no position",
),
("next to the market", "Enter the latitude and longitude"),
] {
let r = app
.post("/companies", &[name, ("office_gps", pasted)])
.await;
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY, "{pasted}");
assert!(r.text().contains(message), "{pasted}: {}", r.text());
}
// a link to the place on Google Maps
let r = app
.post(
"/companies",
&[
name,
(
"office_gps",
"https://www.google.com/maps/place/Demo+Tower/@18.7965,99.0082,17z/data=!3m1",
),
],
)
.await;
assert_eq!(r.status, StatusCode::SEE_OTHER, "{}", r.body);
let id = company_from(r.location());
let stored = || async {
sqlx::query_scalar::<_, String>("SELECT office_gps FROM companies WHERE id = $1")
.bind(id)
.fetch_one(&pool)
.await
.unwrap()
};
assert_eq!(stored().await, "18.796500, 99.008200");
let page = app.get(&format!("/companies/{id}")).await.text();
assert!(page.contains("18.796500, 99.008200"), "{page}");
assert!(
page.contains("https://www.openstreetmap.org/?mlat=18.796500&mlon=99.008200"),
"{page}"
);
assert!(page.contains("https://www.google.com/maps/search/?api=1&query=18.796500,99.008200"));
assert!(page.contains("No photo of the office yet"));
// degrees, minutes and seconds; the audit log names the field
let r = app
.post(
&format!("/companies/{id}"),
&[name, ("office_gps", "18°47'47.4\"N 99°00'29.5\"E")],
)
.await;
assert_eq!(r.status, StatusCode::SEE_OTHER, "{}", r.body);
assert_eq!(stored().await, "18.796500, 99.008194");
assert_eq!(
audit_fields(&pool, "company", id, "update").await,
vec![vec!["office_gps".to_string()]]
);
// and it can be taken out again
let r = app.post(&format!("/companies/{id}"), &[name]).await;
assert_eq!(r.status, StatusCode::SEE_OTHER, "{}", r.body);
assert_eq!(stored().await, "");
let page = app.get(&format!("/companies/{id}")).await.text();
assert!(page.contains("Add the GPS position"), "{page}");
}