Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate type of recipients #141

Merged
merged 4 commits into from
Mar 7, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions sparkpost/transmissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from email.utils import parseaddr

from .base import Resource
from .exceptions import SparkPostException


try:
Expand Down Expand Up @@ -137,6 +138,10 @@ def _parse_address(self, address):
return parsed_address

def _extract_recipients(self, recipients):

if not (isinstance(recipients, list) or isinstance(recipients, dict)):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can pass a tuple as the second argument to isinstance like so:

isinstance(recipients, (list, dict))

raise SparkPostException('recipients must be a list or dict')

formatted_recipients = []
for recip in recipients:
if isinstance(recip, string_types):
Expand Down
15 changes: 14 additions & 1 deletion test/test_transmissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from sparkpost import SparkPost
from sparkpost import Transmissions
from sparkpost.exceptions import SparkPostAPIException
from sparkpost.exceptions import SparkPostAPIException, SparkPostException


def test_translate_keys_with_list():
Expand All @@ -28,6 +28,19 @@ def test_translate_keys_with_recips():
{'key': 'value'},
{'address': {'email': 'foobar'}}]

results = t._translate_keys(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this testing differently than the above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just wanted to test a different data structure but looks like redundant. i've removed it.

recipients=[{'address': {'name': 'foo', 'email': 'bar'}}]
)
assert results['recipients'] == [
{'address': {'name': 'foo', 'email': 'bar'}}
]


def test_exceptions_for_recipients():
t = Transmissions('uri', 'key')
with pytest.raises(SparkPostException):
t._translate_keys(recipients='test')


def test_translate_keys_with_unicode_recips():
t = Transmissions('uri', 'key')
Expand Down