refactor(workflow): delegate start_execution to TestEntity

Replace manual state+field mutation with entity.start_execution() and apply_to(), keeping audit logging and notifications at the service layer.
This commit is contained in:
2026-02-18 15:29:36 +01:00
parent 9e204b78ec
commit 576705d61d

View File

@@ -111,15 +111,33 @@ def start_execution(db: Session, test: Test, user: User) -> Test:
"""Move from ``draft`` → ``red_executing``.
Typically called by a **red_tech** when they begin the attack.
Starts the Red Team timer by recording ``red_started_at``.
Delegates to :meth:`TestEntity.start_execution` which handles the
state transition and sets ``execution_date`` / ``red_started_at``.
"""
now = datetime.utcnow()
test = transition_state(
db, test, TestState.red_executing, user,
action_name="start_execution",
entity = TestEntity.from_orm(test)
entity.start_execution()
entity.apply_to(test)
db.flush()
log_action(
db,
user_id=user.id,
action="start_execution",
entity_type="test",
entity_id=test.id,
details={
"previous_state": "draft",
"new_state": test.state.value,
"test_name": test.name,
"technique_id": str(test.technique_id),
},
)
test.execution_date = now
test.red_started_at = now
try:
notify_test_state_change(db, test, test.state.value)
except Exception as e:
logger.warning("Notification failed for test %s: %s", test.id, e, exc_info=True)
return test