Lines
100 %
Functions
97.06 %
Branches
//! The create page over the shared forms, served on its production paths.
use axum::{
Router,
body::Body,
http::{HeaderMap, Request, StatusCode},
routing::{get, post},
};
use sqlx::types::Uuid;
use std::sync::Arc;
use tower::ServiceExt;
use web::AppState;
use web::model::User;
use web::pages::transaction::new::{SUBMIT_PATH, transaction_new_page, transaction_new_submit};
use crate::common::seed::{account_id, commodity_id, named_account, named_commodity};
use crate::common::tenant::{provision_tenant, tenant_connection};
use crate::common::{authenticated, create_mock_user, create_test_app_state};
pub struct Reply {
pub status: StatusCode,
pub headers: HeaderMap,
pub body: String,
}
async fn send(user: &User, request: Request<Body>) -> Reply {
let router = Router::new()
.route("/transaction/new", get(transaction_new_page))
.route_layer(axum::middleware::from_fn(web::error_render::full_page))
.route(SUBMIT_PATH, post(transaction_new_submit));
send_to(router, user, request).await
/// The reply of `router`, behind the test authentication, to `request`.
pub async fn send_to(router: Router<Arc<AppState>>, user: &User, request: Request<Body>) -> Reply {
let response = authenticated(router, create_test_app_state().await, user.clone())
.oneshot(request)
.await
.expect("response");
let status = response.status();
let headers = response.headers().clone();
let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
.expect("body");
Reply {
status,
headers,
body: String::from_utf8_lossy(&bytes).into_owned(),
pub async fn get_page(user: &User, query: &str) -> Reply {
let request = Request::builder()
.uri(format!("/transaction/new?{query}"))
.body(Body::empty())
.expect("request");
send(user, request).await
pub async fn post_form(user: &User, pairs: &[(&str, &str)]) -> Reply {
let body = serde_urlencoded::to_string(pairs).expect("urlencoded");
.method("POST")
.uri(SUBMIT_PATH)
.header("content-type", "application/x-www-form-urlencoded")
.header("hx-request", "true")
.body(Body::from(body))
/// The id a successful create names in its message.
pub fn created_id(reply: &Reply) -> Uuid {
let id = reply.body.rsplit(' ').next().unwrap_or_default();
Uuid::parse_str(id).unwrap_or_else(|_| panic!("{} {}", reply.status, reply.body))
/// A tenant with `USD`, `EUR`, `Assets:Checking` and `Expenses:Food`.
pub async fn seeded_user() -> User {
let user = create_mock_user();
provision_tenant(user.id).await;
named_commodity(user.id, "USD").await;
named_commodity(user.id, "EUR").await;
named_account(user.id, "Assets:Checking").await;
named_account(user.id, "Expenses:Food").await;
user
/// The ids of the entities `seeded_user` creates.
pub struct Ids {
pub checking: String,
pub food: String,
pub usd: String,
pub eur: String,
pub async fn seeded_ids(user: &User) -> Ids {
Ids {
checking: account_id(user.id, "Assets:Checking").await,
food: account_id(user.id, "Expenses:Food").await,
usd: commodity_id(user.id, "USD").await,
eur: commodity_id(user.id, "EUR").await,
pub async fn snapshot(user: &User, transaction: Uuid) -> Vec<String> {
let mut conn = tenant_connection(user.id).await;
server::lossless::snapshot(&mut *conn, transaction)
.expect("snapshot")
/// Two creates differ only in when they were entered.
pub fn without_entered(facts: Vec<String>) -> Vec<String> {
facts
.into_iter()
.map(|fact| match fact.split_once(" entered ") {
Some((date, _)) if fact.starts_with("date ") => date.to_string(),
_ => fact,
})
.collect()
/// The rendered create form, from its id to its end.
pub fn create_form(html: &str) -> &str {
let start = html
.find("id=\"nf-transaction-create\"")
.expect("the create form");
let form = &html[start..];
&form[..form.find("</form>").expect("form end")]
/// The `name` attributes of the rendered create form, without those of its
/// `<template>` rows.
pub fn form_names(html: &str) -> Vec<String> {
create_form(html)
.split(" name=\"")
.skip(1)
.filter_map(|tail| tail.split('"').next())
.filter(|name| !name.contains("__INDEX__"))
.map(str::to_string)
/// The `value` attribute of the input named `name`.
pub fn input_value<'a>(html: &'a str, name: &str) -> Option<&'a str> {
let (_, tail) = html.split_once(&format!("name=\"{name}\""))?;
let tag = tail.split('>').next()?;
let (_, value) = tag.split_once("value=\"")?;
value.split('"').next()
/// The text of the textarea named `name`, without the newline the renderer
/// puts first.
pub fn textarea<'a>(html: &'a str, name: &str) -> Option<&'a str> {
let (_, tail) = html.split_once(&format!("<textarea name=\"{name}\""))?;
let (_, text) = tail.split_once(">\n")?;
text.split("</textarea>").next()
/// The id of the account or commodity option labelled `label`.
pub async fn option_id(user: &User, source: forms::Source, label: &str) -> Uuid {
rpc::forms::options(user.id, source)
.expect("options")
.find(|option| option.label == label)
.map(|option| option.id)
.expect("an option with the label")
/// Whether the select control labelled `name` is marked as written by a fill.
pub fn marked_filled(html: &str, name: &str) -> bool {
html.split_once(&format!("name=\"label:{name}\""))
.and_then(|(_, tail)| tail.split('>').next())
.is_some_and(|tag| tag.contains("data-filled"))
/// Posts a create of 10 `commodity` from `from` to `to`.
pub async fn spend(user: &User, from: Uuid, to: Uuid, commodity: Uuid) {
let [from, to, commodity] = [from, to, commodity].map(|id| id.to_string());
let reply = post_form(
user,
&[
("date", "2026-01-02T03:04:00Z"),
("note", "n"),
("tags", ""),
("splits", ""),
("splits.0.key", "n1"),
("splits.0.from", from.as_str()),
("splits.0.to", to.as_str()),
("splits.0.from-commodity", commodity.as_str()),
("splits.0.value", "10"),
("splits.0.hidden", ""),
("split-tags", ""),
],
)
.await;
assert_eq!(reply.status, StatusCode::OK, "{}", reply.body);