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_balance
37 assert_equal(
38 { type: "fixed", value: "Current balance: $15.12" },
39 @form.balance
40 )
41 end
42
43 def test_add_to_form
44 iq_form = Blather::Stanza::X.new
45 @form.add_to_form(iq_form)
46 assert_equal :form, iq_form.type
47 assert_equal "Buy Account Credit", iq_form.title
48 assert_equal(
49 [
50 Blather::Stanza::X::Field.new(
51 type: "fixed",
52 value: "Current balance: $15.12"
53 ),
54 Blather::Stanza::X::Field.new(
55 type: "list-single",
56 var: "payment_method",
57 label: "Credit card to pay with",
58 required: true,
59 options: [{ label: "Test 1234", value: "0" }]
60 ),
61 BuyAccountCreditForm::AMOUNT_FIELD
62 ],
63 iq_form.fields
64 )
65 end
66
67 def test_parse_amount
68 iq_form = Blather::Stanza::X.new
69 iq_form.fields = [{ var: "amount", value: "123" }]
70 assert_equal "123", @form.parse(iq_form)[:amount]
71 end
72
73 def test_parse_bad_amount
74 iq_form = Blather::Stanza::X.new
75 iq_form.fields = [{ var: "amount", value: "1" }]
76 assert_raises(BuyAccountCreditForm::AmountValidationError) do
77 @form.parse(iq_form)[:amount]
78 end
79 end
80
81 def test_parse_payment_method
82 iq_form = Blather::Stanza::X.new
83 iq_form.fields = [
84 { var: "payment_method", value: "0" },
85 { var: "amount", value: "15" }
86 ]
87 assert_equal @payment_method, @form.parse(iq_form)[:payment_method]
88 end
89end