From e195e3de691b1dcd48d053c13cefef9e2cad4aad Mon Sep 17 00:00:00 2001 From: Amolith Date: Thu, 22 May 2025 12:27:29 -0600 Subject: [PATCH] fix: redo amount validation in the form --- lib/buy_account_credit_form.rb | 7 +++++++ test/test_buy_account_credit_form.rb | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/buy_account_credit_form.rb b/lib/buy_account_credit_form.rb index 367a95933a8fd36683842029c078489e6aa2b1f8..8084bb0579490230b3767a543f146524c586705f 100644 --- a/lib/buy_account_credit_form.rb +++ b/lib/buy_account_credit_form.rb @@ -50,6 +50,13 @@ class BuyAccountCreditForm def parse(form) amount = form.field("amount")&.value&.to_s + amount_value = amount.to_f + + if amount_value < 15 + raise CreditCardSale::TooLowError + elsif amount_value > @max_top_up_amount + raise CreditCardSale::TooHighError + end { payment_method: @payment_methods.fetch( diff --git a/test/test_buy_account_credit_form.rb b/test/test_buy_account_credit_form.rb index 0c05519fdc4166f34b5e4126a3d56924f66f3cf6..ef0ede190995c479b390098a813299e7dd28af65 100644 --- a/test/test_buy_account_credit_form.rb +++ b/test/test_buy_account_credit_form.rb @@ -94,4 +94,26 @@ class BuyAccountCreditFormTest < Minitest::Test ] assert_equal @payment_method, @form.parse(iq_form)[:payment_method] end + + def test_parse_amount_too_low + iq_form = Blather::Stanza::X.new + iq_form.fields = [ + { var: "payment_method", value: "0" }, + { var: "amount", value: "10" } + ] + assert_raises(CreditCardSale::TooLowError) do + @form.parse(iq_form) + end + end + + def test_parse_amount_too_high + iq_form = Blather::Stanza::X.new + iq_form.fields = [ + { var: "payment_method", value: "0" }, + { var: "amount", value: "200" } + ] + assert_raises(CreditCardSale::TooHighError) do + @form.parse(iq_form) + end + end end