Files
smeta-web/tests/test_system_api.py
T

52 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""P4.4: система — каналы/настройки/история, только админ, кэш 60 с."""
import importlib
import os
import sys
import pytest
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if ROOT not in sys.path:
sys.path.insert(0, ROOT)
@pytest.fixture()
def env(tmp_path, monkeypatch):
monkeypatch.setenv("DB_PATH", str(tmp_path / "t.db"))
monkeypatch.setenv("SESSION_SECRET", "test-secret")
monkeypatch.setenv("AI_BACKEND", "claude")
monkeypatch.chdir(ROOT)
import auth
import db
import main
for m in (db, auth, main):
importlib.reload(m)
db.init_db()
db.create_user("boss@x", auth.hash_password("Passw0rd!ok"), role="admin")
db.admin_verify_user("boss@x")
db.create_user("joe@x", auth.hash_password("Passw0rd!ok"))
db.admin_verify_user("joe@x")
db.set_app_setting("agent_killswitch", "off")
db.log_action("boss@x", "layer_switch", "POST", "/api/admin/layers", "x=on")
from fastapi.testclient import TestClient
client = TestClient(main.app)
h = {"Authorization": "Bearer " + auth._make_token("boss@x", "admin")}
return db, client, h, auth
def test_system_snapshot(env, monkeypatch):
db, client, h, auth = env
import health_checks
monkeypatch.setattr(health_checks, "run_all",
lambda: [health_checks.Check("database", True, "ок")])
d = client.get("/api/admin/v2/system", headers=h).json()
assert d["channels"][0]["name"] == "database"
assert any(x["key"] == "agent_killswitch" for x in d["settings"])
assert d["history"][0]["label"] == "переключение слоя"
def test_system_admin_only(env):
db, client, h, auth = env
user = {"Authorization": "Bearer " + auth._make_token("joe@x", "user")}
assert client.get("/api/admin/v2/system", headers=user).status_code == 403