feat(админка): выгрузка данных клиента
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
"""P4.7a: выгрузка клиента — профиль/сообщения/файлы/счёт/аудит."""
|
||||
import importlib
|
||||
import io
|
||||
import os
|
||||
import sys
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def env(tmp_path, monkeypatch):
|
||||
data = tmp_path / "data"; data.mkdir()
|
||||
monkeypatch.setenv("DB_PATH", str(tmp_path / "t.db"))
|
||||
monkeypatch.setenv("DATA_DIR", str(data))
|
||||
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.save_message("joe@x", "t1", "user", "секрет")
|
||||
db.add_account_entry("joe@x", 10.0, "взнос", "boss@x")
|
||||
(data / "joe@x").mkdir()
|
||||
(data / "joe@x" / "a.txt").write_text("hello", encoding="utf-8")
|
||||
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_export_contains_all_parts(env):
|
||||
db, client, h, auth = env
|
||||
r = client.get("/api/admin/v2/customers/joe@x/export", headers=h)
|
||||
assert r.status_code == 200
|
||||
assert r.headers["content-type"].startswith("application/zip")
|
||||
z = zipfile.ZipFile(io.BytesIO(r.content))
|
||||
names = set(z.namelist())
|
||||
assert {"profile.json", "messages.json", "ledger.json", "audit.json",
|
||||
"files/a.txt"} <= names
|
||||
assert "секрет" in z.read("messages.json").decode()
|
||||
assert any(r["action"] == "customer_export" for r in db.get_audit_log(10))
|
||||
|
||||
|
||||
def test_export_404_and_admin_only(env):
|
||||
db, client, h, auth = env
|
||||
assert client.get("/api/admin/v2/customers/ghost@x/export", headers=h).status_code == 404
|
||||
user = {"Authorization": "Bearer " + auth._make_token("joe@x", "user")}
|
||||
assert client.get("/api/admin/v2/customers/joe@x/export", headers=user).status_code == 403
|
||||
Reference in New Issue
Block a user