Files
smeta-web/tests/test_system_api.py

84 lines
3.4 KiB
Python
Raw Permalink 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
def test_system_fast_path_does_not_probe_channels(env, monkeypatch):
"""Страница «Система» просит быструю часть без каналов: живая проверка SMTP и
провайдеров идёт секундами и не должна задерживать настройки и историю.
Мутация: вернуть проверку каналов в общий ответ — тест упадёт на AssertionError."""
db, client, h, auth = env
import admin_api
import system_admin
admin_api._SYS.update(ts=0.0, data=None)
admin_api._SYS_CH.update(ts=0.0, data=None)
def boom():
raise AssertionError("каналы не должны проверяться на быстром пути")
monkeypatch.setattr(system_admin, "channels", boom)
d = client.get("/api/admin/v2/system?channels=0", headers=h).json()
assert "channels" not in d
assert any(x["key"] == "agent_killswitch" for x in d["settings"])
def test_system_channels_endpoint(env, monkeypatch):
db, client, h, auth = env
import admin_api
import health_checks
admin_api._SYS_CH.update(ts=0.0, data=None)
monkeypatch.setattr(health_checks, "run_all",
lambda: [health_checks.Check("database", True, "ок")])
d = client.get("/api/admin/v2/system/channels", headers=h).json()
assert d["channels"][0]["name"] == "database"
user = {"Authorization": "Bearer " + auth._make_token("joe@x", "user")}
assert client.get("/api/admin/v2/system/channels", headers=user).status_code == 403