From db6cf1fbe56799de24ecba1ed94be8a20bfabba2 Mon Sep 17 00:00:00 2001 From: belthlemar Date: Tue, 23 May 2023 15:41:08 +0200 Subject: [PATCH] fix(commands): it is now impossible to create a study with an empty id --- antarest/study/business/area_management.py | 6 ++++- antarest/study/service.py | 22 +++++++++++++------ .../variantstudy/model/command/create_area.py | 11 ++++++++-- tests/integration/test_integration.py | 18 +++++++++++++++ .../storage/business/test_arealink_manager.py | 7 ++++++ 5 files changed, 54 insertions(+), 10 deletions(-) diff --git a/antarest/study/business/area_management.py b/antarest/study/business/area_management.py index 1f6402286f..2f6bd4c177 100644 --- a/antarest/study/business/area_management.py +++ b/antarest/study/business/area_management.py @@ -334,6 +334,11 @@ def remove_layer(self, study: RawStudy, layer_id: str) -> None: def create_area( self, study: Study, area_creation_info: AreaCreationDTO ) -> AreaInfoDTO: + area_id = transform_name_to_id(area_creation_info.name) + if area_id == "": + raise ValueError( + f"Area name : {area_creation_info.name} only contains forbidden characters" + ) file_study = self.storage_service.get_storage(study).get_raw(study) command = CreateArea( area_name=area_creation_info.name, @@ -342,7 +347,6 @@ def create_area( execute_or_add_commands( study, file_study, [command], self.storage_service ) - area_id = transform_name_to_id(area_creation_info.name) patch = self.patch_service.get(study) patch.areas = patch.areas or {} patch.areas[area_id] = area_creation_info.metadata or PatchArea() diff --git a/antarest/study/service.py b/antarest/study/service.py index 147e6c75a8..c2ff24c86a 100644 --- a/antarest/study/service.py +++ b/antarest/study/service.py @@ -1918,14 +1918,22 @@ def create_area( study = self.get_study(uuid) assert_permission(params.user, study, StudyPermissionType.WRITE) self._assert_study_unarchived(study) - new_area = self.areas.create_area(study, area_creation_dto) - self.event_bus.push( - Event( - type=EventType.STUDY_DATA_EDITED, - payload=study.to_json_summary(), - permissions=PermissionInfo.from_study(study), + try: + new_area = self.areas.create_area(study, area_creation_dto) + except Exception as e: + logger.error( + f"Exception occurs while creating the area : {e}", + exc_info=True, + ) + raise + else: + self.event_bus.push( + Event( + type=EventType.STUDY_DATA_EDITED, + payload=study.to_json_summary(), + permissions=PermissionInfo.from_study(study), + ) ) - ) return new_area def create_link( diff --git a/antarest/study/storage/variantstudy/model/command/create_area.py b/antarest/study/storage/variantstudy/model/command/create_area.py index cd029cbed1..823c827226 100644 --- a/antarest/study/storage/variantstudy/model/command/create_area.py +++ b/antarest/study/storage/variantstudy/model/command/create_area.py @@ -59,14 +59,21 @@ def _apply_config( raise ValueError() area_id = transform_name_to_id(self.area_name) - + if area_id == "": + return ( + CommandOutput( + status=False, + message=f"Area name '{self.area_name}' only contains forbidden characters", + ), + {}, + ) if area_id in study_data.areas.keys(): return ( CommandOutput( status=False, message=f"Area '{self.area_name}' already exists and could not be created", ), - dict(), + {}, ) study_data.areas[area_id] = Area( diff --git a/tests/integration/test_integration.py b/tests/integration/test_integration.py index f0bc853921..7facddb6e3 100644 --- a/tests/integration/test_integration.py +++ b/tests/integration/test_integration.py @@ -643,6 +643,24 @@ def test_area_management(app: FastAPI): "exception": "CommandApplicationError", } + res = client.post( + f"/v1/studies/{study_id}/commands", + headers={ + "Authorization": f'Bearer {admin_credentials["access_token"]}' + }, + json=[ + { + "action": CommandName.CREATE_AREA.value, + "args": {"area_name": "%%%"}, + } + ], + ) + assert res.status_code == 500 + assert res.json() == { + "description": "Area name '%%%' only contains forbidden characters", + "exception": "CommandApplicationError", + } + client.post( f"/v1/studies/{study_id}/areas", headers={ diff --git a/tests/storage/business/test_arealink_manager.py b/tests/storage/business/test_arealink_manager.py index cae6323b49..cca9e14bf3 100644 --- a/tests/storage/business/test_arealink_manager.py +++ b/tests/storage/business/test_arealink_manager.py @@ -241,6 +241,13 @@ def test_area_crud( ], RequestParameters(DEFAULT_ADMIN_USER), ) + with pytest.raises( + ValueError, + match="Area name : %% only contains forbidden characters", + ): + area_manager.create_area( + study, AreaCreationDTO(name="%%", type=AreaType.AREA) + ) def test_get_all_area():