3
0
Fork 0

fix: move email stuff from subpackage to module

This commit is contained in:
Lance Edgar 2024-12-19 18:34:03 -06:00
parent 902412322e
commit 6c8f1c973d
14 changed files with 244 additions and 331 deletions

View file

@ -1,76 +0,0 @@
# -*- coding: utf-8; -*-
from unittest import TestCase
from wuttjamaican.email import message as mod
class TestMessage(TestCase):
def make_message(self, **kwargs):
return mod.Message(**kwargs)
def test_set_recips(self):
msg = self.make_message()
self.assertEqual(msg.to, [])
# set as list
msg.set_recips('to', ['sally@example.com'])
self.assertEqual(msg.to, ['sally@example.com'])
# set as tuple
msg.set_recips('to', ('barney@example.com',))
self.assertEqual(msg.to, ['barney@example.com'])
# set as string
msg.set_recips('to', 'wilma@example.com')
self.assertEqual(msg.to, ['wilma@example.com'])
# set as null
msg.set_recips('to', None)
self.assertEqual(msg.to, [])
# otherwise error
self.assertRaises(ValueError, msg.set_recips, 'to', {'foo': 'foo@example.com'})
def test_as_string(self):
# error if no body
msg = self.make_message()
self.assertRaises(ValueError, msg.as_string)
# txt body
msg = self.make_message(sender='bob@example.com',
txt_body="hello world")
complete = msg.as_string()
self.assertIn('From: bob@example.com', complete)
# html body
msg = self.make_message(sender='bob@example.com',
html_body="<p>hello world</p>")
complete = msg.as_string()
self.assertIn('From: bob@example.com', complete)
# txt + html body
msg = self.make_message(sender='bob@example.com',
txt_body="hello world",
html_body="<p>hello world</p>")
complete = msg.as_string()
self.assertIn('From: bob@example.com', complete)
# everything
msg = self.make_message(sender='bob@example.com',
subject='meeting follow-up',
to='sally@example.com',
cc='marketing@example.com',
bcc='bob@example.com',
replyto='sales@example.com',
txt_body="hello world",
html_body="<p>hello world</p>")
complete = msg.as_string()
self.assertIn('From: bob@example.com', complete)
self.assertIn('Subject: meeting follow-up', complete)
self.assertIn('To: sally@example.com', complete)
self.assertIn('Cc: marketing@example.com', complete)
self.assertIn('Bcc: bob@example.com', complete)
self.assertIn('Reply-To: sales@example.com', complete)

View file

@ -5,8 +5,7 @@ from unittest.mock import patch, MagicMock
import pytest
from wuttjamaican.email import handler as mod
from wuttjamaican.email import Message
from wuttjamaican import email as mod
from wuttjamaican.conf import WuttaConfig
from wuttjamaican.util import resource_path
from wuttjamaican.exc import ConfigurationError
@ -36,28 +35,28 @@ class TestEmailHandler(TestCase):
# provider may specify paths as list
providers = {
'wuttatest': MagicMock(email_templates=['wuttjamaican.email:templates']),
'wuttatest': MagicMock(email_templates=['wuttjamaican:email-templates']),
}
with patch.object(self.app, 'providers', new=providers):
handler = self.make_handler()
path = resource_path('wuttjamaican.email:templates')
path = resource_path('wuttjamaican:email-templates')
self.assertEqual(handler.txt_templates.directories, [path])
self.assertEqual(handler.html_templates.directories, [path])
# provider may specify paths as string
providers = {
'wuttatest': MagicMock(email_templates='wuttjamaican.email:templates'),
'wuttatest': MagicMock(email_templates='wuttjamaican:email-templates'),
}
with patch.object(self.app, 'providers', new=providers):
handler = self.make_handler()
path = resource_path('wuttjamaican.email:templates')
path = resource_path('wuttjamaican:email-templates')
self.assertEqual(handler.txt_templates.directories, [path])
self.assertEqual(handler.html_templates.directories, [path])
def test_make_message(self):
handler = self.make_handler()
msg = handler.make_message()
self.assertIsInstance(msg, Message)
self.assertIsInstance(msg, mod.Message)
def test_make_auto_message(self):
handler = self.make_handler()
@ -70,7 +69,7 @@ class TestEmailHandler(TestCase):
# message is empty by default
msg = handler.make_auto_message('foo')
self.assertIsInstance(msg, Message)
self.assertIsInstance(msg, mod.Message)
self.assertEqual(msg.key, 'foo')
self.assertEqual(msg.sender, 'bob@example.com')
self.assertEqual(msg.subject, "Automated message")
@ -85,7 +84,7 @@ class TestEmailHandler(TestCase):
# then we should get back a more complete message
self.config.setdefault('wutta.email.test_foo.subject', "hello foo")
self.config.setdefault('wutta.email.test_foo.to', 'sally@example.com')
self.config.setdefault('wutta.email.templates', 'tests.email:templates')
self.config.setdefault('wutta.email.templates', 'tests:email-templates')
handler = self.make_handler()
msg = handler.make_auto_message('test_foo')
self.assertEqual(msg.key, 'test_foo')
@ -240,7 +239,7 @@ class TestEmailHandler(TestCase):
# but returns a template if it exists
providers = {
'wuttatest': MagicMock(email_templates=['tests.email:templates']),
'wuttatest': MagicMock(email_templates=['tests:email-templates']),
}
with patch.object(self.app, 'providers', new=providers):
handler = self.make_handler()
@ -257,7 +256,7 @@ class TestEmailHandler(TestCase):
# but returns body if template exists
providers = {
'wuttatest': MagicMock(email_templates=['tests.email:templates']),
'wuttatest': MagicMock(email_templates=['tests:email-templates']),
}
with patch.object(self.app, 'providers', new=providers):
handler = self.make_handler()
@ -273,7 +272,7 @@ class TestEmailHandler(TestCase):
# but returns body if template exists
providers = {
'wuttatest': MagicMock(email_templates=['tests.email:templates']),
'wuttatest': MagicMock(email_templates=['tests:email-templates']),
}
with patch.object(self.app, 'providers', new=providers):
handler = self.make_handler()
@ -456,3 +455,74 @@ class TestEmailHandler(TestCase):
self.config.setdefault('wutta.email.default.enabled', False)
handler.send_email('bar', sender='bar@example.com', txt_body="hello world")
self.assertFalse(deliver_message.called)
class TestMessage(TestCase):
def make_message(self, **kwargs):
return mod.Message(**kwargs)
def test_set_recips(self):
msg = self.make_message()
self.assertEqual(msg.to, [])
# set as list
msg.set_recips('to', ['sally@example.com'])
self.assertEqual(msg.to, ['sally@example.com'])
# set as tuple
msg.set_recips('to', ('barney@example.com',))
self.assertEqual(msg.to, ['barney@example.com'])
# set as string
msg.set_recips('to', 'wilma@example.com')
self.assertEqual(msg.to, ['wilma@example.com'])
# set as null
msg.set_recips('to', None)
self.assertEqual(msg.to, [])
# otherwise error
self.assertRaises(ValueError, msg.set_recips, 'to', {'foo': 'foo@example.com'})
def test_as_string(self):
# error if no body
msg = self.make_message()
self.assertRaises(ValueError, msg.as_string)
# txt body
msg = self.make_message(sender='bob@example.com',
txt_body="hello world")
complete = msg.as_string()
self.assertIn('From: bob@example.com', complete)
# html body
msg = self.make_message(sender='bob@example.com',
html_body="<p>hello world</p>")
complete = msg.as_string()
self.assertIn('From: bob@example.com', complete)
# txt + html body
msg = self.make_message(sender='bob@example.com',
txt_body="hello world",
html_body="<p>hello world</p>")
complete = msg.as_string()
self.assertIn('From: bob@example.com', complete)
# everything
msg = self.make_message(sender='bob@example.com',
subject='meeting follow-up',
to='sally@example.com',
cc='marketing@example.com',
bcc='bob@example.com',
replyto='sales@example.com',
txt_body="hello world",
html_body="<p>hello world</p>")
complete = msg.as_string()
self.assertIn('From: bob@example.com', complete)
self.assertIn('Subject: meeting follow-up', complete)
self.assertIn('To: sally@example.com', complete)
self.assertIn('Cc: marketing@example.com', complete)
self.assertIn('Bcc: bob@example.com', complete)
self.assertIn('Reply-To: sales@example.com', complete)