diff --git a/contract/src/test.rs b/contract/src/test.rs index 767ca57..c47b3b1 100644 --- a/contract/src/test.rs +++ b/contract/src/test.rs @@ -1194,6 +1194,8 @@ fn test_custom_sac_token_end_to_end_flow() { // Verify subscription is still active after pay_per_use let sub_final = client.get_subscription(&user).unwrap(); assert!(sub_final.active, "subscription should remain active after pay_per_use"); +} + // ───────────────────────────────────────────────────────────── // Issue #237: get_token() read function tests // ───────────────────────────────────────────────────────────── @@ -1285,3 +1287,34 @@ fn test_set_grace_period_emits_event() { assert_eq!(topic_symbol, Symbol::new(&env, "grace_period_updated")); assert_eq!(emitted_seconds, 7200u64); } + +// ───────────────────────────────────────────── +// Issue #232: charge() insufficient-allowance error path +// ───────────────────────────────────────────── + +/// If a user's token allowance drops below `sub.amount` between subscribe and +/// charge time, `transfer_from` must fail and propagate the error. +#[test] +#[should_panic] +fn test_charge_insufficient_allowance() { + let (env, contract_id, token_addr, user, merchant) = setup(); + let client = FlowPayClient::new(&env, &contract_id); + + let amount: i128 = 5_0000000; + let interval: u64 = 86400; + + // Subscribe with sufficient allowance + client.subscribe(&user, &merchant, &amount, &interval, &token_addr, &None, &None); + + // Revoke allowance — set it to 0 + let token = TokenClient::new(&env, &token_addr); + token.approve(&user, &contract_id, &0, &200); + + // Advance ledger past the interval + env.ledger().with_mut(|l| { + l.timestamp += interval + 1; + }); + + // charge() should panic because transfer_from fails with insufficient allowance + client.charge(&user); +}