~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smtp_connection.py

  • Committer: Patch Queue Manager
  • Date: 2016-04-21 04:10:52 UTC
  • mfrom: (6616.1.1 fix-en-user-guide)
  • Revision ID: pqm@pqm.ubuntu.com-20160421041052-clcye7ns1qcl2n7w
(richard-wilbur) Ensure build of English use guide always uses English text
 even when user's locale specifies a different language. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2007 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
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
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
21
20
import socket
22
 
import sys
23
21
 
24
22
from bzrlib import (
25
23
    config,
86
84
    """A fake smtp server that implements login by accepting anybody."""
87
85
 
88
86
    def login(self, user, password):
89
 
        pass
 
87
        self._calls.append(('login', user, password))
90
88
 
91
89
 
92
90
class TestSMTPConnection(tests.TestCaseInTempDir):
93
91
 
94
92
    def get_connection(self, text, smtp_factory=None):
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)
 
93
        my_config = config.MemoryStack(text)
 
94
        return smtp_connection.SMTPConnection(
 
95
            my_config, _smtp_factory=smtp_factory)
100
96
 
101
97
    def test_defaults(self):
102
98
        conn = self.get_connection('')
105
101
        self.assertEqual(None, conn._smtp_password)
106
102
 
107
103
    def test_smtp_server(self):
108
 
        conn = self.get_connection('[DEFAULT]\nsmtp_server=host:10\n')
 
104
        conn = self.get_connection('smtp_server=host:10')
109
105
        self.assertEqual('host:10', conn._smtp_server)
110
106
 
111
107
    def test_missing_server(self):
112
108
        conn = self.get_connection('', smtp_factory=connection_refuser)
113
109
        self.assertRaises(errors.DefaultSMTPConnectionRefused, conn._connect)
114
 
        conn = self.get_connection('[DEFAULT]\nsmtp_server=smtp.example.com\n',
 
110
        conn = self.get_connection('smtp_server=smtp.example.com',
115
111
                                   smtp_factory=connection_refuser)
116
112
        self.assertRaises(errors.SMTPConnectionRefused, conn._connect)
117
113
 
119
115
        conn = self.get_connection('')
120
116
        self.assertIs(None, conn._smtp_username)
121
117
 
122
 
        conn = self.get_connection('[DEFAULT]\nsmtp_username=joebody\n')
 
118
        conn = self.get_connection('smtp_username=joebody')
123
119
        self.assertEqual(u'joebody', conn._smtp_username)
124
120
 
125
121
    def test_smtp_password_from_config(self):
126
122
        conn = self.get_connection('')
127
123
        self.assertIs(None, conn._smtp_password)
128
124
 
129
 
        conn = self.get_connection('[DEFAULT]\nsmtp_password=mypass\n')
 
125
        conn = self.get_connection('smtp_password=mypass')
130
126
        self.assertEqual(u'mypass', conn._smtp_password)
131
127
 
132
128
    def test_smtp_password_from_user(self):
137
133
                                   smtp_factory=factory)
138
134
        self.assertIs(None, conn._smtp_password)
139
135
 
140
 
        ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
141
 
                                            stdout=tests.StringIOWrapper())
 
136
        ui.ui_factory = ui.CannedInputUIFactory([password])
142
137
        conn._connect()
143
138
        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())
146
139
 
147
140
    def test_smtp_password_from_auth_config(self):
148
141
        user = 'joe'
162
155
        conn._connect()
163
156
        self.assertEqual(password, conn._smtp_password)
164
157
 
 
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
 
165
176
    def test_create_connection(self):
166
177
        factory = StubSMTPFactory()
167
178
        conn = self.get_connection('', smtp_factory=factory)
243
254
            'pperez@ejemplo.com', 'user@localhost']), sorted(to))
244
255
 
245
256
    def test_destination_address_required(self):
246
 
        class FakeConfig:
247
 
            def get_user_option(self, option):
248
 
                return None
249
 
 
250
257
        msg = Message()
251
258
        msg['From'] = '"J. Random Developer" <jrandom@example.com>'
252
259
        self.assertRaises(
253
260
            errors.NoDestinationAddress,
254
 
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
 
261
            smtp_connection.SMTPConnection(config.MemoryStack("")
 
262
                                           ).send_email, msg)
255
263
 
256
264
        msg = email_message.EmailMessage('from@from.com', '', 'subject')
257
265
        self.assertRaises(
258
266
            errors.NoDestinationAddress,
259
 
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
 
267
            smtp_connection.SMTPConnection(config.MemoryStack("")
 
268
                                           ).send_email, msg)
260
269
 
261
270
        msg = email_message.EmailMessage('from@from.com', [], 'subject')
262
271
        self.assertRaises(
263
272
            errors.NoDestinationAddress,
264
 
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
 
273
            smtp_connection.SMTPConnection(config.MemoryStack("")
 
274
                                           ).send_email, msg)