~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smtp_connection.py

Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.

This is used to replace various ad hoc implementations of the same logic,
notably the version used in registry's _LazyObjectGetter which had a bug when
getting a module without also getting a member.  And of course, this new
function has unit tests, unlike the replaced code.

This also adds a KnownHooksRegistry subclass to provide a more natural home for
some other logic.

I'm not thrilled about the name of the new module or the new functions, but it's
hard to think of good names for such generic functionality.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2007, 2009 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
91
91
class TestSMTPConnection(tests.TestCaseInTempDir):
92
92
 
93
93
    def get_connection(self, text, smtp_factory=None):
94
 
        my_config = config.GlobalConfig()
95
 
        config_file = StringIO(text)
96
 
        my_config._get_parser(config_file)
 
94
        my_config = config.GlobalConfig.from_string(text)
97
95
        return smtp_connection.SMTPConnection(my_config,
98
96
                                              _smtp_factory=smtp_factory)
99
97
 
160
158
 
161
159
    def test_authenticate_with_byte_strings(self):
162
160
        user = 'joe'
163
 
        password = 'h\xC3\xACspass'
 
161
        unicode_pass = u'h\xECspass'
 
162
        utf8_pass = unicode_pass.encode('utf-8')
164
163
        factory = WideOpenSMTPFactory()
165
164
        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)
 
165
            u'[DEFAULT]\nsmtp_username=%s\nsmtp_password=%s\n'
 
166
            % (user, unicode_pass), smtp_factory=factory)
 
167
        self.assertEqual(unicode_pass, conn._smtp_password)
169
168
        conn._connect()
170
169
        self.assertEqual([('connect', 'localhost'),
171
170
                          ('ehlo',),
172
171
                          ('has_extn', 'starttls'),
173
 
                          ('login', user, password)], factory._calls)
 
172
                          ('login', user, utf8_pass)], factory._calls)
174
173
        smtp_username, smtp_password = factory._calls[-1][1:]
175
174
        self.assertIsInstance(smtp_username, str)
176
175
        self.assertIsInstance(smtp_password, str)