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