From 45829f7956716638fe77989a50eb87d3ea34cace Mon Sep 17 00:00:00 2001 From: pacien Date: Fri, 15 Nov 2019 17:00:07 +0100 Subject: envelope: correctly handle folded headers --- tests/db/test_envelope.py | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) (limited to 'tests') diff --git a/tests/db/test_envelope.py b/tests/db/test_envelope.py index be318b0d..b7612f37 100644 --- a/tests/db/test_envelope.py +++ b/tests/db/test_envelope.py @@ -28,20 +28,6 @@ SETTINGS = { } -def email_to_dict(mail): - """Consumes an email, and returns a dict of headers and 'Body'.""" - split = mail.splitlines() - final = {} - for line in split: - if line.strip(): - try: - k, v = line.split(':') - final[k.strip()] = v.strip() - except ValueError: - final['Body'] = line.strip() - return final - - class TestEnvelope(unittest.TestCase): def assertEmailEqual(self, first, second): @@ -100,3 +86,24 @@ class TestEnvelope(unittest.TestCase): e.attach(f.name) self._test_mail(e) + + @mock.patch('alot.db.envelope.settings', SETTINGS) + def test_parse_template(self): + """Tests multi-line header and body parsing""" + raw = ( + 'From: foo@example.com\n' + 'To: bar@example.com,\n' + ' baz@example.com\n' + 'Subject: Test email\n' + '\n' + 'Some body content: which is not a header.\n' + ) + envlp = envelope.Envelope() + envlp.parse_template(raw) + self.assertDictEqual(envlp.headers, { + 'From': ['foo@example.com'], + 'To': ['bar@example.com, baz@example.com'], + 'Subject': ['Test email'] + }) + self.assertEqual(envlp.body, + 'Some body content: which is not a header.') -- cgit v1.2.3