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
134 cust = customer(sgx: sgx, plan_name: "test_usd")
135
136 trust_repo = Minitest::Mock.new
137 trust_repo.expect(:find, TrustLevel::Basement, [cust])
138
139 assert AdminInfo.for(cust, trust_level_repo: trust_repo).sync.form
140 assert_mock sgx
141 assert_mock trust_repo
142 assert_mock CustomerUsage::DB
143 assert_mock Subaccount::DB
144 assert_mock TrivialBackendSgxRepo::REDIS
145 end
146 em :test_admin_info_does_not_crash
147
148 def test_admin_info_with_tel_does_not_crash
149 registered = Struct.new(:phone).new("+12223334444")
150 fwd = CustomerFwd.for(uri: "tel:+12223334444", timeout: 15)
151 sgx = Struct.new(:registered?, :fwd).new(registered, fwd)
152
153 CustomerPlan::DB.expect(
154 :query_one,
155 EMPromise.resolve({ start_date: Time.now }),
156 [String, "test"]
157 )
158
159 CustomerUsage::DB.expect(
160 :query_one,
161 EMPromise.resolve({ charges: 0.to_d }),
162 [String, "test"]
163 )
164
165 Subaccount::DB.expect(
166 :query_defer,
167 EMPromise.resolve([]),
168 [String, ["test"]]
169 )
170
171 TrivialBackendSgxRepo::REDIS.expect(
172 :get,
173 EMPromise.resolve(nil),
174 ["jmp_customer_backend_sgx-test"]
175 )
176
177 cust = customer(sgx: sgx, plan_name: "test_usd")
178
179 call_attempt_repo = Minitest::Mock.new
180 call_attempt_repo.expect(
181 :find_outbound,
182 CallAttempt::Unsupported.new(direction: :outbound),
183 [cust, "+1"], call_id: "dry_run"
184 )
185
186 trust_repo = Minitest::Mock.new
187 trust_repo.expect(:find, TrustLevel::Basement, [cust])
188
189 assert AdminInfo.for(
190 cust,
191 trust_level_repo: trust_repo,
192 call_attempt_repo: call_attempt_repo
193 ).sync.form
194 assert_mock call_attempt_repo
195 assert_mock trust_repo
196 assert_mock CustomerUsage::DB
197 assert_mock Subaccount::DB
198 assert_mock TrivialBackendSgxRepo::REDIS
199 end
200 em :test_admin_info_with_tel_does_not_crash
201
202 def test_inactive_info_does_not_crash
203 sgx = Minitest::Mock.new
204 sgx.expect(:registered?, false)
205
206 plan = CustomerPlan.new("test", plan: nil, expires_at: nil)
207 cust = Customer.new(
208 "test",
209 Blather::JID.new("test@example.net"),
210 plan: plan,
211 sgx: sgx
212 )
213 assert CustomerInfo.for(cust).sync.form
214 assert_mock sgx
215 end
216 em :test_inactive_info_does_not_crash
217
218 def test_inactive_admin_info_does_not_crash
219 sgx = Minitest::Mock.new
220 sgx.expect(:registered?, false)
221 sgx.expect(:registered?, false)
222 sgx.expect(:fwd, CustomerFwd::None.new(uri: nil, timeout: nil))
223
224 Subaccount::DB.expect(
225 :query_defer,
226 EMPromise.resolve([]),
227 [String, ["test"]]
228 )
229
230 TrivialBackendSgxRepo::REDIS.expect(
231 :get,
232 EMPromise.resolve(nil),
233 ["jmp_customer_backend_sgx-test"]
234 )
235
236 plan = CustomerPlan.new("test", plan: nil, expires_at: nil)
237 cust = Customer.new(
238 "test",
239 Blather::JID.new("test@example.net"),
240 plan: plan,
241 sgx: sgx
242 )
243
244 trust_repo = Minitest::Mock.new
245 trust_repo.expect(:find, TrustLevel::Basement, [cust])
246
247 assert AdminInfo.for(cust, trust_level_repo: trust_repo).sync.form
248 assert_mock sgx
249 assert_mock trust_repo
250 assert_mock Subaccount::DB
251 assert_mock TrivialBackendSgxRepo::REDIS
252 end
253 em :test_inactive_admin_info_does_not_crash
254
255 def test_admin_info_subaccount_does_not_crash
256 sgx = Minitest::Mock.new
257 sgx.expect(:registered?, false)
258 fwd = CustomerFwd.for(uri: "tel:+12223334444", timeout: 15)
259 sgx.expect(:fwd, fwd)
260 sgx.expect(:registered?, false)
261
262 CustomerPlan::DB.expect(
263 :query_one,
264 EMPromise.resolve({ start_date: Time.now }),
265 [String, "test"]
266 )
267
268 CustomerUsage::DB.expect(
269 :query_one,
270 EMPromise.resolve({ charges: 0.to_d }),
271 [String, "test"]
272 )
273
274 Subaccount::DB.expect(
275 :query_defer,
276 EMPromise.resolve([]),
277 [String, ["parent"]]
278 )
279
280 TrivialBackendSgxRepo::REDIS.expect(
281 :get,
282 EMPromise.resolve(nil),
283 ["jmp_customer_backend_sgx-test"]
284 )
285
286 cust = customer(
287 sgx: sgx,
288 plan_name: "test_usd",
289 parent_customer_id: "parent"
290 )
291
292 trust_repo = Minitest::Mock.new
293 trust_repo.expect(:find, TrustLevel::Basement, [cust])
294
295 assert AdminInfo.for(cust, trust_level_repo: trust_repo).sync.form
296 assert_mock sgx
297 assert_mock trust_repo
298 assert_mock CustomerUsage::DB
299 assert_mock Subaccount::DB
300 assert_mock TrivialBackendSgxRepo::REDIS
301 end
302 em :test_admin_info_subaccount_does_not_crash
303end