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
20
from bzrlib import config
21
from bzrlib.email_message import EmailMessage
22
from bzrlib.errors import NoDestinationAddress
23
from bzrlib.tests import TestCase
24
from bzrlib.smtp_connection import SMTPConnection
27
class TestSMTPConnection(TestCase):
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)
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)
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)
45
def test_smtp_username(self):
46
conn = self.get_connection('')
47
self.assertIs(None, conn._smtp_username)
49
conn = self.get_connection('[DEFAULT]\nsmtp_username=joebody\n')
50
self.assertEqual(u'joebody', conn._smtp_username)
52
def test_smtp_password(self):
53
conn = self.get_connection('')
54
self.assertIs(None, conn._smtp_password)
56
conn = self.get_connection('[DEFAULT]\nsmtp_password=mypass\n')
57
self.assertEqual(u'mypass', conn._smtp_password)
59
def test_get_message_addresses(self):
62
from_, to = SMTPConnection.get_message_addresses(msg)
63
self.assertEqual('', from_)
64
self.assertEqual([], to)
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'
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))
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' ],
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))
87
def test_destination_address_required(self):
89
def get_user_option(self, option):
93
msg['From'] = '"J. Random Developer" <jrandom@example.com>'
94
self.assertRaises(NoDestinationAddress,
95
SMTPConnection(FakeConfig()).send_email, msg)
97
msg = EmailMessage('from@from.com', '', 'subject')
98
self.assertRaises(NoDestinationAddress,
99
SMTPConnection(FakeConfig()).send_email, msg)
101
msg = EmailMessage('from@from.com', [], 'subject')
102
self.assertRaises(NoDestinationAddress,
103
SMTPConnection(FakeConfig()).send_email, msg)