2013-07-25 23:45:39 -07:00
|
|
|
import os.path
|
|
|
|
|
|
|
|
|
|
import six
|
|
|
|
|
|
|
|
|
|
from django_mailbox.models import Mailbox, Message
|
|
|
|
|
from django_mailbox.tests.base import EmailMessageTestCase
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = ['TestProcessEmail']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestProcessEmail(EmailMessageTestCase):
|
|
|
|
|
def test_message_without_attachments(self):
|
|
|
|
|
message = self._get_email_object('generic_message.eml')
|
|
|
|
|
|
|
|
|
|
mailbox = Mailbox.objects.create()
|
|
|
|
|
msg = mailbox.process_incoming_message(message)
|
|
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
msg.mailbox,
|
|
|
|
|
mailbox
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(msg.subject, 'Message Without Attachment')
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
msg.message_id,
|
|
|
|
|
(
|
|
|
|
|
'<CAMdmm+hGH8Dgn-_0xnXJCd=PhyNAiouOYm5zFP0z'
|
|
|
|
|
'-foqTO60zA@mail.gmail.com>'
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
msg.from_header,
|
|
|
|
|
'Adam Coddington <test@adamcoddington.net>',
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
msg.to_header,
|
|
|
|
|
'Adam Coddington <test@adamcoddington.net>',
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_message_with_attachments(self):
|
|
|
|
|
message = self._get_email_object('message_with_attachment.eml')
|
|
|
|
|
|
|
|
|
|
mailbox = Mailbox.objects.create()
|
|
|
|
|
msg = mailbox.process_incoming_message(message)
|
|
|
|
|
|
|
|
|
|
expected_count = 1
|
|
|
|
|
actual_count = msg.attachments.count()
|
|
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
expected_count,
|
|
|
|
|
actual_count,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
attachment = msg.attachments.all()[0]
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
attachment.get_filename(),
|
|
|
|
|
'heart.png',
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_message_get_text_body(self):
|
|
|
|
|
message = self._get_email_object('multipart_text.eml')
|
|
|
|
|
|
|
|
|
|
mailbox = Mailbox.objects.create()
|
|
|
|
|
msg = mailbox.process_incoming_message(message)
|
|
|
|
|
|
|
|
|
|
expected_results = 'Hello there!'
|
|
|
|
|
actual_results = msg.get_text_body().strip()
|
|
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
expected_results,
|
|
|
|
|
actual_results,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_get_text_body_properly_recomposes_line_continuations(self):
|
|
|
|
|
message = Message()
|
|
|
|
|
email_object = self._get_email_object(
|
|
|
|
|
'message_with_long_text_lines.eml'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
message.get_email_object = lambda: email_object
|
|
|
|
|
|
|
|
|
|
actual_text = message.get_text_body()
|
|
|
|
|
expected_text = (
|
|
|
|
|
'The one of us with a bike pump is far ahead, '
|
|
|
|
|
'but a man stopped to help us and gave us his pump.'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
actual_text,
|
|
|
|
|
expected_text
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_get_body_properly_handles_unicode_body(self):
|
|
|
|
|
with open(
|
|
|
|
|
os.path.join(
|
|
|
|
|
os.path.dirname(__file__),
|
|
|
|
|
'generic_message.eml'
|
|
|
|
|
)
|
|
|
|
|
) as f:
|
|
|
|
|
unicode_body = six.u(f.read())
|
|
|
|
|
|
|
|
|
|
message = Message()
|
|
|
|
|
message.body = unicode_body
|
|
|
|
|
|
|
|
|
|
expected_body = unicode_body
|
|
|
|
|
actual_body = message.get_email_object().as_string()
|
|
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
expected_body,
|
|
|
|
|
actual_body
|
|
|
|
|
)
|
2013-07-26 01:01:07 -07:00
|
|
|
|
|
|
|
|
def test_message_with_misplaced_utf8_content(self):
|
|
|
|
|
""" Ensure that we properly handle incorrectly encoded messages
|
|
|
|
|
|
|
|
|
|
``message_with_utf8_char.eml``'s primary text payload is marked
|
|
|
|
|
as being iso-8859-1 data, but actually contains UTF-8 bytes.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
email_object = self._get_email_object('message_with_utf8_char.eml')
|
|
|
|
|
|
|
|
|
|
msg = self.mailbox.process_incoming_message(email_object)
|
|
|
|
|
|
|
|
|
|
actual_text = msg.get_text_body()
|
|
|
|
|
expected_text = (
|
|
|
|
|
u'This message contains funny UTF16 characters like this one: '
|
|
|
|
|
u'"\xc2\xa0" and this one "\xe2\x9c\xbf".'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
expected_text,
|
|
|
|
|
actual_text,
|
|
|
|
|
)
|