~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smtp_connection.py

  • Committer: Vincent Ladeuil
  • Date: 2012-01-18 14:09:19 UTC
  • mto: This revision was merged to the branch mainline in revision 6468.
  • Revision ID: v.ladeuil+lp@free.fr-20120118140919-rlvdrhpc0nq1lbwi
Change set/remove to require a lock for the branch config files.

This means that tests (or any plugin for that matter) do not requires an
explicit lock on the branch anymore to change a single option. This also
means the optimisation becomes "opt-in" and as such won't be as
spectacular as it may be and/or harder to get right (nothing fails
anymore).

This reduces the diff by ~300 lines.

Code/tests that were updating more than one config option is still taking
a lock to at least avoid some IOs and demonstrate the benefits through
the decreased number of hpss calls.

The duplication between BranchStack and BranchOnlyStack will be removed
once the same sharing is in place for local config files, at which point
the Stack class itself may be able to host the changes.

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
91
90
class TestSMTPConnection(tests.TestCaseInTempDir):
92
91
 
93
92
    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)
 
93
        my_config = config.MemoryStack(text)
 
94
        return smtp_connection.SMTPConnection(
 
95
            my_config, _smtp_factory=smtp_factory)
99
96
 
100
97
    def test_defaults(self):
101
98
        conn = self.get_connection('')
104
101
        self.assertEqual(None, conn._smtp_password)
105
102
 
106
103
    def test_smtp_server(self):
107
 
        conn = self.get_connection('[DEFAULT]\nsmtp_server=host:10\n')
 
104
        conn = self.get_connection('smtp_server=host:10')
108
105
        self.assertEqual('host:10', conn._smtp_server)
109
106
 
110
107
    def test_missing_server(self):
111
108
        conn = self.get_connection('', smtp_factory=connection_refuser)
112
109
        self.assertRaises(errors.DefaultSMTPConnectionRefused, conn._connect)
113
 
        conn = self.get_connection('[DEFAULT]\nsmtp_server=smtp.example.com\n',
 
110
        conn = self.get_connection('smtp_server=smtp.example.com',
114
111
                                   smtp_factory=connection_refuser)
115
112
        self.assertRaises(errors.SMTPConnectionRefused, conn._connect)
116
113
 
118
115
        conn = self.get_connection('')
119
116
        self.assertIs(None, conn._smtp_username)
120
117
 
121
 
        conn = self.get_connection('[DEFAULT]\nsmtp_username=joebody\n')
 
118
        conn = self.get_connection('smtp_username=joebody')
122
119
        self.assertEqual(u'joebody', conn._smtp_username)
123
120
 
124
121
    def test_smtp_password_from_config(self):
125
122
        conn = self.get_connection('')
126
123
        self.assertIs(None, conn._smtp_password)
127
124
 
128
 
        conn = self.get_connection('[DEFAULT]\nsmtp_password=mypass\n')
 
125
        conn = self.get_connection('smtp_password=mypass')
129
126
        self.assertEqual(u'mypass', conn._smtp_password)
130
127
 
131
128
    def test_smtp_password_from_user(self):
160
157
 
161
158
    def test_authenticate_with_byte_strings(self):
162
159
        user = 'joe'
163
 
        password = 'h\xC3\xACspass'
 
160
        unicode_pass = u'h\xECspass'
 
161
        utf8_pass = unicode_pass.encode('utf-8')
164
162
        factory = WideOpenSMTPFactory()
165
163
        conn = self.get_connection(
166
164
            '[DEFAULT]\nsmtp_username=%s\nsmtp_password=%s\n'
167
 
            % (user, password), smtp_factory=factory)
168
 
        self.assertEqual(u'h\xECspass', conn._smtp_password)
 
165
            % (user, utf8_pass), smtp_factory=factory)
 
166
        self.assertEqual(unicode_pass, conn._smtp_password)
169
167
        conn._connect()
170
168
        self.assertEqual([('connect', 'localhost'),
171
169
                          ('ehlo',),
172
170
                          ('has_extn', 'starttls'),
173
 
                          ('login', user, password)], factory._calls)
 
171
                          ('login', user, utf8_pass)], factory._calls)
174
172
        smtp_username, smtp_password = factory._calls[-1][1:]
175
173
        self.assertIsInstance(smtp_username, str)
176
174
        self.assertIsInstance(smtp_password, str)
256
254
            'pperez@ejemplo.com', 'user@localhost']), sorted(to))
257
255
 
258
256
    def test_destination_address_required(self):
259
 
        class FakeConfig:
260
 
            def get_user_option(self, option):
261
 
                return None
262
 
 
263
257
        msg = Message()
264
258
        msg['From'] = '"J. Random Developer" <jrandom@example.com>'
265
259
        self.assertRaises(
266
260
            errors.NoDestinationAddress,
267
 
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
 
261
            smtp_connection.SMTPConnection(config.MemoryStack("")
 
262
                                           ).send_email, msg)
268
263
 
269
264
        msg = email_message.EmailMessage('from@from.com', '', 'subject')
270
265
        self.assertRaises(
271
266
            errors.NoDestinationAddress,
272
 
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
 
267
            smtp_connection.SMTPConnection(config.MemoryStack("")
 
268
                                           ).send_email, msg)
273
269
 
274
270
        msg = email_message.EmailMessage('from@from.com', [], 'subject')
275
271
        self.assertRaises(
276
272
            errors.NoDestinationAddress,
277
 
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
 
273
            smtp_connection.SMTPConnection(config.MemoryStack("")
 
274
                                           ).send_email, msg)