-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from clickonchris/django-post-office3
Fixes a HTML content problem uncovered from django-post-office 3
- Loading branch information
Showing
7 changed files
with
79 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
language: python | ||
python: | ||
- '2.7' | ||
- '3.5' | ||
- '3.6' | ||
- '3.7' | ||
|
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Django==1.11.23 | ||
Django==2.2.9 | ||
mock |
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 |
---|---|---|
|
@@ -9,14 +9,16 @@ class EmailForm(forms.Form): | |
sender = forms.EmailField(max_length=100, initial=settings.POSTMARK_SENDER) | ||
to = forms.CharField(initial='[email protected]') | ||
subject = forms.CharField(initial='test email') | ||
body = forms.CharField(widget=forms.Textarea, initial='this is a test') | ||
body = forms.CharField(widget=forms.Textarea, initial='this is a test') | ||
html_body = forms.CharField(widget=forms.Textarea, initial='<h1>Test</h1>This is a test') | ||
|
||
def save(self, fail_silently=False): | ||
""" | ||
Build and send the email message. | ||
""" | ||
send_mail(subject=unicode(ugettext_lazy(self.cleaned_data['subject'])), | ||
message=self.cleaned_data['body'], | ||
send_mail(subject=str(ugettext_lazy(self.cleaned_data['subject'])), | ||
message=self.cleaned_data['body'], | ||
html_message=self.cleaned_data['html_body'], | ||
from_email=self.cleaned_data['sender'], | ||
recipient_list=[addr.strip() for addr in self.cleaned_data['to'].split(',')], | ||
fail_silently=fail_silently) | ||
|
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 |
---|---|---|
@@ -1,9 +1,15 @@ | ||
import json | ||
import sys | ||
import unittest | ||
from email.mime.image import MIMEImage | ||
|
||
from io import BytesIO | ||
|
||
from django.core.mail import EmailMultiAlternatives, EmailMessage | ||
from django.test import override_settings, TestCase | ||
|
||
from postmark.django_backend import EmailBackend | ||
|
||
if sys.version_info[0] < 3: | ||
from StringIO import StringIO | ||
from urllib2 import HTTPError | ||
|
@@ -206,7 +212,60 @@ def test_activate(self): | |
|
||
with mock.patch('postmark.core.HTTPConnection.getresponse') as mock_response: | ||
mock_response.return_value = StringIO('{"test": "test"}') | ||
self.assertEquals(bounce.activate(1), {'test': 'test'}) | ||
self.assertEqual(bounce.activate(1), {'test': 'test'}) | ||
|
||
|
||
class EmailBackendTests(TestCase): | ||
|
||
def setUp(self): | ||
self.backend = EmailBackend(api_key='dummy') | ||
|
||
def test_send_multi_alternative_html_email(self): | ||
# build a message and send it | ||
message = EmailMultiAlternatives( | ||
connection=self.backend, | ||
from_email='[email protected]', to=['[email protected]'], subject='html test', body='hello there' | ||
) | ||
message.attach_alternative('<b>hello</b> there', 'text/html') | ||
|
||
with mock.patch('postmark.core.urlopen', side_effect=HTTPError('', 200, '', {}, None)) as transport: | ||
message.send() | ||
data = json.loads(transport.call_args[0][0].data.decode('utf-8')) | ||
self.assertEqual('hello there', data['TextBody']) | ||
self.assertEqual('<b>hello</b> there', data['HtmlBody']) | ||
|
||
def test_send_content_subtype_email(self): | ||
# build a message and send it | ||
message = EmailMessage( | ||
connection=self.backend, | ||
from_email='[email protected]', to=['[email protected]'], subject='html test', body='<b>hello</b> there' | ||
) | ||
message.content_subtype = 'html' | ||
|
||
with mock.patch('postmark.core.urlopen', side_effect=HTTPError('', 200, '', {}, None)) as transport: | ||
message.send() | ||
data = json.loads(transport.call_args[0][0].data.decode('utf-8')) | ||
self.assertEqual('<b>hello</b> there', data['HtmlBody']) | ||
self.assertFalse('TextBody' in data) | ||
|
||
def test_send_multi_alternative_with_subtype_html_email(self): | ||
""" | ||
Client uses EmailMultiAlternative but instead of specifying a html alternative they insert html content | ||
into the main message and specify message_subtype | ||
:return: | ||
""" | ||
message = EmailMultiAlternatives( | ||
connection=self.backend, | ||
from_email='[email protected]', to=['[email protected]'], subject='html test', body='<b>hello</b> there' | ||
) | ||
# NO alternatives attached. subtype specified instead | ||
message.content_subtype = 'html' | ||
|
||
with mock.patch('postmark.core.urlopen', side_effect=HTTPError('', 200, '', {}, None)) as transport: | ||
message.send() | ||
data = json.loads(transport.call_args[0][0].data.decode('utf-8')) | ||
self.assertFalse('TextBody' in data) | ||
self.assertEqual('<b>hello</b> there', data['HtmlBody']) | ||
|
||
|
||
if __name__ == '__main__': | ||
|
@@ -215,6 +274,7 @@ def test_activate(self): | |
DATABASES={ | ||
'default': { | ||
'ENGINE': 'django.db.backends.sqlite3', | ||
'NAME': ':memory:' | ||
} | ||
}, | ||
INSTALLED_APPS=[ | ||
|