summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMichael Schwarz <michi@cloudscale.ch>2016-08-03 15:53:06 +0200
committerTim Graham <timograham@gmail.com>2016-08-12 16:35:09 -0400
commit72d541b61cd7b0a14f70242e2207fdb3f600c4d5 (patch)
treeaa90cd09c360ed41657ffca31507c8fef579c2ab /tests
parent311a8e8d505049ff5644a94e16c00246c8a43a18 (diff)
Fixed #27007 -- Handled non-UTF-8 bytes objects for text/* attachments.
The fallback logic which allows non-UTF-8 encoded files to be passed to attach_file() even when a `text/*` mime type has been specified is moved to attach(). Both functions now fall back to a content type of `application/octet-stream`. A side effect is that a file's content is decoded in memory instead of opening it in text mode and reading it into a string. Some mimetype-related logic in _create_attachment() has become obsolete as the code moved from attach_file() to attach() already handles this.
Diffstat (limited to 'tests')
-rw-r--r--tests/mail/tests.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/tests/mail/tests.py b/tests/mail/tests.py
index 0f0eb5bdf1..3d1a74518f 100644
--- a/tests/mail/tests.py
+++ b/tests/mail/tests.py
@@ -422,6 +422,31 @@ class MailTests(HeadersCheckMixin, SimpleTestCase):
self.assertEqual(content, b'file content')
self.assertEqual(mimetype, 'text/plain')
+ def test_attach_utf8_text_as_bytes(self):
+ """
+ Non-ASCII characters encoded as valid UTF-8 are correctly transported
+ and decoded.
+ """
+ msg = EmailMessage('subject', 'body', 'from@example.com', ['to@example.com'])
+ msg.attach('file.txt', b'\xc3\xa4') # UTF-8 encoded a umlaut.
+ filename, content, mimetype = self.get_decoded_attachments(msg)[0]
+ self.assertEqual(filename, 'file.txt')
+ self.assertEqual(content, b'\xc3\xa4')
+ self.assertEqual(mimetype, 'text/plain')
+
+ def test_attach_non_utf8_text_as_bytes(self):
+ """
+ Binary data that can't be decoded as UTF-8 overrides the MIME type
+ instead of decoding the data.
+ """
+ msg = EmailMessage('subject', 'body', 'from@example.com', ['to@example.com'])
+ msg.attach('file.txt', b'\xff') # Invalid UTF-8.
+ filename, content, mimetype = self.get_decoded_attachments(msg)[0]
+ self.assertEqual(filename, 'file.txt')
+ # Content should be passed through unmodified.
+ self.assertEqual(content, b'\xff')
+ self.assertEqual(mimetype, 'application/octet-stream')
+
def test_dummy_backend(self):
"""
Make sure that dummy backends returns correct number of sent messages