feat(админка): ручной взнос v2 с идемпотентностью

This commit is contained in:
anor
2026-11-06 22:18:45 +10:00
parent 19834cd1dc
commit 99b62fb5fb
6 changed files with 207 additions and 0 deletions
+17
View File
@@ -242,3 +242,20 @@ def test_delete_from_drawer_and_undo(browser, site):
paths = [a["path"] for a in Handler.actions]
assert any("/delete" in p for p in paths) and any("/restore" in p for p in paths)
page.close()
def test_topup_from_drawer(browser, site):
Handler.actions = []; Handler.blocked = False; Handler.fail_action = None; Handler.fail_card = False
page = browser.new_page()
page.add_init_script("localStorage.setItem('token','t')")
page.goto(site + "/admin/v2/customers")
page.locator("tr.clickable").first.click()
page.wait_for_selector(".drawer .kv")
page.on("dialog", lambda d: d.accept("500"))
page.locator(".actions-bar button", has_text="Внести").click()
page.wait_for_selector(".toast")
assert "Взнос записан" in page.locator(".toast").inner_text()
assert any("/customers/anna%40x/topup" in a["path"] for a in Handler.actions)
sent = next(a["body"] for a in Handler.actions if "/topup" in a["path"])
assert '"amount":"500"' in sent
page.close()
+84
View File
@@ -0,0 +1,84 @@
"""P2.9: ручной взнос через v2, идемпотентность по request_id."""
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")
db.create_user("joe@x", auth.hash_password("Passw0rd!ok"))
db.admin_verify_user("joe@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_topup_and_idempotency(env):
db, client, h, auth = env
body = {"amount": "100", "comment": "взнос", "request_id": "r1"}
r = client.post("/api/admin/v2/customers/joe@x/topup", json=body, headers=h)
assert r.status_code == 200, r.text
entry = r.json()["entry"]["id"]
assert db.account_summary("joe@x")["deposited_kop"] == 10000
again = client.post("/api/admin/v2/customers/joe@x/topup", json=body, headers=h)
assert again.status_code == 200 and again.json()["entry"]["id"] == entry
assert db.account_summary("joe@x")["deposited_kop"] == 10000
other = client.post("/api/admin/v2/customers/joe@x/topup",
json={**body, "request_id": "r2"}, headers=h)
assert other.status_code == 200
assert db.account_summary("joe@x")["deposited_kop"] == 20000
def test_topup_404_403(env):
db, client, h, auth = env
assert client.post("/api/admin/v2/customers/ghost@x/topup",
json={"amount": "10"}, headers=h).status_code == 404
user = {"Authorization": "Bearer " + auth._make_token("joe@x", "user")}
assert client.post("/api/admin/v2/customers/joe@x/topup",
json={"amount": "10"}, headers=user).status_code == 403
def test_case_normalization_negative_owner_and_stale_claim(env):
db, client, h, auth = env
r = client.post("/api/admin/v2/customers/JOE@X/topup",
json={"amount": "10", "request_id": "case"}, headers=h)
assert r.status_code == 200
assert db.account_summary("joe@x")["deposited_kop"] == 1000
assert db.list_account_entries("joe@x")
assert client.post("/api/admin/v2/customers/joe@x/topup",
json={"amount": "-100"}, headers=h).status_code == 400
db.create_user("canary@local", auth.hash_password("Passw0rd!ok"))
assert client.post("/api/admin/v2/customers/canary@local/topup",
json={"amount": "10"}, headers=h).status_code == 400
db.claim_idempotency("topup:joe@x:busy", "topup")
assert client.post("/api/admin/v2/customers/joe@x/topup",
json={"amount": "10", "request_id": "busy"}, headers=h).status_code == 409
def test_deleted_customer_topup_404(env):
db, client, h, auth = env
db.soft_delete_user("joe@x")
assert client.post("/api/admin/v2/customers/joe@x/topup",
json={"amount": "10"}, headers=h).status_code == 404