Skip to content

Commit

Permalink
[FIX] Unlink comments when deleting an asset
Browse files Browse the repository at this point in the history
  • Loading branch information
c8y3 committed Oct 18, 2024
1 parent c615e78 commit 64eb513
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions source/app/datamgmt/case/case_assets_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,22 @@ def delete_asset(asset_id, caseid):
if case_asset.case_id and case_asset.alerts is not None:
delete_ioc_asset_link(asset_id)

# Delete the relevant records from the CaseEventsAssets table
CaseEventsAssets.query.filter(
case_asset.asset_id == CaseEventsAssets.asset_id
).delete()

# Delete the relevant records from the AssetComments table
com_ids = AssetComments.query.with_entities(
AssetComments.comment_id
).filter(
AssetComments.comment_asset_id == asset_id,
).all()

com_ids = [c.comment_id for c in com_ids]
AssetComments.query.filter(AssetComments.comment_id.in_(com_ids)).delete()

# Directly delete the relevant records from the CaseAssets table
CaseAssets.query.filter(
CaseAssets.asset_id == asset_id,
CaseAssets.case_id == caseid
Expand Down
9 changes: 9 additions & 0 deletions tests/tests_rest_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,12 @@ def test_delele_asset_should_not_fail_when_it_is_linked_to_an_ioc(self):
asset_identifier = response['asset_id']
response = self._subject.delete(f'/api/v2/assets/{asset_identifier}')
self.assertEqual(204, response.status_code)

def test_delete_asset_should_not_fail_when_it_has_associated_comments(self):
case_identifier = self._subject.create_dummy_case()
body = {'asset_type_id': '1', 'asset_name': 'admin_laptop_test'}
response = self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body).json()
asset_identifier = response['asset_id']
self._subject.create(f'/case/assets/{asset_identifier}/comments/add', {'comment_text': 'comment text'})
response = self._subject.delete(f'/api/v2/assets/{asset_identifier}')
self.assertEqual(204, response.status_code)

0 comments on commit 64eb513

Please sign in to comment.