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 # legacy customer
34 "catapult_cred-legacy@example.com" => [
35 "catapult_user", "", "", "+12223334444"
36 ],
37 "catapult_jid-+12223334444" => "legacy@example.com"
38 )
39
40 FAKE_DB = FakeDB.new(
41 ["test"] => [{
42 "balance" => BigDecimal(1234),
43 "plan_name" => "test_usd",
44 "expires_at" => Time.now + 100
45 }],
46 ["test_v2"] => [{
47 "balance" => BigDecimal(2345),
48 "plan_name" => "test_usd",
49 "expires_at" => Time.now + 100
50 }]
51 )
52
53 def mkrepo(
54 redis: FAKE_REDIS,
55 db: FAKE_DB,
56 braintree: Minitest::Mock.new
57 )
58 sgx_repo = Minitest::Mock.new(TrivialBackendSgxRepo.new)
59 CustomerRepo.new(
60 redis: redis,
61 db: db,
62 braintree: braintree,
63 sgx_repo: sgx_repo
64 )
65 end
66
67 def setup
68 @repo = mkrepo
69 end
70
71 def test_find_by_jid
72 customer = @repo.find_by_jid("test@example.com").sync
73 assert_kind_of Customer, customer
74 assert_equal 1234, customer.balance
75 assert_equal "merchant_usd", customer.merchant_account
76 end
77 em :test_find_by_jid
78
79 def test_find_by_id
80 customer = @repo.find("test").sync
81 assert_kind_of Customer, customer
82 assert_equal 1234, customer.balance
83 assert_equal "merchant_usd", customer.merchant_account
84 end
85 em :test_find_by_id
86
87 def test_find_by_customer_jid
88 customer = @repo.find_by_jid("customer_test@component").sync
89 assert_kind_of Customer, customer
90 assert_equal 1234, customer.balance
91 assert_equal "merchant_usd", customer.merchant_account
92 end
93 em :test_find_by_customer_jid
94
95 def test_find_by_jid_not_found
96 assert_raises do
97 @repo.find_by_jid("test2@example.com").sync
98 end
99 end
100 em :test_find_by_jid_not_found
101
102 def test_find_legacy_customer
103 customer = @repo.find_by_jid("legacy@example.com").sync
104 assert_kind_of LegacyCustomer, customer
105 assert_equal "+12223334444", customer.tel
106 end
107 em :test_find_legacy_customer
108
109 def test_find_sgx_customer_by_phone
110 customer = @repo.find_by_tel("+13334445555").sync
111 assert_kind_of Customer, customer
112 assert_equal "test", customer.customer_id
113 end
114 em :test_find_sgx_customer_by_phone
115
116 def test_find_v2_customer_by_phone
117 customer = @repo.find_by_tel("+14445556666").sync
118 assert_kind_of Customer, customer
119 assert_equal "test_v2", customer.customer_id
120 end
121 em :test_find_v2_customer_by_phone
122
123 def test_find_legacy_customer_by_phone
124 customer = @repo.find_by_tel("+12223334444").sync
125 assert_kind_of LegacyCustomer, customer
126 assert_equal "legacy@example.com", customer.jid.to_s
127 end
128 em :test_find_legacy_customer_by_phone
129
130 def test_find_missing_phone
131 assert_raises do
132 @repo.find_by_tel("+15556667777").sync
133 end
134 end
135 em :test_find_missing_phone
136
137 def test_find_db_empty
138 customer = @repo.find("empty").sync
139 assert_equal BigDecimal(0), customer.balance
140 end
141 em :test_find_db_empty
142
143 def test_create
144 redis = Minitest::Mock.new
145 braintree = Minitest::Mock.new
146 repo = mkrepo(redis: redis, braintree: braintree)
147 braintree_customer = Minitest::Mock.new
148 braintree.expect(:customer, braintree_customer)
149 braintree_customer.expect(
150 :create,
151 EMPromise.resolve(
152 OpenStruct.new(success?: true, customer: OpenStruct.new(id: "test"))
153 )
154 )
155 redis.expect(
156 :msetnx,
157 EMPromise.resolve(1),
158 [
159 "jmp_customer_id-test@example.com", "test",
160 "jmp_customer_jid-test", "test@example.com"
161 ]
162 )
163 assert_kind_of Customer, repo.create("test@example.com").sync
164 assert_mock braintree
165 assert_mock braintree_customer
166 assert_mock redis
167 end
168 em :test_create
169
170 def test_put_lidb_name
171 post = stub_request(
172 :post,
173 "https://dashboard.bandwidth.com/v1.0/accounts//lidbs"
174 ).with(body: {
175 CustomerOrderId: "test",
176 LidbTnGroups: {
177 LidbTnGroup: {
178 TelephoneNumbers: "5556667777",
179 SubscriberInformation: "Hank",
180 UseType: "RESIDENTIAL",
181 Visibility: "PUBLIC"
182 }
183 }
184 }.to_xml(root: "LidbOrder", indent: 0)).to_return(
185 status: 201,
186 headers: { location: "/boop/123" }
187 )
188
189 stub_request(
190 :get,
191 "https://dashboard.bandwidth.com/v1.0/accounts//lidbs/123"
192 )
193
194 @repo.put_lidb_name(
195 Customer.new(
196 "test",
197 "test@exmple.com",
198 sgx: OpenStruct.new(registered?: OpenStruct.new(phone: "+15556667777"))
199 ),
200 "Hank"
201 )
202
203 assert_requested post
204 end
205 em :test_put_lidb_name
206
207 def test_put_transcription_enabled
208 @repo.sgx_repo.expect(
209 :put_transcription_enabled,
210 EMPromise.resolve(nil),
211 ["test", true]
212 )
213 @repo.put_transcription_enabled(
214 Customer.new("test", "test@exmple.com"),
215 true
216 )
217 assert_mock @repo.sgx_repo
218 end
219 em :test_put_transcription_enabled
220
221 def test_put_fwd
222 @repo.sgx_repo.expect(
223 :put_fwd,
224 EMPromise.resolve(nil),
225 ["test", "+15556667777", :fwd]
226 )
227 @repo.put_fwd(
228 Customer.new(
229 "test",
230 "test@exmple.com",
231 sgx: OpenStruct.new(registered?: OpenStruct.new(phone: "+15556667777"))
232 ),
233 :fwd
234 )
235 assert_mock @repo.sgx_repo
236 end
237 em :test_put_fwd
238end