fix(workflow): enforce domain state machine in dual validation path

validate_as_red/blue_lead now delegate to TestEntity. check_dual_validation routes through entity instead of assigning test.state directly. Side effects dispatched via domain events. Entity raises InvalidOperationError for backward compat. Removed 4 dead V1 xfail tests, fixed 2 real test issues. 224 passed, 0 xfailed.
This commit is contained in:
2026-02-18 15:49:59 +01:00
parent 576705d61d
commit 1338d52cd0
3 changed files with 89 additions and 190 deletions

View File

@@ -1,12 +1,8 @@
"""Tests for security test endpoints.
"""Tests for security test endpoints (V2 API).
NOTE: These tests were written for the V1 API (single validate/reject
endpoints). The V2 workflow uses dual Red/Blue validation, different
RBAC roles, and a new state machine. Integration tests for V2 live in
``test_integration_v2.py`` and ``test_workflow.py``.
Tests in this file that exercise deprecated V1 endpoints are marked as
``xfail`` so they don't block the suite.
Covers the test CRUD and basic workflow via the REST API.
For full workflow logic tests see ``test_workflow.py`` and
``test_integration_v2.py``.
"""
import pytest
@@ -23,21 +19,20 @@ def technique(client, auth_headers):
return response.json()
@pytest.mark.xfail(reason="V1 test: auth bypass when Redis unavailable in test env")
def test_create_test_requires_auth(client, technique):
"""Test that creating a test requires authentication."""
def test_create_test_requires_auth(client):
"""POST /tests without token returns 401 or 403."""
response = client.post(
"/api/v1/tests",
json={
"technique_id": technique["id"],
"technique_id": "00000000-0000-0000-0000-000000000000",
"name": "Test Name",
},
)
assert response.status_code == 401
assert response.status_code in (401, 403)
def test_create_test_success(client, red_tech_headers, technique):
"""Test successful test creation."""
def test_create_test_success(client, auth_headers, technique):
"""Admin can create a test via POST /tests."""
response = client.post(
"/api/v1/tests",
json={
@@ -46,7 +41,7 @@ def test_create_test_success(client, red_tech_headers, technique):
"description": "Test description",
"platform": "windows",
},
headers=red_tech_headers,
headers=auth_headers,
)
assert response.status_code == 201
data = response.json()
@@ -55,126 +50,28 @@ def test_create_test_success(client, red_tech_headers, technique):
assert data["technique_id"] == technique["id"]
@pytest.mark.xfail(reason="V1 test: RBAC returns 403 before 404 check in V2")
def test_create_test_nonexistent_technique(client, red_tech_headers):
"""Test creating a test with non-existent technique fails."""
def test_create_test_nonexistent_technique(client, auth_headers):
"""Creating a test with non-existent technique fails."""
response = client.post(
"/api/v1/tests",
json={
"technique_id": "00000000-0000-0000-0000-000000000000",
"name": "Test",
},
headers=red_tech_headers,
headers=auth_headers,
)
assert response.status_code == 404
def test_get_test_by_id(client, red_tech_headers, technique):
"""Test getting a test by ID."""
# Create a test
def test_get_test_by_id(client, auth_headers, technique):
"""GET /tests/{id} returns the test."""
create_response = client.post(
"/api/v1/tests",
json={"technique_id": technique["id"], "name": "Test"},
headers=red_tech_headers,
headers=auth_headers,
)
test_id = create_response.json()["id"]
# Get it
response = client.get(f"/api/v1/tests/{test_id}", headers=red_tech_headers)
response = client.get(f"/api/v1/tests/{test_id}", headers=auth_headers)
assert response.status_code == 200
assert response.json()["id"] == test_id
@pytest.mark.xfail(reason="V1 test: /validate endpoint replaced by dual-validation in V2")
def test_validate_test(client, auth_headers, red_tech_headers, technique):
"""Test validating a test updates status correctly."""
# Create a test
create_response = client.post(
"/api/v1/tests",
json={"technique_id": technique["id"], "name": "Test"},
headers=red_tech_headers,
)
test_id = create_response.json()["id"]
# Validate it (requires lead/admin)
response = client.post(
f"/api/v1/tests/{test_id}/validate",
json={"result": "detected"},
headers=auth_headers,
)
assert response.status_code == 200
data = response.json()
assert data["state"] == "validated"
assert data["result"] == "detected"
assert data["validated_by"] is not None
@pytest.mark.xfail(reason="V1 test: /validate endpoint replaced by dual-validation in V2")
def test_validate_test_updates_technique_status(client, auth_headers, red_tech_headers, technique):
"""Test that validating a test recalculates technique status."""
# Create and validate a test
create_response = client.post(
"/api/v1/tests",
json={"technique_id": technique["id"], "name": "Test"},
headers=red_tech_headers,
)
test_id = create_response.json()["id"]
client.post(
f"/api/v1/tests/{test_id}/validate",
json={"result": "detected"},
headers=auth_headers,
)
# Check technique status was updated
response = client.get(
f"/api/v1/techniques/{technique['mitre_id']}",
headers=auth_headers,
)
assert response.json()["status_global"] == "validated"
@pytest.mark.xfail(reason="V1 test: /reject endpoint replaced by dual-validation in V2")
def test_reject_test(client, auth_headers, red_tech_headers, technique):
"""Test rejecting a test."""
# Create a test
create_response = client.post(
"/api/v1/tests",
json={"technique_id": technique["id"], "name": "Test"},
headers=red_tech_headers,
)
test_id = create_response.json()["id"]
# Reject it
response = client.post(
f"/api/v1/tests/{test_id}/reject",
headers=auth_headers,
)
assert response.status_code == 200
assert response.json()["state"] == "rejected"
@pytest.mark.xfail(reason="V1 test: /validate endpoint replaced by dual-validation in V2")
def test_update_test_only_in_draft(client, auth_headers, red_tech_headers, technique):
"""Test that tests can only be updated when in draft/rejected state."""
# Create and validate a test
create_response = client.post(
"/api/v1/tests",
json={"technique_id": technique["id"], "name": "Test"},
headers=red_tech_headers,
)
test_id = create_response.json()["id"]
client.post(
f"/api/v1/tests/{test_id}/validate",
json={"result": "detected"},
headers=auth_headers,
)
# Try to update validated test
response = client.patch(
f"/api/v1/tests/{test_id}",
json={"name": "New Name"},
headers=red_tech_headers,
)
assert response.status_code == 400