91 lines
3.9 KiB
Python
91 lines
3.9 KiB
Python
"""P5.1: дымовой e2e API — старый экран и все разделы v2 отвечают."""
|
|
import importlib
|
|
import os
|
|
import sys
|
|
from urllib.parse import quote
|
|
|
|
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")
|
|
import health_checks
|
|
monkeypatch.setattr(health_checks, "run_all", lambda: [health_checks.Check("database", True, "ок")])
|
|
from fastapi.testclient import TestClient
|
|
client = TestClient(main.app)
|
|
token = client.post("/api/login", json={"username": "boss@x",
|
|
"password": "Passw0rd!ok"}).json()["token"]
|
|
return client, {"Authorization": "Bearer " + token}, db
|
|
|
|
|
|
def test_legacy_and_v2_sections(env):
|
|
client, h, db = env
|
|
assert client.get("/admin").status_code == 200
|
|
r = client.get("/admin/v2", headers=h)
|
|
assert r.status_code == 200 and "unsafe-inline" not in r.headers.get("content-security-policy", "")
|
|
for path in ("/api/admin/v2/ping", "/api/admin/v2/dashboard", "/api/admin/v2/attention",
|
|
"/api/admin/v2/customers", "/api/admin/v2/money", "/api/admin/v2/payments",
|
|
"/api/admin/v2/accounts", "/api/admin/v2/feedback", "/api/admin/v2/system",
|
|
"/api/admin/v2/sessions", "/api/admin/v2/work/dialogs"):
|
|
assert client.get(path, headers=h).status_code == 200, path
|
|
|
|
|
|
def test_admin2_static_revalidates(env):
|
|
"""Модули админки v2 обязаны перепроверяться: без no-cache браузер держал
|
|
старые файлы по своей эвристике, и правки не появлялись после выката.
|
|
Мутация: убрать заголовок — тест упадёт."""
|
|
client, h, db = env
|
|
r = client.get("/static/admin2/app.js")
|
|
assert r.status_code == 200
|
|
assert "no-cache" in r.headers.get("cache-control", "")
|
|
|
|
|
|
def test_customer_lifecycle(env):
|
|
client, h, db = env
|
|
import auth
|
|
db.create_user("e2e@x", auth.hash_password("Passw0rd!ok"))
|
|
db.admin_verify_user("e2e@x")
|
|
r = client.post("/api/admin/v2/customers/e2e@x/action",
|
|
json={"action": "block", "value": {"blocked": True}}, headers=h)
|
|
assert r.status_code == 200
|
|
assert db.get_user_by_email("e2e@x")["blocked"] == 1
|
|
assert client.post(f"/api/admin/v2/actions/{r.json()['id']}/undo", headers=h).status_code == 200
|
|
assert db.get_user_by_email("e2e@x")["blocked"] == 0
|
|
|
|
t = client.post("/api/admin/v2/customers/e2e@x/topup",
|
|
json={"amount": "50", "request_id": "e2e-1"}, headers=h)
|
|
assert t.status_code == 200
|
|
assert db.account_summary("e2e@x")["deposited_kop"] == 5000
|
|
|
|
d = client.post("/api/admin/v2/customers/e2e@x/delete", headers=h)
|
|
assert d.status_code == 200
|
|
archived = d.json()["archived_as"]
|
|
assert client.post("/api/admin/v2/customers/" + quote(archived, safe="") + "/restore", headers=h).status_code == 200
|
|
|
|
|
|
def test_feedback_lifecycle(env):
|
|
client, h, db = env
|
|
db.save_feedback("e2e@x", "t1", "не то", "a", category="off_topic", source="chat")
|
|
items = client.get("/api/admin/v2/feedback?status=new", headers=h).json()["items"]
|
|
assert items
|
|
fid = items[0]["id"]
|
|
assert client.post(f"/api/admin/v2/feedback/{fid}/status",
|
|
json={"status": "resolved"}, headers=h).status_code == 200
|
|
assert client.get("/api/admin/v2/feedback?status=new", headers=h).json()["total"] == 0
|