~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smtp_connection.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-09-03 22:30:56 UTC
  • mfrom: (3644.2.13 index_builder_cleanup)
  • Revision ID: pqm@pqm.ubuntu.com-20080903223056-b108iytb38xkznci
(jam) Streamline BTreeBuilder.add_node et al to make btree creation
        faster.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007, 2009, 2010, 2011 Canonical Ltd
 
1
# Copyright (C) 2005, 2007 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
 
17
from cStringIO import StringIO
17
18
from email.Message import Message
18
19
import errno
19
20
import smtplib
20
21
import socket
 
22
import sys
21
23
 
22
24
from bzrlib import (
23
25
    config,
84
86
    """A fake smtp server that implements login by accepting anybody."""
85
87
 
86
88
    def login(self, user, password):
87
 
        self._calls.append(('login', user, password))
 
89
        pass
88
90
 
89
91
 
90
92
class TestSMTPConnection(tests.TestCaseInTempDir):
91
93
 
92
94
    def get_connection(self, text, smtp_factory=None):
93
 
        my_config = config.MemoryStack(text)
94
 
        return smtp_connection.SMTPConnection(
95
 
            my_config, _smtp_factory=smtp_factory)
 
95
        my_config = config.GlobalConfig()
 
96
        config_file = StringIO(text)
 
97
        my_config._get_parser(config_file)
 
98
        return smtp_connection.SMTPConnection(my_config,
 
99
                                              _smtp_factory=smtp_factory)
96
100
 
97
101
    def test_defaults(self):
98
102
        conn = self.get_connection('')
101
105
        self.assertEqual(None, conn._smtp_password)
102
106
 
103
107
    def test_smtp_server(self):
104
 
        conn = self.get_connection('smtp_server=host:10')
 
108
        conn = self.get_connection('[DEFAULT]\nsmtp_server=host:10\n')
105
109
        self.assertEqual('host:10', conn._smtp_server)
106
110
 
107
111
    def test_missing_server(self):
108
112
        conn = self.get_connection('', smtp_factory=connection_refuser)
109
113
        self.assertRaises(errors.DefaultSMTPConnectionRefused, conn._connect)
110
 
        conn = self.get_connection('smtp_server=smtp.example.com',
 
114
        conn = self.get_connection('[DEFAULT]\nsmtp_server=smtp.example.com\n',
111
115
                                   smtp_factory=connection_refuser)
112
116
        self.assertRaises(errors.SMTPConnectionRefused, conn._connect)
113
117
 
115
119
        conn = self.get_connection('')
116
120
        self.assertIs(None, conn._smtp_username)
117
121
 
118
 
        conn = self.get_connection('smtp_username=joebody')
 
122
        conn = self.get_connection('[DEFAULT]\nsmtp_username=joebody\n')
119
123
        self.assertEqual(u'joebody', conn._smtp_username)
120
124
 
121
125
    def test_smtp_password_from_config(self):
122
126
        conn = self.get_connection('')
123
127
        self.assertIs(None, conn._smtp_password)
124
128
 
125
 
        conn = self.get_connection('smtp_password=mypass')
 
129
        conn = self.get_connection('[DEFAULT]\nsmtp_password=mypass\n')
126
130
        self.assertEqual(u'mypass', conn._smtp_password)
127
131
 
128
132
    def test_smtp_password_from_user(self):
133
137
                                   smtp_factory=factory)
134
138
        self.assertIs(None, conn._smtp_password)
135
139
 
136
 
        ui.ui_factory = ui.CannedInputUIFactory([password])
 
140
        ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
 
141
                                            stdout=tests.StringIOWrapper())
137
142
        conn._connect()
138
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())
139
146
 
140
147
    def test_smtp_password_from_auth_config(self):
141
148
        user = 'joe'
155
162
        conn._connect()
156
163
        self.assertEqual(password, conn._smtp_password)
157
164
 
158
 
    def test_authenticate_with_byte_strings(self):
159
 
        user = 'joe'
160
 
        unicode_pass = u'h\xECspass'
161
 
        utf8_pass = unicode_pass.encode('utf-8')
162
 
        factory = WideOpenSMTPFactory()
163
 
        conn = self.get_connection(
164
 
            '[DEFAULT]\nsmtp_username=%s\nsmtp_password=%s\n'
165
 
            % (user, utf8_pass), smtp_factory=factory)
166
 
        self.assertEqual(unicode_pass, conn._smtp_password)
167
 
        conn._connect()
168
 
        self.assertEqual([('connect', 'localhost'),
169
 
                          ('ehlo',),
170
 
                          ('has_extn', 'starttls'),
171
 
                          ('login', user, utf8_pass)], factory._calls)
172
 
        smtp_username, smtp_password = factory._calls[-1][1:]
173
 
        self.assertIsInstance(smtp_username, str)
174
 
        self.assertIsInstance(smtp_password, str)
175
 
 
176
165
    def test_create_connection(self):
177
166
        factory = StubSMTPFactory()
178
167
        conn = self.get_connection('', smtp_factory=factory)
254
243
            'pperez@ejemplo.com', 'user@localhost']), sorted(to))
255
244
 
256
245
    def test_destination_address_required(self):
 
246
        class FakeConfig:
 
247
            def get_user_option(self, option):
 
248
                return None
 
249
 
257
250
        msg = Message()
258
251
        msg['From'] = '"J. Random Developer" <jrandom@example.com>'
259
252
        self.assertRaises(
260
253
            errors.NoDestinationAddress,
261
 
            smtp_connection.SMTPConnection(config.MemoryStack("")
262
 
                                           ).send_email, msg)
 
254
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
263
255
 
264
256
        msg = email_message.EmailMessage('from@from.com', '', 'subject')
265
257
        self.assertRaises(
266
258
            errors.NoDestinationAddress,
267
 
            smtp_connection.SMTPConnection(config.MemoryStack("")
268
 
                                           ).send_email, msg)
 
259
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
269
260
 
270
261
        msg = email_message.EmailMessage('from@from.com', [], 'subject')
271
262
        self.assertRaises(
272
263
            errors.NoDestinationAddress,
273
 
            smtp_connection.SMTPConnection(config.MemoryStack("")
274
 
                                           ).send_email, msg)
 
264
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)