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