Files
smeta-web/tests/test_topup_v2_api.py
T

85 lines
3.4 KiB
Python

"""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