Files @ 7777a6f70fd5
Branch filter:

Location: rattail-project/rattail/tests/test_mail.py - annotation

Lance Edgar
Remove deprecated `RattailConfig.getboolean()` method.

All calling code should be refactored, I believe..
e1f29a00738a
e1f29a00738a
e1f29a00738a
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e1f29a00738a
e509d25751e4
e509d25751e4
85ab706736d2
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
85ab706736d2
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
60ccfbb3ef9e
60ccfbb3ef9e
85ab706736d2
60ccfbb3ef9e
60ccfbb3ef9e
60ccfbb3ef9e
60ccfbb3ef9e
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
60ccfbb3ef9e
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
60ccfbb3ef9e
60ccfbb3ef9e
60ccfbb3ef9e
e509d25751e4
e509d25751e4
e509d25751e4
60ccfbb3ef9e
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
e509d25751e4
60ccfbb3ef9e
60ccfbb3ef9e
60ccfbb3ef9e
60ccfbb3ef9e
60ccfbb3ef9e
60ccfbb3ef9e
60ccfbb3ef9e
60ccfbb3ef9e
60ccfbb3ef9e
60ccfbb3ef9e
60ccfbb3ef9e
60ccfbb3ef9e
60ccfbb3ef9e
e1f29a00738a
e1f29a00738a
e1f29a00738a
e1f29a00738a
85ab706736d2
e1f29a00738a
e1f29a00738a
e1f29a00738a
e1f29a00738a
e1f29a00738a
e1f29a00738a
e1f29a00738a
e1f29a00738a
e1f29a00738a
e1f29a00738a
e1f29a00738a
ac3edc4b34b0
ac3edc4b34b0
ac3edc4b34b0
ac3edc4b34b0
ac3edc4b34b0
85ab706736d2
ac3edc4b34b0
ac3edc4b34b0
ac3edc4b34b0
ac3edc4b34b0
ac3edc4b34b0
ac3edc4b34b0
ac3edc4b34b0
ac3edc4b34b0
ac3edc4b34b0
ac3edc4b34b0
ac3edc4b34b0
ac3edc4b34b0
ac3edc4b34b0
ac3edc4b34b0
ac3edc4b34b0
ac3edc4b34b0
ac3edc4b34b0
ac3edc4b34b0
ac3edc4b34b0
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

from unittest import TestCase

from mock import patch, Mock, MagicMock
from fixture import TempIO

from rattail import mail
from rattail.config import make_config


class TestMail(TestCase):

    @patch('rattail.mail.deliver_message')
    @patch('rattail.mail.make_message')
    def test_send_message(self, make_message, deliver_message):
        config = make_config([])
        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 = make_config([])
        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()

    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 = make_config([])
        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 = make_config([])
        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')