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)
 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		cust = customer(sgx: sgx, plan_name: "test_usd")
122
123		trust_repo = Minitest::Mock.new
124		trust_repo.expect(:find, TrustLevel::Basement, [cust])
125
126		assert AdminInfo.for(cust, trust_level_repo: trust_repo).sync.form
127		assert_mock sgx
128		assert_mock trust_repo
129		assert_mock CustomerUsage::DB
130	end
131	em :test_admin_info_does_not_crash
132
133	def test_admin_info_with_tel_does_not_crash
134		registered = Struct.new(:phone).new("+12223334444")
135		fwd = CustomerFwd.for(uri: "tel:+12223334444", timeout: 15)
136		sgx = Struct.new(:registered?, :fwd).new(registered, fwd)
137
138		CustomerPlan::DB.expect(
139			:query_one,
140			EMPromise.resolve({ start_date: Time.now }),
141			[String, "test"]
142		)
143
144		CustomerUsage::DB.expect(
145			:query_one,
146			EMPromise.resolve({ charges: 0.to_d }),
147			[String, "test"]
148		)
149
150		cust = customer(sgx: sgx, plan_name: "test_usd")
151
152		call_attempt_repo = Minitest::Mock.new
153		call_attempt_repo.expect(
154			:find_outbound,
155			CallAttempt::Unsupported.new(direction: :outbound),
156			[cust, "+1"], call_id: "dry_run"
157		)
158
159		trust_repo = Minitest::Mock.new
160		trust_repo.expect(:find, TrustLevel::Basement, [cust])
161
162		assert AdminInfo.for(
163			cust,
164			trust_level_repo: trust_repo,
165			call_attempt_repo: call_attempt_repo
166		).sync.form
167		assert_mock call_attempt_repo
168		assert_mock trust_repo
169		assert_mock CustomerUsage::DB
170	end
171	em :test_admin_info_with_tel_does_not_crash
172
173	def test_inactive_info_does_not_crash
174		sgx = Minitest::Mock.new
175		sgx.expect(:registered?, false)
176
177		plan = CustomerPlan.new("test", plan: nil, expires_at: nil)
178		cust = Customer.new(
179			"test",
180			Blather::JID.new("test@example.net"),
181			plan: plan,
182			sgx: sgx
183		)
184		assert CustomerInfo.for(cust).sync.form
185		assert_mock sgx
186	end
187	em :test_inactive_info_does_not_crash
188
189	def test_inactive_admin_info_does_not_crash
190		sgx = Minitest::Mock.new
191		sgx.expect(:registered?, false)
192		sgx.expect(:registered?, false)
193		sgx.expect(:fwd, CustomerFwd::None.new(uri: nil, timeout: nil))
194
195		plan = CustomerPlan.new("test", plan: nil, expires_at: nil)
196		cust = Customer.new(
197			"test",
198			Blather::JID.new("test@example.net"),
199			plan: plan,
200			sgx: sgx
201		)
202
203		trust_repo = Minitest::Mock.new
204		trust_repo.expect(:find, TrustLevel::Basement, [cust])
205
206		assert AdminInfo.for(cust, trust_level_repo: trust_repo).sync.form
207		assert_mock sgx
208		assert_mock trust_repo
209	end
210	em :test_inactive_admin_info_does_not_crash
211end