~bzr-pqm/bzr/bzr.dev

2535.2.5 by Adeodato Simó
Fix copyright statement not to contain "by".
1
# Copyright (C) 2005, 2007 Canonical Ltd
2535.2.1 by Adeodato Simó
New SMTPConnection class, a reduced version of that in bzr-email.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
from cStringIO import StringIO
18
from email.Message import Message
19
20
from bzrlib import config
21
from bzrlib.errors import NoDestinationAddress
22
from bzrlib.tests import TestCase
23
from bzrlib.smtp_connection import SMTPConnection
24
25
26
class TestSMTPConnection(TestCase):
27
28
    def get_connection(self, text):
29
        my_config = config.GlobalConfig()
30
        config_file = StringIO(text)
31
        my_config._get_parser(config_file)
32
        return SMTPConnection(my_config)
33
34
    def test_defaults(self):
35
        conn = self.get_connection('')
36
        self.assertEqual('localhost', conn._smtp_server)
37
        self.assertEqual(None, conn._smtp_username)
38
        self.assertEqual(None, conn._smtp_password)
39
40
    def test_smtp_server(self):
41
        conn = self.get_connection('[DEFAULT]\nsmtp_server=host:10\n')
42
        self.assertEqual('host:10', conn._smtp_server)
43
44
    def test_smtp_username(self):
45
        conn = self.get_connection('')
46
        self.assertIs(None, conn._smtp_username)
47
48
        conn = self.get_connection('[DEFAULT]\nsmtp_username=joebody\n')
49
        self.assertEqual(u'joebody', conn._smtp_username)
50
51
    def test_smtp_password(self):
52
        conn = self.get_connection('')
53
        self.assertIs(None, conn._smtp_password)
54
55
        conn = self.get_connection('[DEFAULT]\nsmtp_password=mypass\n')
56
        self.assertEqual(u'mypass', conn._smtp_password)
57
58
    def test_get_message_addresses(self):
59
        msg = Message()
60
61
        from_, to = SMTPConnection.get_message_addresses(msg)
62
        self.assertEqual('', from_)
63
        self.assertEqual([], to)
64
65
        msg['From'] = '"J. Random Developer" <jrandom@example.com>'
66
        msg['To'] = 'John Doe <john@doe.com>, Jane Doe <jane@doe.com>'
67
        msg['CC'] = u'Pepe P\xe9rez <pperez@ejemplo.com>'
68
        msg['Bcc'] = 'user@localhost'
69
70
        from_, to = SMTPConnection.get_message_addresses(msg)
71
        self.assertEqual('jrandom@example.com', from_)
72
        self.assertEqual(sorted(['john@doe.com', 'jane@doe.com',
73
            'pperez@ejemplo.com', 'user@localhost']), sorted(to))
74
75
    def test_destination_address_required(self):
76
        class FakeConfig:
77
            def get_user_option(self, option):
78
                return None
79
80
        msg = Message()
81
        msg['From'] = '"J. Random Developer" <jrandom@example.com>'
82
        self.assertRaises(NoDestinationAddress,
83
                SMTPConnection(FakeConfig()).send_email, msg)