Skip to content

Commit

Permalink
Delete API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchop committed Nov 9, 2023
1 parent abd894b commit 3018bae
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core/web/apiv2/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ async def details(entity_id) -> entity.EntityTypes:
db_entity.get_tags()
return db_entity

@router.delete("/{entity_id}")
async def delete(entity_id: str) -> None:
"""Deletes an Entity."""
db_entity = entity.Entity.get(entity_id)
if not db_entity:
raise HTTPException(status_code=404, detail="Entity ID {entity_id} not found")
db_entity.delete()

@router.post("/search")
async def search(request: EntitySearchRequest) -> EntitySearchResponse:
Expand Down
7 changes: 7 additions & 0 deletions core/web/apiv2/indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ async def details(indicator_id) -> indicator.IndicatorTypes:
raise HTTPException(status_code=404, detail="indicator not found")
return db_indicator

@router.delete("/{indicator_id}")
async def delete(indicator_id: str) -> None:
"""Deletes an indicator."""
db_indicator = indicator.Indicator.get(indicator_id)
if not db_indicator:
raise HTTPException(status_code=404, detail="Indicator ID {indicator_id} not found")
db_indicator.delete()

@router.post("/search")
async def search(request: IndicatorSearchRequest) -> IndicatorSearchResponse:
Expand Down
7 changes: 7 additions & 0 deletions tests/apiv2/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,10 @@ def test_patch_entity_type_mismatch(self):
data["detail"],
f"Entity {self.entity1.id} type mismatch. Provided 'malware'. Expected 'threat-actor'",
)

def test_delete_entity(self):
"""Tests that an entity gets deleted."""
response = client.delete(f"/api/v2/entities/{self.entity1.id}")
self.assertEqual(response.status_code, 200)
response = client.get(f"/api/v2/entities/{self.entity1.id}")
self.assertEqual(response.status_code, 404)
6 changes: 6 additions & 0 deletions tests/apiv2/indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,9 @@ def test_search_indicators_subfields(self):
self.assertEqual(len(data["indicators"]), 1)
self.assertEqual(data["indicators"][0]["name"], "hex")
self.assertEqual(data["indicators"][0]["type"], "regex")

def test_delete_indicator(self):
response = client.delete(f"/api/v2/indicators/{self.indicator1.id}")
self.assertEqual(response.status_code, 200)
response = client.get(f"/api/v2/indicators/{self.indicator1.id}")
self.assertEqual(response.status_code, 404)

0 comments on commit 3018bae

Please sign in to comment.