forked from mirror/django-mailbox
Base64-encode message body when storing in DB.
* Since Django requires that the content stored in a TextField be unicode, we need some way to encode the message safely. In situations before where a single-byte non-7-bit-clean encoding were used, django-mailbox could explode when writing the message copy to the database; this should ameliorate that problem. * Reverts earlier changesets' encoding normalization.
This commit is contained in:
parent
4c3af900c6
commit
88d122c421
5 changed files with 126 additions and 115 deletions
|
|
@ -2,6 +2,7 @@ import email
|
|||
import os.path
|
||||
|
||||
from django.test import TestCase
|
||||
import sys
|
||||
|
||||
from django_mailbox import models
|
||||
from django_mailbox.models import Mailbox, Message
|
||||
|
|
@ -28,11 +29,12 @@ class EmailMessageTestCase(TestCase):
|
|||
'messages',
|
||||
name,
|
||||
),
|
||||
'r'
|
||||
'rb'
|
||||
) as f:
|
||||
return email.message_from_string(
|
||||
f.read()
|
||||
)
|
||||
if sys.version_info < (3, 0):
|
||||
return email.message_from_string(f.read())
|
||||
else:
|
||||
return email.message_from_bytes(f.read())
|
||||
|
||||
def _headers_identical(self, left, right, header=None):
|
||||
""" Check if headers are (close enough to) identical.
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ Subject: Tjest
|
|||
From: "Somebody" <somebody@somewhere.com>
|
||||
To: idontknow@somewhere.com
|
||||
MIME-Version: 1.0
|
||||
Content-Type: text/plain; charset="raspberries"
|
||||
Content-Type: text/plain; charset="cp1251"
|
||||
Content-Transfer-Encoding: 7bit
|
||||
|
||||
Это сообщение имеет неверная кодировка.
|
||||
Это сообщение имеет неправильную кодировка.
|
||||
|
|
@ -132,48 +132,16 @@ class TestProcessEmail(EmailMessageTestCase):
|
|||
actual_text,
|
||||
)
|
||||
|
||||
def test_message_with_unknown_charset(self):
|
||||
""" Ensure that we gracefully fail at handling unknown encodings.
|
||||
|
||||
Should an unknown encoding be used, we should:
|
||||
|
||||
- Add a X-Django-Mailbox-Altered-Message header
|
||||
- Use UTF-8 (with replacement) as the de-facto (and probably incorrect)
|
||||
encoding for this payload part.
|
||||
|
||||
"""
|
||||
email_object = self._get_email_object(
|
||||
'message_with_invalid_payload_encoding.eml'
|
||||
)
|
||||
|
||||
msg = self.mailbox.process_incoming_message(email_object)
|
||||
|
||||
actual_body = msg.get_text_body()
|
||||
|
||||
expected_body = six.u(
|
||||
'\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd '
|
||||
'\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd'
|
||||
'\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd '
|
||||
'\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd '
|
||||
'\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd'
|
||||
'\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd '
|
||||
'\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd'
|
||||
'\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd.'
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
expected_body,
|
||||
actual_body,
|
||||
)
|
||||
|
||||
def test_message_with_invalid_content_for_declared_encoding(self):
|
||||
""" Ensure that we gracefully handle mis-encoded bodies.
|
||||
|
||||
Should a payload body be misencoded, we should:
|
||||
|
||||
- Decode the message (with replacement) using the declared encoding.
|
||||
- Always return a payload body in the declared encoding, using python's
|
||||
usual replacement mechanisms.
|
||||
- Not explode
|
||||
|
||||
Note: there is (intentionally) no assertion below; the only guarantee
|
||||
we make via this library is that processing this e-mail message will
|
||||
not cause an exception to be raised.
|
||||
|
||||
"""
|
||||
email_object = self._get_email_object(
|
||||
|
|
@ -182,8 +150,25 @@ class TestProcessEmail(EmailMessageTestCase):
|
|||
|
||||
msg = self.mailbox.process_incoming_message(email_object)
|
||||
|
||||
msg.get_text_body()
|
||||
|
||||
def test_message_with_valid_content_in_single_byte_encoding(self):
|
||||
email_object = self._get_email_object(
|
||||
'message_with_single_byte_encoding.eml',
|
||||
)
|
||||
|
||||
msg = self.mailbox.process_incoming_message(email_object)
|
||||
|
||||
actual_body = msg.get_text_body()
|
||||
expected_body = '??? ????????? ????? ???????????? ?????????.'
|
||||
|
||||
expected_body = six.u(
|
||||
'\u042d\u0442\u043e '
|
||||
'\u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 '
|
||||
'\u0438\u043c\u0435\u0435\u0442 '
|
||||
'\u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d'
|
||||
'\u0443\u044e '
|
||||
'\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0430.'
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
actual_body,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue