Files
@ e4169e84981a
Branch filter:
Location: rattail-project/rattail/tests/test_mail.py
e4169e84981a
6.0 KiB
text/x-python
Add custom errors for when SA / Python for Windows Extensions not installed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | # -*- coding: utf-8 -*-
from __future__ import unicode_literals
from unittest import TestCase
from mock import patch, Mock, MagicMock
from fixture import TempIO
from edbob.configuration import AppConfigParser
from rattail import mail
from rattail.config import RattailConfig
class TestMail(TestCase):
@patch('rattail.mail.deliver_message')
@patch('rattail.mail.make_message')
def test_send_message(self, make_message, deliver_message):
config = AppConfigParser('rattail')
mail.send_message(config,
'sender@mailinator.com', ['recip1@mailinator.com', 'recip2@mailinator.com'],
'test subject', 'test body')
make_message.assert_called_once_with(
'sender@mailinator.com', ['recip1@mailinator.com', 'recip2@mailinator.com'],
'test subject', 'test body', content_type='text/plain')
deliver_message.assert_called_once_with(config, make_message.return_value)
def test_make_message(self):
message = mail.make_message('sender@mailinator.com', ['recip1@mailinator.com', 'recip2@mailinator.com'],
'test subject', 'test body', content_type='text/html')
self.assertEqual(message['From'], 'sender@mailinator.com')
self.assertEqual(message.get_all('To'), ['recip1@mailinator.com', 'recip2@mailinator.com'])
self.assertEqual(message['Subject'], 'test subject')
self.assertEqual(message.get_content_type(), 'text/html')
self.assertEqual(message.get_payload(), 'test body')
@patch('rattail.mail.smtplib')
class TestDeliverMessage(TestCase):
def setUp(self):
self.config = AppConfigParser('rattail')
self.config.set('rattail.mail', 'default.from', 'fred@mailinator.com')
self.config.set('rattail.mail', 'default.to', 'ledgar@sacfoodcoop.com')
self.message = mail.make_message_config(self.config, 'default', "Hello world")
def test_config_defaults(self, smtplib):
message = MagicMock()
session = Mock()
smtplib.SMTP.return_value = session
mail.deliver_message(self.config, message)
smtplib.SMTP.assert_called_once_with('localhost')
self.assertFalse(session.login.called)
self.assertEqual(session.sendmail.call_count, 1)
session.quit.assert_called_once_with()
def test_config_custom(self, smtplib):
self.config.set('rattail.mail', 'smtp.server', 'mail.mailinator.com')
self.config.set('rattail.mail', 'smtp.username', 'foo')
self.config.set('rattail.mail', 'smtp.password', 'bar')
message = MagicMock()
session = Mock()
smtplib.SMTP.return_value = session
mail.deliver_message(self.config, message)
smtplib.SMTP.assert_called_once_with('mail.mailinator.com')
session.login.assert_called_once_with('foo', 'bar')
self.assertEqual(session.sendmail.call_count, 1)
session.quit.assert_called_once_with()
@patch('rattail.mail.deprecation_warning')
def test_config_deprecated(self, deprecation_warning, smtplib):
self.config.set('edbob.mail', 'smtp.server', 'mail.mailinator.com')
self.config.set('edbob.mail', 'smtp.username', 'foo')
self.config.set('edbob.mail', 'smtp.password', 'bar')
message = MagicMock()
session = Mock()
smtplib.SMTP.return_value = session
mail.deliver_message(self.config, message)
smtplib.SMTP.assert_called_once_with('mail.mailinator.com')
session.login.assert_called_once_with('foo', 'bar')
self.assertEqual(session.sendmail.call_count, 1)
session.quit.assert_called_once_with()
self.assertEqual(deprecation_warning.call_count, 3)
def test_mail_is_sent_by_default(self, smtplib):
# TODO: Would be nice to set up a mock SMTP server to fully test.
mail.deliver_message(self.config, self.message)
self.assertTrue(smtplib.SMTP.called)
session = smtplib.SMTP.return_value
self.assertTrue(session.sendmail.called)
def test_no_mail_sent_if_config_disables_it(self, smtplib):
# TODO: Would be nice to set up a mock SMTP server to fully test.
self.config.set('rattail.mail', 'send_emails', 'false')
mail.deliver_message(self.config, self.message)
self.assertFalse(smtplib.SMTP.called)
class TestSendEmail(TestCase):
def setUp(self):
self.config = AppConfigParser('rattail')
self.tmp = TempIO()
self.templates_path = self.tmp.mkdir('templates')
self.templates_path.putfile('foo.html.mako', '')
self.config.set('rattail.mail', 'templates', self.templates_path)
self.config.set('rattail.mail', 'default.from', 'rattail@localhost')
self.config.set('rattail.mail', 'default.to', 'root@localhost')
@patch('rattail.mail.deliver_message')
def test_mail_is_sent(self, deliver_message):
mail.send_email(self.config, 'foo')
self.assertEqual(deliver_message.call_count, 1)
class TestMakeMessageConfig(TestCase):
def setUp(self):
self.config = RattailConfig()
self.config.set('rattail.mail', 'default.from', 'rattail@localhost')
self.config.set('rattail.mail', 'default.to', 'root@localhost')
def test_replyto_is_empty_by_default(self):
msg = mail.make_message_config(self.config, 'foo', 'body')
self.assertTrue('Reply-To' not in msg)
def test_default_replyto_may_be_configured(self):
self.config.set('rattail.mail', 'default.replyto', 'info@localhost')
msg = mail.make_message_config(self.config, 'foo', 'body')
self.assertTrue('Reply-To' in msg)
self.assertEqual(msg['Reply-To'], 'info@localhost')
def test_custom_replyto_may_be_configured(self):
self.config.set('rattail.mail', 'default.replyto', 'info@localhost')
self.config.set('rattail.mail', 'foo.replyto', 'foo@localhost')
msg = mail.make_message_config(self.config, 'foo', 'body')
self.assertTrue('Reply-To' in msg)
self.assertEqual(msg['Reply-To'], 'foo@localhost')
|