Skip to content

Commit

Permalink
Cleanup forwarded email attachment test
Browse files Browse the repository at this point in the history
... prep for flake8
  • Loading branch information
medmunds committed Apr 15, 2017
1 parent 7d74480 commit 853a7cd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
32 changes: 32 additions & 0 deletions 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 <someone@example.com>
To: someoneelse@example.com
Reply-To: reply.to@example.com
Message-Id: <20130503182626.18666.16540@example.com>
List-Unsubscribe: <mailto:u+na6tmy3ege4tgnldmyytqojqmfsdembyme3tmy3cha4wcndbgaydqyrgoi6wszdpovrhi5dinfzw63tfmv4gs43uomstimdhnvqws3bomnxw2jtuhusteqjgmq6tm@example.com>
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

<html>
<body>Hi Bob, This is a message. Thanks!
<br>
</body></html>
--eb663d73ae0a4d6c9153cc0aec8b7520--
16 changes: 9 additions & 7 deletions tests/test_mailgun_backend.py
Expand Up @@ -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',
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down
16 changes: 15 additions & 1 deletion tests/utils.py
Expand Up @@ -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):
Expand All @@ -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 <someone@example.com>\nTo: someoneelse@example.com\nReply-To: reply.to@example.com\nMessage-Id: <20130503182626.18666.16540@example.com>\nList-Unsubscribe: <mailto:u+na6tmy3ege4tgnldmyytqojqmfsdembyme3tmy3cha4wcndbgaydqyrgoi6wszdpovrhi5dinfzw63tfmv4gs43uomstimdhnvqws3bomnxw2jtuhusteqjgmq6tm@example.com>\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<html>\n <body>Hi Bob, This is a message. Thanks!\n <br>\n</body></html>\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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 853a7cd

Please sign in to comment.