Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion st2api/st2api/controllers/v1/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,22 @@ def delete(self, ref_or_id, requester_user):
entry_point=entry_point,
metadata_file=metadata_file,
)

except PermissionError as e:
LOG.error("No permission to delete resource files from disk.")
action_db.id = None
Action.add_or_update(action_db)
abort(http_client.FORBIDDEN, six.text_type(e))
return

except Exception as e:
LOG.error(
"Exception encountered during deleting resource files from disk."
"Exception encountered during deleting resource files from disk. "
"Exception was %s",
e,
)
action_db.id = None
Action.add_or_update(action_db)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you modify the unit tests to cover this change?

abort(http_client.INTERNAL_SERVER_ERROR, six.text_type(e))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if the deletion is related to permission error, can you change the error code to 403 Forbidden here? Otherwise for any other error, return 500 is fine.

return

Expand Down
52 changes: 52 additions & 0 deletions st2api/tests/unit/controllers/v1/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,58 @@ def test_delete(self):
del_resp = self.__do_delete(self.__get_action_id(post_resp))
self.assertEqual(del_resp.status_int, 204)

@mock.patch.object(
action_validator, "validate_action", mock.MagicMock(return_value=True)
)
def test_delete_permission_error_and_action_reregistered_to_database(self):
post_resp = self.__do_post(ACTION_1)

with mock.patch(
"st2api.controllers.v1.actions.delete_action_files_from_pack"
) as mock_remove_files:
msg = "No permission to delete action files from disk"
mock_remove_files.side_effect = PermissionError(msg)
del_resp = self.__do_delete(
self.__get_action_id(post_resp), expect_errors=True
)
self.assertEqual(del_resp.status_int, 403)
self.assertEqual(del_resp.json["faultstring"], msg)

# retrieving reregistered action
get_resp = self.__do_get_actions_by_url_parameter("name", ACTION_1["name"])
expected_uid = post_resp.json["uid"]
actual_uid = get_resp.json[0]["uid"]
self.assertEqual(actual_uid, expected_uid)
action_id = get_resp.json[0]["id"]
del_resp = self.__do_delete(action_id)
self.assertEqual(del_resp.status_int, 204)

@mock.patch.object(
action_validator, "validate_action", mock.MagicMock(return_value=True)
)
def test_delete_exception_and_action_reregistered_to_database(self):
post_resp = self.__do_post(ACTION_1)

with mock.patch(
"st2api.controllers.v1.actions.delete_action_files_from_pack"
) as mock_remove_files:
msg = "Exception encountered during removing files from disk"
mock_remove_files.side_effect = Exception(msg)
del_resp = self.__do_delete(
self.__get_action_id(post_resp), expect_errors=True
)
self.assertEqual(del_resp.status_int, 500)
self.assertEqual(del_resp.json["faultstring"], msg)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a check to make sure that ACTION_1 is added back to the database?


# retrieving reregistered action
get_resp = self.__do_get_actions_by_url_parameter("name", ACTION_1["name"])
expected_uid = post_resp.json["uid"]
actual_uid = get_resp.json[0]["uid"]
self.assertEqual(actual_uid, expected_uid)
action_id = get_resp.json[0]["id"]
del_resp = self.__do_delete(action_id)
self.assertEqual(del_resp.status_int, 204)

@mock.patch.object(
action_validator, "validate_action", mock.MagicMock(return_value=True)
)
Expand Down