fix: redo amount validation in the form

Amolith created

Change summary

lib/buy_account_credit_form.rb       |  7 +++++++
test/test_buy_account_credit_form.rb | 22 ++++++++++++++++++++++
2 files changed, 29 insertions(+)

Detailed changes

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(

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