From 1dc851dcca90bb9c9bc289b2c1c61dfb52265f82 Mon Sep 17 00:00:00 2001 From: mhossain Date: Thu, 2 Mar 2017 17:01:14 -0500 Subject: [PATCH 1/4] Validate type of recipients --- sparkpost/transmissions.py | 6 ++++++ test/test_transmissions.py | 10 +++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/sparkpost/transmissions.py b/sparkpost/transmissions.py index 3586e2f..053c71e 100644 --- a/sparkpost/transmissions.py +++ b/sparkpost/transmissions.py @@ -2,8 +2,10 @@ import copy import json from email.utils import parseaddr +import types from .base import Resource +from .exceptions import SparkPostException try: @@ -137,6 +139,10 @@ def _parse_address(self, address): return parsed_address def _extract_recipients(self, recipients): + + if not (isinstance(recipients, types.ListType) or isinstance(recipients, types.DictType)): + raise SparkPostException('recipients must be a list or dict') + formatted_recipients = [] for recip in recipients: if isinstance(recip, string_types): diff --git a/test/test_transmissions.py b/test/test_transmissions.py index 9847fb2..771dfcf 100644 --- a/test/test_transmissions.py +++ b/test/test_transmissions.py @@ -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(): @@ -28,6 +28,14 @@ def test_translate_keys_with_recips(): {'key': 'value'}, {'address': {'email': 'foobar'}}] + results = t._translate_keys(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): + results = t._translate_keys(recipients='test') + def test_translate_keys_with_unicode_recips(): t = Transmissions('uri', 'key') From 11279cfa081a0ce9a7a6ca7579f8ba38e0a9cad1 Mon Sep 17 00:00:00 2001 From: mhossain Date: Thu, 2 Mar 2017 17:10:15 -0500 Subject: [PATCH 2/4] fix styling --- sparkpost/transmissions.py | 5 ++++- test/test_transmissions.py | 11 ++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/sparkpost/transmissions.py b/sparkpost/transmissions.py index 053c71e..147685c 100644 --- a/sparkpost/transmissions.py +++ b/sparkpost/transmissions.py @@ -140,7 +140,10 @@ def _parse_address(self, address): def _extract_recipients(self, recipients): - if not (isinstance(recipients, types.ListType) or isinstance(recipients, types.DictType)): + if not ( + isinstance(recipients, types.ListType) or + isinstance(recipients, types.DictType) + ): raise SparkPostException('recipients must be a list or dict') formatted_recipients = [] diff --git a/test/test_transmissions.py b/test/test_transmissions.py index 771dfcf..f9ec23a 100644 --- a/test/test_transmissions.py +++ b/test/test_transmissions.py @@ -28,13 +28,18 @@ def test_translate_keys_with_recips(): {'key': 'value'}, {'address': {'email': 'foobar'}}] - results = t._translate_keys(recipients=[{'address': {'name': 'foo', 'email': 'bar'}}]) - assert results['recipients'] == [{'address': {'name': 'foo', 'email': 'bar'}}] + results = t._translate_keys( + 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): - results = t._translate_keys(recipients='test') + t._translate_keys(recipients='test') def test_translate_keys_with_unicode_recips(): From 67b464eccf5a99c3e82803deccd25acd9dc7743d Mon Sep 17 00:00:00 2001 From: mhossain Date: Thu, 2 Mar 2017 17:21:22 -0500 Subject: [PATCH 3/4] Remove types module it is not compatible in python 3x --- sparkpost/transmissions.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/sparkpost/transmissions.py b/sparkpost/transmissions.py index 147685c..da79cc9 100644 --- a/sparkpost/transmissions.py +++ b/sparkpost/transmissions.py @@ -2,7 +2,6 @@ import copy import json from email.utils import parseaddr -import types from .base import Resource from .exceptions import SparkPostException @@ -140,10 +139,7 @@ def _parse_address(self, address): def _extract_recipients(self, recipients): - if not ( - isinstance(recipients, types.ListType) or - isinstance(recipients, types.DictType) - ): + if not (isinstance(recipients, list) or isinstance(recipients, dict)): raise SparkPostException('recipients must be a list or dict') formatted_recipients = [] From a99c5aa06e9ab104ead7374a184adb13cf7ba874 Mon Sep 17 00:00:00 2001 From: mhossain Date: Mon, 6 Mar 2017 10:26:40 -0500 Subject: [PATCH 4/4] simplify type check, remove redundant test --- sparkpost/transmissions.py | 2 +- test/test_transmissions.py | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/sparkpost/transmissions.py b/sparkpost/transmissions.py index da79cc9..667b891 100644 --- a/sparkpost/transmissions.py +++ b/sparkpost/transmissions.py @@ -139,7 +139,7 @@ def _parse_address(self, address): def _extract_recipients(self, recipients): - if not (isinstance(recipients, list) or isinstance(recipients, dict)): + if not (isinstance(recipients, (list, dict))): raise SparkPostException('recipients must be a list or dict') formatted_recipients = [] diff --git a/test/test_transmissions.py b/test/test_transmissions.py index f9ec23a..a5d6ae0 100644 --- a/test/test_transmissions.py +++ b/test/test_transmissions.py @@ -28,13 +28,6 @@ def test_translate_keys_with_recips(): {'key': 'value'}, {'address': {'email': 'foobar'}}] - results = t._translate_keys( - recipients=[{'address': {'name': 'foo', 'email': 'bar'}}] - ) - assert results['recipients'] == [ - {'address': {'name': 'foo', 'email': 'bar'}} - ] - def test_exceptions_for_recipients(): t = Transmissions('uri', 'key')