1# frozen_string_literal: true
2
3require "test_helper"
4require "low_balance"
5
6ExpiringLock::REDIS = Minitest::Mock.new
7CustomerPlan::REDIS = Minitest::Mock.new
8CustomerFinancials::REDIS = Minitest::Mock.new
9LowBalance::AutoTopUp::REDIS = Minitest::Mock.new
10
11class LowBalanceTest < Minitest::Test
12 def test_for_locked
13 ExpiringLock::REDIS.expect(
14 :set,
15 EMPromise.resolve(nil),
16 ["jmp_customer_low_balance-test", Time, "EX", 604800, "NX"]
17 )
18 assert_kind_of LowBalance::Locked, LowBalance.for(customer).sync
19 end
20 em :test_for_locked
21
22 def test_for_no_auto_top_up
23 ExpiringLock::REDIS.expect(
24 :set,
25 EMPromise.resolve("OK"),
26 ["jmp_customer_low_balance-test", Time, "EX", 604800, "NX"]
27 )
28 CustomerFinancials::REDIS.expect(
29 :smembers,
30 EMPromise.resolve([]),
31 ["jmp_customer_btc_addresses-test"]
32 )
33 assert_kind_of(
34 LowBalance,
35 LowBalance.for(customer).sync
36 )
37 assert_mock ExpiringLock::REDIS
38 end
39 em :test_for_no_auto_top_up
40
41 def test_for_auto_top_up
42 ExpiringLock::REDIS.expect(
43 :set,
44 EMPromise.resolve("OK"),
45 ["jmp_customer_low_balance-test", Time, "EX", 604800, "NX"]
46 )
47 CustomerFinancials::REDIS.expect(
48 :smembers,
49 EMPromise.resolve([]),
50 ["block_credit_cards"]
51 )
52 LowBalance::AutoTopUp::REDIS.expect(
53 :exists,
54 0,
55 ["jmp_auto_top_up_block-abcd"]
56 )
57 braintree_customer = Minitest::Mock.new
58 CustomerFinancials::BRAINTREE.expect(:customer, braintree_customer)
59 payment_methods = OpenStruct.new(payment_methods: [
60 OpenStruct.new(default?: true, unique_number_identifier: "abcd")
61 ])
62 braintree_customer.expect(
63 :find,
64 EMPromise.resolve(payment_methods),
65 ["test"]
66 )
67 assert_kind_of(
68 LowBalance::AutoTopUp,
69 LowBalance.for(customer(auto_top_up_amount: 15)).sync
70 )
71 assert_mock ExpiringLock::REDIS
72 assert_mock CustomerFinancials::REDIS
73 assert_mock CustomerFinancials::BRAINTREE
74 assert_mock braintree_customer
75 end
76 em :test_for_auto_top_up
77
78 def test_for_auto_top_up_blocked
79 ExpiringLock::REDIS.expect(
80 :set,
81 EMPromise.resolve("OK"),
82 ["jmp_customer_low_balance-test", Time, "EX", 604800, "NX"]
83 )
84 CustomerFinancials::REDIS.expect(
85 :smembers,
86 EMPromise.resolve([]),
87 ["block_credit_cards"]
88 )
89 CustomerFinancials::REDIS.expect(
90 :smembers,
91 EMPromise.resolve([]),
92 ["jmp_customer_btc_addresses-test"]
93 )
94 LowBalance::AutoTopUp::REDIS.expect(
95 :exists,
96 1,
97 ["jmp_auto_top_up_block-blocked"]
98 )
99 braintree_customer = Minitest::Mock.new
100 CustomerFinancials::BRAINTREE.expect(:customer, braintree_customer)
101 payment_methods = OpenStruct.new(payment_methods: [
102 OpenStruct.new(default?: true, unique_number_identifier: "blocked")
103 ])
104 braintree_customer.expect(
105 :find,
106 EMPromise.resolve(payment_methods),
107 ["test"]
108 )
109 assert_kind_of(
110 LowBalance,
111 LowBalance.for(customer(auto_top_up_amount: 15)).sync
112 )
113 assert_mock ExpiringLock::REDIS
114 assert_mock CustomerFinancials::REDIS
115 assert_mock CustomerFinancials::BRAINTREE
116 assert_mock braintree_customer
117 end
118 em :test_for_auto_top_up_blocked
119
120 class AutoTopUpTest < Minitest::Test
121 LowBalance::AutoTopUp::Transaction = Minitest::Mock.new
122
123 def setup
124 @customer = Minitest::Mock.new(customer(auto_top_up_amount: 100))
125 @auto_top_up = LowBalance::AutoTopUp.new(@customer)
126 end
127
128 def test_notify!
129 tx = PromiseMock.new
130 tx.expect(:insert, EMPromise.resolve(nil))
131 LowBalance::AutoTopUp::Transaction.expect(
132 :sale,
133 tx,
134 [@customer, { amount: 100 }]
135 )
136 @auto_top_up.notify!
137 assert_mock tx
138 end
139 em :test_notify!
140
141 def test_very_low_balance_notify!
142 customer = Minitest::Mock.new(customer(
143 balance: -100,
144 auto_top_up_amount: 15
145 ))
146 auto_top_up = LowBalance::AutoTopUp.new(customer)
147
148 tx = PromiseMock.new
149 tx.expect(:insert, EMPromise.resolve(nil))
150 LowBalance::AutoTopUp::Transaction.expect(
151 :sale,
152 tx,
153 [customer, { amount: 115 }]
154 )
155 auto_top_up.notify!
156 assert_mock tx
157 end
158 em :test_very_low_balance_notify!
159
160 def test_border_low_balance_notify!
161 customer = Minitest::Mock.new(customer(
162 balance: -11,
163 auto_top_up_amount: 15
164 ))
165 auto_top_up = LowBalance::AutoTopUp.new(customer)
166
167 tx = PromiseMock.new
168 tx.expect(:insert, EMPromise.resolve(nil))
169 LowBalance::AutoTopUp::Transaction.expect(
170 :sale,
171 tx,
172 [customer, { amount: 26 }]
173 )
174 auto_top_up.notify!
175 assert_mock tx
176 end
177 em :test_border_low_balance_notify!
178
179 def test_decline_notify!
180 @customer.expect(
181 :stanza_to,
182 nil,
183 [Matching.new { |m|
184 assert_equal(
185 "Automatic top-up transaction for $100 failed: test",
186 m.body
187 )
188 }]
189 )
190 LowBalance::AutoTopUp::Transaction.expect(
191 :sale,
192 EMPromise.reject(RuntimeError.new("test")),
193 [@customer, { amount: 100 }]
194 )
195 @auto_top_up.notify!.sync
196 assert_mock @customer
197 end
198 em :test_decline_notify!
199 end
200end