|
20 | 20 | )
|
21 | 21 |
|
22 | 22 |
|
23 |
| -def test_query_address_single_closed_port(): |
24 |
| - """Mock socket connection to return non-zero, indicating the port is closed""" |
25 |
| - result = query_address(VALID_PUBLIC_IPV4, [CLOSED_PORTS[0]]) |
26 |
| - assert result == [{"port": CLOSED_PORTS[0], "status": False}] |
| 23 | +@pytest.mark.parametrize( |
| 24 | + "host,ports,expected", |
| 25 | + [ |
| 26 | + ( |
| 27 | + VALID_PUBLIC_IPV4, |
| 28 | + [CLOSED_PORTS[0]], |
| 29 | + [{"port": CLOSED_PORTS[0], "status": False}], |
| 30 | + ), |
| 31 | + ( |
| 32 | + VALID_PUBLIC_IPV4, |
| 33 | + OPEN_PORTS, |
| 34 | + [{"port": port, "status": True} for port in OPEN_PORTS], |
| 35 | + ), |
| 36 | + ( |
| 37 | + VALID_PUBLIC_IPV4, |
| 38 | + CLOSED_PORTS, |
| 39 | + [{"port": port, "status": False} for port in CLOSED_PORTS], |
| 40 | + ), |
| 41 | + (VALID_PUBLIC_IPV4, [], []), |
| 42 | + ], |
| 43 | +) |
| 44 | +def test_query_address_various(host, ports, expected): |
| 45 | + """Test query_address for various valid scenarios.""" |
| 46 | + assert query_address(host, ports) == expected |
27 | 47 |
|
28 | 48 |
|
29 | 49 | def test_query_address_multiple_ports_mixed_status():
|
30 | 50 | """Test when some ports are open and some are closed."""
|
31 | 51 | result = query_address(VALID_PUBLIC_IPV4, PORTS)
|
32 | 52 | expected = [
|
33 |
| - { |
34 |
| - "port": port, |
35 |
| - "status": mock_connect((VALID_PUBLIC_IPV4, port)) == SOCKET_OPEN, |
36 |
| - } |
| 53 | + {"port": port, "status": mock_connect((VALID_PUBLIC_IPV4, port)) == SOCKET_OPEN} |
37 | 54 | for port in PORTS
|
38 | 55 | ]
|
39 | 56 | assert result == expected
|
40 | 57 |
|
41 | 58 |
|
42 |
| -def test_query_address_empty_ports_list(): |
43 |
| - """Test query_address returns empty list when ports list is empty.""" |
44 |
| - ports = expected_result = [] |
45 |
| - assert query_address(VALID_PUBLIC_IPV4, ports) == expected_result |
46 |
| - |
47 |
| - |
48 |
| -def test_query_address_invalid_address(): |
49 |
| - """Test query_address raises JsonAPIException for an invalid hostname.""" |
| 59 | +def test_query_address_invalid_hostname(mocker): |
| 60 | + """Test query_address raises JsonAPIException for an invalid hostname (mocked DNS failure).""" |
| 61 | + mocker.patch( |
| 62 | + "socket.gethostbyname", |
| 63 | + side_effect=OSError("Hostname does not appear to resolve"), |
| 64 | + ) |
50 | 65 | with pytest.raises(JsonAPIException, match=".*Hostname does not appear to resolve"):
|
51 | 66 | query_address(INVALID_HOST, [OPEN_PORTS[0]])
|
52 | 67 |
|
53 | 68 |
|
| 69 | +def test_query_address_empty_host(): |
| 70 | + """Test query_address raises JsonAPIException for empty host.""" |
| 71 | + with pytest.raises(JsonAPIException, match="A hostname must be provided"): |
| 72 | + query_address("", [80]) |
| 73 | + |
| 74 | + |
| 75 | +def test_query_address_none_host(): |
| 76 | + """Test query_address raises JsonAPIException for None as host.""" |
| 77 | + with pytest.raises(JsonAPIException): |
| 78 | + query_address(None, [80]) |
| 79 | + |
| 80 | + |
| 81 | +def test_query_address_invalid_port_type(): |
| 82 | + """Test query_address raises TypeError for non-integer port.""" |
| 83 | + with pytest.raises(Exception): |
| 84 | + query_address(VALID_PUBLIC_IPV4, ["notaport"]) |
| 85 | + |
| 86 | + |
| 87 | +def test_query_address_ipv6(): |
| 88 | + """Test query_address raises JsonAPIException for IPv6 address.""" |
| 89 | + with pytest.raises(JsonAPIException, match="IPv6 is not currently supported"): |
| 90 | + query_address("2001:4860:4860::8888", [80]) |
| 91 | + |
| 92 | + |
| 93 | +def test_query_address_duplicate_ports(): |
| 94 | + """Test query_address handles duplicate ports gracefully.""" |
| 95 | + ports = [80, 80, 443] |
| 96 | + result = query_address(VALID_PUBLIC_IPV4, ports) |
| 97 | + assert result.count({"port": 80, "status": True}) == 2 |
| 98 | + assert {"port": 443, "status": True} in result |
| 99 | + |
| 100 | + |
54 | 101 | def test_check_ports_all_open():
|
55 | 102 | """Test when all ports are open."""
|
56 | 103 | result = check_ports(VALID_PUBLIC_IPV4, OPEN_PORTS)
|
|
0 commit comments