feat(админка): список клиентов v2
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
"""P2.6a: клиенты v2 — поиск/фильтры/пагинация, без N+1, deleted."""
|
||||
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")
|
||||
for email in ("joe@x", "ann@x", "old@x"):
|
||||
db.create_user(email, auth.hash_password("Passw0rd!ok"))
|
||||
db.admin_verify_user(email)
|
||||
db.save_message("joe@x", "t1", "user", "привет")
|
||||
db.add_account_entry("joe@x", 1000.0, "взнос", "boss@x", ts=0)
|
||||
db.create_user("ex@x", auth.hash_password("Passw0rd!ok"))
|
||||
db.admin_verify_user("ex@x")
|
||||
db.add_account_entry("ex@x", 1000.0, "взнос", "boss@x", ts=0)
|
||||
with db._conn() as c:
|
||||
c.execute("INSERT INTO llm_usage (ts, username, tab_id, path, provider, model, "
|
||||
"input_tokens, cache_tokens, output_tokens, cost_usd, cost_rub, price_rub) "
|
||||
"VALUES (1,'ex@x','t','agent','deepseek','m',1,0,1,0,0,1500)")
|
||||
db.block_user("ann@x", True)
|
||||
db.soft_delete_user("old@x")
|
||||
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_list_search_filters_and_page(env):
|
||||
db, client, h, auth = env
|
||||
r = client.get("/api/admin/v2/customers", headers=h)
|
||||
assert r.status_code == 200
|
||||
d = r.json()
|
||||
assert d["total"] == 4 and d["page"] == 1
|
||||
assert {x["email"] for x in d["items"]} == {"boss@x", "joe@x", "ann@x", "ex@x"}
|
||||
joe = next(x for x in d["items"] if x["email"] == "joe@x")
|
||||
assert joe["balance_rub"] == 1000.0 and joe["last_seen"] is not None
|
||||
assert "is_owner" in joe
|
||||
assert client.get("/api/admin/v2/customers?q=joe", headers=h).json()["total"] == 1
|
||||
assert [x["email"] for x in client.get("/api/admin/v2/customers?status=blocked",
|
||||
headers=h).json()["items"]] == ["ann@x"]
|
||||
page = client.get("/api/admin/v2/customers?page=2&per_page=1", headers=h).json()
|
||||
assert page["total"] == 4 and len(page["items"]) == 1
|
||||
assert client.get("/api/admin/v2/customers?sort=nope", headers=h).status_code == 400
|
||||
|
||||
|
||||
def test_deleted_needs_explicit_status(env):
|
||||
db, client, h, auth = env
|
||||
assert all(x["email"] != "old@x" for x in client.get(
|
||||
"/api/admin/v2/customers", headers=h).json()["items"])
|
||||
deleted = client.get("/api/admin/v2/customers?status=deleted", headers=h).json()
|
||||
assert [x["email"] for x in deleted["items"]] == ["old@x"]
|
||||
|
||||
|
||||
def test_customers_are_admin_only(env):
|
||||
db, client, h, auth = env
|
||||
user = {"Authorization": "Bearer " + auth._make_token("joe@x", "user")}
|
||||
assert client.get("/api/admin/v2/customers", headers=user).status_code == 403
|
||||
|
||||
|
||||
def test_exhausted_filter_and_accounts_failure(env, monkeypatch):
|
||||
db, client, h, auth = env
|
||||
rows = client.get("/api/admin/v2/customers?status=exhausted", headers=h).json()["items"]
|
||||
assert [x["email"] for x in rows] == ["ex@x"]
|
||||
|
||||
monkeypatch.setattr(db, "account_balances_all",
|
||||
lambda: (_ for _ in ()).throw(RuntimeError("down")))
|
||||
assert client.get("/api/admin/v2/customers?status=exhausted",
|
||||
headers=h).status_code == 503
|
||||
assert client.get("/api/admin/v2/customers", headers=h).status_code == 200
|
||||
Reference in New Issue
Block a user