Skip to content

Commit

Permalink
fix: type validation on to_emails parameter on mail object (#920)
Browse files Browse the repository at this point in the history
  • Loading branch information
DougCal authored Jul 7, 2020
1 parent df75f6e commit c51b429
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
11 changes: 8 additions & 3 deletions sendgrid/helpers/mail/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(str), 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
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down
89 changes: 89 additions & 0 deletions test/test_mail_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,95 @@ def test_multiple_emails_to_multiple_recipients(self):
}''')
)

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 = [
['[email protected]', 'Example To Name 0'],
['[email protected]', 'Example To Name 1']
]

with self.assertRaises(ValueError):
Mail(
from_email=From('[email protected]', '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(
'<strong>and easy to do anywhere, even with Python</strong>'))

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 = [
('[email protected]', 'Example To Name 0'),
('[email protected]', 'Example To Name 1')
]

try:
Mail(
from_email=From('[email protected]', '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(
'<strong>and easy to do anywhere, even with Python</strong>'))
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 = ['[email protected]', '[email protected]']

try:
Mail(
from_email=From('[email protected]', '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(
'<strong>and easy to do anywhere, even with Python</strong>'))
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 = '[email protected]'

try:
Mail(
from_email=From('[email protected]', '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(
'<strong>and easy to do anywhere, even with Python</strong>'))
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 = ('[email protected]', 'Example To Name 0')

try:
Mail(
from_email=From('[email protected]', '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(
'<strong>and easy to do anywhere, even with Python</strong>'))
except:
self.fail('Mail() raised an error on a tuple of strings')

def test_dynamic_template_data(self):
self.maxDiff = None

Expand Down

0 comments on commit c51b429

Please sign in to comment.