summaryrefslogtreecommitdiff
path: root/Mailman/testing/test_handlers.py
diff options
context:
space:
mode:
Diffstat (limited to 'Mailman/testing/test_handlers.py')
-rw-r--r--Mailman/testing/test_handlers.py136
1 files changed, 135 insertions, 1 deletions
diff --git a/Mailman/testing/test_handlers.py b/Mailman/testing/test_handlers.py
index 59fd4de08..f963fdf24 100644
--- a/Mailman/testing/test_handlers.py
+++ b/Mailman/testing/test_handlers.py
@@ -48,6 +48,7 @@ from Mailman.Handlers import Hold
from Mailman.Handlers import MimeDel
from Mailman.Handlers import Moderate
from Mailman.Handlers import Replybot
+from Mailman.Handlers import Scrubber
# Don't test handlers such as SMTPDirect and Sendmail here
from Mailman.Handlers import SpamDetect
from Mailman.Handlers import Tagger
@@ -955,7 +956,7 @@ IMAGEDATAIMAGEDATAIMAGEDATA
mlist.description = u'\u65e5\u672c\u8a9e'
msg = Message.Message()
msg.set_payload('Fran\xe7aise', 'iso-8859-1')
- Decorate.process(self._mlist, msg, {})
+ Decorate.process(mlist, msg, {})
self.assertEqual(msg.as_string(unixfrom=0), """\
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
@@ -964,6 +965,46 @@ Content-Transfer-Encoding: base64
5pel5pys6KqeIGhlYWRlcgpGcmFuw6dhaXNlCuaXpeacrOiqniBmb290ZXI=
""")
+ def test_no_multipart_unknown_charset(self):
+ mlist = self._mlist
+ mlist.msg_header = 'header'
+ mlist.msg_footer = 'footer'
+ msg = email.message_from_string("""\
+From: aperson@example.org
+Content-Type: text/plain; charset=unknown
+Content-Transfer-Encoding: 7bit
+
+Here is a message.
+""")
+ Decorate.process(mlist, msg, {})
+ self.assertEqual(len(msg.get_payload()), 3)
+ self.assertEqual(msg.get_payload()[1].as_string(unixfrom=0),"""\
+Content-Type: text/plain; charset=unknown
+Content-Transfer-Encoding: 7bit
+
+Here is a message.
+""")
+
+ def test_no_multipart_flowed(self):
+ mlist = self._mlist
+ mlist.msg_header = 'header'
+ mlist.msg_footer = 'footer'
+ msg = email.message_from_string("""\
+From: aperson@example.org
+Content-Type: text/plain; format=flowed; delsp=no
+
+Here is a message
+with soft line break.
+""")
+ Decorate.process(mlist, msg, {})
+ self.assertEqual(msg.get_param('format'), 'flowed')
+ self.assertEqual(msg.get_param('delsp'), 'no')
+ self.assertEqual(msg.get_payload(), """\
+header
+Here is a message
+with soft line break.
+footer""")
+
class TestFileRecips(TestBase):
@@ -1391,6 +1432,98 @@ class TestReplybot(TestBase):
+class TestScrubber(TestBase):
+ def test_save_attachment(self):
+ mlist = self._mlist
+ msg = email.message_from_string("""\
+Content-Type: image/gif; name="xtest.gif"
+Content-Transfer-Encoding: base64
+Content-Disposition: attachment; filename="xtest.gif"
+
+R0lGODdhAQABAIAAAAAAAAAAACwAAAAAAQABAAACAQUAOw==
+""")
+ Scrubber.save_attachment(mlist, msg, '')
+ f = open(os.path.join(mlist.archive_dir(), 'attachment.gif'))
+ img = f.read()
+ self.assertEqual(img.startswith('GIF87a'), True)
+ self.assertEqual(len(img), 34)
+
+ def _saved_file(self, s):
+ # a convenient function to get the saved attachment file
+ for i in s.splitlines():
+ if i.startswith('URL: '):
+ f = i.replace(
+ 'URL: <' + self._mlist.GetBaseArchiveURL() + '/' , '')
+ f = os.path.join(self._mlist.archive_dir(), f.rstrip('>'))
+ return f
+
+ def test_scrub_image(self):
+ mlist = self._mlist
+ msg = email.message_from_string("""\
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="BOUNDARY"
+
+--BOUNDARY
+Content-type: text/plain; charset=us-ascii
+
+This is a message.
+--BOUNDARY
+Content-Type: image/gif; name="xtest.gif"
+Content-Transfer-Encoding: base64
+Content-Disposition: attachment; filename="xtest.gif"
+
+R0lGODdhAQABAIAAAAAAAAAAACwAAAAAAQABAAACAQUAOw==
+--BOUNDARY--
+""")
+ Scrubber.process(mlist, msg, {})
+ # saved file
+ img = open(self._saved_file(msg.get_payload())).read()
+ self.assertEqual(img.startswith('GIF87a'), True)
+ self.assertEqual(len(img), 34)
+ # scrubbed message
+ s = '\n'.join([l for l in msg.get_payload().splitlines()
+ if not l.startswith('URL: ')])
+ self.assertEqual(s, """\
+This is a message.
+-------------- next part --------------
+A non-text attachment was scrubbed...
+Name: xtest.gif
+Type: image/gif
+Size: 34 bytes
+Desc: not available""")
+
+ def test_scrub_text(self):
+ mlist = self._mlist
+ msg = email.message_from_string("""\
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="BOUNDARY"
+
+--BOUNDARY
+Content-type: text/plain; charset=us-ascii; format=flowed; delsp=no
+
+This is a message.
+--BOUNDARY
+Content-type: text/plain; name="xtext.txt"
+Content-Disposition: attachment; filename="xtext.txt"
+
+This is a text attachment.
+--BOUNDARY--
+""")
+ Scrubber.process(mlist, msg, {})
+ self.assertEqual(msg.get_param('format'), 'flowed')
+ self.assertEqual(msg.get_param('delsp'), 'no')
+ txt = open(self._saved_file(msg.get_payload())).read()
+ self.assertEqual(txt, 'This is a text attachment.')
+ s = '\n'.join([l for l in msg.get_payload().splitlines()
+ if not l.startswith('URL: ')])
+ self.assertEqual(s, """\
+This is a message.
+-------------- next part --------------
+An embedded and charset-unspecified text was scrubbed...
+Name: xtext.txt""")
+
+
+
class TestSpamDetect(TestBase):
def test_short_circuit(self):
msgdata = {'approved': 1}
@@ -1808,6 +1941,7 @@ def test_suite():
suite.addTest(unittest.makeSuite(TestMimeDel))
suite.addTest(unittest.makeSuite(TestModerate))
suite.addTest(unittest.makeSuite(TestReplybot))
+ suite.addTest(unittest.makeSuite(TestScrubber))
suite.addTest(unittest.makeSuite(TestSpamDetect))
suite.addTest(unittest.makeSuite(TestTagger))
suite.addTest(unittest.makeSuite(TestToArchive))