import base64 import pytest from app import create_app from app.config import Settings from app.remnawave import RemnawaveError from .fixtures import make_hosts, make_nodes, make_profiles KEY = "network-v2" class FakeClient: def __init__(self): self.calls = 0 self.fail = False def fetch_all(self): self.calls += 1 if self.fail: raise RemnawaveError("панель недоступна", "upstream_unreachable") return {"profiles": make_profiles(), "hosts": make_hosts(), "nodes": make_nodes()} def make_settings(tmp_path, **kw): base = dict(remnawave_url="http://x", remnawave_token="t", cache_ttl=30, data_dir=tmp_path) base.update(kw) return Settings(**base) @pytest.fixture def client_and_fake(tmp_path): fake = FakeClient() app = create_app(make_settings(tmp_path), client=fake) return app.test_client(), fake def test_graph_endpoint_returns_whole_network(client_and_fake): c, _ = client_and_fake r = c.get("/api/graph") assert r.status_code == 200 body = r.get_json() g = body["graph"] assert {n["type"] for n in g["nodes"]} == {"host", "node", "balancer", "outbound"} assert g["warnings"] and g["paths"] assert body["positionsKey"] == KEY assert "SECRET" not in r.get_data(as_text=True) def test_cache_and_refresh_throttle(client_and_fake): c, fake = client_and_fake c.get("/api/graph"); c.get("/api/graph"); c.get("/api/profiles") assert fake.calls == 1 c.get("/api/graph?refresh=1") # слишком рано, игнорируется assert fake.calls == 1 def test_stale_snapshot_on_upstream_error(tmp_path): fake = FakeClient() c = create_app(make_settings(tmp_path, cache_ttl=0), client=fake).test_client() assert c.get("/api/graph").status_code == 200 fake.fail = True r = c.get("/api/graph") assert r.status_code == 200 meta = r.get_json()["meta"] assert meta["stale"] is True and "недоступна" in meta["lastError"] def test_upstream_error_without_cache(tmp_path): fake = FakeClient() fake.fail = True c = create_app(make_settings(tmp_path), client=fake).test_client() r = c.get("/api/graph") assert r.status_code == 502 and r.get_json()["error"] == "upstream_unreachable" def test_positions_roundtrip(client_and_fake): c, _ = client_and_fake assert c.get(f"/api/positions/{KEY}").get_json() == {"positions": {}} r = c.put(f"/api/positions/{KEY}", json={"positions": {"rule:0": {"x": 10, "y": 20.5}}}) assert r.get_json() == {"saved": 1} assert c.get("/api/graph").get_json()["positions"] == {"rule:0": {"x": 10.0, "y": 20.5}} assert c.delete(f"/api/positions/{KEY}").status_code == 204 assert c.get(f"/api/positions/{KEY}").get_json() == {"positions": {}} @pytest.mark.parametrize("payload", [ {"positions": []}, {"positions": {"a": {"x": "1", "y": 2}}}, {"positions": {"a": {"x": float("inf"), "y": 2}}}, {"positions": {"a": {"x": True, "y": 2}}}, {"nope": 1}, ]) def test_positions_validation(client_and_fake, payload): c, _ = client_and_fake assert c.put(f"/api/positions/{KEY}", json=payload).status_code == 400 def test_positions_bad_key(client_and_fake): c, _ = client_and_fake assert c.put("/api/positions/..%2Fetc", json={"positions": {}}).status_code in (400, 404) assert c.get("/api/positions/x").status_code == 400 def test_basic_auth(tmp_path): app = create_app(make_settings(tmp_path, basic_user="u", basic_password="p"), client=FakeClient()) c = app.test_client() assert c.get("/api/health").status_code == 200 assert c.get("/api/graph").status_code == 401 good = {"Authorization": "Basic " + base64.b64encode(b"u:p").decode()} bad = {"Authorization": "Basic " + base64.b64encode(b"u:x").decode()} assert c.get("/api/graph", headers=good).status_code == 200 assert c.get("/api/graph", headers=bad).status_code == 401 def test_spa_fallback(tmp_path): dist = tmp_path / "dist" (dist / "assets").mkdir(parents=True) (dist / "index.html").write_text("

viz

") (dist / "assets" / "a.js").write_text("1") c = create_app(make_settings(tmp_path, frontend_dist=dist), client=FakeClient()).test_client() assert b"viz" in c.get("/").data assert c.get("/assets/a.js").data == b"1" assert b"viz" in c.get("/some/route").data assert c.get("/api/unknown").status_code == 404