test_customer_info.rb

  1# frozen_string_literal: true
  2
  3require "test_helper"
  4require "customer_info"
  5require "trust_level_repo"
  6require "trust_level"
  7
  8API::REDIS = FakeRedis.new
  9CustomerPlan::DB = Minitest::Mock.new
 10CustomerPlan::REDIS = Minitest::Mock.new
 11CustomerUsage::DB = Minitest::Mock.new
 12PlanInfo::DB = FakeDB.new(
 13	["test"] => [
 14		{
 15			"start_date" => Time.parse("2020-01-01"),
 16			"activation_date" => Time.parse("2021-01-01")
 17		}
 18	]
 19)
 20Subaccount::DB = 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		cust = customer(sgx: sgx, plan_name: "test_usd")
129
130		trust_repo = Minitest::Mock.new
131		trust_repo.expect(:find, TrustLevel::Basement, [cust])
132
133		assert AdminInfo.for(cust, trust_level_repo: trust_repo).sync.form
134		assert_mock sgx
135		assert_mock trust_repo
136		assert_mock CustomerUsage::DB
137		assert_mock Subaccount::DB
138	end
139	em :test_admin_info_does_not_crash
140
141	def test_admin_info_with_tel_does_not_crash
142		registered = Struct.new(:phone).new("+12223334444")
143		fwd = CustomerFwd.for(uri: "tel:+12223334444", timeout: 15)
144		sgx = Struct.new(:registered?, :fwd).new(registered, fwd)
145
146		CustomerPlan::DB.expect(
147			:query_one,
148			EMPromise.resolve({ start_date: Time.now }),
149			[String, "test"]
150		)
151
152		CustomerUsage::DB.expect(
153			:query_one,
154			EMPromise.resolve({ charges: 0.to_d }),
155			[String, "test"]
156		)
157
158		Subaccount::DB.expect(
159			:query_defer,
160			EMPromise.resolve([]),
161			[String, ["test"]]
162		)
163
164		cust = customer(sgx: sgx, plan_name: "test_usd")
165
166		call_attempt_repo = Minitest::Mock.new
167		call_attempt_repo.expect(
168			:find_outbound,
169			CallAttempt::Unsupported.new(direction: :outbound),
170			[cust, "+1"], call_id: "dry_run"
171		)
172
173		trust_repo = Minitest::Mock.new
174		trust_repo.expect(:find, TrustLevel::Basement, [cust])
175
176		assert AdminInfo.for(
177			cust,
178			trust_level_repo: trust_repo,
179			call_attempt_repo: call_attempt_repo
180		).sync.form
181		assert_mock call_attempt_repo
182		assert_mock trust_repo
183		assert_mock CustomerUsage::DB
184		assert_mock Subaccount::DB
185	end
186	em :test_admin_info_with_tel_does_not_crash
187
188	def test_inactive_info_does_not_crash
189		sgx = Minitest::Mock.new
190		sgx.expect(:registered?, false)
191
192		plan = CustomerPlan.new("test", plan: nil, expires_at: nil)
193		cust = Customer.new(
194			"test",
195			Blather::JID.new("test@example.net"),
196			plan: plan,
197			sgx: sgx
198		)
199		assert CustomerInfo.for(cust).sync.form
200		assert_mock sgx
201	end
202	em :test_inactive_info_does_not_crash
203
204	def test_inactive_admin_info_does_not_crash
205		sgx = Minitest::Mock.new
206		sgx.expect(:registered?, false)
207		sgx.expect(:registered?, false)
208		sgx.expect(:fwd, CustomerFwd::None.new(uri: nil, timeout: nil))
209
210		Subaccount::DB.expect(
211			:query_defer,
212			EMPromise.resolve([]),
213			[String, ["test"]]
214		)
215
216		plan = CustomerPlan.new("test", plan: nil, expires_at: nil)
217		cust = Customer.new(
218			"test",
219			Blather::JID.new("test@example.net"),
220			plan: plan,
221			sgx: sgx
222		)
223
224		trust_repo = Minitest::Mock.new
225		trust_repo.expect(:find, TrustLevel::Basement, [cust])
226
227		assert AdminInfo.for(cust, trust_level_repo: trust_repo).sync.form
228		assert_mock sgx
229		assert_mock trust_repo
230		assert_mock Subaccount::DB
231	end
232	em :test_inactive_admin_info_does_not_crash
233
234	def test_admin_info_subaccount_does_not_crash
235		sgx = Minitest::Mock.new
236		sgx.expect(:registered?, false)
237		fwd = CustomerFwd.for(uri: "tel:+12223334444", timeout: 15)
238		sgx.expect(:fwd, fwd)
239		sgx.expect(:registered?, false)
240
241		CustomerPlan::DB.expect(
242			:query_one,
243			EMPromise.resolve({ start_date: Time.now }),
244			[String, "test"]
245		)
246
247		CustomerUsage::DB.expect(
248			:query_one,
249			EMPromise.resolve({ charges: 0.to_d }),
250			[String, "test"]
251		)
252
253		Subaccount::DB.expect(
254			:query_defer,
255			EMPromise.resolve([]),
256			[String, ["parent"]]
257		)
258
259		cust = customer(
260			sgx: sgx,
261			plan_name: "test_usd",
262			parent_customer_id: "parent"
263		)
264
265		trust_repo = Minitest::Mock.new
266		trust_repo.expect(:find, TrustLevel::Basement, [cust])
267
268		assert AdminInfo.for(cust, trust_level_repo: trust_repo).sync.form
269		assert_mock sgx
270		assert_mock trust_repo
271		assert_mock CustomerUsage::DB
272		assert_mock Subaccount::DB
273	end
274	em :test_admin_info_subaccount_does_not_crash
275end