|
| 1 | +from unittest.mock import AsyncMock, PropertyMock |
| 2 | + |
| 3 | +from fastapi import FastAPI |
| 4 | +from fastapi.testclient import TestClient |
| 5 | +from horizon.authentication import enforce_pdp_token |
| 6 | +from horizon.connectivity.api import init_connectivity_router |
| 7 | + |
| 8 | + |
| 9 | +def _noop_auth(): |
| 10 | + pass |
| 11 | + |
| 12 | + |
| 13 | +def _create_test_app(opal_client_mock): |
| 14 | + """Create a test FastAPI app with the connectivity router (no auth).""" |
| 15 | + app = FastAPI() |
| 16 | + router = init_connectivity_router(opal_client_mock) |
| 17 | + app.include_router(router) |
| 18 | + app.dependency_overrides[enforce_pdp_token] = _noop_auth |
| 19 | + return app |
| 20 | + |
| 21 | + |
| 22 | +def _make_opal_mock(*, offline_mode_enabled=True, connectivity_disabled=False): |
| 23 | + mock = AsyncMock() |
| 24 | + type(mock).offline_mode_enabled = PropertyMock(return_value=offline_mode_enabled) |
| 25 | + type(mock).opal_server_connectivity_disabled = PropertyMock(return_value=connectivity_disabled) |
| 26 | + return mock |
| 27 | + |
| 28 | + |
| 29 | +class TestGetConnectivityStatus: |
| 30 | + def test_returns_status(self): |
| 31 | + mock = _make_opal_mock(offline_mode_enabled=True, connectivity_disabled=True) |
| 32 | + client = TestClient(_create_test_app(mock)) |
| 33 | + |
| 34 | + resp = client.get("/control-plane/connectivity") |
| 35 | + assert resp.status_code == 200 |
| 36 | + data = resp.json() |
| 37 | + assert data["control_plane_connectivity_disabled"] is True |
| 38 | + assert data["offline_mode_enabled"] is True |
| 39 | + |
| 40 | + def test_returns_status_when_connected(self): |
| 41 | + mock = _make_opal_mock(offline_mode_enabled=True, connectivity_disabled=False) |
| 42 | + client = TestClient(_create_test_app(mock)) |
| 43 | + |
| 44 | + resp = client.get("/control-plane/connectivity") |
| 45 | + assert resp.status_code == 200 |
| 46 | + data = resp.json() |
| 47 | + assert data["control_plane_connectivity_disabled"] is False |
| 48 | + |
| 49 | + |
| 50 | +class TestEnableConnectivity: |
| 51 | + def test_enable_success(self): |
| 52 | + mock = _make_opal_mock(offline_mode_enabled=True, connectivity_disabled=True) |
| 53 | + client = TestClient(_create_test_app(mock)) |
| 54 | + |
| 55 | + resp = client.post("/control-plane/connectivity/enable") |
| 56 | + assert resp.status_code == 200 |
| 57 | + assert resp.json()["status"] == "enabled" |
| 58 | + mock.enable_opal_server_connectivity.assert_awaited_once() |
| 59 | + |
| 60 | + def test_enable_already_enabled(self): |
| 61 | + mock = _make_opal_mock(offline_mode_enabled=True, connectivity_disabled=False) |
| 62 | + client = TestClient(_create_test_app(mock)) |
| 63 | + |
| 64 | + resp = client.post("/control-plane/connectivity/enable") |
| 65 | + assert resp.status_code == 200 |
| 66 | + assert resp.json()["status"] == "already_enabled" |
| 67 | + mock.enable_opal_server_connectivity.assert_not_awaited() |
| 68 | + |
| 69 | + def test_enable_returns_400_when_offline_mode_disabled(self): |
| 70 | + mock = _make_opal_mock(offline_mode_enabled=False) |
| 71 | + client = TestClient(_create_test_app(mock)) |
| 72 | + |
| 73 | + resp = client.post("/control-plane/connectivity/enable") |
| 74 | + assert resp.status_code == 400 |
| 75 | + |
| 76 | + def test_enable_returns_500_on_opal_error(self): |
| 77 | + mock = _make_opal_mock(offline_mode_enabled=True, connectivity_disabled=True) |
| 78 | + mock.enable_opal_server_connectivity.side_effect = RuntimeError("boom") |
| 79 | + client = TestClient(_create_test_app(mock)) |
| 80 | + |
| 81 | + resp = client.post("/control-plane/connectivity/enable") |
| 82 | + assert resp.status_code == 500 |
| 83 | + assert "Failed to enable" in resp.json()["detail"] |
| 84 | + |
| 85 | + |
| 86 | +class TestDisableConnectivity: |
| 87 | + def test_disable_success(self): |
| 88 | + mock = _make_opal_mock(offline_mode_enabled=True, connectivity_disabled=False) |
| 89 | + client = TestClient(_create_test_app(mock)) |
| 90 | + |
| 91 | + resp = client.post("/control-plane/connectivity/disable") |
| 92 | + assert resp.status_code == 200 |
| 93 | + assert resp.json()["status"] == "disabled" |
| 94 | + mock.disable_opal_server_connectivity.assert_awaited_once() |
| 95 | + |
| 96 | + def test_disable_already_disabled(self): |
| 97 | + mock = _make_opal_mock(offline_mode_enabled=True, connectivity_disabled=True) |
| 98 | + client = TestClient(_create_test_app(mock)) |
| 99 | + |
| 100 | + resp = client.post("/control-plane/connectivity/disable") |
| 101 | + assert resp.status_code == 200 |
| 102 | + assert resp.json()["status"] == "already_disabled" |
| 103 | + mock.disable_opal_server_connectivity.assert_not_awaited() |
| 104 | + |
| 105 | + def test_disable_returns_400_when_offline_mode_disabled(self): |
| 106 | + mock = _make_opal_mock(offline_mode_enabled=False) |
| 107 | + client = TestClient(_create_test_app(mock)) |
| 108 | + |
| 109 | + resp = client.post("/control-plane/connectivity/disable") |
| 110 | + assert resp.status_code == 400 |
| 111 | + |
| 112 | + def test_disable_returns_500_on_opal_error(self): |
| 113 | + mock = _make_opal_mock(offline_mode_enabled=True, connectivity_disabled=False) |
| 114 | + mock.disable_opal_server_connectivity.side_effect = RuntimeError("boom") |
| 115 | + client = TestClient(_create_test_app(mock)) |
| 116 | + |
| 117 | + resp = client.post("/control-plane/connectivity/disable") |
| 118 | + assert resp.status_code == 500 |
| 119 | + assert "Failed to disable" in resp.json()["detail"] |
0 commit comments