forked from django/django
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #35537 -- Improved documentation and test coverage for email att…
…achments and alternatives.
- Loading branch information
1 parent
5424151
commit d5bebc1
Showing
5 changed files
with
128 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ | |
from django.core import mail | ||
from django.core.mail import ( | ||
DNS_NAME, | ||
EmailAlternative, | ||
EmailAttachment, | ||
EmailMessage, | ||
EmailMultiAlternatives, | ||
mail_admins, | ||
|
@@ -557,12 +559,50 @@ def test_alternatives(self): | |
mime_type = "text/html" | ||
msg.attach_alternative(html_content, mime_type) | ||
|
||
self.assertIsInstance(msg.alternatives[0], EmailAlternative) | ||
|
||
self.assertEqual(msg.alternatives[0][0], html_content) | ||
self.assertEqual(msg.alternatives[0].content, html_content) | ||
|
||
self.assertEqual(msg.alternatives[0][1], mime_type) | ||
self.assertEqual(msg.alternatives[0].mimetype, mime_type) | ||
|
||
self.assertIn(html_content, msg.message().as_string()) | ||
|
||
def test_alternatives_constructor(self): | ||
html_content = "<p>This is <strong>html</strong></p>" | ||
mime_type = "text/html" | ||
|
||
msg = EmailMultiAlternatives( | ||
alternatives=[EmailAlternative(html_content, mime_type)] | ||
) | ||
|
||
self.assertIsInstance(msg.alternatives[0], EmailAlternative) | ||
|
||
self.assertEqual(msg.alternatives[0][0], html_content) | ||
self.assertEqual(msg.alternatives[0].content, html_content) | ||
|
||
self.assertEqual(msg.alternatives[0][1], mime_type) | ||
self.assertEqual(msg.alternatives[0].mimetype, mime_type) | ||
|
||
self.assertIn(html_content, msg.message().as_string()) | ||
|
||
def test_alternatives_constructor_from_tuple(self): | ||
html_content = "<p>This is <strong>html</strong></p>" | ||
mime_type = "text/html" | ||
|
||
msg = EmailMultiAlternatives(alternatives=[(html_content, mime_type)]) | ||
|
||
self.assertIsInstance(msg.alternatives[0], EmailAlternative) | ||
|
||
self.assertEqual(msg.alternatives[0][0], html_content) | ||
self.assertEqual(msg.alternatives[0].content, html_content) | ||
|
||
self.assertEqual(msg.alternatives[0][1], mime_type) | ||
self.assertEqual(msg.alternatives[0].mimetype, mime_type) | ||
|
||
self.assertIn(html_content, msg.message().as_string()) | ||
|
||
def test_none_body(self): | ||
msg = EmailMessage("subject", None, "[email protected]", ["[email protected]"]) | ||
self.assertEqual(msg.body, "") | ||
|
@@ -654,6 +694,51 @@ def test_attachments(self): | |
self.assertEqual(msg.attachments[0][2], mime_type) | ||
self.assertEqual(msg.attachments[0].mimetype, mime_type) | ||
|
||
attachments = self.get_decoded_attachments(msg) | ||
self.assertEqual(attachments[0], (file_name, file_content.encode(), mime_type)) | ||
|
||
def test_attachments_constructor(self): | ||
file_name = "example.txt" | ||
file_content = "Text file content" | ||
mime_type = "text/plain" | ||
msg = EmailMessage( | ||
attachments=[EmailAttachment(file_name, file_content, mime_type)] | ||
) | ||
|
||
self.assertIsInstance(msg.attachments[0], EmailAttachment) | ||
|
||
self.assertEqual(msg.attachments[0][0], file_name) | ||
self.assertEqual(msg.attachments[0].filename, file_name) | ||
|
||
self.assertEqual(msg.attachments[0][1], file_content) | ||
self.assertEqual(msg.attachments[0].content, file_content) | ||
|
||
self.assertEqual(msg.attachments[0][2], mime_type) | ||
self.assertEqual(msg.attachments[0].mimetype, mime_type) | ||
|
||
attachments = self.get_decoded_attachments(msg) | ||
self.assertEqual(attachments[0], (file_name, file_content.encode(), mime_type)) | ||
|
||
def test_attachments_constructor_from_tuple(self): | ||
file_name = "example.txt" | ||
file_content = "Text file content" | ||
mime_type = "text/plain" | ||
msg = EmailMessage(attachments=[(file_name, file_content, mime_type)]) | ||
|
||
self.assertIsInstance(msg.attachments[0], EmailAttachment) | ||
|
||
self.assertEqual(msg.attachments[0][0], file_name) | ||
self.assertEqual(msg.attachments[0].filename, file_name) | ||
|
||
self.assertEqual(msg.attachments[0][1], file_content) | ||
self.assertEqual(msg.attachments[0].content, file_content) | ||
|
||
self.assertEqual(msg.attachments[0][2], mime_type) | ||
self.assertEqual(msg.attachments[0].mimetype, mime_type) | ||
|
||
attachments = self.get_decoded_attachments(msg) | ||
self.assertEqual(attachments[0], (file_name, file_content.encode(), mime_type)) | ||
|
||
def test_decoded_attachments(self): | ||
"""Regression test for #9367""" | ||
headers = {"Date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"} | ||
|