/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

  • Committer: Alexander Belchenko
  • Date: 2007-11-02 08:45:10 UTC
  • mto: This revision was merged to the branch mainline in revision 2968.
  • Revision ID: bialix@ukr.net-20071102084510-ngqdd24hjhfdkgw3
start 0.93 development cycle; deprecate osutils.backup_file

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2007, 2009 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
22
22
 
23
23
from bzrlib import (
24
24
    config,
25
 
    email_message,
26
25
    errors,
27
 
    smtp_connection,
28
 
    tests,
29
 
    ui,
30
26
    )
 
27
from bzrlib.email_message import EmailMessage
 
28
from bzrlib.errors import NoDestinationAddress
 
29
from bzrlib.tests import TestCase
 
30
from bzrlib.smtp_connection import SMTPConnection
31
31
 
32
32
 
33
33
def connection_refuser():
81
81
            return 200, 'starttls success'
82
82
 
83
83
 
84
 
class WideOpenSMTPFactory(StubSMTPFactory):
85
 
    """A fake smtp server that implements login by accepting anybody."""
86
 
 
87
 
    def login(self, user, password):
88
 
        self._calls.append(('login', user, password))
89
 
 
90
 
 
91
 
class TestSMTPConnection(tests.TestCaseInTempDir):
 
84
class TestSMTPConnection(TestCase):
92
85
 
93
86
    def get_connection(self, text, smtp_factory=None):
94
87
        my_config = config.GlobalConfig()
95
88
        config_file = StringIO(text)
96
89
        my_config._get_parser(config_file)
97
 
        return smtp_connection.SMTPConnection(my_config,
98
 
                                              _smtp_factory=smtp_factory)
 
90
        return SMTPConnection(my_config, _smtp_factory=smtp_factory)
99
91
 
100
92
    def test_defaults(self):
101
93
        conn = self.get_connection('')
121
113
        conn = self.get_connection('[DEFAULT]\nsmtp_username=joebody\n')
122
114
        self.assertEqual(u'joebody', conn._smtp_username)
123
115
 
124
 
    def test_smtp_password_from_config(self):
 
116
    def test_smtp_password(self):
125
117
        conn = self.get_connection('')
126
118
        self.assertIs(None, conn._smtp_password)
127
119
 
128
120
        conn = self.get_connection('[DEFAULT]\nsmtp_password=mypass\n')
129
121
        self.assertEqual(u'mypass', conn._smtp_password)
130
122
 
131
 
    def test_smtp_password_from_user(self):
132
 
        user = 'joe'
133
 
        password = 'hispass'
134
 
        factory = WideOpenSMTPFactory()
135
 
        conn = self.get_connection('[DEFAULT]\nsmtp_username=%s\n' % user,
136
 
                                   smtp_factory=factory)
137
 
        self.assertIs(None, conn._smtp_password)
138
 
 
139
 
        ui.ui_factory = ui.CannedInputUIFactory([password])
140
 
        conn._connect()
141
 
        self.assertEqual(password, conn._smtp_password)
142
 
 
143
 
    def test_smtp_password_from_auth_config(self):
144
 
        user = 'joe'
145
 
        password = 'hispass'
146
 
        factory = WideOpenSMTPFactory()
147
 
        conn = self.get_connection('[DEFAULT]\nsmtp_username=%s\n' % user,
148
 
                                   smtp_factory=factory)
149
 
        self.assertEqual(user, conn._smtp_username)
150
 
        self.assertIs(None, conn._smtp_password)
151
 
        # Create a config file with the right password
152
 
        conf = config.AuthenticationConfig()
153
 
        conf._get_config().update({'smtptest':
154
 
                                       {'scheme': 'smtp', 'user':user,
155
 
                                        'password': password}})
156
 
        conf._save()
157
 
 
158
 
        conn._connect()
159
 
        self.assertEqual(password, conn._smtp_password)
160
 
 
161
 
    def test_authenticate_with_byte_strings(self):
162
 
        user = 'joe'
163
 
        password = 'h\xC3\xACspass'
164
 
        factory = WideOpenSMTPFactory()
165
 
        conn = self.get_connection(
166
 
            '[DEFAULT]\nsmtp_username=%s\nsmtp_password=%s\n'
167
 
            % (user, password), smtp_factory=factory)
168
 
        self.assertEqual(u'h\xECspass', conn._smtp_password)
169
 
        conn._connect()
170
 
        self.assertEqual([('connect', 'localhost'),
171
 
                          ('ehlo',),
172
 
                          ('has_extn', 'starttls'),
173
 
                          ('login', user, password)], factory._calls)
174
 
        smtp_username, smtp_password = factory._calls[-1][1:]
175
 
        self.assertIsInstance(smtp_username, str)
176
 
        self.assertIsInstance(smtp_password, str)
177
 
 
178
123
    def test_create_connection(self):
179
124
        factory = StubSMTPFactory()
180
125
        conn = self.get_connection('', smtp_factory=factory)
229
174
    def test_get_message_addresses(self):
230
175
        msg = Message()
231
176
 
232
 
        from_, to = smtp_connection.SMTPConnection.get_message_addresses(msg)
 
177
        from_, to = SMTPConnection.get_message_addresses(msg)
233
178
        self.assertEqual('', from_)
234
179
        self.assertEqual([], to)
235
180
 
238
183
        msg['CC'] = u'Pepe P\xe9rez <pperez@ejemplo.com>'
239
184
        msg['Bcc'] = 'user@localhost'
240
185
 
241
 
        from_, to = smtp_connection.SMTPConnection.get_message_addresses(msg)
 
186
        from_, to = SMTPConnection.get_message_addresses(msg)
242
187
        self.assertEqual('jrandom@example.com', from_)
243
188
        self.assertEqual(sorted(['john@doe.com', 'jane@doe.com',
244
189
            'pperez@ejemplo.com', 'user@localhost']), sorted(to))
245
190
 
246
191
        # now with bzrlib's EmailMessage
247
 
        msg = email_message.EmailMessage(
248
 
            '"J. Random Developer" <jrandom@example.com>',
249
 
            ['John Doe <john@doe.com>', 'Jane Doe <jane@doe.com>',
250
 
             u'Pepe P\xe9rez <pperez@ejemplo.com>', 'user@localhost' ],
 
192
        msg = EmailMessage('"J. Random Developer" <jrandom@example.com>', [
 
193
            'John Doe <john@doe.com>', 'Jane Doe <jane@doe.com>',
 
194
            u'Pepe P\xe9rez <pperez@ejemplo.com>', 'user@localhost' ],
251
195
            'subject')
252
196
 
253
 
        from_, to = smtp_connection.SMTPConnection.get_message_addresses(msg)
 
197
        from_, to = SMTPConnection.get_message_addresses(msg)
254
198
        self.assertEqual('jrandom@example.com', from_)
255
199
        self.assertEqual(sorted(['john@doe.com', 'jane@doe.com',
256
200
            'pperez@ejemplo.com', 'user@localhost']), sorted(to))
262
206
 
263
207
        msg = Message()
264
208
        msg['From'] = '"J. Random Developer" <jrandom@example.com>'
265
 
        self.assertRaises(
266
 
            errors.NoDestinationAddress,
267
 
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
268
 
 
269
 
        msg = email_message.EmailMessage('from@from.com', '', 'subject')
270
 
        self.assertRaises(
271
 
            errors.NoDestinationAddress,
272
 
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
273
 
 
274
 
        msg = email_message.EmailMessage('from@from.com', [], 'subject')
275
 
        self.assertRaises(
276
 
            errors.NoDestinationAddress,
277
 
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
 
209
        self.assertRaises(NoDestinationAddress,
 
210
                SMTPConnection(FakeConfig()).send_email, msg)
 
211
 
 
212
        msg = EmailMessage('from@from.com', '', 'subject')
 
213
        self.assertRaises(NoDestinationAddress,
 
214
                SMTPConnection(FakeConfig()).send_email, msg)
 
215
 
 
216
        msg = EmailMessage('from@from.com', [], 'subject')
 
217
        self.assertRaises(NoDestinationAddress,
 
218
                SMTPConnection(FakeConfig()).send_email, msg)