From 6ee21b330d369cc91a9ba99eb2ee010e9e19e624 Mon Sep 17 00:00:00 2001 From: Lucas Hoffmann Date: Mon, 21 Aug 2017 23:18:14 +0200 Subject: Add failing test for alot.helper.email_as_string() Since email_as_string uses io.BytesIO it can not handle the unicode header names that might result from a envelope.construct_mail(). --- tests/helper_test.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'tests') diff --git a/tests/helper_test.py b/tests/helper_test.py index b0e799ca..4cfe704d 100644 --- a/tests/helper_test.py +++ b/tests/helper_test.py @@ -20,6 +20,7 @@ from __future__ import absolute_import import datetime +import email import errno import random import unittest @@ -379,3 +380,26 @@ class TestCallCmd(unittest.TestCase): self.assertEqual(out, u'') self.assertEqual(err, u'foobar') self.assertEqual(code, 42) + + +class TestEmailAsString(unittest.TestCase): + + def test_empty_message(self): + message = email.message.Message() + actual = helper.email_as_string(message) + expected = '\r\n' + self.assertEqual(actual, expected) + + @unittest.expectedFailure + def test_empty_message_with_unicode_header(self): + """Test if unicode header keys can be used in an email that is + converted to string with email_as_string().""" + # This is what alot.db.envelope.Envelope.construct_mail() currently + # does: It constructs a message object and then copies all headers from + # the envelope to the message object. Some header names are stored as + # unicode in the envelope. + message = email.message.Message() + message[u'X-Unicode-Header'] = 'dummy value' + actual = helper.email_as_string(message) + expected = 'X-Unicode-Header: dummy value\r\n\r\n' + self.assertEqual(actual, expected) -- cgit v1.2.3 From f475b96685686cb1f5597ac063de950f4efb0c85 Mon Sep 17 00:00:00 2001 From: Lucas Hoffmann Date: Mon, 21 Aug 2017 23:20:42 +0200 Subject: Use cStringIO.StringIO to fix #1132 This undoes a small subset of the changes from fa3dd1b04567c4ea03fa658c3838b569531c79f5 and thus fixes #1132. The io.BytesIO object was not able to handle the unicode header names that where returned by envelope.construct_mail, which in turn did just copy them from the envelope header. --- alot/helper.py | 3 ++- tests/helper_test.py | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/alot/helper.py b/alot/helper.py index ce307875..1c569a23 100644 --- a/alot/helper.py +++ b/alot/helper.py @@ -10,6 +10,7 @@ from datetime import timedelta from datetime import datetime from collections import deque from io import BytesIO +from cStringIO import StringIO import logging import mimetypes import os @@ -606,7 +607,7 @@ def email_as_string(mail): :param mail: email to convert to string :rtype: str """ - fp = BytesIO() + fp = StringIO() g = Generator(fp, mangle_from_=False, maxheaderlen=78) g.flatten(mail) as_string = RFC3156_canonicalize(fp.getvalue()) diff --git a/tests/helper_test.py b/tests/helper_test.py index 4cfe704d..62e37ed8 100644 --- a/tests/helper_test.py +++ b/tests/helper_test.py @@ -390,7 +390,6 @@ class TestEmailAsString(unittest.TestCase): expected = '\r\n' self.assertEqual(actual, expected) - @unittest.expectedFailure def test_empty_message_with_unicode_header(self): """Test if unicode header keys can be used in an email that is converted to string with email_as_string().""" -- cgit v1.2.3