From 3018bae016ae2e43836ccf38f242f56292f9fb8c Mon Sep 17 00:00:00 2001 From: tomchop Date: Thu, 9 Nov 2023 11:29:56 +0000 Subject: [PATCH] Delete API endpoints --- core/web/apiv2/entities.py | 7 +++++++ core/web/apiv2/indicators.py | 7 +++++++ tests/apiv2/entities.py | 7 +++++++ tests/apiv2/indicators.py | 6 ++++++ 4 files changed, 27 insertions(+) diff --git a/core/web/apiv2/entities.py b/core/web/apiv2/entities.py index 754739c4f..2d1ef6035 100644 --- a/core/web/apiv2/entities.py +++ b/core/web/apiv2/entities.py @@ -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: diff --git a/core/web/apiv2/indicators.py b/core/web/apiv2/indicators.py index 3b7ba2908..e86109a77 100644 --- a/core/web/apiv2/indicators.py +++ b/core/web/apiv2/indicators.py @@ -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: diff --git a/tests/apiv2/entities.py b/tests/apiv2/entities.py index b85722d1e..1b48217c6 100644 --- a/tests/apiv2/entities.py +++ b/tests/apiv2/entities.py @@ -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) diff --git a/tests/apiv2/indicators.py b/tests/apiv2/indicators.py index 3461f8f0c..cad240685 100644 --- a/tests/apiv2/indicators.py +++ b/tests/apiv2/indicators.py @@ -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)