Files
smeta-web/tests/test_money_api.py
T

80 lines
3.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""P3.1b: один период — один итог в копейках; /api/admin/v2/money."""
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")
db.add_account_entry("joe@x", 10.0, "взнос", "boss@x")
import time as _time
now = _time.time()
with db._conn() as c:
c.execute("INSERT INTO payments (ts, username, product, amount_rub, yk_payment_id, "
"status, updated_at) VALUES (?,?,?,?,?,?,?)",
(now, "joe@x", "p", "10.00", "m1", "succeeded", now))
db.record_llm_usage("joe@x", "t", "agent", "deepseek", "m", 1, 0, 1, 0,
cost_rub=2.5, price_rub=0, ts=now)
from fastapi.testclient import TestClient
client = TestClient(main.app)
h = {"Authorization": "Bearer " + auth._make_token("boss@x", "admin")}
return db, client, h
def test_money_kopecks_and_period(env):
db, client, h = env
r = client.get("/api/admin/v2/money?days=30", headers=h)
assert r.status_code == 200
d = r.json()
assert d["period_days"] == 30
assert d["accounts_total_kop"] == 1000
assert d["revenue_kop"] == 1000
assert d["cost_kop"] == 250
assert d["flow_kop"] == 750
assert isinstance(d["revenue_kop"], int)
assert isinstance(d["flow_kop"], int) or d["flow_kop"] is None
assert d["flow_kop"] == d["revenue_kop"] - d["cost_kop"]
assert client.get("/api/admin/v2/money?days=0", headers=h).json()["period_days"] == 1
assert client.get("/api/admin/v2/money?days=9999", headers=h).json()["period_days"] == 366
def test_money_is_admin_only(env):
db, client, h = env
import auth
user = {"Authorization": "Bearer " + auth._make_token("joe@x", "user")}
assert client.get("/api/admin/v2/money", headers=user).status_code == 403
def test_monthly_rounding_happens_once(env):
db, client, h = env
import time as _time
now = _time.time()
with db._conn() as c:
for ts in (now, now - 40 * 86400):
c.execute("INSERT INTO payments (ts, username, product, amount_rub, yk_payment_id, "
"status, updated_at) VALUES (?,?,?,?,?,?,?)",
(ts, "joe@x", "p", "0.125", "m-" + str(ts), "succeeded", ts))
d = client.get("/api/admin/v2/money?days=60", headers=h).json()
# 10,00 + 0,125 + 0,125 = 10,25 → 1025, а не 10,26 от помесячного округления
assert d["revenue_kop"] == 1025