1# frozen_string_literal: true
2
3require "test_helper"
4require "customer_info"
5require "trust_level_repo"
6require "trust_level"
7
8CustomerPlan::DB = Minitest::Mock.new
9CustomerPlan::REDIS = Minitest::Mock.new
10CustomerUsage::DB = Minitest::Mock.new
11PlanInfo::DB = FakeDB.new(
12 ["test"] => [
13 {
14 "start_date" => Time.parse("2020-01-01"),
15 "activation_date" => Time.parse("2021-01-01")
16 }
17 ]
18)
19Subaccount::DB = Minitest::Mock.new
20TrivialBackendSgxRepo::REDIS = Minitest::Mock.new
21
22class CustomerInfoTest < Minitest::Test
23 def test_info_does_not_crash
24 sgx = Minitest::Mock.new
25 sgx.expect(:registered?, false)
26
27 CustomerPlan::DB.expect(
28 :query_one,
29 EMPromise.resolve({ start_date: Time.now }),
30 [String, "test"]
31 )
32
33 CustomerUsage::DB.expect(
34 :query_one,
35 EMPromise.resolve({ charges: 0.to_d }),
36 [String, "test"]
37 )
38
39 cust = customer(sgx: sgx, plan_name: "test_usd")
40
41 assert CustomerInfo.for(cust).sync.form
42 assert_mock sgx
43 assert_mock CustomerUsage::DB
44 end
45 em :test_info_does_not_crash
46
47 def test_info_has_remaining_included_calling_credit
48 sgx = Minitest::Mock.new
49 sgx.expect(:registered?, false)
50
51 CustomerPlan::DB.expect(
52 :query_one,
53 EMPromise.resolve({ start_date: Time.now }),
54 [String, "test"]
55 )
56
57 CustomerUsage::DB.expect(
58 :query_one,
59 EMPromise.resolve({ charges: 0.044.to_d }),
60 [String, "test"]
61 )
62
63 cust = customer(sgx: sgx, plan_name: "test_usd")
64
65 assert_equal(
66 "$1.0000",
67 CustomerInfo.for(cust).sync.form
68 .field("remaining_included_calling_credit").value
69 )
70 assert_mock sgx
71 assert_mock CustomerUsage::DB
72 end
73 em :test_info_has_remaining_included_calling_credit
74
75 def test_info_out_of_remaining_included_calling_credit
76 sgx = Minitest::Mock.new
77 sgx.expect(:registered?, false)
78
79 CustomerPlan::DB.expect(
80 :query_one,
81 EMPromise.resolve({ start_date: Time.now }),
82 [String, "test"]
83 )
84
85 CustomerUsage::DB.expect(
86 :query_one,
87 EMPromise.resolve({ charges: 10.to_d }),
88 [String, "test"]
89 )
90
91 cust = customer(sgx: sgx, plan_name: "test_usd")
92
93 assert_equal(
94 "$0.0000",
95 CustomerInfo.for(cust).sync.form
96 .field("remaining_included_calling_credit").value
97 )
98 assert_mock sgx
99 assert_mock CustomerUsage::DB
100 end
101 em :test_info_out_of_remaining_included_calling_credit
102
103 def test_admin_info_does_not_crash
104 sgx = Minitest::Mock.new
105 sgx.expect(:registered?, false)
106 fwd = CustomerFwd.for(uri: "tel:+12223334444", timeout: 15)
107 sgx.expect(:fwd, fwd)
108 sgx.expect(:registered?, false)
109
110 CustomerPlan::DB.expect(
111 :query_one,
112 EMPromise.resolve({ start_date: Time.now }),
113 [String, "test"]
114 )
115
116 CustomerUsage::DB.expect(
117 :query_one,
118 EMPromise.resolve({ charges: 0.to_d }),
119 [String, "test"]
120 )
121
122 Subaccount::DB.expect(
123 :query_defer,
124 EMPromise.resolve({}),
125 [String, ["test"]]
126 )
127
128 TrivialBackendSgxRepo::REDIS.expect(
129 :get,
130 EMPromise.resolve(nil),
131 ["jmp_customer_backend_sgx-test"]
132 )
133 cust = customer(sgx: sgx, plan_name: "test_usd")
134
135 trust_repo = Minitest::Mock.new
136 trust_repo.expect(:find, TrustLevel::Basement, [cust])
137
138 assert AdminInfo.for(cust, trust_level_repo: trust_repo).sync.form
139 assert_mock sgx
140 assert_mock trust_repo
141 assert_mock CustomerUsage::DB
142 assert_mock Subaccount::DB
143 assert_mock TrivialBackendSgxRepo::REDIS
144 end
145 em :test_admin_info_does_not_crash
146
147 def test_admin_info_with_tel_does_not_crash
148 registered = Struct.new(:phone).new("+12223334444")
149 fwd = CustomerFwd.for(uri: "tel:+12223334444", timeout: 15)
150 sgx = Struct.new(:registered?, :fwd).new(registered, fwd)
151
152 CustomerPlan::DB.expect(
153 :query_one,
154 EMPromise.resolve({ start_date: Time.now }),
155 [String, "test"]
156 )
157
158 CustomerUsage::DB.expect(
159 :query_one,
160 EMPromise.resolve({ charges: 0.to_d }),
161 [String, "test"]
162 )
163
164 Subaccount::DB.expect(
165 :query_defer,
166 EMPromise.resolve([]),
167 [String, ["test"]]
168 )
169
170 TrivialBackendSgxRepo::REDIS.expect(
171 :get,
172 EMPromise.resolve(nil),
173 ["jmp_customer_backend_sgx-test"]
174 )
175
176 cust = customer(sgx: sgx, plan_name: "test_usd")
177
178 call_attempt_repo = Minitest::Mock.new
179 call_attempt_repo.expect(
180 :find_outbound,
181 CallAttempt::Unsupported.new(direction: :outbound),
182 [cust, "+1"], call_id: "dry_run"
183 )
184
185 trust_repo = Minitest::Mock.new
186 trust_repo.expect(:find, TrustLevel::Basement, [cust])
187
188 assert AdminInfo.for(
189 cust,
190 trust_level_repo: trust_repo,
191 call_attempt_repo: call_attempt_repo
192 ).sync.form
193 assert_mock call_attempt_repo
194 assert_mock trust_repo
195 assert_mock CustomerUsage::DB
196 assert_mock Subaccount::DB
197 assert_mock TrivialBackendSgxRepo::REDIS
198 end
199 em :test_admin_info_with_tel_does_not_crash
200
201 def test_inactive_info_does_not_crash
202 sgx = Minitest::Mock.new
203 sgx.expect(:registered?, false)
204
205 plan = CustomerPlan.new("test", plan: nil, expires_at: nil)
206 cust = Customer.new(
207 "test",
208 Blather::JID.new("test@example.net"),
209 plan: plan,
210 sgx: sgx
211 )
212 assert CustomerInfo.for(cust).sync.form
213 assert_mock sgx
214 end
215 em :test_inactive_info_does_not_crash
216
217 def test_inactive_admin_info_does_not_crash
218 sgx = Minitest::Mock.new
219 sgx.expect(:registered?, false)
220 sgx.expect(:registered?, false)
221 sgx.expect(:fwd, CustomerFwd::None.new(uri: nil, timeout: nil))
222
223 Subaccount::DB.expect(
224 :query_defer,
225 EMPromise.resolve([]),
226 [String, ["test"]]
227 )
228
229 TrivialBackendSgxRepo::REDIS.expect(
230 :get,
231 EMPromise.resolve(nil),
232 ["jmp_customer_backend_sgx-test"]
233 )
234
235 plan = CustomerPlan.new("test", plan: nil, expires_at: nil)
236 cust = Customer.new(
237 "test",
238 Blather::JID.new("test@example.net"),
239 plan: plan,
240 sgx: sgx
241 )
242
243 trust_repo = Minitest::Mock.new
244 trust_repo.expect(:find, TrustLevel::Basement, [cust])
245
246 assert AdminInfo.for(cust, trust_level_repo: trust_repo).sync.form
247 assert_mock sgx
248 assert_mock trust_repo
249 assert_mock Subaccount::DB
250 assert_mock TrivialBackendSgxRepo::REDIS
251 end
252 em :test_inactive_admin_info_does_not_crash
253
254 def test_admin_info_subaccount_does_not_crash
255 sgx = Minitest::Mock.new
256 sgx.expect(:registered?, false)
257 fwd = CustomerFwd.for(uri: "tel:+12223334444", timeout: 15)
258 sgx.expect(:fwd, fwd)
259 sgx.expect(:registered?, false)
260
261 CustomerPlan::DB.expect(
262 :query_one,
263 EMPromise.resolve({ start_date: Time.now }),
264 [String, "test"]
265 )
266
267 CustomerUsage::DB.expect(
268 :query_one,
269 EMPromise.resolve({ charges: 0.to_d }),
270 [String, "test"]
271 )
272
273 Subaccount::DB.expect(
274 :query_defer,
275 EMPromise.resolve([]),
276 [String, ["parent"]]
277 )
278
279 TrivialBackendSgxRepo::REDIS.expect(
280 :get,
281 EMPromise.resolve(nil),
282 ["jmp_customer_backend_sgx-test"]
283 )
284
285 cust = customer(
286 sgx: sgx,
287 plan_name: "test_usd",
288 parent_customer_id: "parent"
289 )
290
291 trust_repo = Minitest::Mock.new
292 trust_repo.expect(:find, TrustLevel::Basement, [cust])
293
294 assert AdminInfo.for(cust, trust_level_repo: trust_repo).sync.form
295 assert_mock sgx
296 assert_mock trust_repo
297 assert_mock CustomerUsage::DB
298 assert_mock Subaccount::DB
299 assert_mock TrivialBackendSgxRepo::REDIS
300 end
301 em :test_admin_info_subaccount_does_not_crash
302
303 def test_admin_info_includes_route
304 sgx = Minitest::Mock.new
305 sgx.expect(:registered?, false)
306 fwd = CustomerFwd.for(uri: "tel:+12223334444", timeout: 15)
307 sgx.expect(:fwd, fwd)
308 sgx.expect(:registered?, false)
309
310 CustomerPlan::DB.expect(
311 :query_one,
312 EMPromise.resolve({ start_date: Time.now }),
313 [String, "test"]
314 )
315
316 CustomerUsage::DB.expect(
317 :query_one,
318 EMPromise.resolve({ charges: 0.to_d }),
319 [String, "test"]
320 )
321
322 Subaccount::DB.expect(
323 :query_defer,
324 EMPromise.resolve({}),
325 [String, ["test"]]
326 )
327
328 TrivialBackendSgxRepo::REDIS.expect(
329 :get,
330 EMPromise.resolve("route_value"),
331 ["jmp_customer_backend_sgx-test"]
332 )
333
334 cust = customer(sgx: sgx, plan_name: "test_usd")
335
336 trust_repo = Minitest::Mock.new
337 trust_repo.expect(:find, TrustLevel::Basement, [cust])
338
339 admin_info = AdminInfo.for(cust, trust_level_repo: trust_repo).sync.form
340
341 assert admin_info
342 assert admin_info.field("route").value == "route_value"
343 assert_mock sgx
344 assert_mock trust_repo
345 assert_mock CustomerUsage::DB
346 assert_mock Subaccount::DB
347 assert_mock TrivialBackendSgxRepo::REDIS
348 end
349 em :test_admin_info_includes_route
350end