1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
# Copyright (C) 2005, 2007 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
from cStringIO import StringIO
from email.Message import Message
import errno
import smtplib
import socket
import sys
from bzrlib import (
config,
email_message,
errors,
smtp_connection,
tests,
ui,
)
def connection_refuser():
def connect(server):
raise socket.error(errno.ECONNREFUSED, 'Connection Refused')
smtp = smtplib.SMTP()
smtp.connect = connect
return smtp
class StubSMTPFactory(object):
"""A fake SMTP connection to test the connection setup."""
def __init__(self, fail_on=None, smtp_features=None):
self._fail_on = fail_on or []
self._calls = []
self._smtp_features = smtp_features or []
self._ehlo_called = False
def __call__(self):
# The factory pretends to be a connection
return self
def connect(self, server):
self._calls.append(('connect', server))
def helo(self):
self._calls.append(('helo',))
if 'helo' in self._fail_on:
return 500, 'helo failure'
else:
return 200, 'helo success'
def ehlo(self):
self._calls.append(('ehlo',))
if 'ehlo' in self._fail_on:
return 500, 'ehlo failure'
else:
self._ehlo_called = True
return 200, 'ehlo success'
def has_extn(self, extension):
self._calls.append(('has_extn', extension))
return self._ehlo_called and extension in self._smtp_features
def starttls(self):
self._calls.append(('starttls',))
if 'starttls' in self._fail_on:
return 500, 'starttls failure'
else:
self._ehlo_called = True
return 200, 'starttls success'
class WideOpenSMTPFactory(StubSMTPFactory):
"""A fake smtp server that implements login by accepting anybody."""
def login(self, user, password):
pass
class TestSMTPConnection(tests.TestCaseInTempDir):
def get_connection(self, text, smtp_factory=None):
my_config = config.GlobalConfig()
config_file = StringIO(text)
my_config._get_parser(config_file)
return smtp_connection.SMTPConnection(my_config,
_smtp_factory=smtp_factory)
def test_defaults(self):
conn = self.get_connection('')
self.assertEqual('localhost', conn._smtp_server)
self.assertEqual(None, conn._smtp_username)
self.assertEqual(None, conn._smtp_password)
def test_smtp_server(self):
conn = self.get_connection('[DEFAULT]\nsmtp_server=host:10\n')
self.assertEqual('host:10', conn._smtp_server)
def test_missing_server(self):
conn = self.get_connection('', smtp_factory=connection_refuser)
self.assertRaises(errors.DefaultSMTPConnectionRefused, conn._connect)
conn = self.get_connection('[DEFAULT]\nsmtp_server=smtp.example.com\n',
smtp_factory=connection_refuser)
self.assertRaises(errors.SMTPConnectionRefused, conn._connect)
def test_smtp_username(self):
conn = self.get_connection('')
self.assertIs(None, conn._smtp_username)
conn = self.get_connection('[DEFAULT]\nsmtp_username=joebody\n')
self.assertEqual(u'joebody', conn._smtp_username)
def test_smtp_password_from_config(self):
conn = self.get_connection('')
self.assertIs(None, conn._smtp_password)
conn = self.get_connection('[DEFAULT]\nsmtp_password=mypass\n')
self.assertEqual(u'mypass', conn._smtp_password)
def test_smtp_password_from_user(self):
user = 'joe'
password = 'hispass'
factory = WideOpenSMTPFactory()
conn = self.get_connection('[DEFAULT]\nsmtp_username=%s\n' % user,
smtp_factory=factory)
self.assertIs(None, conn._smtp_password)
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
stdout=tests.StringIOWrapper())
conn._connect()
self.assertEqual(password, conn._smtp_password)
# stdin should be empty (the provided password have been consumed)
self.assertEqual('', ui.ui_factory.stdin.readline())
def test_smtp_password_from_auth_config(self):
user = 'joe'
password = 'hispass'
factory = WideOpenSMTPFactory()
conn = self.get_connection('[DEFAULT]\nsmtp_username=%s\n' % user,
smtp_factory=factory)
self.assertEqual(user, conn._smtp_username)
self.assertIs(None, conn._smtp_password)
# Create a config file with the right password
conf = config.AuthenticationConfig()
conf._get_config().update({'smtptest':
{'scheme': 'smtp', 'user':user,
'password': password}})
conf._save()
conn._connect()
self.assertEqual(password, conn._smtp_password)
def test_create_connection(self):
factory = StubSMTPFactory()
conn = self.get_connection('', smtp_factory=factory)
conn._create_connection()
self.assertEqual([('connect', 'localhost'),
('ehlo',),
('has_extn', 'starttls')], factory._calls)
def test_create_connection_ehlo_fails(self):
# Check that we call HELO if EHLO failed.
factory = StubSMTPFactory(fail_on=['ehlo'])
conn = self.get_connection('', smtp_factory=factory)
conn._create_connection()
self.assertEqual([('connect', 'localhost'),
('ehlo',),
('helo',),
('has_extn', 'starttls')], factory._calls)
def test_create_connection_ehlo_helo_fails(self):
# Check that we raise an exception if both EHLO and HELO fail.
factory = StubSMTPFactory(fail_on=['ehlo', 'helo'])
conn = self.get_connection('', smtp_factory=factory)
self.assertRaises(errors.SMTPError, conn._create_connection)
self.assertEqual([('connect', 'localhost'),
('ehlo',),
('helo',)], factory._calls)
def test_create_connection_starttls(self):
# Check that STARTTLS plus a second EHLO are called if the
# server says it supports the feature.
factory = StubSMTPFactory(smtp_features=['starttls'])
conn = self.get_connection('', smtp_factory=factory)
conn._create_connection()
self.assertEqual([('connect', 'localhost'),
('ehlo',),
('has_extn', 'starttls'),
('starttls',),
('ehlo',)], factory._calls)
def test_create_connection_starttls_fails(self):
# Check that we raise an exception if the server claims to
# support STARTTLS, but then fails when we try to activate it.
factory = StubSMTPFactory(fail_on=['starttls'],
smtp_features=['starttls'])
conn = self.get_connection('', smtp_factory=factory)
self.assertRaises(errors.SMTPError, conn._create_connection)
self.assertEqual([('connect', 'localhost'),
('ehlo',),
('has_extn', 'starttls'),
('starttls',)], factory._calls)
def test_get_message_addresses(self):
msg = Message()
from_, to = smtp_connection.SMTPConnection.get_message_addresses(msg)
self.assertEqual('', from_)
self.assertEqual([], to)
msg['From'] = '"J. Random Developer" <jrandom@example.com>'
msg['To'] = 'John Doe <john@doe.com>, Jane Doe <jane@doe.com>'
msg['CC'] = u'Pepe P\xe9rez <pperez@ejemplo.com>'
msg['Bcc'] = 'user@localhost'
from_, to = smtp_connection.SMTPConnection.get_message_addresses(msg)
self.assertEqual('jrandom@example.com', from_)
self.assertEqual(sorted(['john@doe.com', 'jane@doe.com',
'pperez@ejemplo.com', 'user@localhost']), sorted(to))
# now with bzrlib's EmailMessage
msg = email_message.EmailMessage(
'"J. Random Developer" <jrandom@example.com>',
['John Doe <john@doe.com>', 'Jane Doe <jane@doe.com>',
u'Pepe P\xe9rez <pperez@ejemplo.com>', 'user@localhost' ],
'subject')
from_, to = smtp_connection.SMTPConnection.get_message_addresses(msg)
self.assertEqual('jrandom@example.com', from_)
self.assertEqual(sorted(['john@doe.com', 'jane@doe.com',
'pperez@ejemplo.com', 'user@localhost']), sorted(to))
def test_destination_address_required(self):
class FakeConfig:
def get_user_option(self, option):
return None
msg = Message()
msg['From'] = '"J. Random Developer" <jrandom@example.com>'
self.assertRaises(
errors.NoDestinationAddress,
smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
msg = email_message.EmailMessage('from@from.com', '', 'subject')
self.assertRaises(
errors.NoDestinationAddress,
smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
msg = email_message.EmailMessage('from@from.com', [], 'subject')
self.assertRaises(
errors.NoDestinationAddress,
smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
|