~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smtp_connection.py

  • Committer: Jelmer Vernooij
  • Date: 2011-12-16 16:40:10 UTC
  • mto: This revision was merged to the branch mainline in revision 6391.
  • Revision ID: jelmer@samba.org-20111216164010-z3hy00xrnclnkf7a
Update tests.

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
88
87
        self._calls.append(('login', user, password))
89
88
 
90
89
 
 
90
class StringStack(config.Stack):
 
91
 
 
92
    def __init__(self, text):
 
93
        store = config.IniFileStore()
 
94
        store._load_from_string(text)
 
95
        super(StringStack, self).__init__([store.get_sections])
 
96
 
 
97
 
91
98
class TestSMTPConnection(tests.TestCaseInTempDir):
92
99
 
93
100
    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)
 
101
        my_config = StringStack(text)
 
102
        return smtp_connection.SMTPConnection(
 
103
            my_config, _smtp_factory=smtp_factory)
97
104
 
98
105
    def test_defaults(self):
99
106
        conn = self.get_connection('')
162
169
        utf8_pass = unicode_pass.encode('utf-8')
163
170
        factory = WideOpenSMTPFactory()
164
171
        conn = self.get_connection(
165
 
            u'[DEFAULT]\nsmtp_username=%s\nsmtp_password=%s\n'
166
 
            % (user, unicode_pass), smtp_factory=factory)
 
172
            '[DEFAULT]\nsmtp_username=%s\nsmtp_password=%s\n'
 
173
            % (user, utf8_pass), smtp_factory=factory)
167
174
        self.assertEqual(unicode_pass, conn._smtp_password)
168
175
        conn._connect()
169
176
        self.assertEqual([('connect', 'localhost'),
255
262
            'pperez@ejemplo.com', 'user@localhost']), sorted(to))
256
263
 
257
264
    def test_destination_address_required(self):
258
 
        class FakeConfig:
259
 
            def get_user_option(self, option):
260
 
                return None
261
 
 
262
265
        msg = Message()
263
266
        msg['From'] = '"J. Random Developer" <jrandom@example.com>'
264
267
        self.assertRaises(
265
268
            errors.NoDestinationAddress,
266
 
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
 
269
            smtp_connection.SMTPConnection(StringStack("")).send_email, msg)
267
270
 
268
271
        msg = email_message.EmailMessage('from@from.com', '', 'subject')
269
272
        self.assertRaises(
270
273
            errors.NoDestinationAddress,
271
 
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
 
274
            smtp_connection.SMTPConnection(StringStack("")).send_email, msg)
272
275
 
273
276
        msg = email_message.EmailMessage('from@from.com', [], 'subject')
274
277
        self.assertRaises(
275
278
            errors.NoDestinationAddress,
276
 
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
 
279
            smtp_connection.SMTPConnection(StringStack("")).send_email, msg)