/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_smtp_connection.py

Merge test-run support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
 
42
42
class StubSMTPFactory(object):
43
43
    """A fake SMTP connection to test the connection setup."""
44
 
 
45
44
    def __init__(self, fail_on=None, smtp_features=None):
46
45
        self._fail_on = fail_on or []
47
46
        self._calls = []
98
97
            my_config, _smtp_factory=smtp_factory)
99
98
 
100
99
    def test_defaults(self):
101
 
        conn = self.get_connection(b'')
 
100
        conn = self.get_connection('')
102
101
        self.assertEqual('localhost', conn._smtp_server)
103
102
        self.assertEqual(None, conn._smtp_username)
104
103
        self.assertEqual(None, conn._smtp_password)
105
104
 
106
105
    def test_smtp_server(self):
107
 
        conn = self.get_connection(b'smtp_server=host:10')
 
106
        conn = self.get_connection('smtp_server=host:10')
108
107
        self.assertEqual('host:10', conn._smtp_server)
109
108
 
110
109
    def test_missing_server(self):
111
 
        conn = self.get_connection(b'', smtp_factory=connection_refuser)
 
110
        conn = self.get_connection('', smtp_factory=connection_refuser)
112
111
        self.assertRaises(smtp_connection.DefaultSMTPConnectionRefused,
113
112
                          conn._connect)
114
 
        conn = self.get_connection(b'smtp_server=smtp.example.com',
 
113
        conn = self.get_connection('smtp_server=smtp.example.com',
115
114
                                   smtp_factory=connection_refuser)
116
115
        self.assertRaises(smtp_connection.SMTPConnectionRefused, conn._connect)
117
116
 
118
117
    def test_smtp_username(self):
119
 
        conn = self.get_connection(b'')
 
118
        conn = self.get_connection('')
120
119
        self.assertIs(None, conn._smtp_username)
121
120
 
122
 
        conn = self.get_connection(b'smtp_username=joebody')
 
121
        conn = self.get_connection('smtp_username=joebody')
123
122
        self.assertEqual(u'joebody', conn._smtp_username)
124
123
 
125
124
    def test_smtp_password_from_config(self):
126
 
        conn = self.get_connection(b'')
 
125
        conn = self.get_connection('')
127
126
        self.assertIs(None, conn._smtp_password)
128
127
 
129
 
        conn = self.get_connection(b'smtp_password=mypass')
 
128
        conn = self.get_connection('smtp_password=mypass')
130
129
        self.assertEqual(u'mypass', conn._smtp_password)
131
130
 
132
131
    def test_smtp_password_from_user(self):
133
132
        user = 'joe'
134
133
        password = 'hispass'
135
134
        factory = WideOpenSMTPFactory()
136
 
        conn = self.get_connection(
137
 
            b'[DEFAULT]\nsmtp_username=%s\n' % user.encode('ascii'),
138
 
            smtp_factory=factory)
 
135
        conn = self.get_connection('[DEFAULT]\nsmtp_username=%s\n' % user,
 
136
                                   smtp_factory=factory)
139
137
        self.assertIs(None, conn._smtp_password)
140
138
 
141
139
        ui.ui_factory = ui.CannedInputUIFactory([password])
146
144
        user = 'joe'
147
145
        password = 'hispass'
148
146
        factory = WideOpenSMTPFactory()
149
 
        conn = self.get_connection(
150
 
            b'[DEFAULT]\nsmtp_username=%s\n' % user.encode('ascii'),
151
 
            smtp_factory=factory)
 
147
        conn = self.get_connection('[DEFAULT]\nsmtp_username=%s\n' % user,
 
148
                                   smtp_factory=factory)
152
149
        self.assertEqual(user, conn._smtp_username)
153
150
        self.assertIs(None, conn._smtp_password)
154
151
        # Create a config file with the right password
155
152
        conf = config.AuthenticationConfig()
156
153
        conf._get_config().update({'smtptest':
157
 
                                   {'scheme': 'smtp', 'user': user,
158
 
                                    'password': password}})
 
154
                                       {'scheme': 'smtp', 'user':user,
 
155
                                        'password': password}})
159
156
        conf._save()
160
157
 
161
158
        conn._connect()
162
159
        self.assertEqual(password, conn._smtp_password)
163
160
 
164
161
    def test_authenticate_with_byte_strings(self):
165
 
        user = b'joe'
 
162
        user = 'joe'
166
163
        unicode_pass = u'h\xECspass'
167
164
        utf8_pass = unicode_pass.encode('utf-8')
168
165
        factory = WideOpenSMTPFactory()
169
166
        conn = self.get_connection(
170
 
            b'[DEFAULT]\nsmtp_username=%s\nsmtp_password=%s\n'
 
167
            '[DEFAULT]\nsmtp_username=%s\nsmtp_password=%s\n'
171
168
            % (user, utf8_pass), smtp_factory=factory)
172
169
        self.assertEqual(unicode_pass, conn._smtp_password)
173
170
        conn._connect()
176
173
                          ('has_extn', 'starttls'),
177
174
                          ('login', user, utf8_pass)], factory._calls)
178
175
        smtp_username, smtp_password = factory._calls[-1][1:]
179
 
        self.assertIsInstance(smtp_username, bytes)
180
 
        self.assertIsInstance(smtp_password, bytes)
 
176
        self.assertIsInstance(smtp_username, str)
 
177
        self.assertIsInstance(smtp_password, str)
181
178
 
182
179
    def test_create_connection(self):
183
180
        factory = StubSMTPFactory()
184
 
        conn = self.get_connection(b'', smtp_factory=factory)
 
181
        conn = self.get_connection('', smtp_factory=factory)
185
182
        conn._create_connection()
186
183
        self.assertEqual([('connect', 'localhost'),
187
184
                          ('ehlo',),
190
187
    def test_create_connection_ehlo_fails(self):
191
188
        # Check that we call HELO if EHLO failed.
192
189
        factory = StubSMTPFactory(fail_on=['ehlo'])
193
 
        conn = self.get_connection(b'', smtp_factory=factory)
 
190
        conn = self.get_connection('', smtp_factory=factory)
194
191
        conn._create_connection()
195
192
        self.assertEqual([('connect', 'localhost'),
196
193
                          ('ehlo',),
200
197
    def test_create_connection_ehlo_helo_fails(self):
201
198
        # Check that we raise an exception if both EHLO and HELO fail.
202
199
        factory = StubSMTPFactory(fail_on=['ehlo', 'helo'])
203
 
        conn = self.get_connection(b'', smtp_factory=factory)
 
200
        conn = self.get_connection('', smtp_factory=factory)
204
201
        self.assertRaises(smtp_connection.SMTPError, conn._create_connection)
205
202
        self.assertEqual([('connect', 'localhost'),
206
203
                          ('ehlo',),
210
207
        # Check that STARTTLS plus a second EHLO are called if the
211
208
        # server says it supports the feature.
212
209
        factory = StubSMTPFactory(smtp_features=['starttls'])
213
 
        conn = self.get_connection(b'', smtp_factory=factory)
 
210
        conn = self.get_connection('', smtp_factory=factory)
214
211
        conn._create_connection()
215
212
        self.assertEqual([('connect', 'localhost'),
216
213
                          ('ehlo',),
223
220
        # support STARTTLS, but then fails when we try to activate it.
224
221
        factory = StubSMTPFactory(fail_on=['starttls'],
225
222
                                  smtp_features=['starttls'])
226
 
        conn = self.get_connection(b'', smtp_factory=factory)
 
223
        conn = self.get_connection('', smtp_factory=factory)
227
224
        self.assertRaises(smtp_connection.SMTPError, conn._create_connection)
228
225
        self.assertEqual([('connect', 'localhost'),
229
226
                          ('ehlo',),
245
242
        from_, to = smtp_connection.SMTPConnection.get_message_addresses(msg)
246
243
        self.assertEqual('jrandom@example.com', from_)
247
244
        self.assertEqual(sorted(['john@doe.com', 'jane@doe.com',
248
 
                                 'pperez@ejemplo.com', 'user@localhost']), sorted(to))
 
245
            'pperez@ejemplo.com', 'user@localhost']), sorted(to))
249
246
 
250
247
        # now with breezy's EmailMessage
251
248
        msg = email_message.EmailMessage(
252
249
            '"J. Random Developer" <jrandom@example.com>',
253
250
            ['John Doe <john@doe.com>', 'Jane Doe <jane@doe.com>',
254
 
             u'Pepe P\xe9rez <pperez@ejemplo.com>', 'user@localhost'],
 
251
             u'Pepe P\xe9rez <pperez@ejemplo.com>', 'user@localhost' ],
255
252
            'subject')
256
253
 
257
254
        from_, to = smtp_connection.SMTPConnection.get_message_addresses(msg)
258
255
        self.assertEqual('jrandom@example.com', from_)
259
256
        self.assertEqual(sorted(['john@doe.com', 'jane@doe.com',
260
 
                                 'pperez@ejemplo.com', 'user@localhost']), sorted(to))
 
257
            'pperez@ejemplo.com', 'user@localhost']), sorted(to))
261
258
 
262
259
    def test_destination_address_required(self):
263
260
        msg = Message()
264
261
        msg['From'] = '"J. Random Developer" <jrandom@example.com>'
265
262
        self.assertRaises(
266
263
            smtp_connection.NoDestinationAddress,
267
 
            smtp_connection.SMTPConnection(config.MemoryStack(b"")
 
264
            smtp_connection.SMTPConnection(config.MemoryStack("")
268
265
                                           ).send_email, msg)
269
266
 
270
267
        msg = email_message.EmailMessage('from@from.com', '', 'subject')
271
268
        self.assertRaises(
272
269
            smtp_connection.NoDestinationAddress,
273
 
            smtp_connection.SMTPConnection(config.MemoryStack(b"")
 
270
            smtp_connection.SMTPConnection(config.MemoryStack("")
274
271
                                           ).send_email, msg)
275
272
 
276
273
        msg = email_message.EmailMessage('from@from.com', [], 'subject')
277
274
        self.assertRaises(
278
275
            smtp_connection.NoDestinationAddress,
279
 
            smtp_connection.SMTPConnection(config.MemoryStack(b"")
 
276
            smtp_connection.SMTPConnection(config.MemoryStack("")
280
277
                                           ).send_email, msg)