From 50651f14f62f17f39999405b70463d6143848711 Mon Sep 17 00:00:00 2001 From: John Calhoun Date: Sat, 4 Jul 2020 11:49:15 -0700 Subject: [PATCH 1/3] added type validation to to_emails parameter of mail object --- sendgrid/helpers/mail/mail.py | 11 ++++++++--- test/test_mail_helpers.py | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/sendgrid/helpers/mail/mail.py b/sendgrid/helpers/mail/mail.py index 5d9490ba3..f4d1ccd24 100644 --- a/sendgrid/helpers/mail/mail.py +++ b/sendgrid/helpers/mail/mail.py @@ -37,7 +37,8 @@ def __init__( :param subject: The subject of the email :type subject: Subject, optional :param to_emails: The email address of the recipient - :type to_emails: To, tuple, optional + :type to_emails: To, str, tuple, list(string), list(tuple), + list(To), optional :param plain_text_content: The plain text body of the email :type plain_text_content: string, optional :param html_content: The html body of the email @@ -239,7 +240,7 @@ def add_to( """Adds a To object to the Personalization object :param to_email: A To object - :type to_email: To, str, tuple + :type to_email: To, str, tuple, list(str), list(tuple), list(To) :param global_substitutions: A dict of substitutions for all recipients :type global_substitutions: dict :param is_multiple: Create a new personalization for each recipient @@ -253,8 +254,12 @@ def add_to( for email in to_email: if isinstance(email, str): email = To(email, None) - if isinstance(email, tuple): + elif isinstance(email, tuple): email = To(email[0], email[1]) + elif not isinstance(email, To): + raise ValueError( + 'Please use a tuple, To, or a str for a to_email list.' + ) self._set_emails(email, global_substitutions, is_multiple, p) else: if isinstance(to_email, str): diff --git a/test/test_mail_helpers.py b/test/test_mail_helpers.py index fd4f3a68f..2b63734ca 100644 --- a/test/test_mail_helpers.py +++ b/test/test_mail_helpers.py @@ -284,6 +284,25 @@ def test_multiple_emails_to_multiple_recipients(self): }''') ) + def test_value_error_is_thrown_on_to_emails_set_to_list_of_lists(self): + from sendgrid.helpers.mail import (Mail, From, Subject, + PlainTextContent, HtmlContent) + self.maxDiff = None + to_emails = [ + ['test+to0@example.com', 'Example To Name 0'], + ['test+to1@example.com', 'Example To Name 1'] + ] + + with self.assertRaises(ValueError): + Mail( + from_email=From('test+from@example.com', 'Example From Name'), + to_emails=to_emails, + subject=Subject('Sending with SendGrid is Fun'), + plain_text_content=PlainTextContent( + 'and easy to do anywhere, even with Python'), + html_content=HtmlContent( + 'and easy to do anywhere, even with Python')) + def test_dynamic_template_data(self): self.maxDiff = None From 9e8aec125e5fec599d99a837f2fcdc61d1f07cd2 Mon Sep 17 00:00:00 2001 From: John Calhoun Date: Sat, 4 Jul 2020 11:51:40 -0700 Subject: [PATCH 2/3] string -> str on to_emails docstring --- sendgrid/helpers/mail/mail.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sendgrid/helpers/mail/mail.py b/sendgrid/helpers/mail/mail.py index f4d1ccd24..ce8bb2f0c 100644 --- a/sendgrid/helpers/mail/mail.py +++ b/sendgrid/helpers/mail/mail.py @@ -37,7 +37,7 @@ def __init__( :param subject: The subject of the email :type subject: Subject, optional :param to_emails: The email address of the recipient - :type to_emails: To, str, tuple, list(string), list(tuple), + :type to_emails: To, str, tuple, list(str), list(tuple), list(To), optional :param plain_text_content: The plain text body of the email :type plain_text_content: string, optional From 0f597af7f7626ee01aa4523be1f1033891a3d52b Mon Sep 17 00:00:00 2001 From: John Calhoun Date: Tue, 7 Jul 2020 07:48:46 -0700 Subject: [PATCH 3/3] added tests to check errors are not raised on good to_emails types --- test/test_mail_helpers.py | 76 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 3 deletions(-) diff --git a/test/test_mail_helpers.py b/test/test_mail_helpers.py index 2b63734ca..dff3de5b2 100644 --- a/test/test_mail_helpers.py +++ b/test/test_mail_helpers.py @@ -284,9 +284,8 @@ def test_multiple_emails_to_multiple_recipients(self): }''') ) - def test_value_error_is_thrown_on_to_emails_set_to_list_of_lists(self): - from sendgrid.helpers.mail import (Mail, From, Subject, - PlainTextContent, HtmlContent) + def test_value_error_is_raised_on_to_emails_set_to_list_of_lists(self): + from sendgrid.helpers.mail import (PlainTextContent, HtmlContent) self.maxDiff = None to_emails = [ ['test+to0@example.com', 'Example To Name 0'], @@ -303,6 +302,77 @@ def test_value_error_is_thrown_on_to_emails_set_to_list_of_lists(self): html_content=HtmlContent( 'and easy to do anywhere, even with Python')) + def test_error_is_not_raised_on_to_emails_set_to_list_of_tuples(self): + from sendgrid.helpers.mail import (PlainTextContent, HtmlContent) + self.maxDiff = None + to_emails = [ + ('test+to0@example.com', 'Example To Name 0'), + ('test+to1@example.com', 'Example To Name 1') + ] + + try: + Mail( + from_email=From('test+from@example.com', 'Example From Name'), + to_emails=to_emails, + subject=Subject('Sending with SendGrid is Fun'), + plain_text_content=PlainTextContent( + 'and easy to do anywhere, even with Python'), + html_content=HtmlContent( + 'and easy to do anywhere, even with Python')) + except: + self.fail('Mail() raised an error on list of tuples') + + def test_error_is_not_raised_on_to_emails_set_to_list_of_strs(self): + from sendgrid.helpers.mail import (PlainTextContent, HtmlContent) + self.maxDiff = None + to_emails = ['test+to0@example.com', 'test+to1@example.com'] + + try: + Mail( + from_email=From('test+from@example.com', 'Example From Name'), + to_emails=to_emails, + subject=Subject('Sending with SendGrid is Fun'), + plain_text_content=PlainTextContent( + 'and easy to do anywhere, even with Python'), + html_content=HtmlContent( + 'and easy to do anywhere, even with Python')) + except: + self.fail('Mail() raised an error on list of strings') + + def test_error_is_not_raised_on_to_emails_set_to_a_str(self): + from sendgrid.helpers.mail import (PlainTextContent, HtmlContent) + self.maxDiff = None + to_emails = 'test+to0@example.com' + + try: + Mail( + from_email=From('test+from@example.com', 'Example From Name'), + to_emails=to_emails, + subject=Subject('Sending with SendGrid is Fun'), + plain_text_content=PlainTextContent( + 'and easy to do anywhere, even with Python'), + html_content=HtmlContent( + 'and easy to do anywhere, even with Python')) + except: + self.fail('Mail() raised an error on a string') + + def test_error_is_not_raised_on_to_emails_set_to_a_tuple(self): + from sendgrid.helpers.mail import (PlainTextContent, HtmlContent) + self.maxDiff = None + to_emails = ('test+to0@example.com', 'Example To Name 0') + + try: + Mail( + from_email=From('test+from@example.com', 'Example From Name'), + to_emails=to_emails, + subject=Subject('Sending with SendGrid is Fun'), + plain_text_content=PlainTextContent( + 'and easy to do anywhere, even with Python'), + html_content=HtmlContent( + 'and easy to do anywhere, even with Python')) + except: + self.fail('Mail() raised an error on a tuple of strings') + def test_dynamic_template_data(self): self.maxDiff = None