Skip to content

Commit

Permalink
fix(commands): it is now impossible to create a study with an empty id
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle committed May 23, 2023
1 parent e42bac4 commit db6cf1f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 10 deletions.
6 changes: 5 additions & 1 deletion antarest/study/business/area_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
Expand Down
22 changes: 15 additions & 7 deletions antarest/study/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 9 additions & 2 deletions antarest/study/storage/variantstudy/model/command/create_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
18 changes: 18 additions & 0 deletions tests/integration/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={
Expand Down
7 changes: 7 additions & 0 deletions tests/storage/business/test_arealink_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit db6cf1f

Please sign in to comment.