test_port_repo.rb

  1# frozen_string_literal: true
  2
  3require "date"
  4require "test_helper"
  5require "port_repo"
  6require "db_port"
  7
  8class FakeDbForPorts < FakeDB
  9	def initialize(rows=[])
 10		super({})
 11		@rows = rows
 12	end
 13
 14	def exec_defer(_, _)
 15		EMPromise.resolve(@rows)
 16	end
 17end
 18
 19class PortRepoDbTest < Minitest::Test
 20	def setup
 21		@output = FullManual.new
 22	end
 23
 24	def test_list_returns_db_ports
 25		rows = [
 26			{
 27				"id" => "port1",
 28				"actual_foc_date" => DateTime.now,
 29				"updated_at" => DateTime.now,
 30				"tel" => "+12225551234",
 31				"customer_id" => "cust1",
 32				"backend_sgx" => "sgx"
 33			}
 34		]
 35		# Make sure that `processing_status`
 36		# resolves to "complete"
 37		sleep 1
 38		db = FakeDbForPorts.new(rows)
 39		repo = PortRepo::Db.new(@output, db: db)
 40
 41		ports = repo.list.sync
 42
 43		assert_equal 1, ports.length
 44		assert_kind_of DbPort, ports[0]
 45		assert_equal "port1", ports[0].id
 46		assert_equal DbPort::Status::COMPLETE, ports[0].processing_status
 47	end
 48	em :test_list_returns_db_ports
 49
 50	def test_list_empty_when_no_rows
 51		db = FakeDbForPorts.new([])
 52		repo = PortRepo::Db.new(@output, db: db)
 53
 54		ports = repo.list.sync
 55
 56		assert_equal 0, ports.length
 57	end
 58	em :test_list_empty_when_no_rows
 59end
 60
 61class PortRepoBandwidthTest < Minitest::Test
 62	def setup
 63		@output = FullManual.new
 64	end
 65
 66	def test_list_returns_empty_when_no_ports
 67		stub_request(
 68			:get,
 69			/https:\/\/dashboard\.bandwidth\.com\/v1\.0\/accounts\/.*\/portins/
 70		).to_return(status: 204, body: "")
 71
 72		repo = PortRepo::Bandwidth.new(@output)
 73		ports = repo.list
 74
 75		assert_equal 0, ports.length
 76	end
 77
 78	def test_list_wraps_ports_in_port_with_backend
 79		stub_request(
 80			:get,
 81			/https:\/\/dashboard\.bandwidth\.com\/v1\.0\/accounts\/.*\/portins/
 82		).to_return(status: 200, body: <<~XML)
 83			<LNPResponseWrapper>
 84				<TotalCount>1</TotalCount>
 85				<Links/>
 86				<lnpPortInfoForGivenStatus>
 87					<OrderId>abc123</OrderId>
 88					<CustomerOrderId>cust456</CustomerOrderId>
 89					<BillingTelephoneNumber>5551234567</BillingTelephoneNumber>
 90					<lastModifiedDate>2025-01-13T12:00:00.000Z</lastModifiedDate>
 91					<OrderDate>2025-01-12T12:00:00.000Z</OrderDate>
 92					<ProcessingStatus>COMPLETE</ProcessingStatus>
 93				</lnpPortInfoForGivenStatus>
 94			</LNPResponseWrapper>
 95		XML
 96
 97		repo = PortRepo::Bandwidth.new(@output)
 98		ports = repo.list
 99
100		assert_equal 1, ports.length
101		port = ports[0]
102		assert_kind_of PortRepo::Bandwidth::PortWithBackend, port
103		assert_equal "abc123", port.id
104		assert_equal "cust456", port.customer_id
105		assert_equal "5551234567", port.tel
106		assert_equal CONFIG[:sgx], port.backend_sgx
107	end
108end