Lines
100 %
Functions
Branches
//! The commodity edit page over the `commodity-edit` form, served on its
//! production paths (plan 0016 M3i).
use axum::{
Router,
http::StatusCode,
routing::{get, post},
};
use forms::COMMODITY_EDIT;
use server::command::commodity_tag::SetCommodityTag;
use sqlx::types::Uuid;
use web::model::User;
use web::pages::commodity::edit::{
SUBMIT_PATH, commodity_edit_page, commodity_edit_submit, updated_message,
use crate::common::create_mock_user;
use crate::common::edit_page::{posted, with_value};
use crate::common::entity_edit::{
form_html, form_pairs, fresh_revision, get_from, post_to, with_tags,
use crate::common::new_page::Reply;
use crate::common::seed::commodity_with_name;
use crate::common::tenant::{provision_tenant, tenant_connection};
const FORM: &str = "nf-commodity-edit";
fn router() -> Router<std::sync::Arc<web::AppState>> {
Router::new()
.route("/commodity/{id}/edit", get(commodity_edit_page))
.route_layer(axum::middleware::from_fn(web::error_render::full_page))
.route(SUBMIT_PATH, post(commodity_edit_submit))
}
async fn page(user: &User, id: &str) -> Reply {
get_from(router(), user, &format!("/commodity/{id}/edit")).await
async fn submit(user: &User, pairs: &[(String, String)]) -> Reply {
post_to(router(), user, SUBMIT_PATH, pairs).await
async fn set_tag(user: &User, commodity: Uuid, name: &str, value: &str) {
SetCommodityTag::new()
.user_id(user.id)
.commodity_id(commodity)
.tag_name(name.to_string())
.tag_value(value.to_string())
.run()
.await
.expect("set tag");
async fn seeded() -> (User, Uuid) {
let user = create_mock_user();
provision_tenant(user.id).await;
let usd = commodity_with_name(user.id, "USD", "Dollar").await;
set_tag(&user, usd, "kind", "fiat").await;
(user, usd)
async fn stored(user: &User, commodity: Uuid) -> forms::FormValues {
rpc::forms::load(user.id, &COMMODITY_EDIT, &commodity.to_string())
.expect("load")
#[supp_macro::shared_runtime_test]
async fn the_page_renders_the_edit_form_with_the_loaded_values() {
let (user, usd) = seeded().await;
let reply = page(&user, &usd.to_string()).await;
assert_eq!(reply.status, StatusCode::OK, "{}", reply.body);
let pairs = form_pairs(&reply.body, FORM);
assert_eq!(posted(&pairs, "id"), usd.to_string());
assert_eq!(posted(&pairs, "symbol"), "USD");
assert_eq!(posted(&pairs, "name"), "Dollar");
assert_eq!(posted(&pairs, "tags.0.name"), "kind");
assert_eq!(posted(&pairs, "tags.0.value"), "fiat");
assert!(!posted(&pairs, "revision").is_empty());
async fn a_submit_renames_and_sets_and_clears_tags() {
let loaded = page(&user, &usd.to_string()).await;
let sent = with_tags(
&with_value(
&with_value(&form_pairs(&loaded.body, FORM), "symbol", "DOL"),
"name",
"Buck",
),
&[("color", "green")],
);
let reply = submit(&user, &sent).await;
let after = stored(&user, usd).await;
assert!(
reply.body.starts_with(&updated_message(usd)),
"{}",
reply.body
assert_eq!(after.text("symbol"), Some("DOL"));
assert_eq!(after.text("name"), Some("Buck"));
let rows = after.rows("tags").unwrap_or_default();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0]["name"], "color");
async fn an_invalid_submit_rerenders_the_form_with_its_errors() {
let sent = with_value(&form_pairs(&loaded.body, FORM), "symbol", "");
assert_eq!(
reply.status,
StatusCode::UNPROCESSABLE_ENTITY,
reply
.headers
.get("hx-retarget")
.and_then(|v| v.to_str().ok()),
Some("#nf-commodity-edit")
assert!(form_html(&reply.body, FORM).contains("field-error"));
assert_eq!(after.text("symbol"), Some("USD"));
async fn a_stale_submit_shows_the_banner_with_a_reload_link() {
set_tag(&user, usd, "color", "green").await;
let reply = submit(&user, &form_pairs(&loaded.body, FORM)).await;
let form = form_html(&reply.body, FORM);
assert!(form.contains("form-banner"), "{form}");
assert!(form.contains("form-reload"), "{form}");
form.contains(&format!("href=\"/commodity/{usd}/edit\"")),
"{form}"
async fn a_refused_load_shows_the_refusal_page() {
let twin = Uuid::new_v4();
let mut conn = tenant_connection(user.id).await;
sqlx::query("INSERT INTO tags (id, tag_name, tag_value) VALUES ($1, 'kind', 'again')")
.bind(twin)
.execute(&mut *conn)
.expect("tag");
sqlx::query("INSERT INTO commodity_tags (commodity_id, tag_id) VALUES ($1, $2)")
.bind(usd)
.expect("link");
reply.body.contains("two values of the tag"),
assert!(!reply.body.contains(FORM), "{}", reply.body);
async fn an_unknown_commodity_has_its_typed_page() {
let (user, _) = seeded().await;
let reply = page(&user, &Uuid::new_v4().to_string()).await;
assert_eq!(reply.status, StatusCode::NOT_FOUND, "{}", reply.body);
async fn two_saves_from_one_page_both_succeed_with_the_swapped_revision() {
let first = with_value(&form_pairs(&loaded.body, FORM), "name", "Buck");
let saved = submit(&user, &first).await;
let next = with_value(
&with_value(&first, "revision", &fresh_revision(&saved.body)),
"Bucks",
let again = submit(&user, &next).await;
assert_eq!(saved.status, StatusCode::OK, "{}", saved.body);
saved.body.starts_with(&updated_message(usd)),
saved.body
assert_eq!(again.status, StatusCode::OK, "{}", again.body);
assert_eq!(after.text("name"), Some("Bucks"));