diff --git a/backend/app/core/tree_validation.py b/backend/app/core/tree_validation.py index 38c729fd..de562889 100644 --- a/backend/app/core/tree_validation.py +++ b/backend/app/core/tree_validation.py @@ -1,6 +1,8 @@ """Tree validation helper module for draft/published workflow.""" from typing import Any +PROCEDURAL_TREE_TYPES = {"procedural", "maintenance"} + class TreeValidationError(Exception): """Custom exception for tree validation errors.""" @@ -224,14 +226,14 @@ def can_publish_tree( errors.append({"field": "name", "message": "Tree must have a name to be published"}) # Validate structure based on tree type - if tree_type in ("procedural", "maintenance"): + if tree_type in PROCEDURAL_TREE_TYPES: structure_valid, structure_errors = validate_procedural_structure(tree_structure) else: structure_valid, structure_errors = validate_tree_structure(tree_structure) errors.extend(structure_errors) # Validate intake form if present (procedural only) - if intake_form and tree_type in ("procedural", "maintenance"): + if intake_form and tree_type in PROCEDURAL_TREE_TYPES: form_valid, form_errors = _validate_intake_form(intake_form) errors.extend(form_errors) diff --git a/backend/tests/test_maintenance_tree_type.py b/backend/tests/test_maintenance_tree_type.py index 1ba33bd2..0ba3866b 100644 --- a/backend/tests/test_maintenance_tree_type.py +++ b/backend/tests/test_maintenance_tree_type.py @@ -50,3 +50,24 @@ async def test_list_maintenance_trees_filter(client: AsyncClient, auth_headers: trees = resp.json() assert all(t["tree_type"] == "maintenance" for t in trees) assert len(trees) >= 1 + + +@pytest.mark.asyncio +async def test_invalid_tree_type_rejected(client: AsyncClient, auth_headers: dict): + """An unrecognized tree_type value is rejected with 422.""" + resp = await client.post( + "/api/v1/trees", + json={ + "name": "Bad Type", + "tree_type": "garbage", + "tree_structure": { + "steps": [ + {"id": "s1", "type": "procedure_step", "title": "Step", + "description": "Do it", "content_type": "action"}, + {"id": "end", "type": "procedure_end", "title": "Done"}, + ] + }, + }, + headers=auth_headers, + ) + assert resp.status_code == 422