~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smtp_connection.py

(vila) Forbid more operations on ReadonlyTransportDecorator (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007, 2009, 2010 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.from_string(text)
95
 
        return smtp_connection.SMTPConnection(my_config,
96
 
                                              _smtp_factory=smtp_factory)
 
93
        my_config = config.MemoryStack(text)
 
94
        return smtp_connection.SMTPConnection(
 
95
            my_config, _smtp_factory=smtp_factory)
97
96
 
98
97
    def test_defaults(self):
99
98
        conn = self.get_connection('')
102
101
        self.assertEqual(None, conn._smtp_password)
103
102
 
104
103
    def test_smtp_server(self):
105
 
        conn = self.get_connection('[DEFAULT]\nsmtp_server=host:10\n')
 
104
        conn = self.get_connection('smtp_server=host:10')
106
105
        self.assertEqual('host:10', conn._smtp_server)
107
106
 
108
107
    def test_missing_server(self):
109
108
        conn = self.get_connection('', smtp_factory=connection_refuser)
110
109
        self.assertRaises(errors.DefaultSMTPConnectionRefused, conn._connect)
111
 
        conn = self.get_connection('[DEFAULT]\nsmtp_server=smtp.example.com\n',
 
110
        conn = self.get_connection('smtp_server=smtp.example.com',
112
111
                                   smtp_factory=connection_refuser)
113
112
        self.assertRaises(errors.SMTPConnectionRefused, conn._connect)
114
113
 
116
115
        conn = self.get_connection('')
117
116
        self.assertIs(None, conn._smtp_username)
118
117
 
119
 
        conn = self.get_connection('[DEFAULT]\nsmtp_username=joebody\n')
 
118
        conn = self.get_connection('smtp_username=joebody')
120
119
        self.assertEqual(u'joebody', conn._smtp_username)
121
120
 
122
121
    def test_smtp_password_from_config(self):
123
122
        conn = self.get_connection('')
124
123
        self.assertIs(None, conn._smtp_password)
125
124
 
126
 
        conn = self.get_connection('[DEFAULT]\nsmtp_password=mypass\n')
 
125
        conn = self.get_connection('smtp_password=mypass')
127
126
        self.assertEqual(u'mypass', conn._smtp_password)
128
127
 
129
128
    def test_smtp_password_from_user(self):
162
161
        utf8_pass = unicode_pass.encode('utf-8')
163
162
        factory = WideOpenSMTPFactory()
164
163
        conn = self.get_connection(
165
 
            u'[DEFAULT]\nsmtp_username=%s\nsmtp_password=%s\n'
166
 
            % (user, unicode_pass), smtp_factory=factory)
 
164
            '[DEFAULT]\nsmtp_username=%s\nsmtp_password=%s\n'
 
165
            % (user, utf8_pass), smtp_factory=factory)
167
166
        self.assertEqual(unicode_pass, conn._smtp_password)
168
167
        conn._connect()
169
168
        self.assertEqual([('connect', 'localhost'),
255
254
            'pperez@ejemplo.com', 'user@localhost']), sorted(to))
256
255
 
257
256
    def test_destination_address_required(self):
258
 
        class FakeConfig:
259
 
            def get_user_option(self, option):
260
 
                return None
261
 
 
262
257
        msg = Message()
263
258
        msg['From'] = '"J. Random Developer" <jrandom@example.com>'
264
259
        self.assertRaises(
265
260
            errors.NoDestinationAddress,
266
 
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
 
261
            smtp_connection.SMTPConnection(config.MemoryStack("")
 
262
                                           ).send_email, msg)
267
263
 
268
264
        msg = email_message.EmailMessage('from@from.com', '', 'subject')
269
265
        self.assertRaises(
270
266
            errors.NoDestinationAddress,
271
 
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
 
267
            smtp_connection.SMTPConnection(config.MemoryStack("")
 
268
                                           ).send_email, msg)
272
269
 
273
270
        msg = email_message.EmailMessage('from@from.com', [], 'subject')
274
271
        self.assertRaises(
275
272
            errors.NoDestinationAddress,
276
 
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
 
273
            smtp_connection.SMTPConnection(config.MemoryStack("")
 
274
                                           ).send_email, msg)