~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smtp_connection.py

  • Committer: Aaron Bentley
  • Date: 2007-08-10 16:17:30 UTC
  • mto: This revision was merged to the branch mainline in revision 2696.
  • Revision ID: abentley@panoramicfeedback.com-20070810161730-fda8tva0jxraklk4
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)