Skip to content

Commit 5a44948

Browse files
committed
fixup unit test failure
1 parent a644b57 commit 5a44948

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

intelmq/bots/outputs/smtp_batch/output.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import sys
99
from tempfile import NamedTemporaryFile
1010
import time
11-
from typing import Any, Generator, Optional
11+
from typing import Any, Iterable, Optional, Dict, List
1212
import zipfile
1313
from base64 import b64decode
1414
from collections import OrderedDict
@@ -50,13 +50,13 @@ class Mail:
5050
to: str
5151
path: str
5252
count: int
53-
template_data: dict[str,Any]
53+
template_data: Dict[str, Any]
5454

5555

5656
class SMTPBatchOutputBot(Bot):
5757
# configurable parameters
5858
additional_grouping_keys: Optional[list] = [] # refers to the event directly
59-
templating: Optional[dict[str, bool]] = {'subject': False, 'body': False, 'attachment': False}
59+
templating: Optional[Dict[str, bool]] = {'subject': False, 'body': False, 'attachment': False}
6060
alternative_mails: Optional[str] = None
6161
bcc: Optional[list] = None
6262
email_from: str = ""
@@ -126,8 +126,6 @@ def init(self):
126126
raise MissingDependencyError('envelope', '>=2.0.0')
127127
if jinja2 is None:
128128
self.logger.warning("No jinja2 installed. Thus, the templating is deactivated.")
129-
if self.additional_grouping_keys and len(self.additional_grouping_keys) > 0:
130-
self.additional_grouping_keys_trans = {self.fieldnames_translation.get(k, k) for k in self.additional_grouping_keys}
131129
self.set_cache()
132130
self.key = f"{self._Bot__bot_id}:"
133131

@@ -251,7 +249,7 @@ def set_tester(self, force=True):
251249
print("\nWhat e-mail should I use?")
252250
self.testing_to = input()
253251

254-
def send_mails_to_tester(self, mails):
252+
def send_mails_to_tester(self, mails: List[Mail]):
255253
"""
256254
These mails are going to tester's address. Then prints out their count.
257255
:param mails: list
@@ -260,7 +258,7 @@ def send_mails_to_tester(self, mails):
260258
count = sum([1 for mail in mails if self.build_mail(mail, send=True, override_to=self.testing_to)])
261259
print(f"{count}× mail sent to: {self.testing_to}\n")
262260

263-
def prepare_mails(self) -> Generator[Mail]:
261+
def prepare_mails(self) -> Iterable[Mail]:
264262
""" Generates Mail objects """
265263

266264
for mail_record in self.cache.redis.keys(f"{self.key}*")[slice(self.limit_results)]:
@@ -332,14 +330,16 @@ def prepare_mails(self) -> Generator[Mail]:
332330

333331
# collect all data which must be the same for all events of the
334332
# bucket and thus can be used for templating
335-
template_data = {}
336-
# only collect if templating is enabled (save the memory otherwise)
333+
template_keys = ['source.abuse_contact']
334+
# only collect if templating is enabled (save the memory otherwise)+
337335
if jinja2 and self.templating and any(self.templating.values()):
338-
template_data = {
339-
k.replace(".", "_"): lines[0][k]
340-
for k in ["source.abuse_contact"] + self.additional_grouping_keys
341-
if k in rows_output[0]
342-
}
336+
template_keys.extend(self.additional_grouping_keys)
337+
338+
template_data = {
339+
k.replace(".", "_"): lines[0][k]
340+
for k in template_keys
341+
if k in lines[0]
342+
}
343343

344344
email_to = template_data["source_abuse_contact"]
345345
filename = f'{time.strftime("%y%m%d")}_{count}_events'
@@ -361,7 +361,7 @@ def prepare_mails(self) -> Generator[Mail]:
361361
self.build_mail(mail, send=False)
362362
yield mail
363363

364-
def build_mail(self, mail, send=False, override_to=None):
364+
def build_mail(self, mail: Mail, send=False, override_to=None):
365365
""" creates a MIME message
366366
:param mail: Mail object
367367
:param send: True to send through SMTP, False for just printing the information
@@ -377,6 +377,8 @@ def build_mail(self, mail, send=False, override_to=None):
377377
email_to = mail.to
378378
email_from = self.email_from
379379

380+
template_data = mail.template_data
381+
380382
text = self.mail_contents
381383
if jinja2 and self.templating and self.templating.get('body', False):
382384
jinja_tmpl = jinja_env.from_string(text)

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,21 @@
1111

1212
import intelmq.lib.test as test
1313
from intelmq.bots.outputs.smtp_batch.output import SMTPBatchOutputBot
14-
from intelmq.lib.cache import Cache
1514
from intelmq.lib.exceptions import MissingDependencyError
15+
from hashlib import sha256
1616

1717
BOT_ID = "test-bot"
1818
IDENTITY1 = '[email protected]'
19-
KEY1 = f"{BOT_ID}:{IDENTITY1}".encode()
19+
IDENTITY1_HASH = sha256(sha256(IDENTITY1.encode()).digest()).hexdigest()
20+
KEY1 = f"{BOT_ID}:{IDENTITY1_HASH}".encode()
2021
EVENT1 = {'__type': 'Event',
2122
'source.ip': '127.0.0.1',
2223
'source.url': 'http://example.com/',
2324
'source.abuse_contact': IDENTITY1
2425
}
2526
IDENTITY2 = '[email protected]'
26-
KEY2 = f"{BOT_ID}:{IDENTITY2}".encode()
27+
IDENTITY2_HASH = sha256(sha256(IDENTITY2.encode()).digest()).hexdigest()
28+
KEY2 = f"{BOT_ID}:{IDENTITY2_HASH}".encode()
2729
EVENT2 = {'__type': 'Event',
2830
'source.ip': '127.0.0.2',
2931
'source.url': 'http://example2.com/',
@@ -67,6 +69,7 @@ def compare_envelope(self, envelope: Envelope, subject, message, from_, to):
6769
self.assertEqual(message, envelope.message())
6870
self.assertEqual(from_, envelope.from_())
6971
self.assertEqual(to, envelope.to())
72+
# TODO template_data
7073

7174
def send_message(self):
7275
def _(envelope):

0 commit comments

Comments
 (0)