/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
2694.2.1 by Aaron Bentley
Make error handling nicer when SMTP server not working
19
import errno
20
import smtplib
21
import socket
2535.2.1 by Adeodato Simó
New SMTPConnection class, a reduced version of that in bzr-email.
22
2694.2.1 by Aaron Bentley
Make error handling nicer when SMTP server not working
23
from bzrlib import (
24
    config,
25
    errors,
26
    )
2625.6.1 by Adeodato Simó
New EmailMessage class, façade around email.Message and MIMEMultipart.
27
from bzrlib.email_message import EmailMessage
2535.2.1 by Adeodato Simó
New SMTPConnection class, a reduced version of that in bzr-email.
28
from bzrlib.errors import NoDestinationAddress
29
from bzrlib.tests import TestCase
30
from bzrlib.smtp_connection import SMTPConnection
31
32
2694.2.1 by Aaron Bentley
Make error handling nicer when SMTP server not working
33
def connection_refuser():
34
    def connect(server):
35
        raise socket.error(errno.ECONNREFUSED, 'Connection Refused')
36
    smtp = smtplib.SMTP()
37
    smtp.connect = connect
38
    return smtp
39
40
2535.2.1 by Adeodato Simó
New SMTPConnection class, a reduced version of that in bzr-email.
41
class TestSMTPConnection(TestCase):
42
2694.2.1 by Aaron Bentley
Make error handling nicer when SMTP server not working
43
    def get_connection(self, text, smtp_factory=None):
2535.2.1 by Adeodato Simó
New SMTPConnection class, a reduced version of that in bzr-email.
44
        my_config = config.GlobalConfig()
45
        config_file = StringIO(text)
46
        my_config._get_parser(config_file)
2694.2.1 by Aaron Bentley
Make error handling nicer when SMTP server not working
47
        return SMTPConnection(my_config, _smtp_factory=smtp_factory)
2535.2.1 by Adeodato Simó
New SMTPConnection class, a reduced version of that in bzr-email.
48
49
    def test_defaults(self):
50
        conn = self.get_connection('')
51
        self.assertEqual('localhost', conn._smtp_server)
52
        self.assertEqual(None, conn._smtp_username)
53
        self.assertEqual(None, conn._smtp_password)
54
55
    def test_smtp_server(self):
56
        conn = self.get_connection('[DEFAULT]\nsmtp_server=host:10\n')
57
        self.assertEqual('host:10', conn._smtp_server)
58
2694.2.1 by Aaron Bentley
Make error handling nicer when SMTP server not working
59
    def test_missing_server(self):
60
        conn = self.get_connection('', smtp_factory=connection_refuser)
61
        self.assertRaises(errors.DefaultSMTPConnectionRefused, conn._connect)
62
        conn = self.get_connection('[DEFAULT]\nsmtp_server=smtp.example.com\n',
63
                                   smtp_factory=connection_refuser)
64
        self.assertRaises(errors.SMTPConnectionRefused, conn._connect)
65
2535.2.1 by Adeodato Simó
New SMTPConnection class, a reduced version of that in bzr-email.
66
    def test_smtp_username(self):
67
        conn = self.get_connection('')
68
        self.assertIs(None, conn._smtp_username)
69
70
        conn = self.get_connection('[DEFAULT]\nsmtp_username=joebody\n')
71
        self.assertEqual(u'joebody', conn._smtp_username)
72
73
    def test_smtp_password(self):
74
        conn = self.get_connection('')
75
        self.assertIs(None, conn._smtp_password)
76
77
        conn = self.get_connection('[DEFAULT]\nsmtp_password=mypass\n')
78
        self.assertEqual(u'mypass', conn._smtp_password)
79
80
    def test_get_message_addresses(self):
81
        msg = Message()
82
83
        from_, to = SMTPConnection.get_message_addresses(msg)
84
        self.assertEqual('', from_)
85
        self.assertEqual([], to)
86
87
        msg['From'] = '"J. Random Developer" <jrandom@example.com>'
88
        msg['To'] = 'John Doe <john@doe.com>, Jane Doe <jane@doe.com>'
89
        msg['CC'] = u'Pepe P\xe9rez <pperez@ejemplo.com>'
90
        msg['Bcc'] = 'user@localhost'
91
92
        from_, to = SMTPConnection.get_message_addresses(msg)
93
        self.assertEqual('jrandom@example.com', from_)
94
        self.assertEqual(sorted(['john@doe.com', 'jane@doe.com',
95
            'pperez@ejemplo.com', 'user@localhost']), sorted(to))
96
2625.6.1 by Adeodato Simó
New EmailMessage class, façade around email.Message and MIMEMultipart.
97
        # now with bzrlib's EmailMessage
98
        msg = EmailMessage('"J. Random Developer" <jrandom@example.com>', [
99
            'John Doe <john@doe.com>', 'Jane Doe <jane@doe.com>',
100
            u'Pepe P\xe9rez <pperez@ejemplo.com>', 'user@localhost' ],
101
            'subject')
102
103
        from_, to = SMTPConnection.get_message_addresses(msg)
104
        self.assertEqual('jrandom@example.com', from_)
105
        self.assertEqual(sorted(['john@doe.com', 'jane@doe.com',
106
            'pperez@ejemplo.com', 'user@localhost']), sorted(to))
107
2535.2.1 by Adeodato Simó
New SMTPConnection class, a reduced version of that in bzr-email.
108
    def test_destination_address_required(self):
109
        class FakeConfig:
110
            def get_user_option(self, option):
111
                return None
112
113
        msg = Message()
114
        msg['From'] = '"J. Random Developer" <jrandom@example.com>'
115
        self.assertRaises(NoDestinationAddress,
116
                SMTPConnection(FakeConfig()).send_email, msg)
2625.6.1 by Adeodato Simó
New EmailMessage class, façade around email.Message and MIMEMultipart.
117
118
        msg = EmailMessage('from@from.com', '', 'subject')
119
        self.assertRaises(NoDestinationAddress,
120
                SMTPConnection(FakeConfig()).send_email, msg)
121
122
        msg = EmailMessage('from@from.com', [], 'subject')
123
        self.assertRaises(NoDestinationAddress,
124
                SMTPConnection(FakeConfig()).send_email, msg)