fix: more changes to get the tests to pass

wip_analystdata
Raphaël Vinot 2024-05-06 14:40:25 +02:00
parent 902ed5a92c
commit 10ca6f191a
3 changed files with 29 additions and 20 deletions

View File

@ -634,10 +634,8 @@ class PyMISP:
""" """
type = analyst_data.classObjectType type = analyst_data.classObjectType
if analyst_data_id is None: if analyst_data_id is None:
adid = get_uuid_or_id_from_abstract_misp(analyst_data) analyst_data_id = get_uuid_or_id_from_abstract_misp(analyst_data)
else: r = self._prepare_request('POST', f'analyst_data/edit/{type}/{analyst_data_id}', data=analyst_data)
adid = get_uuid_or_id_from_abstract_misp(analyst_data_id)
r = self._prepare_request('POST', f'analyst_data/edit/{type}/{adid}', data=analyst_data)
updated_analyst_data = self._check_json_response(r) updated_analyst_data = self._check_json_response(r)
if not (self.global_pythonify or pythonify) or 'errors' in updated_analyst_data: if not (self.global_pythonify or pythonify) or 'errors' in updated_analyst_data:
return updated_analyst_data return updated_analyst_data

View File

@ -84,11 +84,20 @@ class AnalystDataBehaviorMixin(AbstractMISP):
def from_dict(self, **kwargs) -> None: # type: ignore[no-untyped-def] def from_dict(self, **kwargs) -> None: # type: ignore[no-untyped-def]
if 'Note' in kwargs and kwargs.get('Note'): if 'Note' in kwargs and kwargs.get('Note'):
self.add_note(**kwargs.pop('Note')) for note in kwargs.pop('Note'):
note.pop('object_uuid', None)
note.pop('object_type', None)
self.add_note(**note)
if 'Opinion' in kwargs and kwargs.get('Opinion'): if 'Opinion' in kwargs and kwargs.get('Opinion'):
self.add_opinion(**kwargs.pop('Opinion')) for opinion in kwargs.pop('Opinion'):
opinion.pop('object_uuid', None)
opinion.pop('object_type', None)
self.add_opinion(**opinion)
if 'Relationship' in kwargs and kwargs.get('Relationship'): if 'Relationship' in kwargs and kwargs.get('Relationship'):
self.add_relationship(**kwargs.pop('Relationship')) for relationship in kwargs.pop('Relationship'):
relationship.pop('object_uuid', None)
relationship.pop('object_type', None)
self.add_relationship(**relationship)
super().from_dict(**kwargs) super().from_dict(**kwargs)
@ -2510,7 +2519,6 @@ class MISPNote(AnalystDataBehaviorMixin, MISPAnalystData):
self.note = kwargs.pop('note', None) self.note = kwargs.pop('note', None)
if self.note is None: if self.note is None:
raise NewNoteError('The text note of the note is required.') raise NewNoteError('The text note of the note is required.')
super().from_dict(**kwargs) super().from_dict(**kwargs)
def __repr__(self) -> str: def __repr__(self) -> str:

View File

@ -26,7 +26,7 @@ try:
MISPSharingGroup, MISPFeed, MISPServer, MISPUserSetting, MISPSharingGroup, MISPFeed, MISPServer, MISPUserSetting,
MISPEventReport, MISPCorrelationExclusion, MISPGalaxyCluster, MISPEventReport, MISPCorrelationExclusion, MISPGalaxyCluster,
MISPGalaxy, MISPOrganisationBlocklist, MISPEventBlocklist, MISPGalaxy, MISPOrganisationBlocklist, MISPEventBlocklist,
MISPNote, MISPOpinion, MISPRelationship) MISPNote)
from pymisp.tools import CSVLoader, DomainIPObject, ASNObject, GenericObjectGenerator from pymisp.tools import CSVLoader, DomainIPObject, ASNObject, GenericObjectGenerator
except ImportError: except ImportError:
raise raise
@ -3200,6 +3200,7 @@ class TestComprehensive(unittest.TestCase):
def test_analyst_data_CRUD(self) -> None: def test_analyst_data_CRUD(self) -> None:
event = self.create_simple_event() event = self.create_simple_event()
self.admin_misp_connector.toggle_global_pythonify()
try: try:
fake_uuid = str(uuid4()) fake_uuid = str(uuid4())
new_note1 = MISPNote() new_note1 = MISPNote()
@ -3221,12 +3222,12 @@ class TestComprehensive(unittest.TestCase):
# Fetch newly added node # Fetch newly added node
new_note1 = self.user_misp_connector.get_note(new_note1) new_note1 = self.user_misp_connector.get_note(new_note1)
# The Opinion shoud be able to be created via the Note # The Opinion shoud be able to be created via the Note
self.assertTrue(new_note1.Opinion[0].opinion == new_opinion.opinion) self.assertTrue(new_note1.opinions[0].opinion == new_opinion.opinion)
response = self.user_misp_connector.delete_note(new_note1) response = self.user_misp_connector.delete_note(new_note1)
# The Note should be deletable # The Note should be deletable
self.assertTrue(response['success']) self.assertTrue(response['success'])
self.assertEqual(response['message'], f'Note deleted.') self.assertEqual(response['message'], 'Note deleted.')
# The Opinion should not be deleted # The Opinion should not be deleted
opinion_resp = self.user_misp_connector.get_opinion(new_opinion) opinion_resp = self.user_misp_connector.get_opinion(new_opinion)
self.assertTrue(opinion_resp.opinion == new_opinion.opinion) self.assertTrue(opinion_resp.opinion == new_opinion.opinion)
@ -3239,8 +3240,9 @@ class TestComprehensive(unittest.TestCase):
self.assertTrue(new_note.object_uuid == event.uuid) self.assertTrue(new_note.object_uuid == event.uuid)
event = self.user_misp_connector.get_event(event) event = self.user_misp_connector.get_event(event)
print(event.to_json(indent=2))
# The Note should be present on the event # The Note should be present on the event
self.assertTrue(event.Note[0].object_uuid == event.uuid) self.assertTrue(event.notes[0].object_uuid == event.uuid)
finally: finally:
self.admin_misp_connector.delete_event(event) self.admin_misp_connector.delete_event(event)
@ -3248,7 +3250,7 @@ class TestComprehensive(unittest.TestCase):
self.admin_misp_connector.delete_opinion(new_opinion) self.admin_misp_connector.delete_opinion(new_opinion)
self.admin_misp_connector.delete_note(new_note) self.admin_misp_connector.delete_note(new_note)
self.admin_misp_connector.delete_note(new_note1) # Should already be deleted self.admin_misp_connector.delete_note(new_note1) # Should already be deleted
except: except Exception:
pass pass
def test_analyst_data_ACL(self) -> None: def test_analyst_data_ACL(self) -> None:
@ -3260,6 +3262,7 @@ class TestComprehensive(unittest.TestCase):
sharing_group = self.admin_misp_connector.add_sharing_group(sg, pythonify=True) sharing_group = self.admin_misp_connector.add_sharing_group(sg, pythonify=True)
# Chec that sharing group was created # Chec that sharing group was created
self.assertEqual(sharing_group.name, 'Testcases SG') self.assertEqual(sharing_group.name, 'Testcases SG')
self.admin_misp_connector.toggle_global_pythonify()
try: try:
new_note: MISPNote = event.add_note(note='Test Note', language='en') new_note: MISPNote = event.add_note(note='Test Note', language='en')
@ -3272,7 +3275,7 @@ class TestComprehensive(unittest.TestCase):
event = self.admin_misp_connector.get_event(event) event = self.admin_misp_connector.get_event(event)
# The note should be visible for the creator # The note should be visible for the creator
self.assertEqual(len(event.Note), 1) self.assertEqual(len(event.notes), 1)
self.assertTrue(new_note.note == "Test Note") self.assertTrue(new_note.note == "Test Note")
resp = self.user_misp_connector.get_note(new_note) resp = self.user_misp_connector.get_note(new_note)
@ -3329,7 +3332,7 @@ class TestComprehensive(unittest.TestCase):
self.admin_misp_connector.delete_sharing_group(sharing_group.id) self.admin_misp_connector.delete_sharing_group(sharing_group.id)
self.admin_misp_connector.delete_organisation(fake_org) self.admin_misp_connector.delete_organisation(fake_org)
self.admin_misp_connector.delete_note(new_note) self.admin_misp_connector.delete_note(new_note)
except: except Exception:
pass pass
@unittest.skip("Internal use only") @unittest.skip("Internal use only")