/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

  • Committer: Jelmer Vernooij
  • Date: 2018-07-08 14:45:27 UTC
  • mto: This revision was merged to the branch mainline in revision 7036.
  • Revision ID: jelmer@jelmer.uk-20180708144527-codhlvdcdg9y0nji
Fix a bunch of merge tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2007, 2009 Canonical Ltd
 
1
# Copyright (C) 2007, 2009, 2010, 2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
from cStringIO import StringIO
18
 
from email.Message import Message
 
17
try:
 
18
    from email.message import Message
 
19
except ImportError:  # python < 3
 
20
    from email.Message import Message
19
21
import errno
20
22
import smtplib
21
23
import socket
22
24
 
23
 
from bzrlib import (
 
25
from breezy import (
24
26
    config,
25
27
    email_message,
26
 
    errors,
27
28
    smtp_connection,
28
29
    tests,
29
30
    ui,
91
92
class TestSMTPConnection(tests.TestCaseInTempDir):
92
93
 
93
94
    def get_connection(self, text, smtp_factory=None):
94
 
        my_config = config.GlobalConfig()
95
 
        config_file = StringIO(text)
96
 
        my_config._get_parser(config_file)
97
 
        return smtp_connection.SMTPConnection(my_config,
98
 
                                              _smtp_factory=smtp_factory)
 
95
        my_config = config.MemoryStack(text)
 
96
        return smtp_connection.SMTPConnection(
 
97
            my_config, _smtp_factory=smtp_factory)
99
98
 
100
99
    def test_defaults(self):
101
100
        conn = self.get_connection('')
104
103
        self.assertEqual(None, conn._smtp_password)
105
104
 
106
105
    def test_smtp_server(self):
107
 
        conn = self.get_connection('[DEFAULT]\nsmtp_server=host:10\n')
 
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
110
        conn = self.get_connection('', smtp_factory=connection_refuser)
112
 
        self.assertRaises(errors.DefaultSMTPConnectionRefused, conn._connect)
113
 
        conn = self.get_connection('[DEFAULT]\nsmtp_server=smtp.example.com\n',
 
111
        self.assertRaises(smtp_connection.DefaultSMTPConnectionRefused,
 
112
                          conn._connect)
 
113
        conn = self.get_connection('smtp_server=smtp.example.com',
114
114
                                   smtp_factory=connection_refuser)
115
 
        self.assertRaises(errors.SMTPConnectionRefused, conn._connect)
 
115
        self.assertRaises(smtp_connection.SMTPConnectionRefused, conn._connect)
116
116
 
117
117
    def test_smtp_username(self):
118
118
        conn = self.get_connection('')
119
119
        self.assertIs(None, conn._smtp_username)
120
120
 
121
 
        conn = self.get_connection('[DEFAULT]\nsmtp_username=joebody\n')
 
121
        conn = self.get_connection('smtp_username=joebody')
122
122
        self.assertEqual(u'joebody', conn._smtp_username)
123
123
 
124
124
    def test_smtp_password_from_config(self):
125
125
        conn = self.get_connection('')
126
126
        self.assertIs(None, conn._smtp_password)
127
127
 
128
 
        conn = self.get_connection('[DEFAULT]\nsmtp_password=mypass\n')
 
128
        conn = self.get_connection('smtp_password=mypass')
129
129
        self.assertEqual(u'mypass', conn._smtp_password)
130
130
 
131
131
    def test_smtp_password_from_user(self):
160
160
 
161
161
    def test_authenticate_with_byte_strings(self):
162
162
        user = 'joe'
163
 
        password = 'h\xC3\xACspass'
 
163
        unicode_pass = u'h\xECspass'
 
164
        utf8_pass = unicode_pass.encode('utf-8')
164
165
        factory = WideOpenSMTPFactory()
165
166
        conn = self.get_connection(
166
167
            '[DEFAULT]\nsmtp_username=%s\nsmtp_password=%s\n'
167
 
            % (user, password), smtp_factory=factory)
168
 
        self.assertEqual(u'h\xECspass', conn._smtp_password)
 
168
            % (user, utf8_pass), smtp_factory=factory)
 
169
        self.assertEqual(unicode_pass, conn._smtp_password)
169
170
        conn._connect()
170
171
        self.assertEqual([('connect', 'localhost'),
171
172
                          ('ehlo',),
172
173
                          ('has_extn', 'starttls'),
173
 
                          ('login', user, password)], factory._calls)
 
174
                          ('login', user, utf8_pass)], factory._calls)
174
175
        smtp_username, smtp_password = factory._calls[-1][1:]
175
176
        self.assertIsInstance(smtp_username, str)
176
177
        self.assertIsInstance(smtp_password, str)
197
198
        # Check that we raise an exception if both EHLO and HELO fail.
198
199
        factory = StubSMTPFactory(fail_on=['ehlo', 'helo'])
199
200
        conn = self.get_connection('', smtp_factory=factory)
200
 
        self.assertRaises(errors.SMTPError, conn._create_connection)
 
201
        self.assertRaises(smtp_connection.SMTPError, conn._create_connection)
201
202
        self.assertEqual([('connect', 'localhost'),
202
203
                          ('ehlo',),
203
204
                          ('helo',)], factory._calls)
220
221
        factory = StubSMTPFactory(fail_on=['starttls'],
221
222
                                  smtp_features=['starttls'])
222
223
        conn = self.get_connection('', smtp_factory=factory)
223
 
        self.assertRaises(errors.SMTPError, conn._create_connection)
 
224
        self.assertRaises(smtp_connection.SMTPError, conn._create_connection)
224
225
        self.assertEqual([('connect', 'localhost'),
225
226
                          ('ehlo',),
226
227
                          ('has_extn', 'starttls'),
243
244
        self.assertEqual(sorted(['john@doe.com', 'jane@doe.com',
244
245
            'pperez@ejemplo.com', 'user@localhost']), sorted(to))
245
246
 
246
 
        # now with bzrlib's EmailMessage
 
247
        # now with breezy's EmailMessage
247
248
        msg = email_message.EmailMessage(
248
249
            '"J. Random Developer" <jrandom@example.com>',
249
250
            ['John Doe <john@doe.com>', 'Jane Doe <jane@doe.com>',
256
257
            'pperez@ejemplo.com', 'user@localhost']), sorted(to))
257
258
 
258
259
    def test_destination_address_required(self):
259
 
        class FakeConfig:
260
 
            def get_user_option(self, option):
261
 
                return None
262
 
 
263
260
        msg = Message()
264
261
        msg['From'] = '"J. Random Developer" <jrandom@example.com>'
265
262
        self.assertRaises(
266
 
            errors.NoDestinationAddress,
267
 
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
 
263
            smtp_connection.NoDestinationAddress,
 
264
            smtp_connection.SMTPConnection(config.MemoryStack("")
 
265
                                           ).send_email, msg)
268
266
 
269
267
        msg = email_message.EmailMessage('from@from.com', '', 'subject')
270
268
        self.assertRaises(
271
 
            errors.NoDestinationAddress,
272
 
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
 
269
            smtp_connection.NoDestinationAddress,
 
270
            smtp_connection.SMTPConnection(config.MemoryStack("")
 
271
                                           ).send_email, msg)
273
272
 
274
273
        msg = email_message.EmailMessage('from@from.com', [], 'subject')
275
274
        self.assertRaises(
276
 
            errors.NoDestinationAddress,
277
 
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
 
275
            smtp_connection.NoDestinationAddress,
 
276
            smtp_connection.SMTPConnection(config.MemoryStack("")
 
277
                                           ).send_email, msg)