/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 bzrlib/tests/test_smtp_connection.py

(gz) Fix deprecations of win32utils path function unicode wrappers (Martin
 Packman)

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
17
from email.Message import Message
19
18
import errno
20
19
import smtplib
88
87
        self._calls.append(('login', user, password))
89
88
 
90
89
 
 
90
class StringStack(config.Stack):
 
91
 
 
92
    def __init__(self, text):
 
93
        store = config.IniFileStore()
 
94
        store._load_from_string(text)
 
95
        super(StringStack, self).__init__([store.get_sections])
 
96
 
 
97
 
91
98
class TestSMTPConnection(tests.TestCaseInTempDir):
92
99
 
93
100
    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)
 
101
        my_config = StringStack(text)
 
102
        return smtp_connection.SMTPConnection(
 
103
            my_config, _smtp_factory=smtp_factory)
99
104
 
100
105
    def test_defaults(self):
101
106
        conn = self.get_connection('')
160
165
 
161
166
    def test_authenticate_with_byte_strings(self):
162
167
        user = 'joe'
163
 
        password = 'h\xC3\xACspass'
 
168
        unicode_pass = u'h\xECspass'
 
169
        utf8_pass = unicode_pass.encode('utf-8')
164
170
        factory = WideOpenSMTPFactory()
165
171
        conn = self.get_connection(
166
172
            '[DEFAULT]\nsmtp_username=%s\nsmtp_password=%s\n'
167
 
            % (user, password), smtp_factory=factory)
168
 
        self.assertEqual(u'h\xECspass', conn._smtp_password)
 
173
            % (user, utf8_pass), smtp_factory=factory)
 
174
        self.assertEqual(unicode_pass, conn._smtp_password)
169
175
        conn._connect()
170
176
        self.assertEqual([('connect', 'localhost'),
171
177
                          ('ehlo',),
172
178
                          ('has_extn', 'starttls'),
173
 
                          ('login', user, password)], factory._calls)
 
179
                          ('login', user, utf8_pass)], factory._calls)
174
180
        smtp_username, smtp_password = factory._calls[-1][1:]
175
181
        self.assertIsInstance(smtp_username, str)
176
182
        self.assertIsInstance(smtp_password, str)
256
262
            'pperez@ejemplo.com', 'user@localhost']), sorted(to))
257
263
 
258
264
    def test_destination_address_required(self):
259
 
        class FakeConfig:
260
 
            def get_user_option(self, option):
261
 
                return None
262
 
 
263
265
        msg = Message()
264
266
        msg['From'] = '"J. Random Developer" <jrandom@example.com>'
265
267
        self.assertRaises(
266
268
            errors.NoDestinationAddress,
267
 
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
 
269
            smtp_connection.SMTPConnection(StringStack("")).send_email, msg)
268
270
 
269
271
        msg = email_message.EmailMessage('from@from.com', '', 'subject')
270
272
        self.assertRaises(
271
273
            errors.NoDestinationAddress,
272
 
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
 
274
            smtp_connection.SMTPConnection(StringStack("")).send_email, msg)
273
275
 
274
276
        msg = email_message.EmailMessage('from@from.com', [], 'subject')
275
277
        self.assertRaises(
276
278
            errors.NoDestinationAddress,
277
 
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
 
279
            smtp_connection.SMTPConnection(StringStack("")).send_email, msg)