47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
"""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
|