Mark Sapiro pushed to branch master at GNU Mailman / Mailman Core
Commits: edb19474 by Mark Sapiro at 2021-07-15T10:15:27-07:00 Stringify any Header instances in tagger.py. - - - - - bd47658f by Mark Sapiro at 2021-07-15T17:38:36+00:00 Merge branch 'tagger' into 'master' Stringify any Header instances in tagger.py. See merge request mailman/mailman!893 - - - - - 3 changed files: - src/mailman/docs/NEWS.rst - src/mailman/handlers/tagger.py - + src/mailman/handlers/tests/test_tagger.py Changes: ===================================== src/mailman/docs/NEWS.rst ===================================== @@ -91,6 +91,7 @@ Bugs #923) * DMARC mitigation wrap message now ensures existing cc and reply-to headers are included in the wrapper. (Closes #926) +* The tagger handler now stringifies any Header instances. (Closes #928) Command line ------------ ===================================== src/mailman/handlers/tagger.py ===================================== @@ -48,8 +48,8 @@ def process(mlist, msg, msgdata): else: # Scan just some of the body lines matchlines.extend(scanbody(msg, mlist.topics_bodylines_limit)) - # Filter out any 'false' items. - matchlines = [item for item in matchlines if item] + # Filter out any 'false' items and stringify any Header instances. + matchlines = [str(item) for item in matchlines if item] # For each regular expression in the topics list, see if any of the lines # of interest from the message match the regexp. If so, the message gets # added to the specific topics bucket. ===================================== src/mailman/handlers/tests/test_tagger.py ===================================== @@ -0,0 +1,101 @@ +# Copyright (C) 2014-2021 by the Free Software Foundation, Inc. +# +# This file is part of GNU Mailman. +# +# GNU Mailman is free software: you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) +# any later version. +# +# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along with +# GNU Mailman. If not, see <https://www.gnu.org/licenses/>. + +"""Test the tagger handler.""" + +import unittest + +from email.header import make_header +from mailman.app.lifecycle import create_list +from mailman.config import config +from mailman.testing.helpers import specialized_message_from_string as mfs +from mailman.testing.layers import ConfigLayer + + +class TestTagger(unittest.TestCase): + """Test the tagger handler.""" + + layer = ConfigLayer + + def setUp(self): + self._mlist = create_list('a...@example.com') + self._mlist.topics = [('topic_1', 'magic word', 'Test topic', False)] + self._mlist.topics_enabled = True + self._mlist.topics_bodylines_limit = 5 + self._msgdata = {} + self._msg = mfs("""\ +Received: from somewhere +To: <a...@example.com> +From: <a...@example.com> +Message-ID: <1...@example.com> + +body +""") + + def test_no_hit(self): + # No hit returns no hits. + config.handlers['tagger'].process(self._mlist, + self._msg, + self._msgdata) + self.assertIsNone(self._msg.get('x-topics', None)) + self.assertFalse('topichits' in self._msgdata) + + def test_hit_subject(self): + # A hit on the Subject: is reported. + self._msg['Subject'] = 'This contains the magic word' + config.handlers['tagger'].process(self._mlist, + self._msg, + self._msgdata) + self.assertEqual('topic_1', self._msg.get('x-topics')) + self.assertEqual(['topic_1'], self._msgdata['topichits']) + + def test_hit_body(self): + # A hit on Keywords: in the body is reported. + self._msg.set_payload("""\ +Keywords: magic word +Some blah ... +etc. +""") + config.handlers['tagger'].process(self._mlist, + self._msg, + self._msgdata) + self.assertEqual('topic_1', self._msg.get('x-topics')) + self.assertEqual(['topic_1'], self._msgdata['topichits']) + + def test_no_hit_body_not_searched(self): + # The body doesn't hit if not searched. + self._msg.set_payload("""\ +Keywords: magic word +Some blah ... +etc. +""") + self._mlist.topics_bodylines_limit = 0 + config.handlers['tagger'].process(self._mlist, + self._msg, + self._msgdata) + self.assertIsNone(self._msg.get('x-topics', None)) + self.assertFalse('topichits' in self._msgdata) + + def test_hit_header_instance(self): + # A hit on a Header instance is reported. + self._msg['Subject'] = make_header([('This contains the magic word', + 'utf-8')]) + config.handlers['tagger'].process(self._mlist, + self._msg, + self._msgdata) + self.assertEqual('topic_1', self._msg.get('x-topics')) + self.assertEqual(['topic_1'], self._msgdata['topichits']) View it on GitLab: https://gitlab.com/mailman/mailman/-/compare/63c5c9a8504e0a2af1c69252c50fe6b0271700a2...bd47658f0c50ea1e57186b3674fc1285067fc9ba -- View it on GitLab: https://gitlab.com/mailman/mailman/-/compare/63c5c9a8504e0a2af1c69252c50fe6b0271700a2...bd47658f0c50ea1e57186b3674fc1285067fc9ba You're receiving this email because of your account on gitlab.com.
_______________________________________________ Mailman-checkins mailing list -- mailman-checkins@python.org To unsubscribe send an email to mailman-checkins-le...@python.org https://mail.python.org/mailman3/lists/mailman-checkins.python.org/ Member address: arch...@jab.org