Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_substitution_data():
email_message = EmailMessage(
to=[
{
"address": "to@example.com",
"substitution_data": {
"key": "value"
}
}
],
from_email='test@from.com'
)
email_message.template = 'template-id'
email_message.substitution_data = {"key2": "value2"}
actual = SparkPostMessage(email_message)
expected = dict(
recipients=[
{
"address": "to@example.com",
"substitution_data": {
"key": "value"
}
}
],
from_email='test@from.com',
template='template-id',
substitution_data={"key2": "value2"}
)
assert actual == expected
def test_content_subtype():
email_message = EmailMessage(
to=['to@example.com'],
from_email='test@from.com',
body='<p>Testing</p>'
)
email_message.content_subtype = 'html'
actual = SparkPostMessage(email_message)
expected = dict(
recipients=['to@example.com'],
from_email='test@from.com',
html='<p>Testing</p>'
)
assert actual == expected
def message(**options):
options.update(base_options)
email_message = EmailMessage(**options)
return SparkPostMessage(email_message)
def test_campaign():
email_message = EmailMessage(**base_options)
email_message.campaign = 'campaign-id'
actual = SparkPostMessage(email_message)
expected = dict(
campaign='campaign-id'
)
expected.update(base_expected)
assert actual == expected
def test_transactional():
email_message = EmailMessage(**base_options)
import json
msys_api = json.dumps({'options': {'transactional': True}})
email_message.extra_headers['X-MSYS-API'] = msys_api
actual = SparkPostMessage(email_message)
expected = dict(
custom_headers={'X-MSYS-API': msys_api},
transactional=True,
)
expected.update(base_expected)
assert actual == expected
def test_template():
email_message = EmailMessage(
to=['to@example.com'],
from_email='test@from.com'
)
email_message.template = 'template-id'
actual = SparkPostMessage(email_message)
expected = dict(
recipients=['to@example.com'],
from_email='test@from.com',
template='template-id'
)
assert actual == expected
def test_attachment():
email_message = EmailMessage(**base_options)
email_message.attach('file.txt', 'test content', 'text/plain')
actual = SparkPostMessage(email_message)
expected = dict(
attachments=[
{
'name': 'file.txt',
'data': 'dGVzdCBjb250ZW50',
'type': 'text/plain'
}
]
)
expected.update(base_expected)
assert actual == expected
def send_messages(self, email_messages):
"""
Send emails, returns integer representing number of successful emails
"""
success = 0
for message in email_messages:
try:
response = self._send(SparkPostMessage(message))
success += response['total_accepted_recipients']
except Exception:
if not self.fail_silently:
raise
return success