diff --git a/.travis.yml b/.travis.yml index b618e91..516b527 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: python python: -- '2.7' - '3.5' - '3.6' - '3.7' diff --git a/postmark/django_backend.py b/postmark/django_backend.py index cac7bcd..b818c88 100644 --- a/postmark/django_backend.py +++ b/postmark/django_backend.py @@ -82,7 +82,7 @@ def _build_message(self, message): html_body = alt[0] break - elif getattr(message, 'content_subtype', None) == 'html': + if getattr(message, 'content_subtype', None) == 'html': # Don't send html content as plain text text_body = None html_body = message.body diff --git a/requirements.txt b/requirements.txt index 05944d0..4514aef 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -Django==1.11.23 +Django==2.2.9 mock diff --git a/test/demo/forms.py b/test/demo/forms.py index 076ad45..d1429f5 100644 --- a/test/demo/forms.py +++ b/test/demo/forms.py @@ -9,14 +9,16 @@ class EmailForm(forms.Form): sender = forms.EmailField(max_length=100, initial=settings.POSTMARK_SENDER) to = forms.CharField(initial='bill@averline.com') 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='

Test

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) diff --git a/test/demo/settings.py b/test/demo/settings.py index fef6898..1a4931b 100644 --- a/test/demo/settings.py +++ b/test/demo/settings.py @@ -33,7 +33,7 @@ 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', - 'django.contrib.admin', + # 'django.contrib.admin', ) MIDDLEWARE_CLASSES = ( @@ -41,9 +41,12 @@ ROOT_URLCONF = 'urls' -TEMPLATE_DIRS = ( - os.path.join(settings_path, 'templates'), -) +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [os.path.join(settings_path, 'templates')], + } +] #EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' #EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' diff --git a/test/demo/urls.py b/test/demo/urls.py index 51fb4d0..6843c23 100644 --- a/test/demo/urls.py +++ b/test/demo/urls.py @@ -3,7 +3,8 @@ # Uncomment the next two lines to enable the admin: # from django.contrib import admin # admin.autodiscover() +import views -urlpatterns = patterns('', - (r'', 'views.test_email'), -) +urlpatterns = [ + url(r'', views.test_email), +] diff --git a/tests.py b/tests.py index fc02abc..ae31de1 100644 --- a/tests.py +++ b/tests.py @@ -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='from@test.com', to=['recipient@test.com'], subject='html test', body='hello there' + ) + message.attach_alternative('hello 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('hello there', data['HtmlBody']) + + def test_send_content_subtype_email(self): + # build a message and send it + message = EmailMessage( + connection=self.backend, + from_email='from@test.com', to=['recipient@test.com'], subject='html test', body='hello 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('hello 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='from@test.com', to=['recipient@test.com'], subject='html test', body='hello 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('hello 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=[