~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smtp_connection.py

  • Committer: Vincent Ladeuil
  • Date: 2011-12-21 14:25:26 UTC
  • mto: This revision was merged to the branch mainline in revision 6397.
  • Revision ID: v.ladeuil+lp@free.fr-20111221142526-pnwau0xnalimujts
Provides MemoryStack to simplify configuration setup in tests

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
 
 
98
90
class TestSMTPConnection(tests.TestCaseInTempDir):
99
91
 
100
92
    def get_connection(self, text, smtp_factory=None):
101
 
        my_config = StringStack(text)
 
93
        my_config = config.MemoryStack(text)
102
94
        return smtp_connection.SMTPConnection(
103
95
            my_config, _smtp_factory=smtp_factory)
104
96
 
109
101
        self.assertEqual(None, conn._smtp_password)
110
102
 
111
103
    def test_smtp_server(self):
112
 
        conn = self.get_connection('[DEFAULT]\nsmtp_server=host:10\n')
 
104
        conn = self.get_connection('smtp_server=host:10')
113
105
        self.assertEqual('host:10', conn._smtp_server)
114
106
 
115
107
    def test_missing_server(self):
116
108
        conn = self.get_connection('', smtp_factory=connection_refuser)
117
109
        self.assertRaises(errors.DefaultSMTPConnectionRefused, conn._connect)
118
 
        conn = self.get_connection('[DEFAULT]\nsmtp_server=smtp.example.com\n',
 
110
        conn = self.get_connection('smtp_server=smtp.example.com',
119
111
                                   smtp_factory=connection_refuser)
120
112
        self.assertRaises(errors.SMTPConnectionRefused, conn._connect)
121
113
 
123
115
        conn = self.get_connection('')
124
116
        self.assertIs(None, conn._smtp_username)
125
117
 
126
 
        conn = self.get_connection('[DEFAULT]\nsmtp_username=joebody\n')
 
118
        conn = self.get_connection('smtp_username=joebody')
127
119
        self.assertEqual(u'joebody', conn._smtp_username)
128
120
 
129
121
    def test_smtp_password_from_config(self):
130
122
        conn = self.get_connection('')
131
123
        self.assertIs(None, conn._smtp_password)
132
124
 
133
 
        conn = self.get_connection('[DEFAULT]\nsmtp_password=mypass\n')
 
125
        conn = self.get_connection('smtp_password=mypass')
134
126
        self.assertEqual(u'mypass', conn._smtp_password)
135
127
 
136
128
    def test_smtp_password_from_user(self):
266
258
        msg['From'] = '"J. Random Developer" <jrandom@example.com>'
267
259
        self.assertRaises(
268
260
            errors.NoDestinationAddress,
269
 
            smtp_connection.SMTPConnection(StringStack("")).send_email, msg)
 
261
            smtp_connection.SMTPConnection(config.MemoryStack("")
 
262
                                           ).send_email, msg)
270
263
 
271
264
        msg = email_message.EmailMessage('from@from.com', '', 'subject')
272
265
        self.assertRaises(
273
266
            errors.NoDestinationAddress,
274
 
            smtp_connection.SMTPConnection(StringStack("")).send_email, msg)
 
267
            smtp_connection.SMTPConnection(config.MemoryStack("")
 
268
                                           ).send_email, msg)
275
269
 
276
270
        msg = email_message.EmailMessage('from@from.com', [], 'subject')
277
271
        self.assertRaises(
278
272
            errors.NoDestinationAddress,
279
 
            smtp_connection.SMTPConnection(StringStack("")).send_email, msg)
 
273
            smtp_connection.SMTPConnection(config.MemoryStack("")
 
274
                                           ).send_email, msg)