1
0
Fork 1
mirror of https://github.com/coddingtonbear/django-mailbox.git synced 2026-07-09 22:38:19 +02:00

Adding tests and minor code changes to ensure that we properly handle the distinction between bytes and text when dehydrating/rehydrating messages.

This commit is contained in:
Adam Coddington 2013-07-26 19:13:35 -07:00
parent 3ee4b43936
commit adb17e7d67
5 changed files with 109 additions and 24 deletions

View file

@ -7,8 +7,7 @@ Message-ID: <CAMdmm+hGH8Dgn-_0xnXJCd=PhyNAiouOYm5zFP0z-foqTO60zA@mail.gmail.com>
Subject: Message Without Attachment
From: Adam Coddington <test@adamcoddington.net>
To: Adam Coddington <test@adamcoddington.net>
Content-Type: text/plain;
charset="iso-8859-1"
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello there.

View file

@ -0,0 +1,9 @@
Date: Fri, 30 Jan 2012 11:30:01 PST
Subject: Tjest
From: "Somebody" <somebody@somewhere.com>
To: idontknow@somewhere.com
MIME-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Ýòî ñîîáùåíèå èìååò íåïðàâèëüíóþ êîäèðîâêà.

View file

@ -0,0 +1,9 @@
Date: Fri, 30 Jan 2012 11:30:01 PST
Subject: Tjest
From: "Somebody" <somebody@somewhere.com>
To: idontknow@somewhere.com
MIME-Version: 1.0
Content-Type: text/plain; charset="raspberries"
Content-Transfer-Encoding: 7bit
Это сообщение имеет неверная кодировка.

View file

@ -131,3 +131,61 @@ class TestProcessEmail(EmailMessageTestCase):
expected_text,
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 = (
u'\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd '
u'\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd'
u'\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd '
u'\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd '
u'\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd'
u'\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd '
u'\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd'
u'\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.
"""
email_object = self._get_email_object(
'message_with_invalid_content_for_declared_encoding.eml',
)
msg = self.mailbox.process_incoming_message(email_object)
actual_body = msg.get_text_body()
expected_body = '??? ????????? ????? ???????????? ?????????.'
self.assertEqual(
actual_body,
expected_body,
)