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