~barry/mailman/lp1423756

« back to all changes in this revision

Viewing changes to src/mailman/runners/tests/test_nntp.py

  • Committer: Barry Warsaw
  • Date: 2015-01-05 01:20:33 UTC
  • mfrom: (7264.4.66 py3)
  • Revision ID: barry@list.org-20150105012033-zdrw9c2odhpf22fz
Merge the Python 3 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""Test the NNTP runner and related utilities."""
19
19
 
20
 
from __future__ import absolute_import, print_function, unicode_literals
21
 
 
22
 
__metaclass__ = type
23
20
__all__ = [
24
21
    'TestPrepareMessage',
25
22
    'TestNNTPRunner',
36
33
from mailman.interfaces.nntp import NewsgroupModeration
37
34
from mailman.runners import nntp
38
35
from mailman.testing.helpers import (
39
 
    LogFileMark,
40
 
    configuration,
41
 
    get_queue_messages,
42
 
    make_testable_runner,
 
36
    LogFileMark, configuration, get_queue_messages, make_testable_runner,
43
37
    specialized_message_from_string as mfs)
44
38
from mailman.testing.layers import ConfigLayer
45
39
 
257
251
    @mock.patch('nntplib.NNTP')
258
252
    def test_connect(self, class_mock):
259
253
        # Test connection to the NNTP server with default values.
260
 
        self._nntpq.enqueue(self._msg, {}, listname='test@example.com')
 
254
        self._nntpq.enqueue(self._msg, {}, listid='test.example.com')
261
255
        self._runner.run()
262
256
        class_mock.assert_called_once_with(
263
257
            '', 119, user='', password='', readermode=True)
267
261
    @mock.patch('nntplib.NNTP')
268
262
    def test_connect_with_configuration(self, class_mock):
269
263
        # Test connection to the NNTP server with specific values.
270
 
        self._nntpq.enqueue(self._msg, {}, listname='test@example.com')
 
264
        self._nntpq.enqueue(self._msg, {}, listid='test.example.com')
271
265
        self._runner.run()
272
266
        class_mock.assert_called_once_with(
273
267
            'nntp.example.com', 2112,
276
270
    @mock.patch('nntplib.NNTP')
277
271
    def test_post(self, class_mock):
278
272
        # Test that the message is posted to the NNTP server.
279
 
        self._nntpq.enqueue(self._msg, {}, listname='test@example.com')
 
273
        self._nntpq.enqueue(self._msg, {}, listid='test.example.com')
280
274
        self._runner.run()
281
275
        # Get the mocked instance, which was used in the runner.
282
276
        conn_mock = class_mock()
295
289
    def test_connection_got_quit(self, class_mock):
296
290
        # The NNTP connection gets closed after a successful post.
297
291
        # Test that the message is posted to the NNTP server.
298
 
        self._nntpq.enqueue(self._msg, {}, listname='test@example.com')
 
292
        self._nntpq.enqueue(self._msg, {}, listid='test.example.com')
299
293
        self._runner.run()
300
294
        # Get the mocked instance, which was used in the runner.
301
295
        conn_mock = class_mock()
304
298
        # and make some simple checks that the message is what we expected.
305
299
        conn_mock.quit.assert_called_once_with()
306
300
 
307
 
    @mock.patch('nntplib.NNTP', side_effect=nntplib.error_temp)
 
301
    @mock.patch('nntplib.NNTP', side_effect=nntplib.NNTPTemporaryError)
308
302
    def test_connect_with_nntplib_failure(self, class_mock):
309
 
        self._nntpq.enqueue(self._msg, {}, listname='test@example.com')
 
303
        self._nntpq.enqueue(self._msg, {}, listid='test.example.com')
310
304
        mark = LogFileMark('mailman.error')
311
305
        self._runner.run()
312
306
        log_message = mark.readline()[:-1]
313
 
        self.assertTrue(log_message.endswith(
314
 
            'NNTP error for test@example.com'))
 
307
        self.assertTrue(
 
308
            log_message.endswith('NNTP error for test@example.com'),
 
309
            log_message)
315
310
 
316
311
    @mock.patch('nntplib.NNTP', side_effect=socket.error)
317
312
    def test_connect_with_socket_failure(self, class_mock):
318
 
        self._nntpq.enqueue(self._msg, {}, listname='test@example.com')
 
313
        self._nntpq.enqueue(self._msg, {}, listid='test.example.com')
319
314
        mark = LogFileMark('mailman.error')
320
315
        self._runner.run()
321
316
        log_message = mark.readline()[:-1]
330
325
            # I.e. stop immediately, since the queue will not be empty.
331
326
            return True
332
327
        runner = make_testable_runner(nntp.NNTPRunner, 'nntp', predicate=once)
333
 
        self._nntpq.enqueue(self._msg, {}, listname='test@example.com')
 
328
        self._nntpq.enqueue(self._msg, {}, listid='test.example.com')
334
329
        mark = LogFileMark('mailman.error')
335
330
        runner.run()
336
331
        log_message = mark.readline()[:-1]
338
333
            'NNTP unexpected exception for test@example.com'))
339
334
        messages = get_queue_messages('nntp')
340
335
        self.assertEqual(len(messages), 1)
341
 
        self.assertEqual(messages[0].msgdata['listname'], 'test@example.com')
 
336
        self.assertEqual(messages[0].msgdata['listid'], 'test.example.com')
342
337
        self.assertEqual(messages[0].msg['subject'], 'A newsgroup posting')
343
338
 
344
 
    @mock.patch('nntplib.NNTP', side_effect=nntplib.error_temp)
 
339
    @mock.patch('nntplib.NNTP', side_effect=nntplib.NNTPTemporaryError)
345
340
    def test_connection_never_gets_quit_after_failures(self, class_mock):
346
341
        # The NNTP connection doesn't get closed after a unsuccessful
347
342
        # connection, since there's nothing to close.
348
 
        self._nntpq.enqueue(self._msg, {}, listname='test@example.com')
 
343
        self._nntpq.enqueue(self._msg, {}, listid='test.example.com')
349
344
        self._runner.run()
350
345
        # Get the mocked instance, which was used in the runner.  Turn off the
351
346
        # exception raising side effect first though!
361
356
        # The NNTP connection does get closed after a unsuccessful post.
362
357
        # Add a side-effect to the instance mock's .post() method.
363
358
        conn_mock = class_mock()
364
 
        conn_mock.post.side_effect = nntplib.error_temp
365
 
        self._nntpq.enqueue(self._msg, {}, listname='test@example.com')
 
359
        conn_mock.post.side_effect = nntplib.NNTPTemporaryError
 
360
        self._nntpq.enqueue(self._msg, {}, listid='test.example.com')
366
361
        self._runner.run()
367
362
        # The connection object's post() method was called once with a
368
363
        # file-like object containing the message's bytes.  Read those bytes