init commit

This commit is contained in:
Udo
2026-12-01 10:02:27 +05:00
commit 200083e3ef
36 changed files with 5611 additions and 0 deletions
+138
View File
@@ -0,0 +1,138 @@
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_profile
KEY = "aaaaaaaa-0000-0000-0000-000000000001"
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")
other = {"uuid": "bbbbbbbb-0000-0000-0000-000000000002", "name": "Default",
"config": {"inbounds": [], "outbounds": [], "routing": {"rules": []}}, "inbounds": []}
return {"profiles": [other, make_profile()], "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_default_profile_is_the_one_with_vless_route(client_and_fake):
c, _ = client_and_fake
r = c.get("/api/graph")
assert r.status_code == 200
body = r.get_json()
assert body["profile"]["name"] == "BridgeOverNetbird"
assert body["graph"]["warnings"]
assert "SECRET" not in r.get_data(as_text=True)
def test_profile_by_name_and_unknown(client_and_fake):
c, _ = client_and_fake
assert c.get("/api/graph?profile=default").get_json()["profile"]["name"] == "Default"
r = c.get("/api/graph?profile=nope")
assert r.status_code == 404 and r.get_json()["error"] == "profile_not_found"
def test_profiles_list(client_and_fake):
c, _ = client_and_fake
names = [p["name"] for p in c.get("/api/profiles").get_json()["profiles"]]
assert names == ["Default", "BridgeOverNetbird"]
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("<h1>viz</h1>")
(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