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	end
 43	em :test_info_does_not_crash
 44
 45	def test_info_has_remaining_included_calling_credit
 46		sgx = Minitest::Mock.new
 47		sgx.expect(:registered?, false)
 48
 49		CustomerPlan::DB.expect(
 50			:query_one,
 51			EMPromise.resolve({ start_date: Time.now }),
 52			[String, "test"]
 53		)
 54
 55		CustomerUsage::DB.expect(
 56			:query_one,
 57			EMPromise.resolve({ charges: 0.044.to_d }),
 58			[String, "test"]
 59		)
 60
 61		cust = customer(sgx: sgx, plan_name: "test_usd")
 62
 63		assert_equal(
 64			"$1.0000",
 65			CustomerInfo.for(cust).sync.form
 66				.field("remaining_included_calling_credit").value
 67		)
 68		assert_mock sgx
 69	end
 70	em :test_info_has_remaining_included_calling_credit
 71
 72	def test_info_out_of_remaining_included_calling_credit
 73		sgx = Minitest::Mock.new
 74		sgx.expect(:registered?, false)
 75
 76		CustomerPlan::DB.expect(
 77			:query_one,
 78			EMPromise.resolve({ start_date: Time.now }),
 79			[String, "test"]
 80		)
 81
 82		CustomerUsage::DB.expect(
 83			:query_one,
 84			EMPromise.resolve({ charges: 10.to_d }),
 85			[String, "test"]
 86		)
 87
 88		cust = customer(sgx: sgx, plan_name: "test_usd")
 89
 90		assert_equal(
 91			"$0.0000",
 92			CustomerInfo.for(cust).sync.form
 93				.field("remaining_included_calling_credit").value
 94		)
 95		assert_mock sgx
 96	end
 97	em :test_info_out_of_remaining_included_calling_credit
 98
 99	def test_admin_info_does_not_crash
100		sgx = Minitest::Mock.new
101		sgx.expect(:registered?, false)
102		fwd = CustomerFwd.for(uri: "tel:+12223334444", timeout: 15)
103		sgx.expect(:fwd, fwd)
104		sgx.expect(:registered?, false)
105
106		CustomerPlan::DB.expect(
107			:query_one,
108			EMPromise.resolve({ start_date: Time.now }),
109			[String, "test"]
110		)
111
112		CustomerUsage::DB.expect(
113			:query_one,
114			EMPromise.resolve({ charges: 0.to_d }),
115			[String, "test"]
116		)
117
118		cust = customer(sgx: sgx, plan_name: "test_usd")
119
120		trust_repo = Minitest::Mock.new
121		trust_repo.expect(:find, TrustLevel::Basement, [cust])
122
123		assert AdminInfo.for(cust, trust_level_repo: trust_repo).sync.form
124		assert_mock sgx
125		assert_mock trust_repo
126	end
127	em :test_admin_info_does_not_crash
128
129	def test_admin_info_with_tel_does_not_crash
130		registered = Struct.new(:phone).new("+12223334444")
131		fwd = CustomerFwd.for(uri: "tel:+12223334444", timeout: 15)
132		sgx = Struct.new(:registered?, :fwd).new(registered, fwd)
133
134		CustomerPlan::DB.expect(
135			:query_one,
136			EMPromise.resolve({ start_date: Time.now }),
137			[String, "test"]
138		)
139
140		CustomerUsage::DB.expect(
141			:query_one,
142			EMPromise.resolve({ charges: 0.to_d }),
143			[String, "test"]
144		)
145
146		cust = customer(sgx: sgx, plan_name: "test_usd")
147
148		call_attempt_repo = Minitest::Mock.new
149		call_attempt_repo.expect(
150			:find_outbound,
151			CallAttempt::Unsupported.new(direction: :outbound),
152			[cust, "+1"], call_id: "dry_run"
153		)
154
155		trust_repo = Minitest::Mock.new
156		trust_repo.expect(:find, TrustLevel::Basement, [cust])
157
158		assert AdminInfo.for(
159			cust,
160			trust_level_repo: trust_repo,
161			call_attempt_repo: call_attempt_repo
162		).sync.form
163		assert_mock call_attempt_repo
164		assert_mock trust_repo
165	end
166	em :test_admin_info_with_tel_does_not_crash
167
168	def test_inactive_info_does_not_crash
169		CustomerUsage::DB.expect(
170			:query_one,
171			EMPromise.resolve({ charges: 0.to_d }),
172			[String, "test"]
173		)
174
175		sgx = Minitest::Mock.new
176		sgx.expect(:registered?, false)
177
178		plan = CustomerPlan.new("test", plan: nil, expires_at: nil)
179		cust = Customer.new(
180			"test",
181			Blather::JID.new("test@example.net"),
182			plan: plan,
183			sgx: sgx
184		)
185		assert CustomerInfo.for(cust).sync.form
186		assert_mock sgx
187	end
188	em :test_inactive_info_does_not_crash
189
190	def test_inactive_admin_info_does_not_crash
191		CustomerUsage::DB.expect(
192			:query_one,
193			EMPromise.resolve({ charges: 0.to_d }),
194			[String, "test"]
195		)
196
197		sgx = Minitest::Mock.new
198		sgx.expect(:registered?, false)
199		sgx.expect(:registered?, false)
200		sgx.expect(:fwd, CustomerFwd::None.new(uri: nil, timeout: nil))
201
202		plan = CustomerPlan.new("test", plan: nil, expires_at: nil)
203		cust = Customer.new(
204			"test",
205			Blather::JID.new("test@example.net"),
206			plan: plan,
207			sgx: sgx
208		)
209
210		trust_repo = Minitest::Mock.new
211		trust_repo.expect(:find, TrustLevel::Basement, [cust])
212
213		assert AdminInfo.for(cust, trust_level_repo: trust_repo).sync.form
214		assert_mock sgx
215		assert_mock trust_repo
216	end
217	em :test_inactive_admin_info_does_not_crash
218end