~mars/launchpad/ghost-line

« back to all changes in this revision

Viewing changes to lib/lp/services/mailman/tests/test_lpsize.py

  • Committer: Tarmac
  • Author(s): Jelmer Vernooij, Henning Eggers, Aaron Bentley, Stuart Bishop, Graham Binns, Tim Penhey, Launchpad Patch Queue Manager, Gary Poster, William Grant, Edwin Grubbs, Robert Collins, j.c.sackett, Abel Deuring, Jonathan Lange, Jeroen Vermeulen, Julian Edwards, Curtis Hovey, Paul Hummer, Brian Murray, Maris Fogels, Danilo Segan, Deryck Hodge, Bryce Harrington, James Westby, Ian Booth, Gavin Panella, LaMont Jones, Danilo Šegan, Francis J. Lacoste, Benji York, Guilherme Salgado, Brad Crittenden, James Westby, Michael Nelson, Henning Eggers, Steve Kowalik, Michael Hudson, Diogo Matsubara, Steve Kowalik, Leonard Richardson, John Arbash Meinel, Ursula Junque, Andrew Mitchell, Anthony Lenton, Martin Pool, Colin Watson, Matthew Revell
  • Date: 2010-12-16 02:19:53 UTC
  • mfrom: (11782.13.2 test-ghost-update)
  • Revision ID: tarmac@sapote-20101216021953-24h56by3bwd0yrx6
[ui=none][r=mars][no-qa] Update to the latest devel

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
"""Test the lpsize monekypatches"""
 
4
 
 
5
from __future__ import with_statement
 
6
 
 
7
__metaclass__ = type
 
8
__all__ = []
 
9
 
 
10
 
 
11
from email.mime.application import MIMEApplication
 
12
 
 
13
from zope.security.proxy import removeSecurityProxy
 
14
 
 
15
from Mailman import Errors
 
16
from Mailman.Handlers import LPSize
 
17
 
 
18
from canonical.config import config
 
19
from canonical.testing import (
 
20
    DatabaseFunctionalLayer,
 
21
    LaunchpadFunctionalLayer,
 
22
    )
 
23
from lp.services.mailman.testing import MailmanTestCase
 
24
 
 
25
 
 
26
class TestLPSizeTestCase(MailmanTestCase):
 
27
    """Test LPSize.
 
28
 
 
29
    Mailman process() methods quietly return. They may set msg_data key-values
 
30
    or raise an error to end processing. These tests often check for errors,
 
31
    but that does not mean there is an error condition, it only means message
 
32
    processing has reached a final decision. Messages that do not cause a
 
33
    final decision pass through, and the process() methods ends without a
 
34
    return.
 
35
    """
 
36
 
 
37
    layer = LaunchpadFunctionalLayer
 
38
 
 
39
    def setUp(self):
 
40
        super(TestLPSizeTestCase, self).setUp()
 
41
        self.team, self.mailing_list = self.factory.makeTeamAndMailingList(
 
42
            'team-1', 'team-1-owner')
 
43
        self.mm_list = self.makeMailmanList(self.mailing_list)
 
44
        self.subscriber_email = removeSecurityProxy(
 
45
            self.team.teamowner.preferredemail).email
 
46
 
 
47
    def tearDown(self):
 
48
        super(TestLPSizeTestCase, self).tearDown()
 
49
        self.cleanMailmanList(self.mm_list)
 
50
 
 
51
    def test_process_size_under_soft_limit(self):
 
52
        # Any message under 40kb is sent to the list.
 
53
        attachment = MIMEApplication(
 
54
            '\n'.join(['x' * 20] * 1000), 'octet-stream')
 
55
        message = self.makeMailmanMessage(
 
56
            self.mm_list, self.subscriber_email, 'subject', 'content',
 
57
            attachment=attachment)
 
58
        msg_data = {}
 
59
        silence = LPSize.process(self.mm_list, message, msg_data)
 
60
        self.assertEqual(None, silence)
 
61
 
 
62
    def test_process_size_over_soft_limit_held(self):
 
63
        # Messages over 40kb held for moderation.
 
64
        self.assertEqual(40000, config.mailman.soft_max_size)
 
65
        attachment = MIMEApplication(
 
66
            '\n'.join(['x' * 40] * 1000), 'octet-stream')
 
67
        message = self.makeMailmanMessage(
 
68
            self.mm_list, self.subscriber_email, 'subject', 'content',
 
69
            attachment=attachment)
 
70
        msg_data = {}
 
71
        args = (self.mm_list, message, msg_data)
 
72
        self.assertRaises(
 
73
            Errors.HoldMessage, LPSize.process, *args)
 
74
        self.assertEqual(1, self.mailing_list.getReviewableMessages().count())
 
75
 
 
76
    def test_process_size_over_hard_limit_discarded(self):
 
77
        # Messages over 1MB are discarded.
 
78
        self.assertEqual(1000000, config.mailman.hard_max_size)
 
79
        attachment = MIMEApplication(
 
80
            '\n'.join(['x' * 1000] * 1000), 'octet-stream')
 
81
        message = self.makeMailmanMessage(
 
82
            self.mm_list, self.subscriber_email, 'subject', 'content',
 
83
            attachment=attachment)
 
84
        msg_data = {}
 
85
        args = (self.mm_list, message, msg_data)
 
86
        self.assertRaises(
 
87
            Errors.DiscardMessage, LPSize.process, *args)
 
88
        self.assertEqual(0, self.mailing_list.getReviewableMessages().count())
 
89
 
 
90
 
 
91
class TestTruncatedMessage(MailmanTestCase):
 
92
    """Test truncated_message helper."""
 
93
 
 
94
    layer = DatabaseFunctionalLayer
 
95
 
 
96
    def setUp(self):
 
97
        super(TestTruncatedMessage, self).setUp()
 
98
        self.team, self.mailing_list = self.factory.makeTeamAndMailingList(
 
99
            'team-1', 'team-1-owner')
 
100
        self.mm_list = self.makeMailmanList(self.mailing_list)
 
101
        self.subscriber_email = removeSecurityProxy(
 
102
            self.team.teamowner.preferredemail).email
 
103
 
 
104
    def test_attchments_are_removed(self):
 
105
        # Plain-text and multipart are preserved, everything else is removed.
 
106
        attachment = MIMEApplication('binary gibberish', 'octet-stream')
 
107
        message = self.makeMailmanMessage(
 
108
            self.mm_list, self.subscriber_email, 'subject', 'content',
 
109
            attachment=attachment)
 
110
        moderated_message = LPSize.truncated_message(message)
 
111
        parts = [part for part in moderated_message.walk()]
 
112
        types = [part.get_content_type() for part in parts]
 
113
        self.assertEqual(['multipart/mixed', 'text/plain'], types)
 
114
 
 
115
    def test_small_text_is_preserved(self):
 
116
        # Text parts below the limit are unchanged.
 
117
        message = self.makeMailmanMessage(
 
118
            self.mm_list, self.subscriber_email, 'subject', 'content')
 
119
        moderated_message = LPSize.truncated_message(message, limit=1000)
 
120
        parts = [part for part in moderated_message.walk()]
 
121
        types = [part.get_content_type() for part in parts]
 
122
        self.assertEqual(['multipart/mixed', 'text/plain'], types)
 
123
        self.assertEqual('content', parts[1].get_payload())
 
124
 
 
125
    def test_large_text_is_truncated(self):
 
126
        # Text parts above the limit are truncated.
 
127
        message = self.makeMailmanMessage(
 
128
            self.mm_list, self.subscriber_email, 'subject', 'content excess')
 
129
        moderated_message = LPSize.truncated_message(message, limit=7)
 
130
        parts = [part for part in moderated_message.walk()]
 
131
        types = [part.get_content_type() for part in parts]
 
132
        self.assertEqual(['multipart/mixed', 'text/plain'], types)
 
133
        self.assertEqual(
 
134
            'content\n[truncated for moderation]', parts[1].get_payload())