summaryrefslogtreecommitdiff
path: root/src/mailman/handlers/tests/test_mimedel.py
diff options
context:
space:
mode:
authorBarry Warsaw2015-06-02 13:51:58 +0000
committerBarry Warsaw2015-06-02 10:09:24 -0400
commite57787d8f6ff0cc8b1e30f2531a56eaf9a28511a (patch)
tree8deb9773fe8e3871b5859a866a70019d92a9a3cb /src/mailman/handlers/tests/test_mimedel.py
parentf0f13923af208d2eab97b6b304b77e9d5a55cc5a (diff)
downloadmailman-e57787d8f6ff0cc8b1e30f2531a56eaf9a28511a.tar.gz
mailman-e57787d8f6ff0cc8b1e30f2531a56eaf9a28511a.tar.zst
mailman-e57787d8f6ff0cc8b1e30f2531a56eaf9a28511a.zip
Diffstat (limited to 'src/mailman/handlers/tests/test_mimedel.py')
-rw-r--r--src/mailman/handlers/tests/test_mimedel.py66
1 files changed, 61 insertions, 5 deletions
diff --git a/src/mailman/handlers/tests/test_mimedel.py b/src/mailman/handlers/tests/test_mimedel.py
index cd80b47ab..a82190065 100644
--- a/src/mailman/handlers/tests/test_mimedel.py
+++ b/src/mailman/handlers/tests/test_mimedel.py
@@ -19,11 +19,18 @@
__all__ = [
'TestDispose',
+ 'TestHTMLFilter',
+ 'dummy_script',
]
+import os
+import sys
+import shutil
+import tempfile
import unittest
+from contextlib import ExitStack, contextmanager
from mailman.app.lifecycle import create_list
from mailman.config import config
from mailman.core import errors
@@ -39,10 +46,32 @@ from zope.component import getUtility
+@contextmanager
+def dummy_script():
+ with ExitStack() as resources:
+ tempdir = tempfile.mkdtemp()
+ resources.callback(shutil.rmtree, tempdir)
+ filter_path = os.path.join(tempdir, 'filter.py')
+ with open(filter_path, 'w', encoding='utf-8') as fp:
+ print("""\
+import sys
+print('Converted text/html to text/plain')
+print('Filename:', sys.argv[1])
+""", file=fp)
+ config.push('dummy script', """\
+[mailman]
+html_to_plain_text_command = {exe} {script} $filename
+""".format(exe=sys.executable, script=filter_path))
+ resources.callback(config.pop, 'dummy script')
+ yield
+
+
+
class TestDispose(unittest.TestCase):
"""Test the mime_delete handler."""
layer = ConfigLayer
+ maxxDiff = None
def setUp(self):
self._mlist = create_list('test@example.com')
@@ -57,11 +86,7 @@ Message-ID: <ant>
[mailman]
site_owner: noreply@example.com
""")
- # Let assertMultiLineEqual work without bounds.
- self.maxDiff = None
-
- def tearDown(self):
- config.pop('dispose')
+ self.addCleanup(config.pop, 'dispose')
def test_dispose_discard(self):
self._mlist.filter_action = FilterAction.discard
@@ -171,3 +196,34 @@ message.
self.assertTrue(line.endswith(
'{0} invalid FilterAction: test@example.com. '
'Treating as discard'.format(action.name)))
+
+
+
+class TestHTMLFilter(unittest.TestCase):
+ """Test the conversion of HTML to plaintext."""
+
+ layer = ConfigLayer
+
+ def setUp(self):
+ self._mlist = create_list('test@example.com')
+ self._mlist.convert_html_to_plaintext = True
+ self._mlist.filter_content = True
+
+ def test_convert_html_to_plaintext(self):
+ # Converting to plain text calls a command line script.
+ msg = mfs("""\
+From: aperson@example.com
+Content-Type: text/html
+MIME-Version: 1.0
+
+<html><head></head>
+<body></body></html>
+""")
+ process = config.handlers['mime-delete'].process
+ with dummy_script():
+ process(self._mlist, msg, {})
+ self.assertEqual(msg.get_content_type(), 'text/plain')
+ self.assertTrue(
+ msg['x-content-filtered-by'].startswith('Mailman/MimeDel'))
+ payload_lines = msg.get_payload().splitlines()
+ self.assertEqual(payload_lines[0], 'Converted text/html to text/plain')