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 "processing_status" => "complete",
29 "actual_foc_date" => DateTime.now,
30 "updated_at" => DateTime.now,
31 "tel" => "+12225551234",
32 "customer_id" => "cust1",
33 "backend_sgx" => "sgx"
34 }
35 ]
36 db = FakeDbForPorts.new(rows)
37 repo = PortRepo::Db.new(@output, db: db)
38
39 ports = repo.list.sync
40
41 assert_equal 1, ports.length
42 assert_kind_of DbPort, ports[0]
43 assert_equal "port1", ports[0].id
44 assert_equal "complete", ports[0].processing_status
45 end
46 em :test_list_returns_db_ports
47
48 def test_list_empty_when_no_rows
49 db = FakeDbForPorts.new([])
50 repo = PortRepo::Db.new(@output, db: db)
51
52 ports = repo.list.sync
53
54 assert_equal 0, ports.length
55 end
56 em :test_list_empty_when_no_rows
57end
58
59class PortRepoBandwidthTest < Minitest::Test
60 def setup
61 @output = FullManual.new
62 end
63
64 def test_list_returns_empty_when_no_ports
65 stub_request(
66 :get,
67 /https:\/\/dashboard\.bandwidth\.com\/v1\.0\/accounts\/.*\/portins/
68 ).to_return(status: 204, body: "")
69
70 repo = PortRepo::Bandwidth.new(@output)
71 ports = repo.list
72
73 assert_equal 0, ports.length
74 end
75
76 def test_list_wraps_ports_in_port_with_backend
77 stub_request(
78 :get,
79 /https:\/\/dashboard\.bandwidth\.com\/v1\.0\/accounts\/.*\/portins/
80 ).to_return(status: 200, body: <<~XML)
81 <LNPResponseWrapper>
82 <TotalCount>1</TotalCount>
83 <Links/>
84 <lnpPortInfoForGivenStatus>
85 <OrderId>abc123</OrderId>
86 <CustomerOrderId>cust456</CustomerOrderId>
87 <BillingTelephoneNumber>5551234567</BillingTelephoneNumber>
88 <lastModifiedDate>2025-01-13T12:00:00.000Z</lastModifiedDate>
89 <OrderDate>2025-01-12T12:00:00.000Z</OrderDate>
90 <ProcessingStatus>COMPLETE</ProcessingStatus>
91 </lnpPortInfoForGivenStatus>
92 </LNPResponseWrapper>
93 XML
94
95 repo = PortRepo::Bandwidth.new(@output)
96 ports = repo.list
97
98 assert_equal 1, ports.length
99 port = ports[0]
100 assert_kind_of PortRepo::Bandwidth::PortWithBackend, port
101 assert_equal "abc123", port.id
102 assert_equal "cust456", port.customer_id
103 assert_equal "5551234567", port.tel
104 assert_equal CONFIG[:sgx], port.backend_sgx
105 end
106end