1# frozen_string_literal: true
2
3require "test_helper"
4require "buy_account_credit_form"
5require "customer"
6
7CustomerFinancials::BRAINTREE = Minitest::Mock.new
8CustomerFinancials::REDIS = Minitest::Mock.new
9
10class BuyAccountCreditFormTest < Minitest::Test
11 def setup
12 @payment_method = OpenStruct.new(card_type: "Test", last_4: "1234")
13 @form = BuyAccountCreditForm.new(
14 BigDecimal("15.1234"),
15 PaymentMethods.new([@payment_method])
16 )
17 end
18
19 def test_for
20 braintree_customer = Minitest::Mock.new
21 CustomerFinancials::BRAINTREE.expect(:customer, braintree_customer)
22 CustomerFinancials::REDIS.expect(:smembers, [], ["block_credit_cards"])
23 braintree_customer.expect(
24 :find,
25 EMPromise.resolve(OpenStruct.new(payment_methods: [])),
26 ["test"]
27 )
28
29 assert_kind_of(
30 BuyAccountCreditForm,
31 BuyAccountCreditForm.for(customer).sync
32 )
33 end
34 em :test_for
35
36 def test_form
37 iq_form = @form.form
38 assert_equal :form, iq_form.type
39 assert_equal "Buy Account Credit", iq_form.title
40 assert_equal(
41 [
42 Blather::Stanza::X::Field.new(
43 type: "fixed",
44 label: "Current balance",
45 value: "$15.12"
46 ),
47 Blather::Stanza::X::Field.new(
48 type: "list-single",
49 var: "payment_method",
50 label: "Credit card to pay with",
51 required: true,
52 options: [{ label: "Test 1234", value: "0" }]
53 ),
54 Blather::Stanza::X::Field.new(
55 var: "amount",
56 label: "Amount of credit to buy",
57 required: true
58 )
59 ],
60 iq_form.fields
61 )
62 end
63
64 def test_parse_amount
65 iq_form = Blather::Stanza::X.new
66 iq_form.fields = [{ var: "amount", value: "123" }]
67 assert_equal "123", @form.parse(iq_form)[:amount]
68 end
69
70 def test_parse_bad_amount
71 iq_form = Blather::Stanza::X.new
72 iq_form.fields = [{ var: "amount", value: "1" }]
73 assert_raises(BuyAccountCreditForm::AmountValidationError) do
74 @form.parse(iq_form)[:amount]
75 end
76 end
77
78 def test_parse_payment_method
79 iq_form = Blather::Stanza::X.new
80 iq_form.fields = [
81 { var: "payment_method", value: "0" },
82 { var: "amount", value: "15" }
83 ]
84 assert_equal @payment_method, @form.parse(iq_form)[:payment_method]
85 end
86end