Skip to content

Commit

Permalink
Cleanup: add reprs for Attachment, EmailAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
medmunds committed Sep 11, 2020
1 parent feee8b5 commit 03dd15d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
16 changes: 16 additions & 0 deletions anymail/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ def __init__(self, display_name='', addr_spec=None):
self.username = addr_spec
self.domain = ''

def __repr__(self):
return "EmailAddress({display_name!r}, {addr_spec!r})".format(
display_name=self.display_name, addr_spec=self.addr_spec)

@property
def address(self):
if self._address is None:
Expand Down Expand Up @@ -309,6 +313,18 @@ def __init__(self, attachment, encoding):
if self.content_type is None:
self.content_type = self.mimetype

def __repr__(self):
details = [
self.mimetype,
"len={length}".format(length=len(self.content)),
]
if self.name:
details.append("name={name!r}".format(name=self.name))
if self.inline:
details.insert(0, "inline")
details.append("content_id={content_id!r}".format(content_id=self.content_id))
return "Attachment<{details}>".format(details=", ".join(details))

@property
def b64content(self):
"""Content encoded as a base64 ascii string"""
Expand Down
12 changes: 12 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ def test_no_newlines(self):
with self.assertRaisesMessage(ValueError, "cannot contain newlines"):
_ = EmailAddress(name, addr)

def test_email_address_repr(self):
self.assertEqual("EmailAddress('Name', 'addr@example.com')",
repr(EmailAddress('Name', 'addr@example.com')))


class NormalizedAttachmentTests(SimpleTestCase):
"""Test utils.Attachment"""
Expand All @@ -188,6 +192,7 @@ def test_content_disposition_attachment(self):
self.assertFalse(att.inline)
self.assertIsNone(att.content_id)
self.assertEqual(att.cid, "")
self.assertEqual(repr(att), "Attachment<image/x-emoticon, len=3, name='emoticon.txt'>")

def test_content_disposition_inline(self):
image = MIMEImage(b";-)", "x-emoticon")
Expand All @@ -198,11 +203,14 @@ def test_content_disposition_inline(self):
self.assertTrue(att.inline) # even without the Content-ID
self.assertIsNone(att.content_id)
self.assertEqual(att.cid, "")
self.assertEqual(repr(att), "Attachment<inline, image/x-emoticon, len=3, content_id=None>")

image["Content-ID"] = "<abc123@example.net>"
att = Attachment(image, "ascii")
self.assertEqual(att.content_id, "<abc123@example.net>")
self.assertEqual(att.cid, "abc123@example.net")
self.assertEqual(repr(att),
"Attachment<inline, image/x-emoticon, len=3, content_id='<abc123@example.net>'>")

def test_content_id_implies_inline(self):
"""A MIME object with a Content-ID should be assumed to be inline"""
Expand All @@ -211,17 +219,21 @@ def test_content_id_implies_inline(self):
att = Attachment(image, "ascii")
self.assertTrue(att.inline)
self.assertEqual(att.content_id, "<abc123@example.net>")
self.assertEqual(repr(att),
"Attachment<inline, image/x-emoticon, len=3, content_id='<abc123@example.net>'>")

# ... but not if explicit Content-Disposition says otherwise
image["Content-Disposition"] = "attachment"
att = Attachment(image, "ascii")
self.assertFalse(att.inline)
self.assertIsNone(att.content_id) # ignored for non-inline Attachment
self.assertEqual(repr(att), "Attachment<image/x-emoticon, len=3>")

def test_content_type(self):
att = Attachment(MIMEText("text", "plain", "iso8859-1"), "ascii")
self.assertEqual(att.mimetype, "text/plain")
self.assertEqual(att.content_type, 'text/plain; charset="iso8859-1"')
self.assertEqual(repr(att), "Attachment<text/plain, len=4>")


class LazyCoercionTests(SimpleTestCase):
Expand Down

0 comments on commit 03dd15d

Please sign in to comment.