/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
2625.6.1 by Adeodato Simó
New EmailMessage class, façade around email.Message and MIMEMultipart.
21
from bzrlib.email_message import EmailMessage
2535.2.1 by Adeodato Simó
New SMTPConnection class, a reduced version of that in bzr-email.
22
from bzrlib.errors import NoDestinationAddress
23
from bzrlib.tests import TestCase
24
from bzrlib.smtp_connection import SMTPConnection
25
26
27
class TestSMTPConnection(TestCase):
28
29
    def get_connection(self, text):
30
        my_config = config.GlobalConfig()
31
        config_file = StringIO(text)
32
        my_config._get_parser(config_file)
33
        return SMTPConnection(my_config)
34
35
    def test_defaults(self):
36
        conn = self.get_connection('')
37
        self.assertEqual('localhost', conn._smtp_server)
38
        self.assertEqual(None, conn._smtp_username)
39
        self.assertEqual(None, conn._smtp_password)
40
41
    def test_smtp_server(self):
42
        conn = self.get_connection('[DEFAULT]\nsmtp_server=host:10\n')
43
        self.assertEqual('host:10', conn._smtp_server)
44
45
    def test_smtp_username(self):
46
        conn = self.get_connection('')
47
        self.assertIs(None, conn._smtp_username)
48
49
        conn = self.get_connection('[DEFAULT]\nsmtp_username=joebody\n')
50
        self.assertEqual(u'joebody', conn._smtp_username)
51
52
    def test_smtp_password(self):
53
        conn = self.get_connection('')
54
        self.assertIs(None, conn._smtp_password)
55
56
        conn = self.get_connection('[DEFAULT]\nsmtp_password=mypass\n')
57
        self.assertEqual(u'mypass', conn._smtp_password)
58
59
    def test_get_message_addresses(self):
60
        msg = Message()
61
62
        from_, to = SMTPConnection.get_message_addresses(msg)
63
        self.assertEqual('', from_)
64
        self.assertEqual([], to)
65
66
        msg['From'] = '"J. Random Developer" <jrandom@example.com>'
67
        msg['To'] = 'John Doe <john@doe.com>, Jane Doe <jane@doe.com>'
68
        msg['CC'] = u'Pepe P\xe9rez <pperez@ejemplo.com>'
69
        msg['Bcc'] = 'user@localhost'
70
71
        from_, to = SMTPConnection.get_message_addresses(msg)
72
        self.assertEqual('jrandom@example.com', from_)
73
        self.assertEqual(sorted(['john@doe.com', 'jane@doe.com',
74
            'pperez@ejemplo.com', 'user@localhost']), sorted(to))
75
2625.6.1 by Adeodato Simó
New EmailMessage class, façade around email.Message and MIMEMultipart.
76
        # now with bzrlib's EmailMessage
77
        msg = EmailMessage('"J. Random Developer" <jrandom@example.com>', [
78
            'John Doe <john@doe.com>', 'Jane Doe <jane@doe.com>',
79
            u'Pepe P\xe9rez <pperez@ejemplo.com>', 'user@localhost' ],
80
            'subject')
81
82
        from_, to = SMTPConnection.get_message_addresses(msg)
83
        self.assertEqual('jrandom@example.com', from_)
84
        self.assertEqual(sorted(['john@doe.com', 'jane@doe.com',
85
            'pperez@ejemplo.com', 'user@localhost']), sorted(to))
86
2535.2.1 by Adeodato Simó
New SMTPConnection class, a reduced version of that in bzr-email.
87
    def test_destination_address_required(self):
88
        class FakeConfig:
89
            def get_user_option(self, option):
90
                return None
91
92
        msg = Message()
93
        msg['From'] = '"J. Random Developer" <jrandom@example.com>'
94
        self.assertRaises(NoDestinationAddress,
95
                SMTPConnection(FakeConfig()).send_email, msg)
2625.6.1 by Adeodato Simó
New EmailMessage class, façade around email.Message and MIMEMultipart.
96
97
        msg = EmailMessage('from@from.com', '', 'subject')
98
        self.assertRaises(NoDestinationAddress,
99
                SMTPConnection(FakeConfig()).send_email, msg)
100
101
        msg = EmailMessage('from@from.com', [], 'subject')
102
        self.assertRaises(NoDestinationAddress,
103
                SMTPConnection(FakeConfig()).send_email, msg)