1
# Copyright (C) 2005, 2007 Canonical Ltd
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.
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.
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
17
from cStringIO import StringIO
18
from email.Message import Message
27
from bzrlib.email_message import EmailMessage
28
from bzrlib.errors import NoDestinationAddress
29
from bzrlib.tests import TestCase
30
from bzrlib.smtp_connection import SMTPConnection
33
def connection_refuser():
35
raise socket.error(errno.ECONNREFUSED, 'Connection Refused')
37
smtp.connect = connect
41
class TestSMTPConnection(TestCase):
43
def get_connection(self, text, smtp_factory=None):
44
my_config = config.GlobalConfig()
45
config_file = StringIO(text)
46
my_config._get_parser(config_file)
47
return SMTPConnection(my_config, _smtp_factory=smtp_factory)
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)
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)
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)
66
def test_smtp_username(self):
67
conn = self.get_connection('')
68
self.assertIs(None, conn._smtp_username)
70
conn = self.get_connection('[DEFAULT]\nsmtp_username=joebody\n')
71
self.assertEqual(u'joebody', conn._smtp_username)
73
def test_smtp_password(self):
74
conn = self.get_connection('')
75
self.assertIs(None, conn._smtp_password)
77
conn = self.get_connection('[DEFAULT]\nsmtp_password=mypass\n')
78
self.assertEqual(u'mypass', conn._smtp_password)
80
def test_get_message_addresses(self):
83
from_, to = SMTPConnection.get_message_addresses(msg)
84
self.assertEqual('', from_)
85
self.assertEqual([], to)
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'
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))
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' ],
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))
108
def test_destination_address_required(self):
110
def get_user_option(self, option):
114
msg['From'] = '"J. Random Developer" <jrandom@example.com>'
115
self.assertRaises(NoDestinationAddress,
116
SMTPConnection(FakeConfig()).send_email, msg)
118
msg = EmailMessage('from@from.com', '', 'subject')
119
self.assertRaises(NoDestinationAddress,
120
SMTPConnection(FakeConfig()).send_email, msg)
122
msg = EmailMessage('from@from.com', [], 'subject')
123
self.assertRaises(NoDestinationAddress,
124
SMTPConnection(FakeConfig()).send_email, msg)