test_customer_info.rb

  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_admin_info_does_not_crash
 48		sgx = Minitest::Mock.new
 49		sgx.expect(:registered?, false)
 50		fwd = CustomerFwd.for(uri: "tel:+12223334444", timeout: 15)
 51		sgx.expect(:fwd, fwd)
 52		sgx.expect(:registered?, false)
 53
 54		CustomerPlan::DB.expect(
 55			:query_one,
 56			EMPromise.resolve({ start_date: Time.now }),
 57			[String, "test"]
 58		)
 59
 60		CustomerUsage::DB.expect(
 61			:query_one,
 62			EMPromise.resolve({ charges: 0.to_d }),
 63			[String, "test"]
 64		)
 65
 66		Subaccount::DB.expect(
 67			:query_defer,
 68			EMPromise.resolve({}),
 69			[String, ["test"]]
 70		)
 71
 72		TrivialBackendSgxRepo::REDIS.expect(
 73			:get,
 74			EMPromise.resolve(nil),
 75			["jmp_customer_backend_sgx-test"]
 76		)
 77		cust = customer(sgx: sgx, plan_name: "test_usd")
 78
 79		trust_repo = Minitest::Mock.new
 80		trust_repo.expect(:find, TrustLevel::Basement, [cust])
 81
 82		assert AdminInfo.for(cust, trust_level_repo: trust_repo).sync.form
 83		assert_mock sgx
 84		assert_mock trust_repo
 85		assert_mock CustomerUsage::DB
 86		assert_mock Subaccount::DB
 87		assert_mock TrivialBackendSgxRepo::REDIS
 88	end
 89	em :test_admin_info_does_not_crash
 90
 91	def test_admin_info_with_tel_does_not_crash
 92		registered = Struct.new(:phone).new("+12223334444")
 93		fwd = CustomerFwd.for(uri: "tel:+12223334444", timeout: 15)
 94		sgx = Struct.new(:registered?, :fwd).new(registered, fwd)
 95
 96		CustomerPlan::DB.expect(
 97			:query_one,
 98			EMPromise.resolve({ start_date: Time.now }),
 99			[String, "test"]
100		)
101
102		CustomerUsage::DB.expect(
103			:query_one,
104			EMPromise.resolve({ charges: 0.to_d }),
105			[String, "test"]
106		)
107
108		Subaccount::DB.expect(
109			:query_defer,
110			EMPromise.resolve([]),
111			[String, ["test"]]
112		)
113
114		TrivialBackendSgxRepo::REDIS.expect(
115			:get,
116			EMPromise.resolve(nil),
117			["jmp_customer_backend_sgx-test"]
118		)
119
120		cust = customer(sgx: sgx, plan_name: "test_usd")
121
122		call_attempt_repo = Minitest::Mock.new
123		call_attempt_repo.expect(
124			:find_outbound,
125			CallAttempt::Unsupported.new(direction: :outbound),
126			[cust, "+1"], call_id: "dry_run"
127		)
128
129		trust_repo = Minitest::Mock.new
130		trust_repo.expect(:find, TrustLevel::Basement, [cust])
131
132		assert AdminInfo.for(
133			cust,
134			trust_level_repo: trust_repo,
135			call_attempt_repo: call_attempt_repo
136		).sync.form
137		assert_mock call_attempt_repo
138		assert_mock trust_repo
139		assert_mock CustomerUsage::DB
140		assert_mock Subaccount::DB
141		assert_mock TrivialBackendSgxRepo::REDIS
142	end
143	em :test_admin_info_with_tel_does_not_crash
144
145	def test_inactive_info_does_not_crash
146		sgx = Minitest::Mock.new
147		sgx.expect(:registered?, false)
148
149		plan = CustomerPlan.new("test", plan: nil, expires_at: nil)
150		cust = Customer.new(
151			"test",
152			Blather::JID.new("test@example.net"),
153			plan: plan,
154			sgx: sgx
155		)
156		assert CustomerInfo.for(cust).sync.form
157		assert_mock sgx
158	end
159	em :test_inactive_info_does_not_crash
160
161	def test_inactive_admin_info_does_not_crash
162		sgx = Minitest::Mock.new
163		sgx.expect(:registered?, false)
164		sgx.expect(:registered?, false)
165		sgx.expect(:fwd, CustomerFwd::None.new(uri: nil, timeout: nil))
166
167		Subaccount::DB.expect(
168			:query_defer,
169			EMPromise.resolve([]),
170			[String, ["test"]]
171		)
172
173		TrivialBackendSgxRepo::REDIS.expect(
174			:get,
175			EMPromise.resolve(nil),
176			["jmp_customer_backend_sgx-test"]
177		)
178
179		plan = CustomerPlan.new("test", plan: nil, expires_at: nil)
180		cust = Customer.new(
181			"test",
182			Blather::JID.new("test@example.net"),
183			plan: plan,
184			sgx: sgx
185		)
186
187		trust_repo = Minitest::Mock.new
188		trust_repo.expect(:find, TrustLevel::Basement, [cust])
189
190		assert AdminInfo.for(cust, trust_level_repo: trust_repo).sync.form
191		assert_mock sgx
192		assert_mock trust_repo
193		assert_mock Subaccount::DB
194		assert_mock TrivialBackendSgxRepo::REDIS
195	end
196	em :test_inactive_admin_info_does_not_crash
197
198	def test_admin_info_subaccount_does_not_crash
199		sgx = Minitest::Mock.new
200		sgx.expect(:registered?, false)
201		fwd = CustomerFwd.for(uri: "tel:+12223334444", timeout: 15)
202		sgx.expect(:fwd, fwd)
203		sgx.expect(:registered?, false)
204
205		CustomerPlan::DB.expect(
206			:query_one,
207			EMPromise.resolve({ start_date: Time.now }),
208			[String, "test"]
209		)
210
211		CustomerUsage::DB.expect(
212			:query_one,
213			EMPromise.resolve({ charges: 0.to_d }),
214			[String, "test"]
215		)
216
217		Subaccount::DB.expect(
218			:query_defer,
219			EMPromise.resolve([]),
220			[String, ["parent"]]
221		)
222
223		TrivialBackendSgxRepo::REDIS.expect(
224			:get,
225			EMPromise.resolve(nil),
226			["jmp_customer_backend_sgx-test"]
227		)
228
229		cust = customer(
230			sgx: sgx,
231			plan_name: "test_usd",
232			parent_customer_id: "parent"
233		)
234
235		trust_repo = Minitest::Mock.new
236		trust_repo.expect(:find, TrustLevel::Basement, [cust])
237
238		assert AdminInfo.for(cust, trust_level_repo: trust_repo).sync.form
239		assert_mock sgx
240		assert_mock trust_repo
241		assert_mock CustomerUsage::DB
242		assert_mock Subaccount::DB
243		assert_mock TrivialBackendSgxRepo::REDIS
244	end
245	em :test_admin_info_subaccount_does_not_crash
246
247	def test_admin_info_includes_route
248		sgx = Minitest::Mock.new
249		sgx.expect(:registered?, false)
250		fwd = CustomerFwd.for(uri: "tel:+12223334444", timeout: 15)
251		sgx.expect(:fwd, fwd)
252		sgx.expect(:registered?, false)
253
254		CustomerPlan::DB.expect(
255			:query_one,
256			EMPromise.resolve({ start_date: Time.now }),
257			[String, "test"]
258		)
259
260		CustomerUsage::DB.expect(
261			:query_one,
262			EMPromise.resolve({ charges: 0.to_d }),
263			[String, "test"]
264		)
265
266		Subaccount::DB.expect(
267			:query_defer,
268			EMPromise.resolve({}),
269			[String, ["test"]]
270		)
271
272		TrivialBackendSgxRepo::REDIS.expect(
273			:get,
274			EMPromise.resolve("route_value"),
275			["jmp_customer_backend_sgx-test"]
276		)
277
278		cust = customer(sgx: sgx, plan_name: "test_usd")
279
280		trust_repo = Minitest::Mock.new
281		trust_repo.expect(:find, TrustLevel::Basement, [cust])
282
283		admin_info = AdminInfo.for(cust, trust_level_repo: trust_repo).sync.form
284
285		assert admin_info
286		assert admin_info.field("route").value == "route_value"
287		assert_mock sgx
288		assert_mock trust_repo
289		assert_mock CustomerUsage::DB
290		assert_mock Subaccount::DB
291		assert_mock TrivialBackendSgxRepo::REDIS
292	end
293	em :test_admin_info_includes_route
294end