~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smtp_connection.py

  • Committer: Andrew Bennetts
  • Date: 2010-10-08 08:15:14 UTC
  • mto: This revision was merged to the branch mainline in revision 5498.
  • Revision ID: andrew.bennetts@canonical.com-20101008081514-dviqzrdfwyzsqbz2
Split NEWS into per-release doc/en/release-notes/bzr-*.txt

Show diffs side-by-side

added added

removed removed

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