Skip to content

Commit 62ce61c

Browse files
TST: add and update tests for correct line recovery
1 parent 5a75d58 commit 62ce61c

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

intelmq/tests/lib/test_parser_bot.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
RAW = """# ignore this
1717
2015/06/04 13:37 +00,example.org,192.0.2.3,reverse.example.net,example description,[email protected],1
1818
19-
2015/06/04 13:38 +00,example.org,19d2.0.2.3,reverse.example.net,example description,[email protected],1
19+
2015/06/04 13:38 +00,example.org,19a2.0.2.3,reverse.example.net,example description,[email protected],1
20+
2015/06/04 13:38 +00,example.org,19b2.0.2.3,reverse.example.net,example description,[email protected],1
2021
#ending line"""
2122
RAW_SPLIT = RAW.splitlines()
2223

@@ -39,11 +40,10 @@
3940
"feed.name": "Example",
4041
"raw": utils.base64_encode('\n'.join(RAW_SPLIT[:2]))}
4142

42-
EXPECTED_DUMP = EXAMPLE_REPORT.copy()
43-
del EXPECTED_DUMP['__type']
44-
EXPECTED_DUMP['raw'] = base64.b64encode(b'''# ignore this
45-
2015/06/04 13:38 +00,example.org,19d2.0.2.3,reverse.example.net,example description,[email protected],1
46-
#ending line''').decode()
43+
EXPECTED_DUMP = [EXAMPLE_REPORT.copy(), EXAMPLE_REPORT.copy()]
44+
del EXPECTED_DUMP[0]['__type'], EXPECTED_DUMP[1]['__type']
45+
EXPECTED_DUMP[0]['raw'] = base64.b64encode('\n'.join((RAW_SPLIT[0], RAW_SPLIT[3], RAW_SPLIT[5])).encode()).decode()
46+
EXPECTED_DUMP[1]['raw'] = base64.b64encode('\n'.join((RAW_SPLIT[0], RAW_SPLIT[4], RAW_SPLIT[5])).encode()).decode()
4747
EXAMPLE_EMPTY_REPORT = {"feed.url": "http://www.example.com/",
4848
"__type": "Report",
4949
"feed.name": "Example"}
@@ -129,11 +129,12 @@ class TestDummyParserBot(test.BotTestCase, unittest.TestCase):
129129
def set_bot(cls):
130130
cls.bot_reference = DummyParserBot
131131
cls.default_input_message = EXAMPLE_REPORT
132-
cls.allowed_error_count = 1
133132
cls.sysconfig = {'error_dump_message': True}
133+
cls.call_counter = 0
134134

135135
def dump_message(self, error_traceback, message=None):
136-
self.assertDictEqual(EXPECTED_DUMP, message)
136+
self.assertDictEqual(EXPECTED_DUMP[self.call_counter], message)
137+
self.call_counter += 1
137138

138139
def run_bot(self, *args, **kwargs):
139140
with mock.patch.object(bot.Bot, "_dump_message",
@@ -142,7 +143,7 @@ def run_bot(self, *args, **kwargs):
142143

143144
def test_event(self):
144145
""" Test DummyParserBot """
145-
self.run_bot()
146+
self.run_bot(allowed_error_count=2)
146147
self.assertMessageEqual(0, EXAMPLE_EVENT)
147148

148149
def test_missing_raw(self):
@@ -200,6 +201,13 @@ def test_event(self):
200201
'classification.type': 'other',
201202
},
202203
]
204+
JSON_STREAM_BOGUS_REPORT = {'__type': 'Report',
205+
'raw': utils.base64_encode('''{"a": 1, "ip": "10.0.0.a"}
206+
{"a": 2, "ip": "10.0.0.b"}''')}
207+
JSON_STREAM_BOGUS_DUMP = [
208+
{'raw': utils.base64_encode('{"a": 1, "ip": "10.0.0.a"}')},
209+
{'raw': utils.base64_encode('{"a": 2, "ip": "10.0.0.b"}')}
210+
]
203211

204212

205213
class DummyJSONStreamParserBot(bot.ParserBot):
@@ -210,7 +218,9 @@ def parse_line(self, line, report):
210218
event = self.new_event(report)
211219
event['event_description.text'] = line['a']
212220
event['classification.type'] = 'other'
213-
event['raw'] = self.recover_line(line)
221+
event['raw'] = self.recover_line()
222+
if 'ip' in line:
223+
event['source.ip'] = line['ip']
214224
yield event
215225

216226

@@ -219,12 +229,26 @@ class TestJSONStreamParserBot(test.BotTestCase, unittest.TestCase):
219229
def set_bot(cls):
220230
cls.bot_reference = DummyJSONStreamParserBot
221231
cls.default_input_message = EXAMPLE_JSON_STREAM_REPORT
232+
cls.call_counter = 0
222233

223234
def test_event(self):
224235
self.run_bot()
225236
self.assertMessageEqual(0, EXAMPLE_JSON_STREAM_EVENTS[0])
226237
self.assertMessageEqual(1, EXAMPLE_JSON_STREAM_EVENTS[1])
227238

239+
def dump_message(self, error_traceback, message=None):
240+
self.assertDictEqual(JSON_STREAM_BOGUS_DUMP[self.call_counter], message)
241+
self.call_counter += 1
242+
243+
def run_bot(self, *args, **kwargs):
244+
with mock.patch.object(bot.Bot, "_dump_message",
245+
self.dump_message):
246+
super().run_bot(*args, **kwargs)
247+
248+
def test_dump(self):
249+
self.input_message = JSON_STREAM_BOGUS_REPORT
250+
self.run_bot(allowed_error_count=2)
251+
228252

229253
if __name__ == '__main__': # pragma: no cover
230254
unittest.main()

0 commit comments

Comments
 (0)