- GET/POST /api/admin/users, GET/PATCH/DELETE /api/admin/users/{id}
- Deaktivieren und Passwort-Reset beenden alle Sessions des Benutzers
- kein Ändern/Löschen des eigenen Accounts, letzter aktiver Admin geschützt
- INITIAL_ADMIN_USERNAME/PASSWORD legen den ersten Admin an, solange keine Benutzer existieren
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
161 lines
5.5 KiB
Python
161 lines
5.5 KiB
Python
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.models import Role, User, UserSession
|
|
from app.services.users import UserError, delete_user, update_user
|
|
from tests.conftest import login
|
|
|
|
|
|
@pytest.fixture
|
|
def admin_client(client, make_user):
|
|
make_user("boss", Role.ADMIN)
|
|
login(client, "boss")
|
|
return client
|
|
|
|
|
|
def test_regular_user_cannot_use_admin_api(client, make_user):
|
|
make_user("anna")
|
|
login(client, "anna")
|
|
|
|
assert client.get("/api/admin/users").status_code == 403
|
|
response = client.post(
|
|
"/api/admin/users", json={"username": "evil", "password": "long enough password"}
|
|
)
|
|
assert response.status_code == 403
|
|
|
|
|
|
def test_admin_api_requires_login(client):
|
|
assert client.get("/api/admin/users").status_code == 401
|
|
|
|
|
|
def test_admin_api_requires_csrf(admin_client):
|
|
del admin_client.headers["X-CSRF-Token"]
|
|
response = admin_client.post(
|
|
"/api/admin/users", json={"username": "anna", "password": "long enough password"}
|
|
)
|
|
assert response.status_code == 403
|
|
|
|
|
|
def test_create_and_list_users(admin_client):
|
|
response = admin_client.post(
|
|
"/api/admin/users", json={"username": "Anna", "password": "long enough password"}
|
|
)
|
|
|
|
assert response.status_code == 201
|
|
assert response.json()["username"] == "anna"
|
|
assert response.json()["role"] == "user"
|
|
names = [u["username"] for u in admin_client.get("/api/admin/users").json()]
|
|
assert names == ["anna", "boss"]
|
|
assert "password_hash" not in response.text
|
|
|
|
|
|
def test_create_user_validation_errors(admin_client):
|
|
short = admin_client.post("/api/admin/users", json={"username": "anna", "password": "x"})
|
|
bad_name = admin_client.post(
|
|
"/api/admin/users", json={"username": "a b", "password": "long enough password"}
|
|
)
|
|
duplicate = admin_client.post(
|
|
"/api/admin/users", json={"username": "BOSS", "password": "long enough password"}
|
|
)
|
|
|
|
assert short.status_code == bad_name.status_code == duplicate.status_code == 400
|
|
|
|
|
|
def test_deactivate_user_ends_sessions(admin_client, make_user, db):
|
|
anna = make_user("anna")
|
|
with TestClient(admin_client.app, base_url="https://testserver") as anna_client:
|
|
login(anna_client, "anna")
|
|
|
|
response = admin_client.patch(f"/api/admin/users/{anna.id}", json={"is_active": False})
|
|
|
|
assert response.status_code == 200
|
|
assert response.json()["is_active"] is False
|
|
assert anna_client.get("/api/auth/me").status_code == 401
|
|
assert db.query(UserSession).filter_by(user_id=anna.id).count() == 0
|
|
assert login(anna_client, "anna").status_code == 401
|
|
|
|
|
|
def test_reset_password_ends_sessions(admin_client, make_user):
|
|
anna = make_user("anna")
|
|
with TestClient(admin_client.app, base_url="https://testserver") as anna_client:
|
|
login(anna_client, "anna")
|
|
|
|
admin_client.patch(f"/api/admin/users/{anna.id}", json={"password": "reset by admin!"})
|
|
|
|
assert anna_client.get("/api/auth/me").status_code == 401
|
|
assert login(anna_client, "anna", "reset by admin!").status_code == 200
|
|
|
|
|
|
def test_promote_user(admin_client, make_user):
|
|
anna = make_user("anna")
|
|
|
|
response = admin_client.patch(f"/api/admin/users/{anna.id}", json={"role": "admin"})
|
|
|
|
assert response.json()["role"] == "admin"
|
|
|
|
|
|
def test_delete_user_cascades_sessions(admin_client, make_user, db):
|
|
anna_id = make_user("anna").id
|
|
with TestClient(admin_client.app, base_url="https://testserver") as anna_client:
|
|
login(anna_client, "anna")
|
|
|
|
assert admin_client.delete(f"/api/admin/users/{anna_id}").status_code == 204
|
|
|
|
db.expire_all()
|
|
assert db.get(User, anna_id) is None
|
|
assert db.query(UserSession).filter_by(user_id=anna_id).count() == 0
|
|
|
|
|
|
def test_admin_cannot_modify_or_delete_self(admin_client, db):
|
|
boss = db.query(User).filter_by(username="boss").one()
|
|
|
|
assert (
|
|
admin_client.patch(f"/api/admin/users/{boss.id}", json={"role": "user"}).status_code == 400
|
|
)
|
|
assert admin_client.delete(f"/api/admin/users/{boss.id}").status_code == 400
|
|
|
|
|
|
def test_unknown_user_returns_404(admin_client):
|
|
assert admin_client.get("/api/admin/users/999").status_code == 404
|
|
assert admin_client.delete("/api/admin/users/999").status_code == 404
|
|
|
|
|
|
def test_last_active_admin_is_protected(make_user, db):
|
|
boss = make_user("boss", Role.ADMIN)
|
|
other = make_user("other", Role.ADMIN)
|
|
other.is_active = False
|
|
db.commit()
|
|
|
|
# An inactive admin acting (e.g. via future tooling) must not remove the last active one
|
|
with pytest.raises(UserError):
|
|
update_user(db, other, boss, role=Role.USER)
|
|
with pytest.raises(UserError):
|
|
update_user(db, other, boss, is_active=False)
|
|
with pytest.raises(UserError):
|
|
delete_user(db, other, boss)
|
|
|
|
|
|
@pytest.fixture
|
|
def initial_admin_env(monkeypatch, app_env):
|
|
from app.config import get_settings
|
|
|
|
monkeypatch.setenv("INITIAL_ADMIN_USERNAME", "root-admin")
|
|
monkeypatch.setenv("INITIAL_ADMIN_PASSWORD", "initial admin password")
|
|
get_settings.cache_clear()
|
|
|
|
|
|
def test_initial_admin_created_from_env(initial_admin_env, client, db):
|
|
user = db.query(User).one()
|
|
assert user.username == "root-admin"
|
|
assert user.role == Role.ADMIN
|
|
assert login(client, "root-admin", "initial admin password").status_code == 200
|
|
|
|
|
|
def test_initial_admin_ignored_when_users_exist(initial_admin_env, make_user, db):
|
|
from app.main import _bootstrap_admin
|
|
|
|
make_user("anna")
|
|
_bootstrap_admin()
|
|
|
|
assert [u.username for u in db.query(User).all()] == ["anna"]
|