feat(админка): сессии и сброс 2FA
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
"""P4.5c: CLI-сброс 2FA только с --yes и с отзывом сессий."""
|
||||
import importlib
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
def test_cli_reset_requires_yes(tmp_path):
|
||||
env = {**os.environ, "DB_PATH": str(tmp_path / "t.db"), "SESSION_SECRET": "s"}
|
||||
sys.path.insert(0, str(ROOT))
|
||||
import auth
|
||||
import db
|
||||
for m in (auth, db):
|
||||
importlib.reload(m)
|
||||
db.DB_PATH = Path(env["DB_PATH"])
|
||||
db.init_db()
|
||||
db.create_user("boss42@x", auth.hash_password("p"), role="admin")
|
||||
db.admin_verify_user("boss42@x")
|
||||
secret = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"
|
||||
db.set_totp_secret("boss42@x", secret)
|
||||
db.enable_totp("boss42@x", "[]")
|
||||
r = subprocess.run([sys.executable, str(ROOT / "scripts/admin_2fa_reset.py"), "boss42@x"],
|
||||
env=env, capture_output=True, text=True)
|
||||
assert r.returncode == 1 and "--yes" in r.stdout
|
||||
assert db.get_user_by_email("boss42@x")["totp_enabled"] == 1
|
||||
r = subprocess.run([sys.executable, str(ROOT / "scripts/admin_2fa_reset.py"), "boss42@x", "--yes"],
|
||||
env=env, capture_output=True, text=True)
|
||||
assert r.returncode == 0, r.stderr
|
||||
assert db.get_user_by_email("boss42@x")["totp_enabled"] == 0
|
||||
@@ -28,6 +28,10 @@ class Handler(http.server.SimpleHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
if self.path.startswith("/api/admin/v2/system"):
|
||||
return self._json(SYS)
|
||||
if self.path.startswith("/api/admin/v2/sessions"):
|
||||
return self._json({"items": [{"jti": "j1", "created_at": 1793000000,
|
||||
"ip": "127.0.0.1", "user_agent": "test",
|
||||
"current": True, "active": True}]})
|
||||
if self.path == "/admin/v2" or self.path.startswith("/admin/v2/"):
|
||||
data = (ROOT / "static/admin2/index.html").read_bytes()
|
||||
self.send_response(200); self.send_header("Content-Type", "text/html; charset=utf-8")
|
||||
@@ -67,5 +71,6 @@ def test_system_page(browser, site):
|
||||
assert "smtp" in page.locator("main").inner_text()
|
||||
assert "agent_killswitch" in page.locator("main").inner_text()
|
||||
assert "переключение слоя" in page.locator("main").inner_text()
|
||||
assert "127.0.0.1" in page.locator("main").inner_text()
|
||||
assert errors == []
|
||||
page.close()
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
"""P4.5c: список сессий/отзыв/выйти на всех."""
|
||||
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 (auth, db, 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")
|
||||
from fastapi.testclient import TestClient
|
||||
client = TestClient(main.app)
|
||||
login = client.post("/api/login", json={"username": "boss@x", "password": "Passw0rd!ok"}).json()
|
||||
h = {"Authorization": "Bearer " + login["token"]}
|
||||
return db, client, h, login
|
||||
|
||||
|
||||
def test_list_and_revoke_current(env):
|
||||
db, client, h, login = env
|
||||
items = client.get("/api/admin/v2/sessions", headers=h).json()["items"]
|
||||
assert len(items) == 1 and items[0]["current"] is True and items[0]["active"] is True
|
||||
jti = items[0]["jti"]
|
||||
assert client.post(f"/api/admin/v2/sessions/{jti}/revoke", headers=h).status_code == 200
|
||||
assert client.get("/api/admin/v2/ping", headers=h).status_code == 401
|
||||
|
||||
|
||||
def test_revoke_all(env):
|
||||
db, client, h, login = env
|
||||
assert client.post("/api/admin/v2/sessions/revoke-all", headers=h).json()["revoked"] >= 1
|
||||
assert client.get("/api/admin/v2/ping", headers=h).status_code == 401
|
||||
Reference in New Issue
Block a user