Skip to content

Commit d7675bb

Browse files
creideikiWagner
authored andcommitted
TST: Test JSON parser in Jinja2 templates
1 parent 8cc7f83 commit d7675bb

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

intelmq/tests/bots/outputs/templated_smtp/test_output.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,57 @@ def test_multiple_recipients_event(self):
131131
self.assertEqual({"from_addr": "myself", "to_addrs": ["[email protected]", "[email protected]"]},
132132
SENT_MESSAGE[1])
133133

134+
def test_json_parsing(self):
135+
event = EVENT.copy()
136+
event["output"] = json.dumps(
137+
{
138+
"title": "Test title",
139+
"lines": [
140+
{
141+
"time": "2021-10-11 12:13Z",
142+
"log": "An event occurred"
143+
},
144+
{
145+
"time": "2021-10-14 15:16Z",
146+
"log": "Another event occurred"
147+
}
148+
]
149+
}
150+
)
151+
self.input_message = event
152+
with unittest.mock.patch("smtplib.SMTP.send_message", new=send_message), \
153+
unittest.mock.patch("smtplib.SMTP.connect", return_value=(220, "Mock server")), \
154+
unittest.mock.patch("smtplib.SMTP.close"):
155+
self.run_bot(parameters={"body": """
156+
{%- set output = from_json(event['output']) %}
157+
{{ output['title'] }}
158+
159+
{% for line in output['lines'] -%}
160+
{{ line['time'] }} : {{ line['log'] }}
161+
{% endfor -%}
162+
"""})
163+
self.assertEqual(SENT_MESSAGE[0].get_payload()[0].get_payload(), """
164+
Test title
165+
166+
2021-10-11 12:13Z : An event occurred
167+
2021-10-14 15:16Z : Another event occurred
168+
""")
169+
170+
def test_malformed_json(self):
171+
event = EVENT.copy()
172+
event["output"] = "{ malformed"
173+
self.input_message = event
174+
with unittest.mock.patch("smtplib.SMTP.send_message", new=send_message), \
175+
unittest.mock.patch("smtplib.SMTP.connect", return_value=(220, "Mock server")), \
176+
unittest.mock.patch("smtplib.SMTP.close"):
177+
self.run_bot(parameters={"body": """
178+
{%- set output = from_json(event['output']) %}
179+
A{{ output }}B
180+
"""})
181+
self.assertEqual(SENT_MESSAGE[0].get_payload()[0].get_payload(), """
182+
A{ malformedB
183+
""")
184+
134185

135186
@test.skip_exotic()
136187
class TestDefaultTemplatedSMTPOutputBot(test.BotTestCase, unittest.TestCase):

0 commit comments

Comments
 (0)