Skip to content

Commit

Permalink
first draft with little tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle committed Sep 18, 2024
1 parent 2a9ccea commit cf2eb65
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
10 changes: 10 additions & 0 deletions antarest/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,16 @@ def __init__(self, is_variant: bool) -> None:
super().__init__(HTTPStatus.EXPECTATION_FAILED, "Upgrade not supported for parent of variants")


class FileOrFolderDeletionFailed(HTTPException):
"""
Exception raised when deleting a file or a folder inside the User folder.
"""

def __init__(self, message: str) -> None:
msg = f"Raw deletion failed because {message}"
super().__init__(HTTPStatus.EXPECTATION_FAILED, msg)


class ReferencedObjectDeletionNotAllowed(HTTPException):
"""
Exception raised when a binding constraint is not allowed to be deleted because it references
Expand Down
16 changes: 16 additions & 0 deletions antarest/study/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
BadEditInstructionException,
ChildNotFoundError,
CommandApplicationError,
FileOrFolderDeletionFailed,
IncorrectPathError,
NotAManagedStudyException,
ReferencedObjectDeletionNotAllowed,
Expand Down Expand Up @@ -2639,3 +2640,18 @@ def asserts_no_thermal_in_binding_constraints(
if ref_bcs:
binding_ids = [bc.id for bc in ref_bcs]
raise ReferencedObjectDeletionNotAllowed(cluster_id, binding_ids, object_type="Cluster")

def delete_file_or_folder(self, study_id: str, path: str, current_user: JWTUser) -> None:
study = self.get_study(study_id)
assert_permission(current_user, study, StudyPermissionType.WRITE)
study_tree = self.storage_service.raw_study_service.get_raw(study, True).tree
whole_study_built = study_tree.build()
url = [item for item in path.split("/") if item]
if len(url) == 0:
raise FileOrFolderDeletionFailed(f"the path is invalid: {path}")
try:
whole_study_built["user"].delete(url[1:])
except ChildNotFoundError as e:
raise FileOrFolderDeletionFailed(
f"the file doesn't exist or you're not inside the 'User' folder: {e.detail}"
)
13 changes: 8 additions & 5 deletions antarest/study/web/raw_studies_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,17 @@ def get_study(
@bp.delete(
"/studies/{uuid}/raw",
tags=[APITag.study_raw_data],
summary="Delete Raw Data from Study: JSON, Text, or File Attachment",
summary="Delete files or folders from Study inside User folder",
response_model=None,
)
def delete_file(
uuid: str,
path: str = Param("/", examples=get_path_examples()), # type: ignore
current_user: JWTUser = Depends(auth.get_current_user),
uuid: str,
path: str = Param("/", examples=get_path_examples()), # type: ignore
current_user: JWTUser = Depends(auth.get_current_user),
) -> t.Any:
pass
uuid = sanitize_uuid(uuid)
logger.info(f"Deleting some data for study {uuid}", extra={"user": current_user.id})
study_service.delete_file_or_folder(uuid, path, current_user)

@bp.get(
"/studies/{uuid}/areas/aggregate/mc-ind/{output_id}",
Expand Down
32 changes: 32 additions & 0 deletions tests/integration/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1913,3 +1913,35 @@ def test_links_deletion_with_binding_constraints(
# delete the link
res = client.delete(f"/v1/studies/{internal_study_id}/links/area_1/area_2")
assert res.status_code == 200, res.json()


def test_delete_raw(client: TestClient, user_access_token: str, internal_study_id: str) -> None:
client.headers = {"Authorization": f"Bearer {user_access_token}"}

# =============================
# SET UP
# =============================

# =============================
# NOMINAL CASES
# =============================

# =============================
# ERRORS
# =============================

# try to delete a file which isn't inside the 'User' folder
res = client.delete(f"/v1/studies/{internal_study_id}/raw?path=/input/thermal")
assert res.status_code == 417
assert res.json()["exception"] == "FileOrFolderDeletionFailed"
assert "the file doesn't exist or you're not inside the 'User' folder" in res.json()["description"]
# With an empty path.
res = client.delete(f"/v1/studies/{internal_study_id}/raw?path=")
assert res.status_code == 417
assert res.json()["exception"] == "FileOrFolderDeletionFailed"
assert "the path is invalid" in res.json()["description"]
# With a path that doesn't exist
res = client.delete(f"/v1/studies/{internal_study_id}/raw?path=fake_folder/fake_file")
assert res.status_code == 417
assert res.json()["exception"] == "FileOrFolderDeletionFailed"
assert "the file doesn't exist or you're not inside the 'User' folder" in res.json()["description"]

0 comments on commit cf2eb65

Please sign in to comment.