From 3ee4b43936ac05f4007ed2acabd67077658ddef8 Mon Sep 17 00:00:00 2001 From: Adam Coddington Date: Fri, 26 Jul 2013 18:07:31 -0700 Subject: [PATCH] Cleaning up tests; moving test e-mail messages; fixing bugs relating to message encoding rehydration. --HG-- rename : django_mailbox/tests/generic_message.eml => django_mailbox/tests/messages/generic_message.eml rename : django_mailbox/tests/message_with_attachment.eml => django_mailbox/tests/messages/message_with_attachment.eml rename : django_mailbox/tests/message_with_defective_attachment_association.eml => django_mailbox/tests/messages/message_with_defective_attachment_association.eml rename : django_mailbox/tests/message_with_defective_attachment_association_result.eml => django_mailbox/tests/messages/message_with_defective_attachment_association_result.eml rename : django_mailbox/tests/message_with_image_jpg_mimetype.eml => django_mailbox/tests/messages/message_with_image_jpg_mimetype.eml rename : django_mailbox/tests/message_with_long_text_lines.eml => django_mailbox/tests/messages/message_with_long_text_lines.eml rename : django_mailbox/tests/message_with_many_multiparts.eml => django_mailbox/tests/messages/message_with_many_multiparts.eml rename : django_mailbox/tests/message_with_many_multiparts_stripped_html.eml => django_mailbox/tests/messages/message_with_many_multiparts_stripped_html.eml rename : django_mailbox/tests/message_with_utf8_char.eml => django_mailbox/tests/messages/message_with_utf8_char.eml rename : django_mailbox/tests/multipart_text.eml => django_mailbox/tests/messages/multipart_text.eml --- MANIFEST.in | 7 +++ django_mailbox/models.py | 2 + django_mailbox/tests/base.py | 43 +++++++++++++++---- .../tests/{ => messages}/generic_message.eml | 0 .../message_with_attachment.eml | 0 ..._with_defective_attachment_association.eml | 0 ...efective_attachment_association_result.eml | 0 .../message_with_image_jpg_mimetype.eml | 0 .../message_with_long_text_lines.eml | 0 .../message_with_many_multiparts.eml | 0 ...age_with_many_multiparts_stripped_html.eml | 0 .../{ => messages}/message_with_utf8_char.eml | 0 .../tests/{ => messages}/multipart_text.eml | 0 django_mailbox/tests/test_process_email.py | 2 +- 14 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 MANIFEST.in rename django_mailbox/tests/{ => messages}/generic_message.eml (100%) rename django_mailbox/tests/{ => messages}/message_with_attachment.eml (100%) rename django_mailbox/tests/{ => messages}/message_with_defective_attachment_association.eml (100%) rename django_mailbox/tests/{ => messages}/message_with_defective_attachment_association_result.eml (100%) rename django_mailbox/tests/{ => messages}/message_with_image_jpg_mimetype.eml (100%) rename django_mailbox/tests/{ => messages}/message_with_long_text_lines.eml (100%) rename django_mailbox/tests/{ => messages}/message_with_many_multiparts.eml (100%) rename django_mailbox/tests/{ => messages}/message_with_many_multiparts_stripped_html.eml (100%) rename django_mailbox/tests/{ => messages}/message_with_utf8_char.eml (100%) rename django_mailbox/tests/{ => messages}/multipart_text.eml (100%) diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..355d641 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,7 @@ +include *.rst +include .travis.yml +include MANIFEST +recursive-include django_mailbox *.eml +recursive-include docs *.py +recursive-include docs *.rst +recursive-include docs Makefile diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 0c40e53..dbe3424 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -250,6 +250,8 @@ class Mailbox(models.Model): if charset: new[ORIGINAL_CHARSET_HEADER] = str(charset) new.set_charset('utf-8') + else: + charset = 'ascii' payload = msg.get_payload() new.set_payload( # Remove any characters that are invalid in the diff --git a/django_mailbox/tests/base.py b/django_mailbox/tests/base.py index 73d449d..2c9bb90 100644 --- a/django_mailbox/tests/base.py +++ b/django_mailbox/tests/base.py @@ -8,6 +8,11 @@ from django_mailbox.models import Mailbox, Message class EmailMessageTestCase(TestCase): + ALLOWED_EXTRA_HEADERS = [ + 'MIME-Version', + 'Content-Transfer-Encoding', + ] + def setUp(self): self._ALLOWED_MIMETYPES = models.ALLOWED_MIMETYPES self._STRIP_UNALLOWED_MIMETYPES = models.STRIP_UNALLOWED_MIMETYPES @@ -17,19 +22,37 @@ class EmailMessageTestCase(TestCase): super(EmailMessageTestCase, self).setUp() def _get_email_object(self, name): - with open(os.path.join(os.path.dirname(__file__), name), 'r') as f: + with open( + os.path.join( + os.path.dirname(__file__), + 'messages', + name, + ), + 'r' + ) as f: return email.message_from_string( f.read() ) - def _headers_identical(self, left, right): - """ Check if headers are identical. + def _headers_identical(self, left, right, header=None): + """ Check if headers are (close enough to) identical. - This is particularly tricky because Python 2.6, Python 2.7 and Python 3 - each handle this slightly differently. This should mash away all - of the differences, though. + * This is particularly tricky because Python 2.6, Python 2.7 and + Python 3 each handle header strings slightly differently. This + should mash away all of the differences, though. + * This also has a small loophole in that when re-writing e-mail + payload encodings, we re-build the Content-Type header, so if the + header was originally unquoted, it will be quoted when rehydrating + the e-mail message. """ + if header.lower() == 'content-type': + # Special case; given that we re-write the header, we'll be quoting + # the new content type; we need to make sure that doesn't cause + # this comparison to fail. Also, the case of the encoding could + # be changed, etc. etc. etc. + left = left.replace('"', '').upper() + right = right.replace('"', '').upper() left = left.replace('\n\t', ' ').replace('\n ', ' ') right = right.replace('\n\t', ' ').replace('\n ', ' ') if right != left: @@ -39,9 +62,11 @@ class EmailMessageTestCase(TestCase): def compare_email_objects(self, left, right): # Compare headers for key, value in left.items(): + if not right[key] and key in self.ALLOWED_EXTRA_HEADERS: + continue if not right[key]: raise AssertionError("Extra header '%s'" % key) - if not self._headers_identical(right[key], value): + if not self._headers_identical(right[key], value, header=key): raise AssertionError( "Header '%s' unequal:\n%s\n%s" % ( key, @@ -50,9 +75,11 @@ class EmailMessageTestCase(TestCase): ) ) for key, value in right.items(): + if not left[key] and key in self.ALLOWED_EXTRA_HEADERS: + continue if not left[key]: raise AssertionError("Extra header '%s'" % key) - if not self._headers_identical(left[key], value): + if not self._headers_identical(left[key], value, header=key): raise AssertionError( "Header '%s' unequal:\n%s\n%s" % ( key, diff --git a/django_mailbox/tests/generic_message.eml b/django_mailbox/tests/messages/generic_message.eml similarity index 100% rename from django_mailbox/tests/generic_message.eml rename to django_mailbox/tests/messages/generic_message.eml diff --git a/django_mailbox/tests/message_with_attachment.eml b/django_mailbox/tests/messages/message_with_attachment.eml similarity index 100% rename from django_mailbox/tests/message_with_attachment.eml rename to django_mailbox/tests/messages/message_with_attachment.eml diff --git a/django_mailbox/tests/message_with_defective_attachment_association.eml b/django_mailbox/tests/messages/message_with_defective_attachment_association.eml similarity index 100% rename from django_mailbox/tests/message_with_defective_attachment_association.eml rename to django_mailbox/tests/messages/message_with_defective_attachment_association.eml diff --git a/django_mailbox/tests/message_with_defective_attachment_association_result.eml b/django_mailbox/tests/messages/message_with_defective_attachment_association_result.eml similarity index 100% rename from django_mailbox/tests/message_with_defective_attachment_association_result.eml rename to django_mailbox/tests/messages/message_with_defective_attachment_association_result.eml diff --git a/django_mailbox/tests/message_with_image_jpg_mimetype.eml b/django_mailbox/tests/messages/message_with_image_jpg_mimetype.eml similarity index 100% rename from django_mailbox/tests/message_with_image_jpg_mimetype.eml rename to django_mailbox/tests/messages/message_with_image_jpg_mimetype.eml diff --git a/django_mailbox/tests/message_with_long_text_lines.eml b/django_mailbox/tests/messages/message_with_long_text_lines.eml similarity index 100% rename from django_mailbox/tests/message_with_long_text_lines.eml rename to django_mailbox/tests/messages/message_with_long_text_lines.eml diff --git a/django_mailbox/tests/message_with_many_multiparts.eml b/django_mailbox/tests/messages/message_with_many_multiparts.eml similarity index 100% rename from django_mailbox/tests/message_with_many_multiparts.eml rename to django_mailbox/tests/messages/message_with_many_multiparts.eml diff --git a/django_mailbox/tests/message_with_many_multiparts_stripped_html.eml b/django_mailbox/tests/messages/message_with_many_multiparts_stripped_html.eml similarity index 100% rename from django_mailbox/tests/message_with_many_multiparts_stripped_html.eml rename to django_mailbox/tests/messages/message_with_many_multiparts_stripped_html.eml diff --git a/django_mailbox/tests/message_with_utf8_char.eml b/django_mailbox/tests/messages/message_with_utf8_char.eml similarity index 100% rename from django_mailbox/tests/message_with_utf8_char.eml rename to django_mailbox/tests/messages/message_with_utf8_char.eml diff --git a/django_mailbox/tests/multipart_text.eml b/django_mailbox/tests/messages/multipart_text.eml similarity index 100% rename from django_mailbox/tests/multipart_text.eml rename to django_mailbox/tests/messages/multipart_text.eml diff --git a/django_mailbox/tests/test_process_email.py b/django_mailbox/tests/test_process_email.py index 9df4b7f..5932799 100644 --- a/django_mailbox/tests/test_process_email.py +++ b/django_mailbox/tests/test_process_email.py @@ -94,7 +94,7 @@ class TestProcessEmail(EmailMessageTestCase): with open( os.path.join( os.path.dirname(__file__), - 'generic_message.eml' + 'messages/generic_message.eml' ) ) as f: unicode_body = six.u(f.read())