Skip to content

Commit

Permalink
Resolve global settings update bug
Browse files Browse the repository at this point in the history
The Django settings object was being inadvertently modified, causing mixed
emails to be sent - e.g. when sending text email after sending an HTML email.

Resolves #88
  • Loading branch information
Guillermo Narvaja authored and Rich Leland committed Mar 18, 2016
1 parent 96a8492 commit e85cf29
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sparkpost/django/email_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ def send_messages(self, email_messages):
return success

def _send(self, message):
params = getattr(settings, 'SPARKPOST_OPTIONS', {})
params = getattr(settings, 'SPARKPOST_OPTIONS', {}).copy()
params.update(message)
return self.client.transmissions.send(**params)
53 changes: 52 additions & 1 deletion test/django/test_email_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,51 @@ def new_send(**kwargs):
)


def test_send_plain_mail_after_html_mail():
SPARKPOST_OPTIONS = {
'track_opens': False,
'track_clicks': False,
'transactional': True,
}

reconfigure_settings(SPARKPOST_OPTIONS=SPARKPOST_OPTIONS)

def new_send(**kwargs):
assert kwargs['text'] == 'hello there'
assert kwargs['html'] == '<p>Hello There</p>'

return {
'total_accepted_recipients': 0,
'total_rejected_recipients': 0
}

def new_send_text_only(**kwargs):
assert kwargs['text'] == 'hello there again in text only'
assert "html" not in kwargs

return {
'total_accepted_recipients': 0,
'total_rejected_recipients': 0
}

with mock.patch.object(Transmissions, 'send') as mock_send:
mock_send.side_effect = new_send
send_mail(
'test subject',
'hello there',
'from@example.com',
['to@example.com'],
html_message='<p>Hello There</p>'
)
mock_send.side_effect = new_send_text_only
send_mail(
'test subject 2',
'hello there again in text only',
'from@example.com',
['to@example.com'],
)


def test_unsupported_content_types():
params = get_params()

Expand All @@ -172,4 +217,10 @@ def test_settings_options():

with mock.patch.object(Transmissions, 'send'):
mailer(get_params())
Transmissions.send.assert_called_with(**SPARKPOST_OPTIONS)
expected_kargs = get_params().copy()
expected_kargs["text"] = expected_kargs["message"]
expected_kargs["recipients"] = expected_kargs["recipient_list"]
del expected_kargs["message"]
del expected_kargs["recipient_list"]
expected_kargs.update(SPARKPOST_OPTIONS)
Transmissions.send.assert_called_with(**expected_kargs)

0 comments on commit e85cf29

Please sign in to comment.