From a6f5fa0936c546f05e508275edab21e0a3b9d33c Mon Sep 17 00:00:00 2001 From: Romulo Quidute Filho Date: Mon, 23 Nov 2026 11:00:49 -0300 Subject: [PATCH 1/2] Validate CLI execution_config before creating a run (#1210) create_cli_test_run_execution stored execution_config without validating it, so a typo or wrong type was only found after the run started. Extract the project validation into crud_project.validate_config and call it before any project or execution is created, returning 422 on failure. --- .../api_v1/endpoints/test_run_executions.py | 11 ++++ app/crud/crud_project.py | 17 ++++-- .../api/api_v1/test_test_run_executions.py | 57 +++++++++++++++++++ 3 files changed, 81 insertions(+), 4 deletions(-) diff --git a/app/api/api_v1/endpoints/test_run_executions.py b/app/api/api_v1/endpoints/test_run_executions.py index b33c1235..c1f13fe0 100644 --- a/app/api/api_v1/endpoints/test_run_executions.py +++ b/app/api/api_v1/endpoints/test_run_executions.py @@ -27,12 +27,14 @@ from app import crud, log_utils, models, schemas from app.api import DEFAULT_404_MESSAGE +from app.crud.crud_project import validate_config from app.crud.crud_test_run_execution import ImportError from app.db.session import get_db from app.default_environment_config import default_environment_config from app.models.project import Project from app.models.test_run_execution import TestRunExecution from app.pics.pics_writer import PICSWriter +from app.schemas.test_environment_config import TestEnvironmentConfigError from app.schemas.test_run_execution import TestRunExecutionUpdate from app.test_engine import TEST_ENGINE_ABORTING_TESTING_MESSAGE from app.test_engine.test_runner import AbortError, LoadingError, TestRunner @@ -188,6 +190,15 @@ def create_cli_test_run_execution( The created TestRunExecution. """ + # Reject an invalid config up front, before any project or execution is created + if execution_config is not None: + try: + validate_config(execution_config) + except TestEnvironmentConfigError as e: + raise HTTPException( + status_code=HTTPStatus.UNPROCESSABLE_ENTITY, detail=str(e) + ) + # Retrieve or create the CLI project cli_project = _cli_project(db, test_run_execution_in.project_id) test_run_execution_in.project_id = cli_project.id diff --git a/app/crud/crud_project.py b/app/crud/crud_project.py index a3a76aaa..7ebfd4f7 100644 --- a/app/crud/crud_project.py +++ b/app/crud/crud_project.py @@ -26,6 +26,17 @@ from app.utils import program_class +def validate_config(config: Dict[str, Any]) -> None: + """Validate a test environment config against the program's config model. + + Instantiates the program class, which raises TestEnvironmentConfigError when + the config is not in accordance with the model. Does nothing when no program + class is available. + """ + if program_class: + program_class(**config) + + class CRUDProject( CRUDBaseRead[Project], CRUDBaseDelete[Project], @@ -85,8 +96,7 @@ def create(self, db: Session, *, obj_in: ProjectCreate) -> Project: obj_in.config = default_environment_config.__dict__ json_obj_in = jsonable_encoder(obj_in) # Try to instantiate the program class in order to validate the input data - if program_class: - program_class(**json_obj_in["config"]) + validate_config(json_obj_in["config"]) obj_in_data = json_obj_in db_obj = Project(**obj_in_data) @@ -115,8 +125,7 @@ def update( setattr(db_obj, field, update_data[field]) # Try to instantiate the program class in order to validate the input data - if program_class: - program_class(**jsonable_encoder(db_obj)["config"]) + validate_config(jsonable_encoder(db_obj)["config"]) db.add(db_obj) db.commit() diff --git a/app/tests/api/api_v1/test_test_run_executions.py b/app/tests/api/api_v1/test_test_run_executions.py index b810ec0b..e4ac4eb7 100644 --- a/app/tests/api/api_v1/test_test_run_executions.py +++ b/app/tests/api/api_v1/test_test_run_executions.py @@ -1952,6 +1952,63 @@ def test_create_cli_test_run_execution_does_not_update_existing_project_no_pics( assert execution_in.execution_pics == {} +def test_create_cli_test_run_execution_rejects_invalid_execution_config( + mock_db, test_run_execution_create, test_selection, default_config +): + """An invalid execution_config is rejected with 422 before anything is created.""" + invalid_config = {**default_config, "dut_config": {"pairing_mode": "not-a-mode"}} + + with patch( + "app.api.api_v1.endpoints.test_run_executions.get_db", return_value=mock_db + ), patch( + "app.api.api_v1.endpoints.test_run_executions._cli_project" + ) as mock_cli_project, patch( + "app.api.api_v1.endpoints.test_run_executions.crud.test_run_execution.create" + ) as mock_create: + response = client.post( + f"{settings.API_V1_STR}/test_run_executions/cli", + json={ + "test_run_execution_in": test_run_execution_create.dict(), + "selected_tests": test_selection, + "execution_config": invalid_config, + }, + ) + + assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY + assert "invalid properties" in response.json()["detail"] + mock_cli_project.assert_not_called() + mock_create.assert_not_called() + + +def test_create_cli_test_run_execution_without_execution_config_skips_validation( + mock_db, test_run_execution_create, test_selection +): + """An absent execution_config is not validated (the project config is used).""" + with patch( + "app.api.api_v1.endpoints.test_run_executions.get_db", return_value=mock_db + ), patch( + "app.api.api_v1.endpoints.test_run_executions.validate_config" + ) as mock_validate, patch( + "app.api.api_v1.endpoints.test_run_executions._cli_project" + ) as mock_cli_project, patch( + "app.api.api_v1.endpoints.test_run_executions._reject_invalid_test_parameters" + ), patch( + "app.api.api_v1.endpoints.test_run_executions.crud.test_run_execution.create" + ) as mock_create: + mock_cli_project.return_value = MagicMock(id=1, config={}) + mock_create.return_value = MagicMock() + client.post( + f"{settings.API_V1_STR}/test_run_executions/cli", + json={ + "test_run_execution_in": test_run_execution_create.dict(), + "selected_tests": test_selection, + }, + ) + + mock_validate.assert_not_called() + mock_create.assert_called_once() + + _RUNNER_OPTIONS_PATH = ( "app.api.api_v1.endpoints.test_run_executions.load_runner_options" ) From 46061476126bd5792b24e36e25f049cfa3bc2963 Mon Sep 17 00:00:00 2001 From: Romulo Quidute Filho Date: Tue, 24 Nov 2026 14:24:52 -0300 Subject: [PATCH 2/2] Fix absent-execution_config test: return serializable run model --- .../api/api_v1/test_test_run_executions.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/app/tests/api/api_v1/test_test_run_executions.py b/app/tests/api/api_v1/test_test_run_executions.py index e4ac4eb7..65112b09 100644 --- a/app/tests/api/api_v1/test_test_run_executions.py +++ b/app/tests/api/api_v1/test_test_run_executions.py @@ -1995,9 +1995,21 @@ def test_create_cli_test_run_execution_without_execution_config_skips_validation ), patch( "app.api.api_v1.endpoints.test_run_executions.crud.test_run_execution.create" ) as mock_create: - mock_cli_project.return_value = MagicMock(id=1, config={}) - mock_create.return_value = MagicMock() - client.post( + mock_cli_project.return_value = Project(id=1, name="CLI Project", config={}) + mock_create.return_value = TestRunExecution( + id=1, + title=test_run_execution_create.title, + description=test_run_execution_create.description, + project_id=1, + operator_id=1, + certification_mode=False, + state=TestStateEnum.PENDING, + started_at=None, + completed_at=None, + imported_at=None, + archived_at=None, + ) + response = client.post( f"{settings.API_V1_STR}/test_run_executions/cli", json={ "test_run_execution_in": test_run_execution_create.dict(), @@ -2005,6 +2017,7 @@ def test_create_cli_test_run_execution_without_execution_config_skips_validation }, ) + assert response.status_code == 200 mock_validate.assert_not_called() mock_create.assert_called_once()