93 lines
4.2 KiB
Rust
93 lines
4.2 KiB
Rust
//! Day-2 mandatory test (the inbox's half in the core): the iPhone Shortcut post is hostile input. A malformed,
|
|
//! oversized or ambiguous body is an error with a reason, never a partial payment.
|
|
|
|
use fiscus_capture::shortcut::{parse, CaptureError, MAX_BODY};
|
|
use fiscus_ledger::{Date, Haler, Timestamp};
|
|
use proptest::prelude::*;
|
|
|
|
/// 2026-11-03T13:05:00Z in milliseconds.
|
|
const AT: i64 = 1_793_711_100_000;
|
|
|
|
fn post(amount: &str) -> String {
|
|
format!(r#"{{"amount":{amount},"merchant":"ALBERT HYPERMARKET","card":"Apple Card","time":"2026-11-03T14:05:00+01:00"}}"#)
|
|
}
|
|
|
|
#[test]
|
|
fn a_good_post_becomes_one_outflow() {
|
|
let c = parse(post("129.5").as_bytes()).unwrap();
|
|
assert_eq!(c.amount, Haler(-12_950));
|
|
assert_eq!(c.merchant, "ALBERT HYPERMARKET");
|
|
assert_eq!(c.card.as_deref(), Some("Apple Card"));
|
|
assert_eq!(c.date, "2026-11-03".parse::<Date>().unwrap());
|
|
// 14:05 at +01:00 is 13:05 UTC.
|
|
assert_eq!(c.at, Timestamp(AT));
|
|
}
|
|
|
|
#[test]
|
|
fn the_amount_forms_shortcuts_writes_are_read_from_text_never_as_a_float() {
|
|
for (text, want) in [
|
|
("129", 12_900),
|
|
("129.00", 12_900),
|
|
("0.1", 10),
|
|
(r#""129,50""#, 12_950),
|
|
(r#""129,50 Kč""#, 12_950),
|
|
(r#""Kč 129,50""#, 12_950),
|
|
(r#""CZK 1 234,50""#, 123_450),
|
|
("\"1\u{a0}234,50\u{a0}Kč\"", 123_450),
|
|
("\"1\u{202f}234,50 CZK\"", 123_450),
|
|
] {
|
|
assert_eq!(parse(post(text).as_bytes()).map(|c| c.amount), Ok(Haler(-want)), "{text}");
|
|
}
|
|
for text in ["0", "-129", "1e3", "129.555", "\"\"", "\"abc\"", "\"$129\"", "\"129 EUR\"", "null", "true", "[129]", "99999999999"] {
|
|
assert_eq!(parse(post(text).as_bytes()).map(|c| c.amount), Err(CaptureError::Amount), "{text}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn every_field_is_checked() {
|
|
let ok = r#"{"amount":1,"merchant":"M","card":"","time":"2026-11-03T14:05:00Z"}"#;
|
|
assert!(parse(ok.as_bytes()).is_ok());
|
|
let cases: &[(&str, CaptureError)] = &[
|
|
(r#"{"merchant":"M","card":"","time":"2026-11-03T14:05:00Z"}"#, CaptureError::Malformed),
|
|
(r#"{"amount":1,"merchant":"M","card":"","time":"2026-11-03T14:05:00Z","extra":1}"#, CaptureError::Malformed),
|
|
(r#"{"amount":1,"amount":2,"merchant":"M","card":"","time":"2026-11-03T14:05:00Z"}"#, CaptureError::Malformed),
|
|
(r#"{"amount":1,"merchant":"","card":"","time":"2026-11-03T14:05:00Z"}"#, CaptureError::Merchant),
|
|
(r#"{"amount":1,"merchant":"a\u0000b","card":"","time":"2026-11-03T14:05:00Z"}"#, CaptureError::Merchant),
|
|
(r#"{"amount":1,"merchant":"M","card":"a\nb","time":"2026-11-03T14:05:00Z"}"#, CaptureError::Card),
|
|
(r#"{"amount":1,"merchant":"M","card":"","time":"2026-11-03 14:05"}"#, CaptureError::Time),
|
|
(r#"{"amount":1,"merchant":"M","card":"","time":"2026-02-30T14:05:00Z"}"#, CaptureError::Time),
|
|
(r#"{"amount":1,"merchant":"M","card":"","time":"2026-11-03T24:05:00Z"}"#, CaptureError::Time),
|
|
(r#"{"amount":1,"merchant":"M","card":"","time":"2026-11-03T14:05:00"}"#, CaptureError::Time),
|
|
(r#"{"amount":1,"merchant":"M","card":"","time":"2026-11-03T14:05:00Z","currency":"EUR"}"#, CaptureError::Currency),
|
|
(r#"[1,2]"#, CaptureError::Malformed),
|
|
("", CaptureError::Malformed),
|
|
];
|
|
for (body, want) in cases {
|
|
assert_eq!(parse(body.as_bytes()).err().as_ref(), Some(want), "{body}");
|
|
}
|
|
let long = format!(r#"{{"amount":1,"merchant":"{}","card":"","time":"2026-11-03T14:05:00Z"}}"#, "m".repeat(201));
|
|
assert_eq!(parse(long.as_bytes()).err(), Some(CaptureError::Merchant));
|
|
assert!(parse(r#"{"amount":1,"merchant":"M","card":"","time":"2026-11-03T14:05:00Z","currency":"CZK"}"#.as_bytes()).is_ok());
|
|
}
|
|
|
|
#[test]
|
|
fn an_oversized_body_is_refused_before_parsing() {
|
|
let mut big = post("1").into_bytes();
|
|
big.resize(MAX_BODY + 1, b' ');
|
|
assert_eq!(parse(&big).err(), Some(CaptureError::TooLarge));
|
|
}
|
|
|
|
proptest! {
|
|
#[test]
|
|
fn random_bytes_never_panic_and_never_half_parse(bytes in proptest::collection::vec(any::<u8>(), 0..600)) {
|
|
let _ = parse(&bytes);
|
|
}
|
|
|
|
#[test]
|
|
fn every_truncation_of_a_good_post_is_an_error(cut in 0usize..90) {
|
|
let full = post("\"129,50 Kč\"");
|
|
let cut = cut.min(full.len() - 1);
|
|
prop_assert!(parse(&full.as_bytes()[..cut]).is_err());
|
|
}
|
|
}
|