From 07f00a68f126efde4fb1b718412b1feda1efef08 Mon Sep 17 00:00:00 2001 From: Tom King Date: Fri, 15 Jan 2021 15:26:41 +0000 Subject: [PATCH] chg: Allow response of delete to be pythonify, add in nosetest --- pymisp/api.py | 9 ++++++-- tests/testlive_comprehensive.py | 40 ++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/pymisp/api.py b/pymisp/api.py index 15bed32..22fbf82 100644 --- a/pymisp/api.py +++ b/pymisp/api.py @@ -460,7 +460,7 @@ class PyMISP: er.from_dict(**updated_event_report) return er - def delete_event_report(self, event_report: Union[MISPEventReport, int, str, UUID], hard: bool = False) -> Dict: + def delete_event_report(self, event_report: Union[MISPEventReport, int, str, UUID], hard: bool = False, pythonify: bool = False) -> Dict: """Delete an event report from a MISP instance :param event_report: event report to delete @@ -472,7 +472,12 @@ class PyMISP: request_url += "/1" r = self._prepare_request('POST', request_url) response = self._check_json_response(r) - return response + if not (self.global_pythonify or pythonify) or 'errors' in response or hard: + # Hard will permanently delete, must return JSON + return response + er = MISPEventReport() + er.from_dict(**response) + return er # ## END Event Report ### diff --git a/tests/testlive_comprehensive.py b/tests/testlive_comprehensive.py index 13ef9cc..93e8c0c 100644 --- a/tests/testlive_comprehensive.py +++ b/tests/testlive_comprehensive.py @@ -27,7 +27,7 @@ logger = logging.getLogger('pymisp') try: - from pymisp import register_user, PyMISP, MISPEvent, MISPOrganisation, MISPUser, Distribution, ThreatLevel, Analysis, MISPObject, MISPAttribute, MISPSighting, MISPShadowAttribute, MISPTag, MISPSharingGroup, MISPFeed, MISPServer, MISPUserSetting, MISPEventBlocklist + from pymisp import register_user, PyMISP, MISPEvent, MISPOrganisation, MISPUser, Distribution, ThreatLevel, Analysis, MISPObject, MISPAttribute, MISPSighting, MISPShadowAttribute, MISPTag, MISPSharingGroup, MISPFeed, MISPServer, MISPUserSetting, MISPEventBlocklist, MISPEventReport from pymisp.tools import CSVLoader, DomainIPObject, ASNObject, GenericObjectGenerator from pymisp.exceptions import MISPServerError except ImportError: @@ -2574,6 +2574,44 @@ class TestComprehensive(unittest.TestCase): for blo in to_delete['bl_organisations']: self.admin_misp_connector.delete_organisation_blocklist(blo) + def test_event_report(self): + event = self.create_simple_event() + new_event_report = MISPEventReport() + new_event_report.name = "Test Event Report" + new_event_report.content = "# Example report markdown" + new_event_report.distribution = 5 # Inherit + try: + event = self.user_misp_connector.add_event(event) + new_event_report = self.user_misp_connector.add_event_report(event.id, new_event_report) + # The event report should be linked by Event ID + self.assertEqual(event.id, new_event_report.event_id) + + event = self.user_misp_connector.get_event(event) + # The Event Report should be present on the event + self.assertEqual(new_event_report.id, event.event_reports[0].id) + + new_event_report.name = "Updated Event Report" + new_event_report.content = "Updated content" + new_event_report = self.user_misp_connector.update_event_report(new_event_report) + # The event report should be updatable + self.assertTrue(new_event_report.name == "Updated Event Report") + self.assertTrue(new_event_report.content == "Updated content") + + event_reports = self.user_misp_connector.get_event_reports(event.id) + # The event report should be requestable by the Event ID + self.assertEqual(new_event_report.id, event_reports[0].id) + + new_event_report = self.user_misp_connector.delete_event_report(new_event_report) + # The event report should be soft-deletable + self.assertTrue(new_event_report.deleted) + + response = self.user_misp_connector.delete_event_report(new_event_report, True) + self.assertTrue(response['success']) + finally: + self.user_misp_connector.delete_event(event) + self.user_misp_connector.delete_event_report(new_event_report) + + @unittest.skip("Internal use only") def missing_methods(self): skip = [