From 853a7cd31da5178c90b83769e4c24e515f19e2c7 Mon Sep 17 00:00:00 2001 From: medmunds Date: Sat, 15 Apr 2017 12:46:19 -0700 Subject: [PATCH] Cleanup forwarded email attachment test ... prep for flake8 --- tests/sample_email.txt | 32 ++++++++++++++++++++++++++++++++ tests/test_mailgun_backend.py | 16 +++++++++------- tests/utils.py | 16 +++++++++++++++- 3 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 tests/sample_email.txt diff --git a/tests/sample_email.txt b/tests/sample_email.txt new file mode 100644 index 00000000..5cd26146 --- /dev/null +++ b/tests/sample_email.txt @@ -0,0 +1,32 @@ +Received: by luna.mailgun.net with SMTP mgrt 8734663311733; Fri, 03 May 2013 + 18:26:27 +0000 +Content-Type: multipart/alternative; boundary="eb663d73ae0a4d6c9153cc0aec8b7520" +Mime-Version: 1.0 +Subject: Test email +From: Someone +To: someoneelse@example.com +Reply-To: reply.to@example.com +Message-Id: <20130503182626.18666.16540@example.com> +List-Unsubscribe: +X-Mailgun-Sid: WyIwNzI5MCIsICJhbGljZUBleGFtcGxlLmNvbSIsICI2Il0= +X-Mailgun-Variables: {"my_var_1": "Mailgun Variable #1", "my-var-2": "awesome"} +Date: Fri, 03 May 2013 18:26:27 +0000 +Sender: someone@example.com + +--eb663d73ae0a4d6c9153cc0aec8b7520 +Mime-Version: 1.0 +Content-Type: text/plain; charset="ascii" +Content-Transfer-Encoding: 7bit + +Hi Bob, This is a message. Thanks! + +--eb663d73ae0a4d6c9153cc0aec8b7520 +Mime-Version: 1.0 +Content-Type: text/html; charset="ascii" +Content-Transfer-Encoding: 7bit + + + Hi Bob, This is a message. Thanks! +
+ +--eb663d73ae0a4d6c9153cc0aec8b7520-- diff --git a/tests/test_mailgun_backend.py b/tests/test_mailgun_backend.py index ed96bbf1..bb1f3e52 100644 --- a/tests/test_mailgun_backend.py +++ b/tests/test_mailgun_backend.py @@ -22,7 +22,8 @@ def message_from_bytes(s): from anymail.message import attach_inline_image_file from .mock_requests_backend import RequestsBackendMockAPITestCase, SessionSharingTestCasesMixin -from .utils import sample_image_content, sample_image_path, SAMPLE_FORWARDED_EMAIL, SAMPLE_IMAGE_FILENAME, AnymailTestMixin +from .utils import (AnymailTestMixin, sample_email_content, + sample_image_content, sample_image_path, SAMPLE_IMAGE_FILENAME) @override_settings(EMAIL_BACKEND='anymail.backends.mailgun.EmailBackend', @@ -140,7 +141,8 @@ def test_attachments(self): self.message.attach(mimeattachment) # And also with an message/rfc822 attachment - forwarded_email = message_from_bytes(SAMPLE_FORWARDED_EMAIL) + forwarded_email_content = sample_email_content() + forwarded_email = message_from_bytes(forwarded_email_content) rfcmessage = MIMEBase("message", "rfc822") rfcmessage.attach(forwarded_email) self.message.attach(rfcmessage) @@ -152,12 +154,12 @@ def test_attachments(self): self.assertEqual(attachments[0], ('test.txt', text_content, 'text/plain')) self.assertEqual(attachments[1], ('test.png', png_content, 'image/png')) # type inferred from filename self.assertEqual(attachments[2], (None, pdf_content, 'application/pdf')) # no filename - # Email messages can get a bit changed with respect to - # whitespace characters in headers, without breaking the message, so we - # tolerate that: + # Email messages can get a bit changed with respect to whitespace characters + # in headers, without breaking the message, so we tolerate that: self.assertEqual(attachments[3][0], None) - self.assertEqual(attachments[3][1].replace(b'\n', b'').replace(b' ', b''), - (b'Content-Type: message/rfc822\nMIME-Version: 1.0\n\n' + SAMPLE_FORWARDED_EMAIL).replace(b'\n', b'').replace(b' ', b'')) + self.assertEqualIgnoringWhitespace( + attachments[3][1], + b'Content-Type: message/rfc822\nMIME-Version: 1.0\n\n' + forwarded_email_content) self.assertEqual(attachments[3][2], 'message/rfc822') # Make sure the image attachment is not treated as embedded: diff --git a/tests/utils.py b/tests/utils.py index e3417978..89daec1c 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -36,6 +36,7 @@ def decode_att(att): SAMPLE_IMAGE_FILENAME = "sample_image.png" +SAMPLE_EMAIL_FILENAME = "sample_email.txt" def sample_image_path(filename=SAMPLE_IMAGE_FILENAME): @@ -52,7 +53,14 @@ def sample_image_content(filename=SAMPLE_IMAGE_FILENAME): return f.read() -SAMPLE_FORWARDED_EMAIL = b'Received: by luna.mailgun.net with SMTP mgrt 8734663311733; Fri, 03 May 2013\n 18:26:27 +0000\nContent-Type: multipart/alternative; boundary="eb663d73ae0a4d6c9153cc0aec8b7520"\nMime-Version: 1.0\nSubject: Test email\nFrom: Someone \nTo: someoneelse@example.com\nReply-To: reply.to@example.com\nMessage-Id: <20130503182626.18666.16540@example.com>\nList-Unsubscribe: \nX-Mailgun-Sid: WyIwNzI5MCIsICJhbGljZUBleGFtcGxlLmNvbSIsICI2Il0=\nX-Mailgun-Variables: {"my_var_1": "Mailgun Variable #1", "my-var-2": "awesome"}\nDate: Fri, 03 May 2013 18:26:27 +0000\nSender: someone@example.com\n\n--eb663d73ae0a4d6c9153cc0aec8b7520\nMime-Version: 1.0\nContent-Type: text/plain; charset="ascii"\nContent-Transfer-Encoding: 7bit\n\nHi Bob, This is a message. Thanks!\n\n--eb663d73ae0a4d6c9153cc0aec8b7520\nMime-Version: 1.0\nContent-Type: text/html; charset="ascii"\nContent-Transfer-Encoding: 7bit\n\n\n Hi Bob, This is a message. Thanks!\n
\n\n--eb663d73ae0a4d6c9153cc0aec8b7520--\n' +def sample_email_path(filename=SAMPLE_EMAIL_FILENAME): + """Returns path to an email file (e.g., for forwarding as an attachment)""" + return sample_image_path(filename) # borrow path logic from above + + +def sample_email_content(filename=SAMPLE_EMAIL_FILENAME): + """Returns bytes contents of an email file (e.g., for forwarding as an attachment)""" + return sample_image_content(filename) # borrow read logic from above # noinspection PyUnresolvedReferences @@ -107,6 +115,12 @@ def assertRegex(self, *args, **kwargs): except TypeError: return self.assertRegexpMatches(*args, **kwargs) # Python 2 + def assertEqualIgnoringWhitespace(self, first, second, msg=None): + # Useful for message/rfc822 attachment tests + self.assertEqual(first.replace(b'\n', b'').replace(b' ', b''), + second.replace(b'\n', b'').replace(b' ', b''), + msg) + # Backported from python 3.5 class _AssertWarnsContext(object):