~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smtp_connection.py

(jelmer) Convert bzrlib.smtp_connection to use config stacks. (Jelmer
 Vernooij)

Show diffs side-by-side

added added

removed removed

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