Ship the first feature end-to-end on the UI: users log in with JWT,
admins manage user accounts, and any authenticated user (per RBAC)
can create, list, view, edit, and delete engagements.
Backend (Flask + SQLAlchemy + SQLite, 63 pytest)
- User / Engagement models, Alembic 0001 initial schema
- argon2 password hashing, JWT bearer (60-min TTL), @login_required
and @role_required decorators
- 13 API endpoints under /api/*, including last-admin protection on
DELETE/PATCH user and JSON 404 on unknown /api/* paths
- `flask create-admin` CLI with duplicate / short-password handling
Frontend (React + Vite + Tailwind + TanStack Query, 20 vitest)
- Inter font bundled locally (no CDN), DESIGN.md tokens in Tailwind
- LoginPage / EngagementsList / EngagementForm / EngagementDetail /
UsersAdmin pages with role-aware UI
- Layout, ProtectedRoute, StatusBadge, FormField, LoadingState,
ErrorState, EmptyState, Toast + provider
- Axios client: Bearer interceptor, 401 → purge + /login + "Session
expirée" toast, 403 → "Accès refusé" toast (declarative <Navigate>
for already-authed users, Fragment-keyed admin user rows)
Deployment
- Single multistage Dockerfile (node:20-alpine → python:3.12-slim)
- docker/entrypoint.sh runs `flask db upgrade` before `flask run`
- Makefile: build/start/stop/restart/update/logs/create-admin/
update-mitre/test-{backend,frontend,e2e}/clean
- .env.example documenting MIMIC_JWT_SECRET / MIMIC_DB_PATH / MIMIC_PORT
- SQLite at /data/mimic.sqlite on named volume mimic-data
Acceptance suite (Playwright, 36 tests, all 27 ACs)
- e2e/ scaffold with playwright.config + auth/api fixtures
- One spec per user story (us1-bootstrap through us6-deployment)
- Portable via MIMIC_CONTAINER_CMD / MIMIC_BASE_URL (docker or podman)
Docs
- README.md with quick-start and architecture overview
- CHANGELOG.md updated with Sprint 1 deliverables
- pyrightconfig.json so the Python LSP sees backend/.venv and
resolves the `backend.app.*` absolute imports
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
202 lines
5.9 KiB
Python
202 lines
5.9 KiB
Python
"""User management endpoint tests."""
|
|
from __future__ import annotations
|
|
|
|
from flask.testing import FlaskClient
|
|
|
|
from backend.app.auth import hash_password
|
|
from backend.app.extensions import db
|
|
from backend.app.models import User, UserRole
|
|
from backend.tests.conftest import auth_headers as _h
|
|
|
|
|
|
def test_list_users_admin_only(
|
|
client: FlaskClient, admin_user: User, admin_token: str
|
|
) -> None:
|
|
resp = client.get("/api/users", headers=_h(admin_token))
|
|
assert resp.status_code == 200
|
|
body = resp.get_json()
|
|
assert isinstance(body, list)
|
|
assert any(u["username"] == "admin1" for u in body)
|
|
assert all("password_hash" not in u for u in body)
|
|
|
|
|
|
def test_list_users_forbidden_for_redteam(
|
|
client: FlaskClient, redteam_token: str
|
|
) -> None:
|
|
resp = client.get("/api/users", headers=_h(redteam_token))
|
|
assert resp.status_code == 403
|
|
|
|
|
|
def test_list_users_forbidden_for_soc(client: FlaskClient, soc_token: str) -> None:
|
|
resp = client.get("/api/users", headers=_h(soc_token))
|
|
assert resp.status_code == 403
|
|
|
|
|
|
def test_list_users_unauth(client: FlaskClient) -> None:
|
|
resp = client.get("/api/users")
|
|
assert resp.status_code == 401
|
|
|
|
|
|
def test_create_user_success(client: FlaskClient, admin_token: str) -> None:
|
|
resp = client.post(
|
|
"/api/users",
|
|
headers=_h(admin_token),
|
|
json={"username": "newbie", "password": "longenough1", "role": "redteam"},
|
|
)
|
|
assert resp.status_code == 201
|
|
body = resp.get_json()
|
|
assert body["username"] == "newbie"
|
|
assert body["role"] == "redteam"
|
|
assert "password_hash" not in body
|
|
|
|
|
|
def test_create_user_duplicate_username(
|
|
client: FlaskClient, admin_user: User, admin_token: str
|
|
) -> None:
|
|
resp = client.post(
|
|
"/api/users",
|
|
headers=_h(admin_token),
|
|
json={"username": "admin1", "password": "longenough1", "role": "redteam"},
|
|
)
|
|
assert resp.status_code == 400
|
|
assert "exists" in resp.get_json()["error"]
|
|
|
|
|
|
def test_create_user_short_password(client: FlaskClient, admin_token: str) -> None:
|
|
resp = client.post(
|
|
"/api/users",
|
|
headers=_h(admin_token),
|
|
json={"username": "short", "password": "abc", "role": "soc"},
|
|
)
|
|
assert resp.status_code == 400
|
|
assert "8 characters" in resp.get_json()["error"]
|
|
|
|
|
|
def test_create_user_invalid_role(client: FlaskClient, admin_token: str) -> None:
|
|
resp = client.post(
|
|
"/api/users",
|
|
headers=_h(admin_token),
|
|
json={"username": "x", "password": "longenough1", "role": "godmode"},
|
|
)
|
|
assert resp.status_code == 400
|
|
|
|
|
|
def test_create_user_forbidden_for_non_admin(
|
|
client: FlaskClient, redteam_token: str
|
|
) -> None:
|
|
resp = client.post(
|
|
"/api/users",
|
|
headers=_h(redteam_token),
|
|
json={"username": "x", "password": "longenough1", "role": "soc"},
|
|
)
|
|
assert resp.status_code == 403
|
|
|
|
|
|
def test_patch_user_change_role(
|
|
client: FlaskClient, admin_token: str, soc_user: User
|
|
) -> None:
|
|
resp = client.patch(
|
|
f"/api/users/{soc_user.id}",
|
|
headers=_h(admin_token),
|
|
json={"role": "redteam"},
|
|
)
|
|
assert resp.status_code == 200
|
|
assert resp.get_json()["role"] == "redteam"
|
|
|
|
|
|
def test_patch_user_change_password(
|
|
client: FlaskClient, admin_token: str, soc_user: User
|
|
) -> None:
|
|
resp = client.patch(
|
|
f"/api/users/{soc_user.id}",
|
|
headers=_h(admin_token),
|
|
json={"password": "anotherone1"},
|
|
)
|
|
assert resp.status_code == 200
|
|
# New password should now allow login.
|
|
login = client.post(
|
|
"/api/auth/login", json={"username": "soc1", "password": "anotherone1"}
|
|
)
|
|
assert login.status_code == 200
|
|
|
|
|
|
def test_patch_user_short_password(
|
|
client: FlaskClient, admin_token: str, soc_user: User
|
|
) -> None:
|
|
resp = client.patch(
|
|
f"/api/users/{soc_user.id}",
|
|
headers=_h(admin_token),
|
|
json={"password": "no"},
|
|
)
|
|
assert resp.status_code == 400
|
|
|
|
|
|
def test_patch_user_404(client: FlaskClient, admin_token: str) -> None:
|
|
resp = client.patch(
|
|
"/api/users/9999", headers=_h(admin_token), json={"role": "soc"}
|
|
)
|
|
assert resp.status_code == 404
|
|
|
|
|
|
def test_patch_user_forbidden_for_redteam(
|
|
client: FlaskClient, redteam_token: str, soc_user: User
|
|
) -> None:
|
|
resp = client.patch(
|
|
f"/api/users/{soc_user.id}", headers=_h(redteam_token), json={"role": "admin"}
|
|
)
|
|
assert resp.status_code == 403
|
|
|
|
|
|
def test_delete_user_success(
|
|
client: FlaskClient, admin_token: str, soc_user: User
|
|
) -> None:
|
|
resp = client.delete(f"/api/users/{soc_user.id}", headers=_h(admin_token))
|
|
assert resp.status_code == 204
|
|
|
|
|
|
def test_delete_last_admin_blocked(
|
|
client: FlaskClient, admin_user: User, admin_token: str
|
|
) -> None:
|
|
resp = client.delete(f"/api/users/{admin_user.id}", headers=_h(admin_token))
|
|
assert resp.status_code == 409
|
|
assert "last admin" in resp.get_json()["error"]
|
|
|
|
|
|
def test_delete_admin_when_other_admin_exists(
|
|
client: FlaskClient, admin_user: User, admin_token: str
|
|
) -> None:
|
|
other = User(
|
|
username="admin2",
|
|
password_hash=hash_password("adminpass2"),
|
|
role=UserRole.ADMIN,
|
|
)
|
|
db.session.add(other)
|
|
db.session.commit()
|
|
other_id = other.id
|
|
|
|
resp = client.delete(f"/api/users/{other_id}", headers=_h(admin_token))
|
|
assert resp.status_code == 204
|
|
|
|
|
|
def test_demote_last_admin_blocked(
|
|
client: FlaskClient, admin_user: User, admin_token: str
|
|
) -> None:
|
|
resp = client.patch(
|
|
f"/api/users/{admin_user.id}",
|
|
headers=_h(admin_token),
|
|
json={"role": "redteam"},
|
|
)
|
|
assert resp.status_code == 409
|
|
|
|
|
|
def test_delete_user_404(client: FlaskClient, admin_token: str) -> None:
|
|
resp = client.delete("/api/users/9999", headers=_h(admin_token))
|
|
assert resp.status_code == 404
|
|
|
|
|
|
def test_delete_user_forbidden_for_soc(
|
|
client: FlaskClient, soc_token: str, redteam_user: User
|
|
) -> None:
|
|
resp = client.delete(f"/api/users/{redteam_user.id}", headers=_h(soc_token))
|
|
assert resp.status_code == 403
|