~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: 2009-03-17 07:05:37 UTC
  • mfrom: (4152.1.2 branch.stacked.streams)
  • Revision ID: pqm@pqm.ubuntu.com-20090317070537-zaud24vjs2szna87
(robertc) Add client-side streaming from stacked branches (over
        bzr:// protocols) when the sort order is compatible with doing
        that. (Robert Collins, Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007, 2009, 2010 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
17
from cStringIO import StringIO
18
18
from email.Message import Message
19
19
import errno
20
20
import smtplib
21
21
import socket
 
22
import sys
22
23
 
23
24
from bzrlib import (
24
25
    config,
85
86
    """A fake smtp server that implements login by accepting anybody."""
86
87
 
87
88
    def login(self, user, password):
88
 
        self._calls.append(('login', user, password))
 
89
        pass
89
90
 
90
91
 
91
92
class TestSMTPConnection(tests.TestCaseInTempDir):
92
93
 
93
94
    def get_connection(self, text, smtp_factory=None):
94
 
        my_config = config.GlobalConfig.from_string(text)
 
95
        my_config = config.GlobalConfig()
 
96
        config_file = StringIO(text)
 
97
        my_config._get_parser(config_file)
95
98
        return smtp_connection.SMTPConnection(my_config,
96
99
                                              _smtp_factory=smtp_factory)
97
100
 
134
137
                                   smtp_factory=factory)
135
138
        self.assertIs(None, conn._smtp_password)
136
139
 
137
 
        ui.ui_factory = ui.CannedInputUIFactory([password])
 
140
        ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
 
141
                                            stdout=tests.StringIOWrapper())
138
142
        conn._connect()
139
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())
140
146
 
141
147
    def test_smtp_password_from_auth_config(self):
142
148
        user = 'joe'
156
162
        conn._connect()
157
163
        self.assertEqual(password, conn._smtp_password)
158
164
 
159
 
    def test_authenticate_with_byte_strings(self):
160
 
        user = 'joe'
161
 
        unicode_pass = u'h\xECspass'
162
 
        utf8_pass = unicode_pass.encode('utf-8')
163
 
        factory = WideOpenSMTPFactory()
164
 
        conn = self.get_connection(
165
 
            u'[DEFAULT]\nsmtp_username=%s\nsmtp_password=%s\n'
166
 
            % (user, unicode_pass), smtp_factory=factory)
167
 
        self.assertEqual(unicode_pass, conn._smtp_password)
168
 
        conn._connect()
169
 
        self.assertEqual([('connect', 'localhost'),
170
 
                          ('ehlo',),
171
 
                          ('has_extn', 'starttls'),
172
 
                          ('login', user, utf8_pass)], factory._calls)
173
 
        smtp_username, smtp_password = factory._calls[-1][1:]
174
 
        self.assertIsInstance(smtp_username, str)
175
 
        self.assertIsInstance(smtp_password, str)
176
 
 
177
165
    def test_create_connection(self):
178
166
        factory = StubSMTPFactory()
179
167
        conn = self.get_connection('', smtp_factory=factory)