A company's office: its GPS position and a photo
CI / secret scan (push) Successful in 4s
CI / fmt, clippy, tests (push) Successful in 22s

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.
This commit is contained in:
grabowski committed 2026-11-18 11:56:26 +07:00
1 parent e88bb74e1e
commit 2a6e972b68
14 files changed
+642 -8

No files matched your search

+73
View File
@@ -263,3 +263,76 @@ async fn an_archived_company_is_no_longer_offered(pool: PgPool) {
.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}");
}
+73
View File
@@ -610,3 +610,76 @@ async fn the_demo_documents_open_as_what_they_are(pool: PgPool) {
.is_empty()
);
}
/// A 1 x 1 PNG.
const DOT_PNG: &[u8] = &[
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4,
0x89, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0x00, 0x01, 0x00, 0x00,
0x05, 0x00, 0x01, 0x0d, 0x0a, 0x2d, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae,
0x42, 0x60, 0x82,
];
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
async fn the_office_card_takes_a_photo_and_shows_it(pool: PgPool) {
visa::seed::demo(&pool, visa::dates::today()).await.unwrap();
let app = local(pool.clone());
let company: i64 =
sqlx::query_scalar("SELECT id FROM companies WHERE name = 'Demo Engineering Co., Ltd.'")
.fetch_one(&pool)
.await
.unwrap();
let base = format!("/companies/{company}/documents");
let office = [("kind", "office-photo"), ("back", "office")];
// refused from the card: the card says so, the documents' strip does not
let r = app.upload(&base, &office, &[("office.png", b"")]).await;
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
let t = r.text();
let (card, list) = (
t.find("id=\"office\"").unwrap(),
t.find("id=\"documents\"").unwrap(),
);
let notice = t
.find("Nothing was uploaded")
.expect("the card has the notice");
assert!(
card < notice && notice < list,
"the notice is on the office card"
);
assert_eq!(t.matches("Nothing was uploaded").count(), 1, "{t}");
let r = app.upload(&base, &office, &[("office.png", DOT_PNG)]).await;
assert_eq!(r.status, StatusCode::SEE_OTHER, "{}", r.body);
assert_eq!(r.location(), format!("/companies/{company}#office"));
let did: i64 = sqlx::query_scalar("SELECT id FROM documents WHERE company_id = $1")
.bind(company)
.fetch_one(&pool)
.await
.unwrap();
let t = app.get(&format!("/companies/{company}")).await.text();
assert!(
t.contains(&format!("<img src=\"{base}/{did}/file\"")),
"the card shows the photo: {t}"
);
assert!(t.contains("Add a newer photo"));
assert!(
t.contains("18.796500, 99.008200"),
"the demo office's position"
);
let r = app.get(&format!("{base}/{did}/file")).await;
assert_eq!(r.bytes, DOT_PNG);
assert_eq!(r.header("content-type"), "image/png");
// a PDF of the office is listed, but the card shows only pictures
let r = app
.upload(&base, &office, &[("office.pdf", &pdf("office plan"))])
.await;
assert_eq!(r.status, StatusCode::SEE_OTHER, "{}", r.body);
let t = app.get(&format!("/companies/{company}")).await.text();
assert!(
t.contains(&format!("<img src=\"{base}/{did}/file\"")),
"{t}"
);
assert!(t.contains("1 more under"), "{t}");
}