65 lines
3.1 KiB
Python
65 lines
3.1 KiB
Python
"""Админ задаёт IP ключа; сервер не принимает изменение от обычного пользователя."""
|
|
import importlib
|
|
|
|
from fastapi.testclient import TestClient
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture()
|
|
def env(tmp_path, monkeypatch):
|
|
monkeypatch.setenv("DB_PATH", str(tmp_path / "t.db"))
|
|
monkeypatch.setenv("SESSION_SECRET", "test-secret")
|
|
monkeypatch.setenv("DATA_DIR", str(tmp_path / "data"))
|
|
import auth
|
|
import db
|
|
import main
|
|
for module in (db, auth, main):
|
|
importlib.reload(module)
|
|
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")
|
|
admin = {"Authorization": "Bearer " + auth._make_token("boss@x", "admin")}
|
|
user = {"Authorization": "Bearer " + auth._make_token("joe@x", "user")}
|
|
return db, TestClient(main.app), admin, user
|
|
|
|
|
|
def test_admin_creates_bound_key_and_changes_address(env):
|
|
db, client, admin, _ = env
|
|
made = client.post("/api/admin/v2/stove-api/keys", headers=admin,
|
|
json={"name": "site", "allowed_ip": "203.0.113.7"})
|
|
assert made.status_code == 200, made.text
|
|
key = made.json()
|
|
assert key["allowed_ip"] == "203.0.113.7"
|
|
changed = client.patch(f"/api/admin/v2/stove-api/keys/{key['id']}/allowed-ip",
|
|
headers=admin, json={"allowed_ip": "198.51.100.0/24"})
|
|
assert changed.status_code == 200, changed.text
|
|
assert changed.json()["allowed_ip"] == "198.51.100.0/24"
|
|
import stove_keys
|
|
assert stove_keys.authenticate(key["secret"], "203.0.113.7")["status"] == "ip_not_allowed"
|
|
assert stove_keys.authenticate(key["secret"], "198.51.100.8")["status"] == "ok"
|
|
assert client.get("/api/admin/v2/stove-api", headers=admin).json()["keys"][0]["allowed_ip"] == "198.51.100.0/24"
|
|
with db._conn() as conn:
|
|
audit = conn.execute("SELECT action,detail FROM audit_log WHERE action=? ORDER BY id DESC LIMIT 1",
|
|
("stove_api.key_allowed_ip",)).fetchone()
|
|
assert audit["detail"] == f"id={key['id']},allowed_ip=198.51.100.0/24"
|
|
|
|
|
|
def test_key_ip_edit_requires_admin(env):
|
|
_, client, admin, user = env
|
|
key = client.post("/api/admin/v2/stove-api/keys", headers=admin,
|
|
json={"name": "site", "allowed_ip": "203.0.113.7"}).json()
|
|
path = f"/api/admin/v2/stove-api/keys/{key['id']}/allowed-ip"
|
|
assert client.patch(path, json={"allowed_ip": "198.51.100.8"}).status_code == 401
|
|
assert client.patch(path, headers=user, json={"allowed_ip": "198.51.100.8"}).status_code == 403
|
|
assert client.patch(path, headers=admin, json={"allowed_ip": "invalid"}).status_code == 422
|
|
assert client.get("/api/admin/v2/stove-api", headers=admin).json()["keys"][0]["allowed_ip"] == "203.0.113.7"
|
|
|
|
|
|
def test_admin_cannot_issue_unbound_key(env):
|
|
_, client, admin, _ = env
|
|
response = client.post("/api/admin/v2/stove-api/keys", headers=admin, json={"name": "no-ip"})
|
|
assert response.status_code == 422
|
|
assert client.get("/api/admin/v2/stove-api", headers=admin).json()["keys"] == []
|