| 1 |
import pytest |
| 2 |
import mock |
| 3 |
|
| 4 |
from django.conf import settings |
| 5 |
from django.core.mail import send_mail |
| 6 |
from django.core.mail import send_mass_mail |
| 7 |
from django.core.mail import EmailMultiAlternatives |
| 8 |
from django.utils.functional import empty |
| 9 |
|
| 10 |
from sparkpost import EU_API, US_API |
| 11 |
from sparkpost.django.email_backend import SparkPostEmailBackend |
| 12 |
from sparkpost.django.exceptions import UnsupportedContent |
| 13 |
from sparkpost.transmissions import Transmissions |
| 14 |
|
| 15 |
API_KEY = 'API_Key' |
| 16 |
|
| 17 |
|
| 18 |
def reconfigure_settings(**new_settings): |
| 19 |
old_settings = settings._wrapped |
| 20 |
|
| 21 |
settings._wrapped = empty |
| 22 |
settings.configure(default_settings=old_settings, **new_settings) |
| 23 |
|
| 24 |
|
| 25 |
reconfigure_settings( |
| 26 |
DEBUG=True, |
| 27 |
EMAIL_BACKEND='sparkpost.django.email_backend.SparkPostEmailBackend', |
| 28 |
SPARKPOST_API_KEY=API_KEY |
| 29 |
) |
| 30 |
|
| 31 |
|
| 32 |
def get_params(overrides=None): |
| 33 |
if overrides is None: |
| 34 |
overrides = {} |
| 35 |
|
| 36 |
defaults = { |
| 37 |
'subject': 'test subject', |
| 38 |
'message': 'test body', |
| 39 |
'from_email': '[email protected]', |
| 40 |
'recipient_list': ['[email protected]'], |
| 41 |
} |
| 42 |
|
| 43 |
params = defaults.copy() |
| 44 |
params.update(overrides) |
| 45 |
return params |
| 46 |
|
| 47 |
|
| 48 |
def mailer(params): |
| 49 |
return send_mail(**params) |
| 50 |
|
| 51 |
|
| 52 |
def test_password_retrieval(): |
| 53 |
backend = SparkPostEmailBackend() |
| 54 |
assert backend.client.api_key == API_KEY |
| 55 |
|
| 56 |
|
| 57 |
def test_default_base_uri_retrieval(): |
| 58 |
backend = SparkPostEmailBackend() |
| 59 |
assert backend.client.base_uri.startswith('https://' + US_API) |
| 60 |
|
| 61 |
|
| 62 |
def test_eu_base_uri_retrieval(): |
| 63 |
reconfigure_settings(SPARKPOST_BASE_URI=EU_API) |
| 64 |
backend = SparkPostEmailBackend() |
| 65 |
assert backend.client.base_uri.startswith('https://' + EU_API) |
| 66 |
|
| 67 |
|
| 68 |
def test_fail_silently(): |
| 69 |
# should not raise |
| 70 |
with mock.patch.object(Transmissions, 'send') as mock_send: |
| 71 |
mock_send.side_effect = Exception('i should not be raised') |
| 72 |
mailer(get_params({'fail_silently': True})) |
| 73 |
|
| 74 |
# should raise |
| 75 |
with mock.patch.object(Transmissions, 'send') as mock_send: |
| 76 |
mock_send.side_effect = Exception('i should be raised') |
| 77 |
with pytest.raises(Exception): |
| 78 |
mailer(get_params()) |
| 79 |
|
| 80 |
|
| 81 |
def test_successful_sending(): |
| 82 |
with mock.patch.object(Transmissions, 'send') as mock_send: |
| 83 |
mock_send.return_value = {'total_accepted_recipients': 1, |
| 84 |
'total_rejected_recipients': 2} |
| 85 |
|
| 86 |
result = mailer(get_params({ |
| 87 |
'recipient_list': ['[email protected]', '[email protected]'], |
| 88 |
'fail_silently': True |
| 89 |
})) |
| 90 |
assert result == 1 |
| 91 |
|
| 92 |
with mock.patch.object(Transmissions, 'send') as mock_send: |
| 93 |
mock_send.return_value = {'total_accepted_recipients': 10, |
| 94 |
'total_rejected_recipients': 2} |
| 95 |
|
| 96 |
result = mailer( |
| 97 |
get_params( |
| 98 |
{'recipient_list': ['[email protected]', '[email protected]'], |
| 99 |
'fail_silently': True |
| 100 |
} |
| 101 |
)) |
| 102 |
|
| 103 |
assert result == 10 |
| 104 |
|
| 105 |
|
| 106 |
def test_send_number_of_emails_correctly(): |
| 107 |
with mock.patch.object(Transmissions, 'send') as mock_send: |
| 108 |
mailer(get_params({ |
| 109 |
'recipient_list': ['[email protected]', '[email protected]'], |
| 110 |
'fail_silently': True |
| 111 |
})) |
| 112 |
assert mock_send.call_count == 1 |
| 113 |
|
| 114 |
with mock.patch.object(Transmissions, 'send') as mock_send: |
| 115 |
message1 = ('message 1 subject', 'message 1 body', '[email protected]', |
| 116 |
['[email protected]']) |
| 117 |
message2 = ('message 2 subject', 'message 2 body', '[email protected]', |
| 118 |
['[email protected]']) |
| 119 |
message3 = ('message 3 subject', 'message 3 body', '[email protected]', |
| 120 |
['[email protected]']) |
| 121 |
|
| 122 |
send_mass_mail((message1, message2, message3), fail_silently=False) |
| 123 |
assert mock_send.call_count == 3 |
| 124 |
|
| 125 |
|
| 126 |
def test_params(): |
| 127 |
recipients = ['[email protected]', '[email protected]'] |
| 128 |
with mock.patch.object(Transmissions, 'send'): |
| 129 |
mailer(get_params( |
| 130 |
{'recipient_list': recipients, |
| 131 |
'fail_silently': True |
| 132 |
} |
| 133 |
)) |
| 134 |
|
| 135 |
Transmissions.send.assert_called_with(recipients=recipients, |
| 136 |
text='test body', |
| 137 |
from_email='[email protected]', |
| 138 |
subject='test subject' |
| 139 |
) |
| 140 |
|
| 141 |
|
| 142 |
def test_content_types(): |
| 143 |
def new_send(**kwargs): |
| 144 |
assert kwargs['text'] == 'hello there' |
| 145 |
assert kwargs['html'] == '<p>Hello There</p>' |
| 146 |
|
| 147 |
return { |
| 148 |
'total_accepted_recipients': 0, |
| 149 |
'total_rejected_recipients': 0 |
| 150 |
} |
| 151 |
|
| 152 |
with mock.patch.object(Transmissions, 'send') as mock_send: |
| 153 |
mock_send.side_effect = new_send |
| 154 |
send_mail( |
| 155 |
'test subject', |
| 156 |
'hello there', |
| 157 |
'[email protected]', |
| 158 |
['[email protected]'], |
| 159 |
html_message='<p>Hello There</p>' |
| 160 |
) |
| 161 |
|
| 162 |
|
| 163 |
def test_send_plain_mail_after_html_mail(): |
| 164 |
SPARKPOST_OPTIONS = { |
| 165 |
'track_opens': False, |
| 166 |
'track_clicks': False, |
| 167 |
'transactional': True, |
| 168 |
} |
| 169 |
|
| 170 |
reconfigure_settings(SPARKPOST_OPTIONS=SPARKPOST_OPTIONS) |
| 171 |
|
| 172 |
def new_send(**kwargs): |
| 173 |
assert kwargs['text'] == 'hello there' |
| 174 |
assert kwargs['html'] == '<p>Hello There</p>' |
| 175 |
|
| 176 |
return { |
| 177 |
'total_accepted_recipients': 0, |
| 178 |
'total_rejected_recipients': 0 |
| 179 |
} |
| 180 |
|
| 181 |
def new_send_text_only(**kwargs): |
| 182 |
assert kwargs['text'] == 'hello there again in text only' |
| 183 |
assert "html" not in kwargs |
| 184 |
|
| 185 |
return { |
| 186 |
'total_accepted_recipients': 0, |
| 187 |
'total_rejected_recipients': 0 |
| 188 |
} |
| 189 |
|
| 190 |
with mock.patch.object(Transmissions, 'send') as mock_send: |
| 191 |
mock_send.side_effect = new_send |
| 192 |
send_mail( |
| 193 |
'test subject', |
| 194 |
'hello there', |
| 195 |
'[email protected]', |
| 196 |
['[email protected]'], |
| 197 |
html_message='<p>Hello There</p>' |
| 198 |
) |
| 199 |
mock_send.side_effect = new_send_text_only |
| 200 |
send_mail( |
| 201 |
'test subject 2', |
| 202 |
'hello there again in text only', |
| 203 |
'[email protected]', |
| 204 |
['[email protected]'], |
| 205 |
) |
| 206 |
|
| 207 |
|
| 208 |
def test_unsupported_content_types(): |
| 209 |
params = get_params() |
| 210 |
|
| 211 |
with pytest.raises(UnsupportedContent): |
| 212 |
mail = EmailMultiAlternatives( |
| 213 |
params['subject'], |
| 214 |
'plain text', |
| 215 |
params['from_email'], |
| 216 |
params['recipient_list']) |
| 217 |
mail.attach_alternative('<ppp>non-plain content</ppp>', 'text/alien') |
| 218 |
mail.send() |
| 219 |
|
| 220 |
|
| 221 |
def test_settings_options(): |
| 222 |
SPARKPOST_OPTIONS = { |
| 223 |
'track_opens': False, |
| 224 |
'track_clicks': False, |
| 225 |
'transactional': True, |
| 226 |
} |
| 227 |
|
| 228 |
reconfigure_settings(SPARKPOST_OPTIONS=SPARKPOST_OPTIONS) |
| 229 |
|
| 230 |
with mock.patch.object(Transmissions, 'send'): |
| 231 |
mailer(get_params()) |
| 232 |
expected_kargs = get_params().copy() |
| 233 |
expected_kargs["text"] = expected_kargs["message"] |
| 234 |
expected_kargs["recipients"] = expected_kargs["recipient_list"] |
| 235 |
del expected_kargs["message"] |
| 236 |
del expected_kargs["recipient_list"] |
| 237 |
expected_kargs.update(SPARKPOST_OPTIONS) |
| 238 |
Transmissions.send.assert_called_with(**expected_kargs) |