test: add automated api tester
This commit is contained in:
1 parent
882efeb963
commit
572d551a2c
7 files changed
+113
-4
No files matched your search
@@ -0,0 +1,74 @@
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app import main as dashboard
|
||||
from app.services import ServiceRecord
|
||||
|
||||
|
||||
client = TestClient(dashboard.app)
|
||||
|
||||
|
||||
class DummyTunnelInfo:
|
||||
def __init__(self, port: int):
|
||||
self.port = port
|
||||
self.command = "gradio-tunneling --port %d" % port
|
||||
self.pid = 42
|
||||
self.url = "https://example.com/%d" % port
|
||||
self.status = "running"
|
||||
self.exit_code = None
|
||||
self.last_output = "started"
|
||||
|
||||
|
||||
def test_list_ports(monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
dashboard.scanner,
|
||||
"scan",
|
||||
lambda: [
|
||||
ServiceRecord(
|
||||
port=4444,
|
||||
protocol="tcp",
|
||||
service="svc1",
|
||||
pid=123,
|
||||
user="alice",
|
||||
command="cmd",
|
||||
container="ctr",
|
||||
local_address="0.0.0.0:4444",
|
||||
allowed=True,
|
||||
policy_reason=None,
|
||||
),
|
||||
ServiceRecord(
|
||||
port=2222,
|
||||
protocol="tcp",
|
||||
service="svc2",
|
||||
pid=321,
|
||||
user="bob",
|
||||
command="cmd2",
|
||||
container=None,
|
||||
local_address="0.0.0.0:2222",
|
||||
allowed=False,
|
||||
policy_reason="blocked",
|
||||
),
|
||||
],
|
||||
)
|
||||
|
||||
response = client.get("/api/ports")
|
||||
assert response.status_code == 200
|
||||
assert response.json()[0]["port"] == 2222
|
||||
assert response.json()[1]["port"] == 4444
|
||||
|
||||
|
||||
def test_start_tunnel_blocked(monkeypatch):
|
||||
monkeypatch.setattr(dashboard.policy, "is_port_allowed", lambda port: (False, "blocked"))
|
||||
response = client.post("/api/tunnels", json={"port": 2222})
|
||||
assert response.status_code == 403
|
||||
assert "blocked" in response.json()["detail"]
|
||||
|
||||
|
||||
def test_start_tunnel_success(monkeypatch):
|
||||
monkeypatch.setattr(dashboard.policy, "is_port_allowed", lambda port: (True, None))
|
||||
info = DummyTunnelInfo(3333)
|
||||
monkeypatch.setattr(dashboard.manager, "start_tunnel", lambda port: info)
|
||||
response = client.post("/api/tunnels", json={"port": 3333})
|
||||
assert response.status_code == 200
|
||||
payload = response.json()
|
||||
assert payload["port"] == 3333
|
||||
assert payload["url"] == info.url
|
||||
Reference in new issue
Block a user