1# frozen_string_literal: true
2
3require "test_helper"
4require "customer_repo"
5
6class CustomerRepo
7 attr_reader :sgx_repo
8end
9
10CustomerPlan::DB = Minitest::Mock.new
11
12class CustomerRepoTest < Minitest::Test
13 FAKE_REDIS = FakeRedis.new(
14 # sgx-jmp customer
15 "jmp_customer_jid-test" => "test@example.com",
16 "jmp_customer_id-test@example.com" => "test",
17 "catapult_jid-+13334445555" => "customer_test@component",
18 "catapult_cred-customer_test@component" => [
19 "test_bw_customer", "", "", "+13334445555"
20 ],
21 # sgx-jmp customer, empty DB
22 "jmp_customer_jid-empty" => "empty@example.com",
23 "jmp_customer_id-empty@example.com" => "empty",
24 "catapult_jid-+16667778888" => "customer_empty@component",
25 "catapult_cred-customer_empty@component" => [
26 "test_bw_customer", "", "", "+16667778888"
27 ],
28 # v2 customer
29 "jmp_customer_jid-test_v2" => "test_v2@example.com",
30 "jmp_customer_id-test_v2@example.com" => "test_v2",
31 "catapult_jid-+14445556666" => "test_v2@example.com",
32 "catapult_cred-test_v2@example.com" => [
33 "test_bw_customer", "", "", "+14445556666"
34 ]
35 )
36
37 FAKE_DB = FakeDB.new(
38 ["test"] => [{
39 "balance" => BigDecimal(1234),
40 "plan_name" => "test_usd",
41 "expires_at" => Time.now + 100
42 }],
43 ["testp"] => [{
44 "balance" => BigDecimal(1234),
45 "plan_name" => "test_usd",
46 "expires_at" => Time.now + 100,
47 "parent_customer_id" => "1234"
48 }],
49 ["test_v2"] => [{
50 "balance" => BigDecimal(2345),
51 "plan_name" => "test_usd",
52 "expires_at" => Time.now + 100
53 }]
54 )
55
56 def mkrepo(
57 redis: FAKE_REDIS,
58 db: FAKE_DB,
59 braintree: Minitest::Mock.new
60 )
61 sgx_repo = Minitest::Mock.new(TrivialBackendSgxRepo.new)
62 CustomerRepo.new(
63 redis: redis,
64 db: db,
65 braintree: braintree,
66 sgx_repo: sgx_repo
67 )
68 end
69
70 def setup
71 @repo = mkrepo
72 end
73
74 def test_find_by_jid
75 customer = @repo.find_by_jid("test@example.com").sync
76 assert_kind_of Customer, customer
77 assert_equal 1234, customer.balance
78 assert_equal "merchant_usd", customer.merchant_account
79 end
80 em :test_find_by_jid
81
82 def test_find_by_id
83 customer = @repo.find("test").sync
84 assert_kind_of Customer, customer
85 assert_equal 1234, customer.balance
86 assert_equal "merchant_usd", customer.merchant_account
87 end
88 em :test_find_by_id
89
90 def test_find_by_customer_jid
91 customer = @repo.find_by_jid("customer_test@component").sync
92 assert_kind_of Customer, customer
93 assert_equal 1234, customer.balance
94 assert_equal "merchant_usd", customer.merchant_account
95 end
96 em :test_find_by_customer_jid
97
98 def test_find_by_jid_not_found
99 assert_raises do
100 @repo.find_by_jid("test2@example.com").sync
101 end
102 end
103 em :test_find_by_jid_not_found
104
105 def test_find_sgx_customer_by_phone
106 customer = @repo.find_by_tel("+13334445555").sync
107 assert_kind_of Customer, customer
108 assert_equal "test", customer.customer_id
109 end
110 em :test_find_sgx_customer_by_phone
111
112 def test_find_v2_customer_by_phone
113 customer = @repo.find_by_tel("+14445556666").sync
114 assert_kind_of Customer, customer
115 assert_equal "test_v2", customer.customer_id
116 end
117 em :test_find_v2_customer_by_phone
118
119 def test_find_missing_phone
120 assert_raises do
121 @repo.find_by_tel("+15556667777").sync
122 end
123 end
124 em :test_find_missing_phone
125
126 def test_find_db_empty
127 customer = @repo.find("empty").sync
128 assert_equal BigDecimal(0), customer.balance
129 end
130 em :test_find_db_empty
131
132 def test_create
133 redis = Minitest::Mock.new
134 braintree = Minitest::Mock.new
135 repo = mkrepo(redis: redis, braintree: braintree)
136 braintree_customer = Minitest::Mock.new
137 braintree.expect(:customer, braintree_customer)
138 braintree_customer.expect(
139 :create,
140 EMPromise.resolve(
141 OpenStruct.new(success?: true, customer: OpenStruct.new(id: "test"))
142 )
143 )
144 redis.expect(
145 :msetnx,
146 EMPromise.resolve(1),
147 [
148 "jmp_customer_id-test@example.com", "test",
149 "jmp_customer_jid-test", "test@example.com"
150 ]
151 )
152 assert_kind_of Customer, repo.create("test@example.com").sync
153 assert_mock braintree
154 assert_mock braintree_customer
155 assert_mock redis
156 end
157 em :test_create
158
159 def test_create_parented
160 redis = Minitest::Mock.new
161 braintree = Minitest::Mock.new
162 repo = mkrepo(redis: redis, braintree: braintree)
163 braintree_customer = Minitest::Mock.new
164 braintree.expect(:customer, braintree_customer)
165 braintree_customer.expect(
166 :create,
167 EMPromise.resolve(
168 OpenStruct.new(success?: true, customer: OpenStruct.new(id: "testp"))
169 )
170 )
171 redis.expect(
172 :msetnx,
173 EMPromise.resolve(1),
174 [
175 "jmp_customer_id-test@parented.example.com", "testp",
176 "jmp_customer_jid-testp", "test@parented.example.com"
177 ]
178 )
179 redis.expect(
180 :get,
181 EMPromise.resolve("test@parented.example.com"),
182 ["jmp_customer_jid-testp"]
183 )
184 redis.expect(
185 :mget,
186 EMPromise.resolve([nil, nil]),
187 [
188 "jmp_customer_auto_top_up_amount-testp",
189 "jmp_customer_monthly_overage_limit-testp"
190 ]
191 )
192 redis.expect(
193 :smembers,
194 EMPromise.resolve([]),
195 ["jmp_customer_feature_flags-testp"]
196 )
197 CustomerPlan::DB.expect(
198 :exec_defer,
199 EMPromise.resolve(nil),
200 [String, ["testp", "test_usd", "1234"]]
201 )
202 result = repo.create("test@parented.example.com").sync
203 assert_kind_of Customer::ChildCustomer, result
204 assert_equal "1234", result.billing_customer_id
205 assert_mock braintree
206 assert_mock braintree_customer
207 assert_mock redis
208 assert_mock CustomerPlan::DB
209 end
210 em :test_create_parented
211
212 def test_put_lidb_name
213 post = stub_request(
214 :post,
215 "https://dashboard.bandwidth.com/v1.0/accounts//lidbs"
216 ).with(body: {
217 LidbTnGroups: {
218 LidbTnGroup: {
219 TelephoneNumbers: {
220 TelephoneNumber: "5556667777"
221 },
222 SubscriberInformation: "Hank",
223 UseType: "RESIDENTIAL",
224 Visibility: "PUBLIC"
225 }
226 }
227 }.to_xml(root: "LidbOrder", indent: 0)).to_return(
228 status: 201,
229 headers: { location: "/boop/123" }
230 )
231
232 stub_request(
233 :get,
234 "https://dashboard.bandwidth.com/v1.0/accounts//lidbs/123"
235 )
236
237 @repo.put_lidb_name(
238 Customer.new(
239 "test",
240 "test@exmple.com",
241 sgx: OpenStruct.new(registered?: OpenStruct.new(phone: "+15556667777"))
242 ),
243 "Hank"
244 )
245
246 assert_requested post
247 end
248 em :test_put_lidb_name
249
250 def test_put_transcription_enabled
251 @repo.sgx_repo.expect(
252 :put_transcription_enabled,
253 EMPromise.resolve(nil),
254 ["test", true]
255 )
256 @repo.put_transcription_enabled(
257 Customer.new("test", "test@exmple.com"),
258 true
259 )
260 assert_mock @repo.sgx_repo
261 end
262 em :test_put_transcription_enabled
263
264 def test_put_fwd
265 @repo.sgx_repo.expect(
266 :put_fwd,
267 EMPromise.resolve(nil),
268 ["test", "+15556667777", :fwd]
269 )
270 @repo.put_fwd(
271 Customer.new(
272 "test",
273 "test@exmple.com",
274 sgx: OpenStruct.new(registered?: OpenStruct.new(phone: "+15556667777"))
275 ),
276 :fwd
277 )
278 assert_mock @repo.sgx_repo
279 end
280 em :test_put_fwd
281end