Lines
100 %
Functions
Branches
//! The account and commodity create pages over their descriptor forms, served
//! on their production paths (plan 0016 M3h).
use axum::{
Router,
http::StatusCode,
routing::{get, post},
};
use web::model::User;
use web::pages::account::create as account;
use web::pages::commodity::create as commodity;
use crate::common::create_mock_user;
use crate::common::entity_edit::{form_html, form_pairs, get_from, post_to};
use crate::common::new_page::Reply;
use crate::common::seed::{account_id, commodity_id, named_account};
use crate::common::tenant::provision_tenant;
fn account_router() -> Router<std::sync::Arc<web::AppState>> {
Router::new()
.route("/account/create", get(account::account_create_page))
.route_layer(axum::middleware::from_fn(web::error_render::full_page))
.route(account::SUBMIT_PATH, post(account::account_create_submit))
}
fn commodity_router() -> Router<std::sync::Arc<web::AppState>> {
.route("/commodity/create", get(commodity::commodity_create_page))
.route(
commodity::SUBMIT_PATH,
post(commodity::commodity_create_submit),
)
async fn tenant() -> User {
let user = create_mock_user();
provision_tenant(user.id).await;
user
fn pairs(fields: &[(&str, &str)]) -> Vec<(String, String)> {
fields
.iter()
.map(|(name, value)| (name.to_string(), value.to_string()))
.collect()
fn assert_invalid(reply: &Reply, form: &str) {
assert_eq!(
reply.status,
StatusCode::UNPROCESSABLE_ENTITY,
"{}",
reply.body
);
reply
.headers
.get("hx-retarget")
.map(|v| v.to_str().unwrap()),
Some(format!("#{form}").as_str())
assert!(reply.body.contains("field invalid"), "{}", reply.body);
#[supp_macro::shared_runtime_test]
async fn the_account_create_page_renders_the_descriptor_form() {
let user = tenant().await;
let reply = get_from(account_router(), &user, "/account/create").await;
assert_eq!(reply.status, StatusCode::OK, "{}", reply.body);
let form = form_html(&reply.body, "nf-account-create");
assert!(
form.contains(&format!("hx-post=\"{}\"", account::SUBMIT_PATH)),
"{form}"
assert!(form.contains("name=\"name\""), "{form}");
assert!(form.contains("name=\"parent\""), "{form}");
for attr in [
"hx-sync=\"this:drop\"",
"hx-disabled-elt=\"find button[type=submit]\"",
] {
assert!(form.contains(attr), "{attr}: {form}");
assert!(!reply.body.contains("json-enc\""), "{}", reply.body);
async fn an_account_without_a_name_re_renders_with_a_field_error() {
let posted = form_pairs(&reply.body, "nf-account-create");
let refused = post_to(account_router(), &user, account::SUBMIT_PATH, &posted).await;
assert_invalid(&refused, "nf-account-create");
async fn a_submitted_account_is_created_under_its_parent() {
named_account(user.id, "Assets").await;
let parent = account_id(user.id, "Assets").await;
let reply = post_to(
account_router(),
&user,
account::SUBMIT_PATH,
&pairs(&[("name", "Cash"), ("parent", &parent)]),
.await;
let created = account_id(user.id, "Cash").await;
assert_eq!(reply.body, format!("New account id: {created}"));
async fn the_commodity_create_page_renders_the_descriptor_form() {
let reply = get_from(commodity_router(), &user, "/commodity/create").await;
let form = form_html(&reply.body, "nf-commodity-create");
form.contains(&format!("hx-post=\"{}\"", commodity::SUBMIT_PATH)),
assert!(form.contains("name=\"symbol\""), "{form}");
async fn a_commodity_without_a_symbol_re_renders_with_a_field_error() {
let refused = post_to(
commodity_router(),
&pairs(&[("symbol", ""), ("name", "Dollar")]),
assert_invalid(&refused, "nf-commodity-create");
refused.body.contains("value=\"Dollar\""),
refused.body
async fn a_submitted_commodity_is_created() {
&pairs(&[("symbol", "USD"), ("name", "Dollar")]),
let created = commodity_id(user.id, "USD").await;
assert_eq!(reply.body, format!("New commodity id: {created}"));