feat(backend): sprint 5 — SimulationTemplate CRUD + instantiation

- SimulationTemplate model + migration 0005 (CREATE TABLE + name index)
- 5 CRUD endpoints under /api/templates (admin|redteam only, SOC 403)
- POST /api/engagements/<eid>/simulations extended with optional template_id
- serialize_template() reusing _enrich_techniques/_enrich_tactics helpers
- IntegrityError → 409 for duplicate name on both POST and PATCH
- 28 new tests (CRUD, RBAC, dedup, instantiation, migration round-trip)
- 221 tests pass; ruff clean; mypy clean

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Knacky
2026-05-28 06:25:19 +02:00
parent 9873c535c6
commit 1f327e9aa8
10 changed files with 695 additions and 3 deletions

View File

@@ -5,6 +5,7 @@ from typing import Any
from backend.app.models import Engagement, User
from backend.app.models.simulation import Simulation
from backend.app.models.simulation_template import SimulationTemplate
def serialize_user(user: User) -> dict[str, Any]:
@@ -69,6 +70,23 @@ def serialize_simulation(simulation: Simulation) -> dict[str, Any]:
}
def serialize_template(t: SimulationTemplate) -> dict[str, Any]:
return {
"id": t.id,
"name": t.name,
"description": t.description,
"commands": t.commands,
"prerequisites": t.prerequisites,
"techniques": _enrich_techniques(t.techniques or []),
"tactics": _enrich_tactics(t.tactic_ids or []),
"created_at": t.created_at.isoformat() if t.created_at else None,
"updated_at": t.updated_at.isoformat() if t.updated_at else None,
"created_by": serialize_user_brief(t.created_by) # type: ignore[arg-type]
if t.created_by
else None,
}
def serialize_engagement(engagement: Engagement) -> dict[str, Any]:
return {
"id": engagement.id,