Skip to content

Commit 2dcf46d

Browse files
tst: move all library-related tests to the dedicated test file
1 parent 28aed78 commit 2dcf46d

File tree

2 files changed

+51
-67
lines changed

2 files changed

+51
-67
lines changed

intelmq/tests/lib/test_bot.py

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66
"""
77
Tests the Bot class itself.
88
"""
9-
import json
109
import unittest
1110

1211
import intelmq.lib.test as test
1312
from intelmq.tests.lib import test_parser_bot
14-
from intelmq.lib.message import MessageFactory, Message
15-
from intelmq.lib.bot import BotLibSettings
1613

1714

1815
class TestDummyParserBot(test.BotTestCase, unittest.TestCase):
@@ -81,65 +78,5 @@ def test_invalid_value_on_input_message(self):
8178
self.assertEqual(self.pipe.state['test-bot-output'], [])
8279

8380

84-
def send_message(self, *messages, path: str = "_default", auto_add=None,
85-
path_permissive: bool = False):
86-
self._sent_messages.extend(messages)
87-
88-
89-
class TestBotAsLibrary(unittest.TestCase):
90-
def assertMessageEqual(self, actual, expected):
91-
"""
92-
Compare two messages as dicts.
93-
"""
94-
if isinstance(actual, Message):
95-
actual = actual.to_dict(with_type=True)
96-
else:
97-
actual = actual.copy()
98-
99-
if isinstance(expected, Message):
100-
expected = expected.to_dict(with_type=True)
101-
else:
102-
expected = expected.copy()
103-
104-
if 'time.observation' in actual:
105-
del actual['time.observation']
106-
if 'time.observation' in expected:
107-
del expected['time.observation']
108-
if 'output' in actual:
109-
actual['output'] = json.loads(actual['output'])
110-
if 'output' in expected:
111-
expected['output'] = json.loads(expected['output'])
112-
113-
self.assertDictEqual(actual, expected)
114-
115-
"""def test_dummy_mocked(self):
116-
bot = test_parser_bot.DummyParserBot('dummy-bot', settings={'global': {'logging_path': None, 'skip_pipeline': True, 'broker': 'pythonlist'}, 'dummy-bot': {}})
117-
bot._Bot__current_message = MessageFactory.from_dict(test_parser_bot.EXAMPLE_REPORT)
118-
bot._Bot__connect_pipelines = lambda self: None
119-
bot._sent_messages = []
120-
bot._dumped_messages = []
121-
bot.send_message = send_message.__get__(bot, test_parser_bot.DummyParserBot)
122-
bot._dump_message = _dump_message.__get__(bot, test_parser_bot.DummyParserBot)
123-
bot.process()
124-
assert bot._sent_messages == [MessageFactory.from_dict(test_parser_bot.EXAMPLE_EVENT)]
125-
assert bot._dumped_messages[0][1] == test_parser_bot.EXPECTED_DUMP[0]
126-
assert bot._dumped_messages[1][1] == test_parser_bot.EXPECTED_DUMP[1]"""
127-
128-
def test_dummy_pythonlist(self):
129-
bot = test_parser_bot.DummyParserBot('dummy-bot', settings=BotLibSettings)
130-
sent_messages = bot.process_message(test_parser_bot.EXAMPLE_REPORT.copy())
131-
self.assertMessageEqual(sent_messages['output'][0], test_parser_bot.EXAMPLE_EVENT)
132-
self.assertMessageEqual(sent_messages['error'][0], MessageFactory.from_dict(test_parser_bot.EXPECTED_DUMP[0].copy(), default_type='Report'))
133-
self.assertMessageEqual(sent_messages['error'][1], MessageFactory.from_dict(test_parser_bot.EXPECTED_DUMP[1].copy(), default_type='Report'))
134-
135-
def test_domain_suffix(self):
136-
from intelmq.bots.experts.domain_suffix.expert import DomainSuffixExpertBot
137-
domain_suffix = DomainSuffixExpertBot('domain-suffix',
138-
settings=BotLibSettings | {'field': 'fqdn',
139-
'suffix_file': '/usr/share/publicsuffix/public_suffix_list.dat'})
140-
queues = domain_suffix.process_message({'source.fqdn': 'www.example.com'})
141-
assert queues['output'][0]['source.domain_suffix'] == 'com'
142-
143-
14481
if __name__ == '__main__': # pragma: no cover
14582
unittest.main()

intelmq/tests/lib/test_bot_library_mode.py

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@
99
1010
This tests IntelMQ bots in library mode (IEP007)
1111
"""
12+
import json
1213
import unittest
14+
from os.path import dirname, join
1315

14-
from intelmq.lib.bot import Dict39, BotLibSettings
15-
16-
from intelmq.bots.experts.url.expert import URLExpertBot
17-
from intelmq.bots.experts.taxonomy.expert import TaxonomyExpertBot
16+
import intelmq.tests.bots.experts.domain_suffix.test_expert as domain_suffix_expert_test
17+
from intelmq.bots.experts.domain_suffix.expert import DomainSuffixExpertBot
1818
from intelmq.bots.experts.jinja.expert import JinjaExpertBot
19+
from intelmq.bots.experts.taxonomy.expert import TaxonomyExpertBot
20+
from intelmq.bots.experts.url.expert import URLExpertBot
21+
from intelmq.lib.bot import BotLibSettings, Dict39
22+
from intelmq.lib.message import Message, MessageFactory
23+
from intelmq.tests.lib import test_parser_bot
1924

2025
EXAMPLE_DATA_URL = Dict39({'source.url': 'http://example.com/'})
2126
EXAMPLE_DATA_URL_OUT = EXAMPLE_DATA_URL | {'source.fqdn': 'example.com',
@@ -30,6 +35,48 @@
3035
})
3136

3237

38+
def assertMessageEqual(actual, expected):
39+
"""
40+
Compare two messages as dicts.
41+
"""
42+
if isinstance(actual, Message):
43+
actual = actual.to_dict(with_type=True)
44+
else:
45+
actual = actual.copy()
46+
47+
if isinstance(expected, Message):
48+
expected = expected.to_dict(with_type=True)
49+
else:
50+
expected = expected.copy()
51+
52+
if 'time.observation' in actual:
53+
del actual['time.observation']
54+
if 'time.observation' in expected:
55+
del expected['time.observation']
56+
if 'output' in actual:
57+
actual['output'] = json.loads(actual['output'])
58+
if 'output' in expected:
59+
expected['output'] = json.loads(expected['output'])
60+
61+
assert actual == expected
62+
63+
64+
def test_dummy_parser_bot():
65+
bot = test_parser_bot.DummyParserBot('dummy-bot', settings=BotLibSettings)
66+
sent_messages = bot.process_message(test_parser_bot.EXAMPLE_REPORT.copy())
67+
assertMessageEqual(sent_messages['output'][0], test_parser_bot.EXAMPLE_EVENT)
68+
assertMessageEqual(sent_messages['error'][0], MessageFactory.from_dict(test_parser_bot.EXPECTED_DUMP[0].copy(), default_type='Report'))
69+
assertMessageEqual(sent_messages['error'][1], MessageFactory.from_dict(test_parser_bot.EXPECTED_DUMP[1].copy(), default_type='Report'))
70+
71+
72+
def test_domain_suffix():
73+
domain_suffix = DomainSuffixExpertBot('domain-suffix',
74+
settings=BotLibSettings | {'field': 'fqdn',
75+
'suffix_file': join(dirname(domain_suffix_expert_test.__file__), 'public_suffix_list.dat')})
76+
queues = domain_suffix.process_message({'source.fqdn': 'www.example.com'})
77+
assert queues['output'][0]['source.domain_suffix'] == 'example.com'
78+
79+
3380
def test_url_expert():
3481
url_expert = URLExpertBot('url', settings=BotLibSettings)
3582
queues = url_expert.process_message(EXAMPLE_DATA_URL.copy())

0 commit comments

Comments
 (0)