~singpolyma/sgx-jmp

84c77d857f375a3e673f088cd2a48ca070d36f94 — Christopher Vollick 20 days ago ebd6fcc
Test Admin Info with Numbers

My current two AdminInfo tests have unregistered customers because it's
easier, but it means that some parts of the logic don't get tested.

This one digs a bit deeper to test this part of the forms, but it
requires injecting a repo
2 files changed, 42 insertions(+), 5 deletions(-)

M lib/customer_info.rb
M test/test_customer_info.rb
M lib/customer_info.rb => lib/customer_info.rb +9 -5
@@ 103,14 103,18 @@ class AdminInfo
		trust_level String
	end

	def self.for(customer, plan, trust_level_repo: TrustLevelRepo.new)
	def self.for(
		customer, plan,
		trust_level_repo: TrustLevelRepo.new,
		call_attempt_repo: CallAttemptRepo.new
	)
		PromiseHash.all(
			jid: customer.jid,
			customer_id: customer.customer_id,
			fwd: customer.fwd,
			info: CustomerInfo.for(customer, plan),
			api: customer.api,
			call_info: call_info(customer),
			call_info: call_info(customer, call_attempt_repo),
			trust_level: trust_level_repo.find(customer).then(&:to_s)
		).then(&method(:new))
	end


@@ 125,10 129,10 @@ class AdminInfo
		end
	end

	def self.call_info(customer)
	def self.call_info(customer, call_attempt_repo)
		if customer.registered?
			CallAttemptRepo.new
				.find_outbound(customer, "+1", call_id: "", low_balance: FakeLowBalance)
			call_attempt_repo
				.find_outbound(customer, "+1", call_id: "dry_run")
				.then(&:to_s)
		else
			EMPromise.resolve("No calling")

M test/test_customer_info.rb => test/test_customer_info.rb +33 -0
@@ 58,6 58,39 @@ class CustomerInfoTest < Minitest::Test
	end
	em :test_admin_info_does_not_crash

	def test_admin_info_with_tel_does_not_crash
		registered = Struct.new(:phone).new("+12223334444")
		fwd = CustomerFwd.for(uri: "tel:+12223334444", timeout: 15)
		sgx = Struct.new(:registered?, :fwd).new(registered, fwd)

		CustomerPlan::DB.expect(
			:query_one,
			EMPromise.resolve({ start_date: Time.now }),
			[String, "test"]
		)

		cust = customer(sgx: sgx, plan_name: "test_usd")

		call_attempt_repo = Minitest::Mock.new
		call_attempt_repo.expect(
			:find_outbound,
			CallAttempt::Unsupported.new(direction: :outbound),
			[cust, "+1", { call_id: "dry_run" }]
		)

		trust_repo = Minitest::Mock.new
		trust_repo.expect(:find, TrustLevel::Basement, [cust])

		assert cust
			.admin_info(
				trust_level_repo: trust_repo,
				call_attempt_repo: call_attempt_repo
			).sync.form
		assert_mock call_attempt_repo
		assert_mock trust_repo
	end
	em :test_admin_info_with_tel_does_not_crash

	def test_inactive_info_does_not_crash
		sgx = Minitest::Mock.new
		sgx.expect(:registered?, false)