PyMISP/tests/test_offline.py

73 lines
2.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# -*- coding: utf-8 -*-
2016-08-18 00:23:49 +02:00
import unittest
import requests_mock
import json
2016-08-18 00:23:49 +02:00
import pymisp
from pymisp import PyMISP
@requests_mock.Mocker()
2016-08-18 00:23:49 +02:00
class TestOffline(unittest.TestCase):
def setUp(self):
2016-08-18 00:40:30 +02:00
self.maxDiff = None
self.domain = 'http://misp.local/'
self.key = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
2016-08-18 00:40:30 +02:00
self.event = {'Event': json.load(open('tests/misp_event.json', 'r'))}
2016-08-17 18:36:01 +02:00
self.types = json.load(open('tests/describeTypes.json', 'r'))
def initURI(self, m):
2016-08-18 00:23:49 +02:00
m.register_uri('GET', self.domain + 'servers/getVersion', json={"version": pymisp.__version__[1:]})
m.register_uri('GET', self.domain + 'attributes/describeTypes.json', json=self.types)
m.register_uri('GET', self.domain + 'events/2', json=self.event)
2016-08-18 00:23:49 +02:00
m.register_uri('POST', self.domain + 'events/2', json=self.event)
m.register_uri('DELETE', self.domain + 'events/2', json={'message': 'Event deleted.'})
m.register_uri('DELETE', self.domain + 'events/3', json={'errors': ['Invalid event'], 'message': 'Invalid event', 'name': 'Invalid event', 'url': '/events/3'})
2016-08-18 00:40:30 +02:00
m.register_uri('DELETE', self.domain + 'attributes/2', json={'message': 'Attribute deleted.'})
def test_getEvent(self, m):
self.initURI(m)
pymisp = PyMISP(self.domain, self.key, debug=True)
2016-08-18 00:23:49 +02:00
e1 = pymisp.get_event(2)
e2 = pymisp.get(2)
self.assertEqual(e1, e2)
self.assertEqual(self.event, e2)
def test_updateEvent(self, m):
self.initURI(m)
pymisp = PyMISP(self.domain, self.key, debug=True)
e0 = pymisp.update_event(2, json.dumps(self.event))
e1 = pymisp.update_event(2, self.event)
self.assertEqual(e0, e1)
2016-08-18 00:40:30 +02:00
e2 = pymisp.update(e0)
2016-08-18 00:23:49 +02:00
self.assertEqual(e1, e2)
self.assertEqual(self.event, e2)
def test_deleteEvent(self, m):
self.initURI(m)
pymisp = PyMISP(self.domain, self.key, debug=True)
d = pymisp.delete_event(2)
self.assertEqual(d, {'message': 'Event deleted.'})
d = pymisp.delete_event(3)
self.assertEqual(d, {'errors': ['Invalid event'], 'message': 'Invalid event', 'name': 'Invalid event', 'url': '/events/3'})
def test_deleteAttribute(self, m):
# FIXME: https://github.com/MISP/MISP/issues/1449
self.initURI(m)
pymisp = PyMISP(self.domain, self.key, debug=True)
d = pymisp.delete_attribute(2)
2016-08-18 00:40:30 +02:00
self.assertEqual(d, {'message': 'Attribute deleted.'})
def test_publish(self, m):
self.initURI(m)
pymisp = PyMISP(self.domain, self.key, debug=True)
e = pymisp.publish(self.event)
pub = self.event
pub['Event']['published'] = True
self.assertEqual(e, pub)
e = pymisp.publish(self.event)
self.assertEqual(e, {'error': 'Already published'})