diff --git a/examples/helpers/mail_example.py b/examples/helpers/mail_example.py index f6905787..f52719e3 100644 --- a/examples/helpers/mail_example.py +++ b/examples/helpers/mail_example.py @@ -316,7 +316,8 @@ def build_kitchen_sink(): mail_settings = MailSettings() mail_settings.bcc_settings = BccSettings(False, BccSettingsTo("bcc@twilio.com")) mail_settings.bypass_list_management = BypassListManagement(False) - mail_settings.footer_settings = FooterSettings(True, FooterText("w00t"), FooterHtml("w00t!")) + # Footer text and HTML also accept strings directly. + mail_settings.footer_settings = FooterSettings(True, "w00t", "w00t!") mail_settings.sandbox_mode = SandBoxMode(True) mail_settings.spam_check = SpamCheck(True, SpamThreshold(5), SpamUrl("https://example.com")) message.mail_settings = mail_settings diff --git a/sendgrid/helpers/mail/footer_settings.py b/sendgrid/helpers/mail/footer_settings.py index 1b0efeb1..cfbaadb0 100644 --- a/sendgrid/helpers/mail/footer_settings.py +++ b/sendgrid/helpers/mail/footer_settings.py @@ -1,3 +1,7 @@ +from .footer_html import FooterHtml +from .footer_text import FooterText + + class FooterSettings(object): """The default footer that you would like included on every email.""" @@ -7,9 +11,9 @@ def __init__(self, enable=None, text=None, html=None): :param enable: Whether this footer should be applied. :type enable: boolean, optional :param text: Text content of this footer - :type text: FooterText, optional + :type text: FooterText or string, optional :param html: HTML content of this footer - :type html: FooterHtml, optional + :type html: FooterHtml or string, optional """ self._enable = None self._text = None @@ -45,7 +49,7 @@ def enable(self, value): def text(self): """The plain text content of your footer. - :rtype: string + :rtype: FooterText """ return self._text @@ -54,15 +58,16 @@ def text(self, value): """The plain text content of your footer. :param value: The plain text content of your footer. - :type value: string + :type value: FooterText or string """ - self._text = value + self._text = (value if value is None or isinstance(value, FooterText) + else FooterText(value)) @property def html(self): """The HTML content of your footer. - :rtype: string + :rtype: FooterHtml """ return self._html @@ -71,9 +76,10 @@ def html(self, value): """The HTML content of your footer. :param value: The HTML content of your footer. - :type value: string + :type value: FooterHtml or string """ - self._html = value + self._html = (value if value is None or isinstance(value, FooterHtml) + else FooterHtml(value)) def get(self): """ diff --git a/test/unit/test_footer_settings.py b/test/unit/test_footer_settings.py new file mode 100644 index 00000000..1e6bfc1b --- /dev/null +++ b/test/unit/test_footer_settings.py @@ -0,0 +1,38 @@ +import unittest + +from sendgrid.helpers.mail import FooterHtml, FooterSettings, FooterText + + +class FooterSettingsTests(unittest.TestCase): + def test_string_constructor_arguments(self): + footer = FooterSettings(True, 'footer', '

footer

') + self.assertEqual(footer.get(), { + 'enable': True, 'text': 'footer', 'html': '

footer

'}) + + def test_string_property_assignment(self): + footer = FooterSettings() + footer.text = 'footer' + footer.html = '

footer

' + self.assertEqual(footer.get(), { + 'text': 'footer', 'html': '

footer

'}) + + def test_empty_strings_are_preserved(self): + footer = FooterSettings(False, '', '') + self.assertEqual(footer.get(), { + 'enable': False, 'text': '', 'html': ''}) + + def test_wrapped_values_are_preserved(self): + text = FooterText('footer') + html = FooterHtml('

footer

') + footer = FooterSettings(text=text, html=html) + self.assertIs(footer.text, text) + self.assertIs(footer.html, html) + self.assertEqual(footer.get(), { + 'text': 'footer', 'html': '

footer

'}) + + def test_none_clears_properties(self): + footer = FooterSettings(text=FooterText('footer'), + html=FooterHtml('

footer

')) + footer.text = None + footer.html = None + self.assertEqual(footer.get(), {})