23
24
from bzrlib import (
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
34
def connection_refuser():
81
82
return 200, 'starttls success'
84
class TestSMTPConnection(TestCase):
85
class WideOpenSMTPFactory(StubSMTPFactory):
86
"""A fake smtp server that implements login by accepting anybody."""
88
def login(self, user, password):
92
class TestSMTPConnection(tests.TestCaseInTempDir):
86
94
def get_connection(self, text, smtp_factory=None):
87
95
my_config = config.GlobalConfig()
88
96
config_file = StringIO(text)
89
97
my_config._get_parser(config_file)
90
return SMTPConnection(my_config, _smtp_factory=smtp_factory)
98
return smtp_connection.SMTPConnection(my_config,
99
_smtp_factory=smtp_factory)
92
101
def test_defaults(self):
93
102
conn = self.get_connection('')
113
122
conn = self.get_connection('[DEFAULT]\nsmtp_username=joebody\n')
114
123
self.assertEqual(u'joebody', conn._smtp_username)
116
def test_smtp_password(self):
125
def test_smtp_password_from_config(self):
117
126
conn = self.get_connection('')
118
127
self.assertIs(None, conn._smtp_password)
120
129
conn = self.get_connection('[DEFAULT]\nsmtp_password=mypass\n')
121
130
self.assertEqual(u'mypass', conn._smtp_password)
132
def test_smtp_password_from_user(self):
135
factory = WideOpenSMTPFactory()
136
conn = self.get_connection('[DEFAULT]\nsmtp_username=%s\n' % user,
137
smtp_factory=factory)
138
self.assertIs(None, conn._smtp_password)
140
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
141
stdout=tests.StringIOWrapper())
143
self.assertEqual(password, conn._smtp_password)
144
# stdin should be empty (the provided password have been consumed)
145
self.assertEqual('', ui.ui_factory.stdin.readline())
147
def test_smtp_password_from_auth_config(self):
150
factory = WideOpenSMTPFactory()
151
conn = self.get_connection('[DEFAULT]\nsmtp_username=%s\n' % user,
152
smtp_factory=factory)
153
self.assertEqual(user, conn._smtp_username)
154
self.assertIs(None, conn._smtp_password)
155
# Create a config file with the right password
156
conf = config.AuthenticationConfig()
157
conf._get_config().update({'smtptest':
158
{'scheme': 'smtp', 'user':user,
159
'password': password}})
163
self.assertEqual(password, conn._smtp_password)
123
165
def test_create_connection(self):
124
166
factory = StubSMTPFactory()
125
167
conn = self.get_connection('', smtp_factory=factory)
174
216
def test_get_message_addresses(self):
177
from_, to = SMTPConnection.get_message_addresses(msg)
219
from_, to = smtp_connection.SMTPConnection.get_message_addresses(msg)
178
220
self.assertEqual('', from_)
179
221
self.assertEqual([], to)
183
225
msg['CC'] = u'Pepe P\xe9rez <pperez@ejemplo.com>'
184
226
msg['Bcc'] = 'user@localhost'
186
from_, to = SMTPConnection.get_message_addresses(msg)
228
from_, to = smtp_connection.SMTPConnection.get_message_addresses(msg)
187
229
self.assertEqual('jrandom@example.com', from_)
188
230
self.assertEqual(sorted(['john@doe.com', 'jane@doe.com',
189
231
'pperez@ejemplo.com', 'user@localhost']), sorted(to))
191
233
# now with bzrlib's EmailMessage
192
msg = EmailMessage('"J. Random Developer" <jrandom@example.com>', [
193
'John Doe <john@doe.com>', 'Jane Doe <jane@doe.com>',
194
u'Pepe P\xe9rez <pperez@ejemplo.com>', 'user@localhost' ],
234
msg = email_message.EmailMessage(
235
'"J. Random Developer" <jrandom@example.com>',
236
['John Doe <john@doe.com>', 'Jane Doe <jane@doe.com>',
237
u'Pepe P\xe9rez <pperez@ejemplo.com>', 'user@localhost' ],
197
from_, to = SMTPConnection.get_message_addresses(msg)
240
from_, to = smtp_connection.SMTPConnection.get_message_addresses(msg)
198
241
self.assertEqual('jrandom@example.com', from_)
199
242
self.assertEqual(sorted(['john@doe.com', 'jane@doe.com',
200
243
'pperez@ejemplo.com', 'user@localhost']), sorted(to))
208
251
msg['From'] = '"J. Random Developer" <jrandom@example.com>'
209
self.assertRaises(NoDestinationAddress,
210
SMTPConnection(FakeConfig()).send_email, msg)
212
msg = EmailMessage('from@from.com', '', 'subject')
213
self.assertRaises(NoDestinationAddress,
214
SMTPConnection(FakeConfig()).send_email, msg)
216
msg = EmailMessage('from@from.com', [], 'subject')
217
self.assertRaises(NoDestinationAddress,
218
SMTPConnection(FakeConfig()).send_email, msg)
253
errors.NoDestinationAddress,
254
smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
256
msg = email_message.EmailMessage('from@from.com', '', 'subject')
258
errors.NoDestinationAddress,
259
smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
261
msg = email_message.EmailMessage('from@from.com', [], 'subject')
263
errors.NoDestinationAddress,
264
smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)