108 lines
4.6 KiB
Python
108 lines
4.6 KiB
Python
"""P2.8: обратимая блокировка/роль/пределы, undo идемпотентен."""
|
|
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_block_and_undo(env):
|
|
db, client, h, auth = env
|
|
r = client.post("/api/admin/v2/customers/joe@x/action",
|
|
json={"action": "block", "value": {"blocked": True}}, headers=h)
|
|
assert r.status_code == 200, r.text
|
|
aid = r.json()["id"]
|
|
assert db.get_user_by_email("joe@x")["blocked"] == 1
|
|
u = client.post(f"/api/admin/v2/actions/{aid}/undo", headers=h)
|
|
assert u.status_code == 200
|
|
assert db.get_user_by_email("joe@x")["blocked"] == 0
|
|
assert client.post(f"/api/admin/v2/actions/{aid}/undo", headers=h).json()["already"] is True
|
|
|
|
|
|
def test_role_and_limits_roundtrip(env):
|
|
db, client, h, auth = env
|
|
r = client.post("/api/admin/v2/customers/joe@x/action",
|
|
json={"action": "role", "value": {"role": "admin"}}, headers=h).json()
|
|
assert db.get_user_by_email("joe@x")["role"] == "admin"
|
|
client.post(f"/api/admin/v2/actions/{r['id']}/undo", headers=h)
|
|
assert db.get_user_by_email("joe@x")["role"] == "user"
|
|
|
|
r = client.post("/api/admin/v2/customers/joe@x/action",
|
|
json={"action": "limits", "value": {"rub": 10}}, headers=h).json()
|
|
assert db.get_user_spend_limits("joe@x")["rub"] == 10
|
|
client.post(f"/api/admin/v2/actions/{r['id']}/undo", headers=h)
|
|
assert db.get_user_spend_limits("joe@x")["rub"] is None
|
|
|
|
|
|
def test_bad_action_and_permissions(env):
|
|
db, client, h, auth = env
|
|
assert client.post("/api/admin/v2/customers/joe@x/action",
|
|
json={"action": "nope", "value": {}}, headers=h).status_code == 400
|
|
assert client.post("/api/admin/v2/customers/joe@x/action",
|
|
json={"action": "role", "value": {"role": "root"}},
|
|
headers=h).status_code == 400
|
|
user = {"Authorization": "Bearer " + auth._make_token("joe@x", "user")}
|
|
assert client.post("/api/admin/v2/actions/1/undo", headers=user).status_code == 403
|
|
|
|
|
|
def test_bool_strings_and_bad_json_are_refused(env):
|
|
db, client, h, auth = env
|
|
assert client.post("/api/admin/v2/customers/joe@x/action",
|
|
json={"action": "block", "value": {"blocked": "false"}},
|
|
headers=h).status_code == 400
|
|
assert client.post("/api/admin/v2/customers/joe@x/action",
|
|
data="{oops", headers={**h, "Content-Type": "application/json"},
|
|
).status_code == 400
|
|
|
|
|
|
def test_undo_after_soft_delete_does_not_lie(env):
|
|
db, client, h, auth = env
|
|
aid = client.post("/api/admin/v2/customers/joe@x/action",
|
|
json={"action": "block", "value": {"blocked": True}},
|
|
headers=h).json()["id"]
|
|
db.soft_delete_user("joe@x")
|
|
r = client.post(f"/api/admin/v2/actions/{aid}/undo", headers=h)
|
|
assert r.status_code == 404
|
|
assert "не найден" in r.json()["detail"]
|
|
# состояние не помечено undone — откат не врёт
|
|
assert db.get_admin_action(aid)["undone_at"] is None
|
|
|
|
|
|
def test_admin_cannot_demote_or_block_self(env):
|
|
db, client, h, auth = env
|
|
for action, value in (("role", {"role": "user"}), ("block", {"blocked": True})):
|
|
r = client.post("/api/admin/v2/customers/boss@x/action",
|
|
json={"action": action, "value": value}, headers=h)
|
|
assert r.status_code == 400
|
|
assert db.get_user_by_email("boss@x")["role"] == "admin"
|
|
assert db.get_user_by_email("boss@x")["blocked"] == 0
|
|
assert client.post("/api/admin/v2/customers/boss@x/action",
|
|
json={"action": "read_only", "value": {"read_only": True}},
|
|
headers=h).status_code == 200
|