Skip to content

Commit

Permalink
TST: mail attach collector: test text attachment
Browse files Browse the repository at this point in the history
related: #2021
  • Loading branch information
Sebastian Wagner committed Jul 28, 2021
1 parent d6e8c35 commit 8a8107e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ CHANGELOG
### Tests
- Replace calls to deprecated/undocumented `logging.warn` with `logging.warning` (by Sebastian Wagner, fixes #2013).
- `intelmq.tests.bots.experts.rdap.test_expert`: Declare cache use, fixes build failures (by Sebastian Wagner, fixes #2014).
- `intelmq.tests.bots.collectors.mail.test_collector_attach`: Test text attachment (by Sebastian Wagner).

### Tools

Expand Down
7 changes: 7 additions & 0 deletions intelmq/tests/bots/collectors/mail/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
EMAIL_TXT_FOOBAR = parse_email(handle.read())
with open(os.path.join(os.path.dirname(__file__), 'fake_attachment.eml')) as handle:
EMAIL_FAKE_ATTACHMENT = parse_email(handle.read())
with open(os.path.join(os.path.dirname(__file__), 'text_attachment.eml')) as handle:
EMAIL_TEXT_ATTACHMENT = parse_email(handle.read())


class MockedImbox():
Expand Down Expand Up @@ -50,3 +52,8 @@ def messages(self, *args, **kwargs):
class MockedBadAttachmentImbox(MockedImbox):
def messages(self, *args, **kwargs):
yield 0, deepcopy(EMAIL_FAKE_ATTACHMENT)


class MockedTextAttachmentImbox(MockedImbox):
def messages(self, *args, **kwargs):
yield 0, deepcopy(EMAIL_TEXT_ATTACHMENT)
13 changes: 12 additions & 1 deletion intelmq/tests/bots/collectors/mail/test_collector_attach.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from intelmq.bots.collectors.mail.collector_mail_attach import MailAttachCollectorBot
from intelmq.lib.utils import base64_encode
if os.getenv('INTELMQ_TEST_EXOTIC'):
from .lib import MockedZipImbox, MockedBadAttachmentImbox
from .lib import MockedZipImbox, MockedBadAttachmentImbox, MockedTextAttachmentImbox

REPORT_FOOBARZIP = {
'__type': 'Report',
Expand All @@ -28,6 +28,8 @@
'raw': base64_encode('bar text\n'),
'extra.file_name': 'foobar',
}
REPORT_FOOBARTXT = REPORT_FOOBARZIP.copy()
REPORT_FOOBARTXT['extra.file_name'] = 'foobar.txt'


@test.skip_exotic()
Expand Down Expand Up @@ -67,6 +69,15 @@ def test_attach_no_filename(self):
self.run_bot()
self.assertOutputQueueLen(0)

def test_text_attachment(self):
"""
https://github.com/certtools/intelmq/pull/2021
"""
with mock.patch('imbox.Imbox', new=MockedTextAttachmentImbox):
self.run_bot(parameters={'attach_regex': '.*.txt$',
'extract_files': False})
self.assertMessageEqual(0, REPORT_FOOBARTXT)


if __name__ == '__main__': # pragma: no cover
unittest.main()
34 changes: 34 additions & 0 deletions intelmq/tests/bots/collectors/mail/text_attachment.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
To: [email protected]
From: Sebastian Wagner <[email protected]>
Subject: foobar zip
Message-ID: <[email protected]>
Date: Tue, 3 Sep 2019 16:57:40 +0200
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="------------1D845FBEEAAC1F68B4B45905"
Content-Language: en-US

This is a multi-part message in MIME format.
--------------1D845FBEEAAC1F68B4B45905
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 8bit

<html>
<head>

<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
Please look at the attachment<br>
</body>
</html>

--------------1D845FBEEAAC1F68B4B45905
Content-Type: text/plain; charset=UTF-8;
name="foobar.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="foobar.txt"
YmFyIHRleHQK
--------------1D845FBEEAAC1F68B4B45905--
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SPDX-FileCopyrightText: 2019 Sebastian Wagner
SPDX-License-Identifier: AGPL-3.0-or-later

0 comments on commit 8a8107e

Please sign in to comment.