feat(админка): счета клиентов и CSV
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
"""P3.5: счета v2 — итог, статусы, CSV сходится."""
|
||||
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, price in (("ok@x", 1), ("low@x", 9.5), ("zero@x", 1500)):
|
||||
db.create_user(email, auth.hash_password("Passw0rd!ok"))
|
||||
db.admin_verify_user(email)
|
||||
db.add_account_entry(email, 10.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,?,'t','agent','deepseek','m',1,0,1,0,0,?)", (email, price))
|
||||
from fastapi.testclient import TestClient
|
||||
client = TestClient(main.app)
|
||||
h = {"Authorization": "Bearer " + auth._make_token("boss@x", "admin")}
|
||||
return client, h, auth
|
||||
|
||||
|
||||
def test_accounts_total_status_and_csv(env):
|
||||
client, h, auth = env
|
||||
d = client.get("/api/admin/v2/accounts", headers=h).json()
|
||||
assert d["total"] == 3
|
||||
assert d["total_kop"] == (900 + 50 + -149000)
|
||||
assert client.get("/api/admin/v2/accounts?status=zero", headers=h).json()["total"] == 0
|
||||
assert client.get("/api/admin/v2/accounts?status=exhausted", headers=h).json()["total"] == 1
|
||||
assert client.get("/api/admin/v2/accounts?status=low", headers=h).json()["total"] == 1
|
||||
csv = client.get("/api/admin/v2/accounts.csv", headers=h)
|
||||
assert csv.status_code == 200
|
||||
lines = [x for x in csv.text.splitlines() if x]
|
||||
assert len(lines) == 4 # header + 3
|
||||
assert "zero@x" in csv.text and "-1490.00" in csv.text
|
||||
|
||||
|
||||
def test_accounts_admin_only(env):
|
||||
client, h, auth = env
|
||||
user = {"Authorization": "Bearer " + auth._make_token("ok@x", "user")}
|
||||
assert client.get("/api/admin/v2/accounts", headers=user).status_code == 403
|
||||
assert client.get("/api/admin/v2/accounts.csv", headers=user).status_code == 403
|
||||
@@ -38,6 +38,18 @@ class Handler(http.server.SimpleHTTPRequestHandler):
|
||||
if u.path == "/api/admin/v2/money":
|
||||
Handler.calls.append(u.query)
|
||||
return self._json({**MONEY, "period_days": int(parse_qs(u.query).get("days", ["30"])[0])})
|
||||
if u.path == "/api/admin/v2/accounts":
|
||||
return self._json({"items": [
|
||||
{"email": "anna@x", "balance_rub": 100.0, "deposited_rub": 200.0,
|
||||
"charged_rub": 100.0, "status": "ok"},
|
||||
{"email": "bob@x", "balance_rub": 0.0, "deposited_rub": 100.0,
|
||||
"charged_rub": 100.0, "status": "exhausted"}],
|
||||
"total": 2, "total_kop": 10000, "total_rub": 100.0})
|
||||
if u.path == "/api/admin/v2/accounts.csv":
|
||||
body = "email;balance_rub\nanna@x;100.00\n".encode()
|
||||
self.send_response(200); self.send_header("Content-Type", "text/csv")
|
||||
self.send_header("Content-Length", str(len(body))); self.end_headers()
|
||||
self.wfile.write(body); return
|
||||
if u.path == "/api/admin/v2/payments":
|
||||
q = parse_qs(u.query)
|
||||
items = PAYMENTS
|
||||
@@ -112,10 +124,23 @@ def test_payments_table_and_filter(browser, site):
|
||||
page = browser.new_page()
|
||||
page.add_init_script("localStorage.setItem('token','t')")
|
||||
page.goto(site + "/admin/v2/money")
|
||||
page.wait_for_selector("table tbody tr")
|
||||
assert page.locator("table tbody tr").count() == 2
|
||||
assert "yk1" in page.locator("table").inner_text()
|
||||
page.fill("input[type=search]", "bob")
|
||||
page.wait_for_function("document.querySelectorAll('table tbody tr').length === 1")
|
||||
assert "bob@x" in page.locator("table").inner_text()
|
||||
page.wait_for_selector(".payments table tbody tr")
|
||||
assert page.locator(".payments table tbody tr").count() == 2
|
||||
assert "yk1" in page.locator(".payments table").inner_text()
|
||||
page.fill(".payments input[type=search]", "bob")
|
||||
page.wait_for_function("document.querySelectorAll('.payments table tbody tr').length === 1")
|
||||
assert page.locator(".payments table tbody tr").count() == 1
|
||||
assert "bob@x" in page.locator(".payments table").inner_text()
|
||||
page.close()
|
||||
|
||||
|
||||
def test_accounts_table_and_csv(browser, site):
|
||||
page = browser.new_page()
|
||||
page.add_init_script("localStorage.setItem('token','t')")
|
||||
page.goto(site + "/admin/v2/money")
|
||||
page.wait_for_selector(".accounts table")
|
||||
assert "anna@x" in page.locator(".accounts table").inner_text()
|
||||
with page.expect_download() as dl:
|
||||
page.locator("button", has_text="CSV").click()
|
||||
assert dl.value.suggested_filename == "accounts.csv"
|
||||
page.close()
|
||||
|
||||
Reference in New Issue
Block a user