fix: Get the tests to pass.

pull/1227/head
Raphaël Vinot 2024-05-22 12:39:29 +02:00
parent feadedeadb
commit f7b28e7bc9
2 changed files with 28 additions and 35 deletions

View File

@ -34,6 +34,7 @@ class NewNoteError(PyMISPError):
class NewOpinionError(PyMISPError):
pass
class NewRelationshipError(PyMISPError):
pass

View File

@ -79,7 +79,6 @@ class TestComprehensive(unittest.TestCase):
cls.admin_misp_connector.set_server_setting('debug', 1, force=True)
if not fast_mode:
r = cls.admin_misp_connector.update_misp()
print(r)
# Creates an org
organisation = MISPOrganisation()
organisation.name = 'Test Org'
@ -3076,16 +3075,13 @@ class TestComprehensive(unittest.TestCase):
self.user_misp_connector.delete_event_report(new_event_report)
def test_search_galaxy(self) -> None:
self.admin_misp_connector.toggle_global_pythonify()
galaxies: list[MISPGalaxy] = self.admin_misp_connector.galaxies() # type: ignore[assignment]
galaxies: list[MISPGalaxy] = self.admin_misp_connector.galaxies(pythonify=True) # type: ignore[assignment]
galaxy: MISPGalaxy = galaxies[0]
ret = self.admin_misp_connector.search_galaxy(value=galaxy.name)
ret = self.admin_misp_connector.search_galaxy(value=galaxy.name, pythonify=True)
self.assertEqual(len(ret), 1)
self.admin_misp_connector.toggle_global_pythonify()
def test_galaxy_cluster(self) -> None:
self.admin_misp_connector.toggle_global_pythonify()
galaxies: list[MISPGalaxy] = self.admin_misp_connector.galaxies() # type: ignore[assignment]
galaxies: list[MISPGalaxy] = self.admin_misp_connector.galaxies(pythonify=True) # type: ignore[assignment]
galaxy: MISPGalaxy = galaxies[0]
new_galaxy_cluster: MISPGalaxyCluster = MISPGalaxyCluster()
new_galaxy_cluster.value = "Test Cluster"
@ -3094,13 +3090,13 @@ class TestComprehensive(unittest.TestCase):
new_galaxy_cluster.description = "Example test cluster"
try:
if gid := galaxy.id:
galaxy = self.admin_misp_connector.get_galaxy(gid, withCluster=True) # type: ignore[assignment]
galaxy = self.admin_misp_connector.get_galaxy(gid, withCluster=True, pythonify=True) # type: ignore[assignment]
else:
raise Exception("No galaxy found")
existing_galaxy_cluster = galaxy.clusters[0]
if gid := galaxy.id:
new_galaxy_cluster = self.admin_misp_connector.add_galaxy_cluster(gid, new_galaxy_cluster) # type: ignore[assignment]
new_galaxy_cluster = self.admin_misp_connector.add_galaxy_cluster(gid, new_galaxy_cluster, pythonify=True) # type: ignore[assignment]
else:
raise Exception("No galaxy found")
# The new galaxy cluster should be under the selected galaxy
@ -3109,7 +3105,7 @@ class TestComprehensive(unittest.TestCase):
self.assertEqual(new_galaxy_cluster.value, "Test Cluster")
new_galaxy_cluster.add_cluster_element("synonyms", "Test2")
new_galaxy_cluster = self.admin_misp_connector.update_galaxy_cluster(new_galaxy_cluster) # type: ignore[assignment]
new_galaxy_cluster = self.admin_misp_connector.update_galaxy_cluster(new_galaxy_cluster, pythonify=True) # type: ignore[assignment]
# The cluster should have one element that is a synonym
self.assertEqual(len(new_galaxy_cluster.cluster_elements), 1)
@ -3122,22 +3118,22 @@ class TestComprehensive(unittest.TestCase):
# The cluster element should be updatable
element.value = "Test3"
new_galaxy_cluster = self.admin_misp_connector.update_galaxy_cluster(new_galaxy_cluster) # type: ignore[assignment]
new_galaxy_cluster = self.admin_misp_connector.update_galaxy_cluster(new_galaxy_cluster, pythonify=True) # type: ignore[assignment]
element = new_galaxy_cluster.cluster_elements[0]
self.assertEqual(element.value, "Test3")
new_galaxy_cluster.add_cluster_element("synonyms", "ToDelete")
new_galaxy_cluster = self.admin_misp_connector.update_galaxy_cluster(new_galaxy_cluster) # type: ignore[assignment]
new_galaxy_cluster = self.admin_misp_connector.update_galaxy_cluster(new_galaxy_cluster, pythonify=True) # type: ignore[assignment]
# The cluster should have two elements
self.assertEqual(len(new_galaxy_cluster.cluster_elements), 2)
new_galaxy_cluster.cluster_elements = [e for e in new_galaxy_cluster.cluster_elements if e.value != "ToDelete"]
new_galaxy_cluster = self.admin_misp_connector.update_galaxy_cluster(new_galaxy_cluster) # type: ignore[assignment]
new_galaxy_cluster = self.admin_misp_connector.update_galaxy_cluster(new_galaxy_cluster, pythonify=True) # type: ignore[assignment]
# The cluster elements should be deletable
self.assertEqual(len(new_galaxy_cluster.cluster_elements), 1)
new_galaxy_cluster.add_cluster_relation(existing_galaxy_cluster, "is-tested-by")
new_galaxy_cluster = self.admin_misp_connector.update_galaxy_cluster(new_galaxy_cluster) # type: ignore[assignment]
new_galaxy_cluster = self.admin_misp_connector.update_galaxy_cluster(new_galaxy_cluster, pythonify=True) # type: ignore[assignment]
# The cluster should have a relationship
self.assertEqual(len(new_galaxy_cluster.cluster_relations), 1)
relation = new_galaxy_cluster.cluster_relations[0]
@ -3145,7 +3141,7 @@ class TestComprehensive(unittest.TestCase):
self.assertEqual(relation.referenced_galaxy_cluster_uuid, existing_galaxy_cluster.uuid)
relation.add_tag("tlp:amber")
new_galaxy_cluster = self.admin_misp_connector.update_galaxy_cluster(new_galaxy_cluster) # type: ignore[assignment]
new_galaxy_cluster = self.admin_misp_connector.update_galaxy_cluster(new_galaxy_cluster, pythonify=True) # type: ignore[assignment]
relation = new_galaxy_cluster.cluster_relations[0]
# The relationship should have a tag of tlp:amber
self.assertEqual(len(relation.tags), 1)
@ -3155,13 +3151,13 @@ class TestComprehensive(unittest.TestCase):
resp = self.admin_misp_connector.delete_galaxy_cluster_relation(relation)
self.assertTrue(resp['success'])
# The cluster relation should no longer be present
new_galaxy_cluster = self.admin_misp_connector.get_galaxy_cluster(new_galaxy_cluster) # type: ignore[assignment]
new_galaxy_cluster = self.admin_misp_connector.get_galaxy_cluster(new_galaxy_cluster, pythonify=True) # type: ignore[assignment]
self.assertEqual(len(new_galaxy_cluster.cluster_relations), 0)
resp = self.admin_misp_connector.delete_galaxy_cluster(new_galaxy_cluster)
# Galaxy clusters should be soft deletable
self.assertTrue(resp['success'])
new_galaxy_cluster = self.admin_misp_connector.get_galaxy_cluster(new_galaxy_cluster) # type: ignore[assignment]
new_galaxy_cluster = self.admin_misp_connector.get_galaxy_cluster(new_galaxy_cluster, pythonify=True) # type: ignore[assignment]
self.assertTrue(isinstance(new_galaxy_cluster, MISPGalaxyCluster))
resp = self.admin_misp_connector.delete_galaxy_cluster(new_galaxy_cluster, hard=True)
@ -3170,23 +3166,20 @@ class TestComprehensive(unittest.TestCase):
resp = self.admin_misp_connector.get_galaxy_cluster(new_galaxy_cluster) # type: ignore[assignment]
self.assertTrue("errors" in resp)
finally:
self.admin_misp_connector.delete_galaxy_cluster_relation(relation)
self.admin_misp_connector.delete_galaxy_cluster(new_galaxy_cluster, hard=True)
self.admin_misp_connector.toggle_global_pythonify()
pass
def test_event_galaxy(self) -> None:
self.admin_misp_connector.toggle_global_pythonify()
event = self.create_simple_event()
try:
galaxies: list[MISPGalaxy] = self.admin_misp_connector.galaxies() # type: ignore[assignment]
galaxies: list[MISPGalaxy] = self.admin_misp_connector.galaxies(pythonify=True) # type: ignore[assignment]
galaxy: MISPGalaxy = galaxies[0]
if gid := galaxy.id:
galaxy = self.admin_misp_connector.get_galaxy(gid, withCluster=True) # type: ignore[assignment]
galaxy = self.admin_misp_connector.get_galaxy(gid, withCluster=True, pythonify=True) # type: ignore[assignment]
else:
raise Exception("No galaxy found")
galaxy_cluster: MISPGalaxyCluster = galaxy.clusters[0]
event.add_tag(galaxy_cluster.tag_name)
event = self.admin_misp_connector.add_event(event)
event = self.admin_misp_connector.add_event(event, pythonify=True)
# The event should have a galaxy attached
self.assertEqual(len(event.galaxies), 1)
event_galaxy = event.galaxies[0]
@ -3196,7 +3189,6 @@ class TestComprehensive(unittest.TestCase):
self.assertEqual(event_galaxy.clusters[0].id, galaxy_cluster.id)
finally:
self.admin_misp_connector.delete_event(event)
self.admin_misp_connector.toggle_global_pythonify()
def test_attach_galaxy_cluster(self) -> None:
event = self.create_simple_event()
@ -3209,7 +3201,8 @@ class TestComprehensive(unittest.TestCase):
else:
raise Exception("No galaxy found")
galaxy_cluster: MISPGalaxyCluster = galaxy.clusters[0]
self.admin_misp_connector.attach_galaxy_cluster(event, galaxy_cluster)
response = self.admin_misp_connector.attach_galaxy_cluster(event, galaxy_cluster)
self.assertTrue(response['saved'])
event = self.admin_misp_connector.get_event(event.id, pythonify=True)
self.assertEqual(len(event.galaxies), 1)
@ -3222,9 +3215,11 @@ class TestComprehensive(unittest.TestCase):
galaxy_cluster: MISPGalaxyCluster = galaxy.clusters[1]
# Test on attribute
attribute = event.Attribute[0]
attribute = event.attributes[0]
response = self.admin_misp_connector.attach_galaxy_cluster(attribute, galaxy_cluster)
self.assertTrue(response['saved'])
event = self.admin_misp_connector.get_event(event.id, pythonify=True)
attribute = event.Attribute[0]
attribute = event.attributes[0]
self.assertEqual(len(attribute.galaxies), 1)
attribute_galaxy = attribute.galaxies[0]
# The galaxy ID should equal the galaxy from which the cluster came from
@ -3233,11 +3228,9 @@ class TestComprehensive(unittest.TestCase):
self.assertEqual(attribute_galaxy.clusters[0].id, galaxy_cluster.id)
finally:
self.admin_misp_connector.delete_event(event)
self.admin_misp_connector.toggle_global_pythonify()
def test_analyst_data_CRUD(self) -> None:
event = self.create_simple_event()
self.admin_misp_connector.toggle_global_pythonify()
try:
fake_uuid = str(uuid4())
new_note1 = MISPNote()
@ -3298,18 +3291,17 @@ class TestComprehensive(unittest.TestCase):
sharing_group = self.admin_misp_connector.add_sharing_group(sg, pythonify=True)
# Chec that sharing group was created
self.assertEqual(sharing_group.name, 'Testcases SG')
self.admin_misp_connector.toggle_global_pythonify()
try:
new_note: MISPNote = event.add_note(note='Test Note', language='en')
new_note.distribution = 0 # Org only
event = self.admin_misp_connector.add_event(event)
event = self.admin_misp_connector.add_event(event, pythonify=True)
# The note should be linked by Event UUID
self.assertEqual(new_note.object_type, 'Event')
self.assertEqual(event.uuid, new_note.object_uuid)
event = self.admin_misp_connector.get_event(event)
event = self.admin_misp_connector.get_event(event, pythonify=True)
# The note should be visible for the creator
self.assertEqual(len(event.notes), 1)
self.assertTrue(new_note.note == "Test Note")
@ -3322,10 +3314,10 @@ class TestComprehensive(unittest.TestCase):
# The Note attached to the event should not be visible for another org than the creator
self.assertEqual(len(event.Note), 0)
new_note = self.admin_misp_connector.get_note(new_note)
new_note = self.admin_misp_connector.get_note(new_note, pythonify=True)
new_note.distribution = 4
new_note.sharing_group_id = sharing_group.id
new_note = self.admin_misp_connector.update_note(new_note)
new_note = self.admin_misp_connector.update_note(new_note, pythonify=True)
self.assertEqual(int(new_note.sharing_group_id), int(sharing_group.id))
event = self.user_misp_connector.get_event(event)