feat(админка): платежи v2 с фильтрами

This commit is contained in:
anor
2026-11-06 22:42:18 +10:00
parent 72cba3600f
commit e0372e8b33
7 changed files with 192 additions and 3 deletions
+27
View File
@@ -18,6 +18,12 @@ MONEY = {"period_days": 30, "revenue_rub": 1000, "refunded_rub": 0, "cost_rub":
"accounts_count": 2, "unpriced": 0}
MONTHS = [{"month": "2026-09", "revenue_rub": 500, "cost_rub": 100},
{"month": "2026-10", "revenue_rub": 500, "cost_rub": 150}]
PAYMENTS = [
{"id": 1, "ts": 1793000000, "username": "anna@x", "product": "tariff1",
"amount_rub": "100.50", "yk_payment_id": "yk1", "status": "succeeded"},
{"id": 2, "ts": 1793000001, "username": "bob@x", "product": "tariff2",
"amount_rub": "200.00", "yk_payment_id": "yk2", "status": "pending"},
]
class Handler(http.server.SimpleHTTPRequestHandler):
@@ -32,6 +38,14 @@ 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/payments":
q = parse_qs(u.query)
items = PAYMENTS
if q.get("q"):
items = [r for r in items if q["q"][0] in r["username"] or q["q"][0] in r["yk_payment_id"]]
if q.get("status"):
items = [r for r in items if r["status"] == q["status"][0]]
return self._json({"items": items, "total": len(items), "page": 1, "per_page": 50})
if u.path == "/api/admin/v2/finance":
return self._json({"months": MONTHS, "days": int(parse_qs(u.query).get("days", ["30"])[0])})
if self.path == "/admin/v2" or self.path.startswith("/admin/v2/"):
@@ -92,3 +106,16 @@ def test_money_empty_months(browser, site):
page.wait_for_selector(".note")
assert page.locator(".chart").count() == 0
page.close()
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.close()
+59
View File
@@ -0,0 +1,59 @@
"""P3.3: платежи v2 — период/поиск/статус/пагинация, только админ."""
import importlib
import os
import sys
import time
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")
db.create_user("joe@x", auth.hash_password("Passw0rd!ok"))
db.admin_verify_user("joe@x")
now = time.time()
with db._conn() as c:
for i, st in enumerate(("succeeded", "pending", "refunded")):
c.execute("INSERT INTO payments (ts, username, product, amount_rub, yk_payment_id, "
"status, updated_at) VALUES (?,?,?,?,?,?,?)",
(now - i, "joe@x", "tariff" + str(i), "100.50", "yk" + str(i), st, now))
c.execute("INSERT INTO payments (ts, username, product, amount_rub, yk_payment_id, "
"status, updated_at) VALUES (?,?,?,?,?,?,?)",
(now - 40 * 86400, "old@x", "old", "1.00", "old", "succeeded", now))
from fastapi.testclient import TestClient
client = TestClient(main.app)
h = {"Authorization": "Bearer " + auth._make_token("boss@x", "admin")}
return client, h, auth
def test_payments_filters_and_page(env):
client, h, auth = env
d = client.get("/api/admin/v2/payments?days=30", headers=h).json()
assert d["total"] == 3 and len(d["items"]) == 3
assert client.get("/api/admin/v2/payments?days=3650", headers=h).json()["total"] == 4
assert client.get("/api/admin/v2/payments?status=pending", headers=h).json()["total"] == 1
assert client.get("/api/admin/v2/payments?q=yk1", headers=h).json()["total"] == 1
p2 = client.get("/api/admin/v2/payments?page=2&per_page=1", headers=h).json()
assert p2["page"] == 2 and len(p2["items"]) == 1
def test_payments_admin_only(env):
client, h, auth = env
user = {"Authorization": "Bearer " + auth._make_token("joe@x", "user")}
assert client.get("/api/admin/v2/payments", headers=user).status_code == 403