~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-08-13 12:24:44 UTC
  • mfrom: (2694.2.2 no-local-smtp)
  • Revision ID: pqm@pqm.ubuntu.com-20070813122444-5pi8f4fwxqpgxs1x
Make error handling nicer when SMTP server not working

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
from cStringIO import StringIO
18
18
from email.Message import Message
 
19
import errno
 
20
import smtplib
 
21
import socket
19
22
 
20
 
from bzrlib import config
 
23
from bzrlib import (
 
24
    config,
 
25
    errors,
 
26
    )
21
27
from bzrlib.email_message import EmailMessage
22
28
from bzrlib.errors import NoDestinationAddress
23
29
from bzrlib.tests import TestCase
24
30
from bzrlib.smtp_connection import SMTPConnection
25
31
 
26
32
 
 
33
def connection_refuser():
 
34
    def connect(server):
 
35
        raise socket.error(errno.ECONNREFUSED, 'Connection Refused')
 
36
    smtp = smtplib.SMTP()
 
37
    smtp.connect = connect
 
38
    return smtp
 
39
 
 
40
 
27
41
class TestSMTPConnection(TestCase):
28
42
 
29
 
    def get_connection(self, text):
 
43
    def get_connection(self, text, smtp_factory=None):
30
44
        my_config = config.GlobalConfig()
31
45
        config_file = StringIO(text)
32
46
        my_config._get_parser(config_file)
33
 
        return SMTPConnection(my_config)
 
47
        return SMTPConnection(my_config, _smtp_factory=smtp_factory)
34
48
 
35
49
    def test_defaults(self):
36
50
        conn = self.get_connection('')
42
56
        conn = self.get_connection('[DEFAULT]\nsmtp_server=host:10\n')
43
57
        self.assertEqual('host:10', conn._smtp_server)
44
58
 
 
59
    def test_missing_server(self):
 
60
        conn = self.get_connection('', smtp_factory=connection_refuser)
 
61
        self.assertRaises(errors.DefaultSMTPConnectionRefused, conn._connect)
 
62
        conn = self.get_connection('[DEFAULT]\nsmtp_server=smtp.example.com\n',
 
63
                                   smtp_factory=connection_refuser)
 
64
        self.assertRaises(errors.SMTPConnectionRefused, conn._connect)
 
65
 
45
66
    def test_smtp_username(self):
46
67
        conn = self.get_connection('')
47
68
        self.assertIs(None, conn._smtp_username)