~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smtp_connection.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-11-04 18:51:39 UTC
  • mfrom: (2961.1.1 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20071104185139-kaio3sneodg2kp71
Authentication ring implementation (read-only)

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import errno
20
20
import smtplib
21
21
import socket
 
22
import sys
22
23
 
23
24
from bzrlib import (
24
25
    config,
 
26
    email_message,
25
27
    errors,
 
28
    smtp_connection,
 
29
    tests,
 
30
    ui,
26
31
    )
27
 
from bzrlib.email_message import EmailMessage
28
 
from bzrlib.errors import NoDestinationAddress
29
 
from bzrlib.tests import TestCase
30
 
from bzrlib.smtp_connection import SMTPConnection
31
32
 
32
33
 
33
34
def connection_refuser():
81
82
            return 200, 'starttls success'
82
83
 
83
84
 
84
 
class TestSMTPConnection(TestCase):
 
85
class WideOpenSMTPFactory(StubSMTPFactory):
 
86
    """A fake smtp server that implements login by accepting anybody."""
 
87
 
 
88
    def login(self, user, password):
 
89
        pass
 
90
 
 
91
 
 
92
class TestSMTPConnection(tests.TestCaseInTempDir):
85
93
 
86
94
    def get_connection(self, text, smtp_factory=None):
87
95
        my_config = config.GlobalConfig()
88
96
        config_file = StringIO(text)
89
97
        my_config._get_parser(config_file)
90
 
        return SMTPConnection(my_config, _smtp_factory=smtp_factory)
 
98
        return smtp_connection.SMTPConnection(my_config,
 
99
                                              _smtp_factory=smtp_factory)
91
100
 
92
101
    def test_defaults(self):
93
102
        conn = self.get_connection('')
113
122
        conn = self.get_connection('[DEFAULT]\nsmtp_username=joebody\n')
114
123
        self.assertEqual(u'joebody', conn._smtp_username)
115
124
 
116
 
    def test_smtp_password(self):
 
125
    def test_smtp_password_from_config(self):
117
126
        conn = self.get_connection('')
118
127
        self.assertIs(None, conn._smtp_password)
119
128
 
120
129
        conn = self.get_connection('[DEFAULT]\nsmtp_password=mypass\n')
121
130
        self.assertEqual(u'mypass', conn._smtp_password)
122
131
 
 
132
    def test_smtp_password_from_user(self):
 
133
        user = 'joe'
 
134
        password = 'hispass'
 
135
        factory = WideOpenSMTPFactory()
 
136
        conn = self.get_connection('[DEFAULT]\nsmtp_username=%s\n' % user,
 
137
                                   smtp_factory=factory)
 
138
        self.assertIs(None, conn._smtp_password)
 
139
 
 
140
        ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
 
141
                                            stdout=tests.StringIOWrapper())
 
142
        conn._connect()
 
143
        self.assertEqual(password, conn._smtp_password)
 
144
        # stdin should be empty (the provided password have been consumed)
 
145
        self.assertEqual('', ui.ui_factory.stdin.readline())
 
146
 
 
147
    def test_smtp_password_from_auth_config(self):
 
148
        user = 'joe'
 
149
        password = 'hispass'
 
150
        factory = WideOpenSMTPFactory()
 
151
        conn = self.get_connection('[DEFAULT]\nsmtp_username=%s\n' % user,
 
152
                                   smtp_factory=factory)
 
153
        self.assertEqual(user, conn._smtp_username)
 
154
        self.assertIs(None, conn._smtp_password)
 
155
        # Create a config file with the right password
 
156
        conf = config.AuthenticationConfig()
 
157
        conf._get_config().update({'smtptest':
 
158
                                       {'scheme': 'smtp', 'user':user,
 
159
                                        'password': password}})
 
160
        conf._save()
 
161
 
 
162
        conn._connect()
 
163
        self.assertEqual(password, conn._smtp_password)
 
164
 
123
165
    def test_create_connection(self):
124
166
        factory = StubSMTPFactory()
125
167
        conn = self.get_connection('', smtp_factory=factory)
174
216
    def test_get_message_addresses(self):
175
217
        msg = Message()
176
218
 
177
 
        from_, to = SMTPConnection.get_message_addresses(msg)
 
219
        from_, to = smtp_connection.SMTPConnection.get_message_addresses(msg)
178
220
        self.assertEqual('', from_)
179
221
        self.assertEqual([], to)
180
222
 
183
225
        msg['CC'] = u'Pepe P\xe9rez <pperez@ejemplo.com>'
184
226
        msg['Bcc'] = 'user@localhost'
185
227
 
186
 
        from_, to = SMTPConnection.get_message_addresses(msg)
 
228
        from_, to = smtp_connection.SMTPConnection.get_message_addresses(msg)
187
229
        self.assertEqual('jrandom@example.com', from_)
188
230
        self.assertEqual(sorted(['john@doe.com', 'jane@doe.com',
189
231
            'pperez@ejemplo.com', 'user@localhost']), sorted(to))
190
232
 
191
233
        # now with bzrlib's EmailMessage
192
 
        msg = EmailMessage('"J. Random Developer" <jrandom@example.com>', [
193
 
            'John Doe <john@doe.com>', 'Jane Doe <jane@doe.com>',
194
 
            u'Pepe P\xe9rez <pperez@ejemplo.com>', 'user@localhost' ],
 
234
        msg = email_message.EmailMessage(
 
235
            '"J. Random Developer" <jrandom@example.com>',
 
236
            ['John Doe <john@doe.com>', 'Jane Doe <jane@doe.com>',
 
237
             u'Pepe P\xe9rez <pperez@ejemplo.com>', 'user@localhost' ],
195
238
            'subject')
196
239
 
197
 
        from_, to = SMTPConnection.get_message_addresses(msg)
 
240
        from_, to = smtp_connection.SMTPConnection.get_message_addresses(msg)
198
241
        self.assertEqual('jrandom@example.com', from_)
199
242
        self.assertEqual(sorted(['john@doe.com', 'jane@doe.com',
200
243
            'pperez@ejemplo.com', 'user@localhost']), sorted(to))
206
249
 
207
250
        msg = Message()
208
251
        msg['From'] = '"J. Random Developer" <jrandom@example.com>'
209
 
        self.assertRaises(NoDestinationAddress,
210
 
                SMTPConnection(FakeConfig()).send_email, msg)
211
 
 
212
 
        msg = EmailMessage('from@from.com', '', 'subject')
213
 
        self.assertRaises(NoDestinationAddress,
214
 
                SMTPConnection(FakeConfig()).send_email, msg)
215
 
 
216
 
        msg = EmailMessage('from@from.com', [], 'subject')
217
 
        self.assertRaises(NoDestinationAddress,
218
 
                SMTPConnection(FakeConfig()).send_email, msg)
 
252
        self.assertRaises(
 
253
            errors.NoDestinationAddress,
 
254
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
 
255
 
 
256
        msg = email_message.EmailMessage('from@from.com', '', 'subject')
 
257
        self.assertRaises(
 
258
            errors.NoDestinationAddress,
 
259
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
 
260
 
 
261
        msg = email_message.EmailMessage('from@from.com', [], 'subject')
 
262
        self.assertRaises(
 
263
            errors.NoDestinationAddress,
 
264
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)