Skip to content

Commit 1dc851d

Browse files
committed
Validate type of recipients
1 parent 8808080 commit 1dc851d

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

sparkpost/transmissions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
import copy
33
import json
44
from email.utils import parseaddr
5+
import types
56

67
from .base import Resource
8+
from .exceptions import SparkPostException
79

810

911
try:
@@ -137,6 +139,10 @@ def _parse_address(self, address):
137139
return parsed_address
138140

139141
def _extract_recipients(self, recipients):
142+
143+
if not (isinstance(recipients, types.ListType) or isinstance(recipients, types.DictType)):
144+
raise SparkPostException('recipients must be a list or dict')
145+
140146
formatted_recipients = []
141147
for recip in recipients:
142148
if isinstance(recip, string_types):

test/test_transmissions.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from sparkpost import SparkPost
1111
from sparkpost import Transmissions
12-
from sparkpost.exceptions import SparkPostAPIException
12+
from sparkpost.exceptions import SparkPostAPIException, SparkPostException
1313

1414

1515
def test_translate_keys_with_list():
@@ -28,6 +28,14 @@ def test_translate_keys_with_recips():
2828
{'key': 'value'},
2929
{'address': {'email': 'foobar'}}]
3030

31+
results = t._translate_keys(recipients=[{'address': {'name': 'foo', 'email': 'bar'}}])
32+
assert results['recipients'] == [{'address': {'name': 'foo', 'email': 'bar'}}]
33+
34+
def test_exceptions_for_recipients():
35+
t = Transmissions('uri', 'key')
36+
with pytest.raises(SparkPostException):
37+
results = t._translate_keys(recipients='test')
38+
3139

3240
def test_translate_keys_with_unicode_recips():
3341
t = Transmissions('uri', 'key')

0 commit comments

Comments
 (0)