3
0
Fork 0

fix: add fallback_key support for email settings

sometimes (e.g. for "import/export warning") we need some common
template and/or config
This commit is contained in:
Lance Edgar 2025-12-19 21:16:43 -06:00
parent cca34bca1f
commit 2a9ace2a38
4 changed files with 250 additions and 47 deletions

View file

@ -5,6 +5,7 @@ from unittest import TestCase
from unittest.mock import patch, MagicMock
import pytest
from mako.template import Template
from wuttjamaican import email as mod
from wuttjamaican.util import resource_path
@ -20,6 +21,16 @@ class TestEmailSetting(ConfigTestCase):
self.assertIs(setting.app, self.app)
self.assertEqual(setting.key, "EmailSetting")
def test_get_description(self):
class MockSetting(mod.EmailSetting):
"""
this should be a good test
"""
setting = MockSetting(self.config)
self.assertEqual(setting.get_description(), "this should be a good test")
def test_sample_data(self):
setting = mod.EmailSetting(self.config)
self.assertEqual(setting.sample_data(), {})
@ -299,7 +310,9 @@ class TestEmailHandler(ConfigTestCase):
msg = handler.make_auto_message("foo", subject=None)
get_auto_subject.assert_not_called()
msg = handler.make_auto_message("foo")
get_auto_subject.assert_called_once_with("foo", {}, default=None)
get_auto_subject.assert_called_once_with(
"foo", {}, default=None, fallback_key=None
)
# to
with patch.object(handler, "get_auto_to") as get_auto_to:
@ -330,14 +343,18 @@ class TestEmailHandler(ConfigTestCase):
msg = handler.make_auto_message("foo", txt_body=None)
get_auto_txt_body.assert_not_called()
msg = handler.make_auto_message("foo")
get_auto_txt_body.assert_called_once_with("foo", {})
get_auto_txt_body.assert_called_once_with(
"foo", {"config": self.config, "app": self.app}, fallback_key=None
)
# html_body
with patch.object(handler, "get_auto_html_body") as get_auto_html_body:
msg = handler.make_auto_message("foo", html_body=None)
get_auto_html_body.assert_not_called()
msg = handler.make_auto_message("foo")
get_auto_html_body.assert_called_once_with("foo", {})
get_auto_html_body.assert_called_once_with(
"foo", {"config": self.config, "app": self.app}, fallback_key=None
)
def test_get_auto_sender(self):
handler = self.make_handler()
@ -384,6 +401,11 @@ class TestEmailHandler(ConfigTestCase):
template = handler.get_auto_subject_template("foo")
self.assertEqual(template, "Foo Message")
# can configure via fallback_key
self.config.setdefault("wutta.email.bar.subject", "Bar Message")
template = handler.get_auto_subject_template("baz", fallback_key="bar")
self.assertEqual(template, "Bar Message")
# EmailSetting can provide default subject
providers = {
"wuttatest": MagicMock(email_modules=["tests.test_email"]),
@ -446,26 +468,48 @@ class TestEmailHandler(ConfigTestCase):
self.assertEqual(recips, ["bob@example.com"])
def test_get_auto_body_template(self):
from mako.template import Template
handler = self.make_handler()
# error if bad request
self.assertRaises(ValueError, handler.get_auto_body_template, "foo", "BADTYPE")
# error if invalid mode (must be 'html' or 'txt')
self.assertRaises(ValueError, handler.get_auto_body_template, "foo", "BAD_MODE")
# empty by default
template = handler.get_auto_body_template("foo", "txt")
self.assertIsNone(template)
# no template by default
self.assertIsNone(handler.get_auto_body_template("foo", "html"))
self.assertIsNone(handler.get_auto_body_template("foo", "txt"))
# but returns a template if it exists
# mock template lookup
providers = {
"wuttatest": MagicMock(email_templates=["tests:email-templates"]),
}
with patch.object(self.app, "providers", new=providers):
handler = self.make_handler()
template = handler.get_auto_body_template("test_foo", "txt")
self.assertIsInstance(template, Template)
self.assertEqual(template.uri, "test_foo.txt.mako")
# template exists (txt)
template = handler.get_auto_body_template("test_foo", "txt")
self.assertIsInstance(template, Template)
self.assertEqual(template.uri, "test_foo.txt.mako")
# template exists (html)
template = handler.get_auto_body_template("test_foo", "html")
self.assertIsInstance(template, Template)
self.assertEqual(template.uri, "test_foo.html.mako")
# no such template
template = handler.get_auto_body_template("no_such_template", "html")
self.assertIsNone(template)
# but can use fallback
template = handler.get_auto_body_template(
"no_such_template", "html", fallback_key="test_foo"
)
self.assertIsInstance(template, Template)
self.assertEqual(template.uri, "test_foo.html.mako")
# what if fallback is also not found
template = handler.get_auto_body_template(
"no_such_template", "html", fallback_key="this_neither"
)
self.assertIsNone(template)
def test_get_auto_txt_body(self):
handler = self.make_handler()