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(:create, EMPromise.resolve(
150 OpenStruct.new(success?: true, customer: OpenStruct.new(id: "test"))
151 ))
152 redis.expect(
153 :msetnx,
154 EMPromise.resolve(1),
155 [
156 "jmp_customer_id-test@example.com", "test",
157 "jmp_customer_jid-test", "test@example.com"
158 ]
159 )
160 assert_kind_of Customer, repo.create("test@example.com").sync
161 assert_mock braintree
162 assert_mock braintree_customer
163 assert_mock redis
164 end
165 em :test_create
166
167 def test_put_lidb_name
168 post = stub_request(
169 :post,
170 "https://dashboard.bandwidth.com/v1.0/accounts//lidbs"
171 ).with(body: {
172 CustomerOrderId: "test",
173 LidbTnGroups: {
174 LidbTnGroup: {
175 TelephoneNumbers: "5556667777",
176 SubscriberInformation: "Hank",
177 UseType: "RESIDENTIAL",
178 Visibility: "PUBLIC"
179 }
180 }
181 }.to_xml(root: "LidbOrder", indent: 0)).to_return(
182 status: 201,
183 headers: { location: "/boop/123" }
184 )
185
186 stub_request(
187 :get,
188 "https://dashboard.bandwidth.com/v1.0/accounts//lidbs/123"
189 )
190
191 @repo.put_lidb_name(
192 Customer.new(
193 "test",
194 "test@exmple.com",
195 sgx: OpenStruct.new(registered?: OpenStruct.new(phone: "+15556667777"))
196 ),
197 "Hank"
198 )
199
200 assert_requested post
201 end
202 em :test_put_lidb_name
203
204 def test_put_transcription_enabled
205 @repo.sgx_repo.expect(
206 :put_transcription_enabled,
207 EMPromise.resolve(nil),
208 ["test", true]
209 )
210 @repo.put_transcription_enabled(
211 Customer.new("test", "test@exmple.com"),
212 true
213 )
214 assert_mock @repo.sgx_repo
215 end
216 em :test_put_transcription_enabled
217
218 def test_put_fwd
219 @repo.sgx_repo.expect(
220 :put_fwd,
221 EMPromise.resolve(nil),
222 ["test", "+15556667777", :fwd]
223 )
224 @repo.put_fwd(
225 Customer.new(
226 "test",
227 "test@exmple.com",
228 sgx: OpenStruct.new(registered?: OpenStruct.new(phone: "+15556667777"))
229 ),
230 :fwd
231 )
232 assert_mock @repo.sgx_repo
233 end
234 em :test_put_fwd
235end