feat(админка): admin_gate на копии
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
"""admin_gate: инварианты на копии — зелёный на чистой, красный на подмене."""
|
||||
import importlib
|
||||
import os
|
||||
import sqlite3
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(ROOT))
|
||||
sys.path.insert(0, str(ROOT / "scripts"))
|
||||
import admin_gate # noqa: E402
|
||||
|
||||
|
||||
def _db(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("DB_PATH", str(tmp_path / "t.db"))
|
||||
monkeypatch.setenv("SESSION_SECRET", "test-secret")
|
||||
monkeypatch.chdir(ROOT)
|
||||
import auth
|
||||
import db
|
||||
for m in (auth, db):
|
||||
importlib.reload(m)
|
||||
db.init_db()
|
||||
return tmp_path / "t.db"
|
||||
|
||||
|
||||
def _html(tmp_path, body="<html><body>ok</body></html>"):
|
||||
p = tmp_path / "admin.html"
|
||||
p.write_text(body, encoding="utf-8")
|
||||
return p
|
||||
|
||||
|
||||
def test_clean_db_and_html_pass(tmp_path, monkeypatch):
|
||||
db = _db(tmp_path, monkeypatch)
|
||||
r = admin_gate.run(db, _html(tmp_path))
|
||||
assert r["ok"] is True, r
|
||||
|
||||
|
||||
def test_duplicate_payment_key_fails(tmp_path, monkeypatch):
|
||||
# Настоящая схема запрещает дубль UNIQUE-индексом; gate страхует старые копии,
|
||||
# поэтому проверяем на минимальной схеме без индекса.
|
||||
db = tmp_path / "legacy.db"
|
||||
with sqlite3.connect(db) as c:
|
||||
c.executescript("""
|
||||
CREATE TABLE payments (id INTEGER PRIMARY KEY, amount_rub TEXT, yk_payment_id TEXT,
|
||||
status TEXT, username TEXT, ts REAL);
|
||||
CREATE TABLE llm_usage (username TEXT, ts REAL, output_tokens INTEGER,
|
||||
price_rub REAL, cost_rub REAL, input_tokens INTEGER,
|
||||
cache_tokens INTEGER);
|
||||
CREATE TABLE audit_log (username TEXT, action TEXT);
|
||||
CREATE TABLE account_entries (id INTEGER PRIMARY KEY, amount_kop INTEGER,
|
||||
reverses INTEGER, ext_ref TEXT);
|
||||
""")
|
||||
for _ in range(2):
|
||||
c.execute("INSERT INTO payments (amount_rub, yk_payment_id, status, username, ts) "
|
||||
"VALUES ('100.00','same-key','succeeded','a@x',1)")
|
||||
r = admin_gate.run(db, _html(tmp_path))
|
||||
assert r["ok"] is False
|
||||
assert any("same-key" in e for e in r["errors"])
|
||||
|
||||
|
||||
def test_bad_amount_and_bad_html_fail(tmp_path, monkeypatch):
|
||||
db = _db(tmp_path, monkeypatch)
|
||||
with sqlite3.connect(db) as c:
|
||||
c.execute("INSERT INTO payments (ts, username, product, amount_rub, "
|
||||
"yk_payment_id, status, updated_at) VALUES (?,?,?,?,?,?,?)",
|
||||
(1, "a@x", "p", "abc", "k", "succeeded", 1))
|
||||
r = admin_gate.run(db, _html(tmp_path, "<script>eval('x')</script>"))
|
||||
assert r["ok"] is False
|
||||
assert any("abc" in e for e in r["errors"])
|
||||
assert any("eval(" in e for e in r["errors"])
|
||||
|
||||
|
||||
def test_unregistered_usage_or_payment_fails(tmp_path, monkeypatch):
|
||||
db = _db(tmp_path, monkeypatch)
|
||||
with sqlite3.connect(db) as c:
|
||||
c.execute("INSERT INTO llm_usage (ts, username, path, output_tokens, price_rub) "
|
||||
"VALUES (1, 'ghost@x', 'chat', 5, 1.0)")
|
||||
r = admin_gate.run(db, _html(tmp_path))
|
||||
assert r["ok"] is False
|
||||
assert any("без регистрации" in e for e in r["errors"])
|
||||
|
||||
|
||||
def test_bad_reversal_and_negative_payment_fail(tmp_path, monkeypatch):
|
||||
db = _db(tmp_path, monkeypatch)
|
||||
with sqlite3.connect(db) as c:
|
||||
c.execute("INSERT INTO account_entries (ts, username, amount_kop, created_by, reverses) "
|
||||
"VALUES (1,'a@x',1000,'boss@x',NULL)")
|
||||
c.execute("INSERT INTO account_entries (ts, username, amount_kop, created_by, reverses) "
|
||||
"VALUES (1,'a@x',-1000,'boss@x',999)")
|
||||
c.execute("INSERT INTO payments (ts, username, product, amount_rub, yk_payment_id, "
|
||||
"status, updated_at) VALUES (1,'a@x','p','-5.00','neg','succeeded',1)")
|
||||
r = admin_gate.run(db, _html(tmp_path))
|
||||
assert r["ok"] is False
|
||||
assert any("несуществующий" in e for e in r["errors"])
|
||||
assert any("неположительную" in e for e in r["errors"])
|
||||
|
||||
|
||||
def test_unreadable_db_is_code_2(tmp_path, monkeypatch):
|
||||
db = tmp_path / "empty.db"
|
||||
with sqlite3.connect(db) as c:
|
||||
c.execute("CREATE TABLE users (id INTEGER)")
|
||||
r = admin_gate.run(db, _html(tmp_path))
|
||||
assert r["ok"] is False and r.get("code") == 2
|
||||
Reference in New Issue
Block a user