13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
from cStringIO import StringIO
18
18
from email.Message import Message
33
def connection_refuser():
35
raise socket.error(errno.ECONNREFUSED, 'Connection Refused')
37
smtp.connect = connect
41
class StubSMTPFactory(object):
42
"""A fake SMTP connection to test the connection setup."""
43
def __init__(self, fail_on=None, smtp_features=None):
44
self._fail_on = fail_on or []
46
self._smtp_features = smtp_features or []
47
self._ehlo_called = False
50
# The factory pretends to be a connection
53
def connect(self, server):
54
self._calls.append(('connect', server))
57
self._calls.append(('helo',))
58
if 'helo' in self._fail_on:
59
return 500, 'helo failure'
61
return 200, 'helo success'
64
self._calls.append(('ehlo',))
65
if 'ehlo' in self._fail_on:
66
return 500, 'ehlo failure'
68
self._ehlo_called = True
69
return 200, 'ehlo success'
71
def has_extn(self, extension):
72
self._calls.append(('has_extn', extension))
73
return self._ehlo_called and extension in self._smtp_features
76
self._calls.append(('starttls',))
77
if 'starttls' in self._fail_on:
78
return 500, 'starttls failure'
80
self._ehlo_called = True
81
return 200, 'starttls success'
84
class WideOpenSMTPFactory(StubSMTPFactory):
85
"""A fake smtp server that implements login by accepting anybody."""
87
def login(self, user, password):
88
self._calls.append(('login', user, password))
91
class TestSMTPConnection(tests.TestCaseInTempDir):
93
def get_connection(self, text, smtp_factory=None):
94
my_config = config.GlobalConfig.from_string(text)
95
return smtp_connection.SMTPConnection(my_config,
96
_smtp_factory=smtp_factory)
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)
98
35
def test_defaults(self):
99
36
conn = self.get_connection('')
119
49
conn = self.get_connection('[DEFAULT]\nsmtp_username=joebody\n')
120
50
self.assertEqual(u'joebody', conn._smtp_username)
122
def test_smtp_password_from_config(self):
52
def test_smtp_password(self):
123
53
conn = self.get_connection('')
124
54
self.assertIs(None, conn._smtp_password)
126
56
conn = self.get_connection('[DEFAULT]\nsmtp_password=mypass\n')
127
57
self.assertEqual(u'mypass', conn._smtp_password)
129
def test_smtp_password_from_user(self):
132
factory = WideOpenSMTPFactory()
133
conn = self.get_connection('[DEFAULT]\nsmtp_username=%s\n' % user,
134
smtp_factory=factory)
135
self.assertIs(None, conn._smtp_password)
137
ui.ui_factory = ui.CannedInputUIFactory([password])
139
self.assertEqual(password, conn._smtp_password)
141
def test_smtp_password_from_auth_config(self):
144
factory = WideOpenSMTPFactory()
145
conn = self.get_connection('[DEFAULT]\nsmtp_username=%s\n' % user,
146
smtp_factory=factory)
147
self.assertEqual(user, conn._smtp_username)
148
self.assertIs(None, conn._smtp_password)
149
# Create a config file with the right password
150
conf = config.AuthenticationConfig()
151
conf._get_config().update({'smtptest':
152
{'scheme': 'smtp', 'user':user,
153
'password': password}})
157
self.assertEqual(password, conn._smtp_password)
159
def test_authenticate_with_byte_strings(self):
161
unicode_pass = u'h\xECspass'
162
utf8_pass = unicode_pass.encode('utf-8')
163
factory = WideOpenSMTPFactory()
164
conn = self.get_connection(
165
u'[DEFAULT]\nsmtp_username=%s\nsmtp_password=%s\n'
166
% (user, unicode_pass), smtp_factory=factory)
167
self.assertEqual(unicode_pass, conn._smtp_password)
169
self.assertEqual([('connect', 'localhost'),
171
('has_extn', 'starttls'),
172
('login', user, utf8_pass)], factory._calls)
173
smtp_username, smtp_password = factory._calls[-1][1:]
174
self.assertIsInstance(smtp_username, str)
175
self.assertIsInstance(smtp_password, str)
177
def test_create_connection(self):
178
factory = StubSMTPFactory()
179
conn = self.get_connection('', smtp_factory=factory)
180
conn._create_connection()
181
self.assertEqual([('connect', 'localhost'),
183
('has_extn', 'starttls')], factory._calls)
185
def test_create_connection_ehlo_fails(self):
186
# Check that we call HELO if EHLO failed.
187
factory = StubSMTPFactory(fail_on=['ehlo'])
188
conn = self.get_connection('', smtp_factory=factory)
189
conn._create_connection()
190
self.assertEqual([('connect', 'localhost'),
193
('has_extn', 'starttls')], factory._calls)
195
def test_create_connection_ehlo_helo_fails(self):
196
# Check that we raise an exception if both EHLO and HELO fail.
197
factory = StubSMTPFactory(fail_on=['ehlo', 'helo'])
198
conn = self.get_connection('', smtp_factory=factory)
199
self.assertRaises(errors.SMTPError, conn._create_connection)
200
self.assertEqual([('connect', 'localhost'),
202
('helo',)], factory._calls)
204
def test_create_connection_starttls(self):
205
# Check that STARTTLS plus a second EHLO are called if the
206
# server says it supports the feature.
207
factory = StubSMTPFactory(smtp_features=['starttls'])
208
conn = self.get_connection('', smtp_factory=factory)
209
conn._create_connection()
210
self.assertEqual([('connect', 'localhost'),
212
('has_extn', 'starttls'),
214
('ehlo',)], factory._calls)
216
def test_create_connection_starttls_fails(self):
217
# Check that we raise an exception if the server claims to
218
# support STARTTLS, but then fails when we try to activate it.
219
factory = StubSMTPFactory(fail_on=['starttls'],
220
smtp_features=['starttls'])
221
conn = self.get_connection('', smtp_factory=factory)
222
self.assertRaises(errors.SMTPError, conn._create_connection)
223
self.assertEqual([('connect', 'localhost'),
225
('has_extn', 'starttls'),
226
('starttls',)], factory._calls)
228
59
def test_get_message_addresses(self):
231
from_, to = smtp_connection.SMTPConnection.get_message_addresses(msg)
62
from_, to = SMTPConnection.get_message_addresses(msg)
232
63
self.assertEqual('', from_)
233
64
self.assertEqual([], to)
237
68
msg['CC'] = u'Pepe P\xe9rez <pperez@ejemplo.com>'
238
69
msg['Bcc'] = 'user@localhost'
240
from_, to = smtp_connection.SMTPConnection.get_message_addresses(msg)
71
from_, to = SMTPConnection.get_message_addresses(msg)
241
72
self.assertEqual('jrandom@example.com', from_)
242
73
self.assertEqual(sorted(['john@doe.com', 'jane@doe.com',
243
74
'pperez@ejemplo.com', 'user@localhost']), sorted(to))
245
76
# now with bzrlib's EmailMessage
246
msg = email_message.EmailMessage(
247
'"J. Random Developer" <jrandom@example.com>',
248
['John Doe <john@doe.com>', 'Jane Doe <jane@doe.com>',
249
u'Pepe P\xe9rez <pperez@ejemplo.com>', 'user@localhost' ],
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' ],
252
from_, to = smtp_connection.SMTPConnection.get_message_addresses(msg)
82
from_, to = SMTPConnection.get_message_addresses(msg)
253
83
self.assertEqual('jrandom@example.com', from_)
254
84
self.assertEqual(sorted(['john@doe.com', 'jane@doe.com',
255
85
'pperez@ejemplo.com', 'user@localhost']), sorted(to))
263
93
msg['From'] = '"J. Random Developer" <jrandom@example.com>'
265
errors.NoDestinationAddress,
266
smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
268
msg = email_message.EmailMessage('from@from.com', '', 'subject')
270
errors.NoDestinationAddress,
271
smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
273
msg = email_message.EmailMessage('from@from.com', [], 'subject')
275
errors.NoDestinationAddress,
276
smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
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)