Files
smeta-web/tests/test_admin_money_labels.py
T

62 lines
2.5 KiB
Python

"""Деньги в админке: «денежный поток» — не «прибыль», счета — обязательство.
План P1.3: старый экран не должен называть поступления минус себестоимость прибылью,
пока на счетах клиентов лежат их деньги. Итог по счетам — отдельной строкой.
"""
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)
ADMIN_HTML = os.path.join(ROOT, "static", "admin.html")
def test_labels_say_cash_flow_not_profit():
html = open(ADMIN_HTML, encoding="utf-8").read()
assert "Денежный поток" in html
assert "Прибыль, ₽" not in html
assert "без пополнений" in html
assert "обязательство, не прибыль" in html
@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", 1000.0, "взнос", "boss@x")
# Служебный счёт не должен попасть в «на счетах клиентов».
db.create_user("canary@local", auth.hash_password("Passw0rd!ok"))
db.add_account_entry("canary@local", 500.0, "служебный", "boss@x")
from fastapi.testclient import TestClient
client = TestClient(main.app)
h = {"Authorization": "Bearer " + auth._make_token("boss@x", "admin")}
return db, client, h
def test_finance_separates_flow_and_client_accounts(env):
db, client, h = env
r = client.get("/api/admin/finance?days=180", headers=h)
assert r.status_code == 200, r.text
t = r.json()["totals"]
assert t["flow_rub"] == t["profit_rub"] # старый ключ не сломан
assert t["accounts_total_rub"] == pytest.approx(1000.0)
assert t["accounts_count"] == 1
assert t["topups_included"] is False
assert "flow_share" in t