40
class StubSMTPFactory(object):
41
"""A fake SMTP connection to test the connection setup."""
42
def __init__(self, fail_on=None, smtp_features=None):
43
self._fail_on = fail_on or []
45
self._smtp_features = smtp_features or []
46
self._ehlo_called = False
49
# The factory pretends to be a connection
52
def connect(self, server):
53
self._calls.append(('connect', server))
56
self._calls.append(('helo',))
57
if 'helo' in self._fail_on:
58
return 500, 'helo failure'
60
return 200, 'helo success'
63
self._calls.append(('ehlo',))
64
if 'ehlo' in self._fail_on:
65
return 500, 'ehlo failure'
67
self._ehlo_called = True
68
return 200, 'ehlo success'
70
def has_extn(self, extension):
71
self._calls.append(('has_extn', extension))
72
return self._ehlo_called and extension in self._smtp_features
75
self._calls.append(('starttls',))
76
if 'starttls' in self._fail_on:
77
return 500, 'starttls failure'
79
self._ehlo_called = True
80
return 200, 'starttls success'
83
class WideOpenSMTPFactory(StubSMTPFactory):
84
"""A fake smtp server that implements login by accepting anybody."""
86
def login(self, user, password):
87
self._calls.append(('login', user, password))
90
class TestSMTPConnection(tests.TestCaseInTempDir):
41
class TestSMTPConnection(TestCase):
92
43
def get_connection(self, text, smtp_factory=None):
93
my_config = config.GlobalConfig.from_string(text)
94
return smtp_connection.SMTPConnection(my_config,
95
_smtp_factory=smtp_factory)
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)
97
49
def test_defaults(self):
98
50
conn = self.get_connection('')
118
70
conn = self.get_connection('[DEFAULT]\nsmtp_username=joebody\n')
119
71
self.assertEqual(u'joebody', conn._smtp_username)
121
def test_smtp_password_from_config(self):
73
def test_smtp_password(self):
122
74
conn = self.get_connection('')
123
75
self.assertIs(None, conn._smtp_password)
125
77
conn = self.get_connection('[DEFAULT]\nsmtp_password=mypass\n')
126
78
self.assertEqual(u'mypass', conn._smtp_password)
128
def test_smtp_password_from_user(self):
131
factory = WideOpenSMTPFactory()
132
conn = self.get_connection('[DEFAULT]\nsmtp_username=%s\n' % user,
133
smtp_factory=factory)
134
self.assertIs(None, conn._smtp_password)
136
ui.ui_factory = ui.CannedInputUIFactory([password])
138
self.assertEqual(password, conn._smtp_password)
140
def test_smtp_password_from_auth_config(self):
143
factory = WideOpenSMTPFactory()
144
conn = self.get_connection('[DEFAULT]\nsmtp_username=%s\n' % user,
145
smtp_factory=factory)
146
self.assertEqual(user, conn._smtp_username)
147
self.assertIs(None, conn._smtp_password)
148
# Create a config file with the right password
149
conf = config.AuthenticationConfig()
150
conf._get_config().update({'smtptest':
151
{'scheme': 'smtp', 'user':user,
152
'password': password}})
156
self.assertEqual(password, conn._smtp_password)
158
def test_authenticate_with_byte_strings(self):
160
unicode_pass = u'h\xECspass'
161
utf8_pass = unicode_pass.encode('utf-8')
162
factory = WideOpenSMTPFactory()
163
conn = self.get_connection(
164
u'[DEFAULT]\nsmtp_username=%s\nsmtp_password=%s\n'
165
% (user, unicode_pass), smtp_factory=factory)
166
self.assertEqual(unicode_pass, conn._smtp_password)
168
self.assertEqual([('connect', 'localhost'),
170
('has_extn', 'starttls'),
171
('login', user, utf8_pass)], factory._calls)
172
smtp_username, smtp_password = factory._calls[-1][1:]
173
self.assertIsInstance(smtp_username, str)
174
self.assertIsInstance(smtp_password, str)
176
def test_create_connection(self):
177
factory = StubSMTPFactory()
178
conn = self.get_connection('', smtp_factory=factory)
179
conn._create_connection()
180
self.assertEqual([('connect', 'localhost'),
182
('has_extn', 'starttls')], factory._calls)
184
def test_create_connection_ehlo_fails(self):
185
# Check that we call HELO if EHLO failed.
186
factory = StubSMTPFactory(fail_on=['ehlo'])
187
conn = self.get_connection('', smtp_factory=factory)
188
conn._create_connection()
189
self.assertEqual([('connect', 'localhost'),
192
('has_extn', 'starttls')], factory._calls)
194
def test_create_connection_ehlo_helo_fails(self):
195
# Check that we raise an exception if both EHLO and HELO fail.
196
factory = StubSMTPFactory(fail_on=['ehlo', 'helo'])
197
conn = self.get_connection('', smtp_factory=factory)
198
self.assertRaises(errors.SMTPError, conn._create_connection)
199
self.assertEqual([('connect', 'localhost'),
201
('helo',)], factory._calls)
203
def test_create_connection_starttls(self):
204
# Check that STARTTLS plus a second EHLO are called if the
205
# server says it supports the feature.
206
factory = StubSMTPFactory(smtp_features=['starttls'])
207
conn = self.get_connection('', smtp_factory=factory)
208
conn._create_connection()
209
self.assertEqual([('connect', 'localhost'),
211
('has_extn', 'starttls'),
213
('ehlo',)], factory._calls)
215
def test_create_connection_starttls_fails(self):
216
# Check that we raise an exception if the server claims to
217
# support STARTTLS, but then fails when we try to activate it.
218
factory = StubSMTPFactory(fail_on=['starttls'],
219
smtp_features=['starttls'])
220
conn = self.get_connection('', smtp_factory=factory)
221
self.assertRaises(errors.SMTPError, conn._create_connection)
222
self.assertEqual([('connect', 'localhost'),
224
('has_extn', 'starttls'),
225
('starttls',)], factory._calls)
227
80
def test_get_message_addresses(self):
230
from_, to = smtp_connection.SMTPConnection.get_message_addresses(msg)
83
from_, to = SMTPConnection.get_message_addresses(msg)
231
84
self.assertEqual('', from_)
232
85
self.assertEqual([], to)
236
89
msg['CC'] = u'Pepe P\xe9rez <pperez@ejemplo.com>'
237
90
msg['Bcc'] = 'user@localhost'
239
from_, to = smtp_connection.SMTPConnection.get_message_addresses(msg)
92
from_, to = SMTPConnection.get_message_addresses(msg)
240
93
self.assertEqual('jrandom@example.com', from_)
241
94
self.assertEqual(sorted(['john@doe.com', 'jane@doe.com',
242
95
'pperez@ejemplo.com', 'user@localhost']), sorted(to))
244
97
# now with bzrlib's EmailMessage
245
msg = email_message.EmailMessage(
246
'"J. Random Developer" <jrandom@example.com>',
247
['John Doe <john@doe.com>', 'Jane Doe <jane@doe.com>',
248
u'Pepe P\xe9rez <pperez@ejemplo.com>', 'user@localhost' ],
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' ],
251
from_, to = smtp_connection.SMTPConnection.get_message_addresses(msg)
103
from_, to = SMTPConnection.get_message_addresses(msg)
252
104
self.assertEqual('jrandom@example.com', from_)
253
105
self.assertEqual(sorted(['john@doe.com', 'jane@doe.com',
254
106
'pperez@ejemplo.com', 'user@localhost']), sorted(to))
262
114
msg['From'] = '"J. Random Developer" <jrandom@example.com>'
264
errors.NoDestinationAddress,
265
smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
267
msg = email_message.EmailMessage('from@from.com', '', 'subject')
269
errors.NoDestinationAddress,
270
smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
272
msg = email_message.EmailMessage('from@from.com', [], 'subject')
274
errors.NoDestinationAddress,
275
smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
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)