From 5a02cc713f9897cd9ec0df48dd7718f019a6e510 Mon Sep 17 00:00:00 2001 From: Abhilash Raj Date: Fri, 14 Oct 2016 12:29:57 -0700 Subject: Return 'defective message' for a bad held message If a message can't be parsed by Python due to bad structure, don't raise an error but return a generic 'this message is defective' string instead. --- src/mailman/rest/tests/data/bad_email | 8 ++++++++ src/mailman/rest/tests/test_moderation.py | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 src/mailman/rest/tests/data/bad_email (limited to 'src/mailman/rest/tests') diff --git a/src/mailman/rest/tests/data/bad_email b/src/mailman/rest/tests/data/bad_email new file mode 100644 index 000000000..83f9337dd --- /dev/null +++ b/src/mailman/rest/tests/data/bad_email @@ -0,0 +1,8 @@ +To: +Subject: =?koi8-r?B?UF9AX/NfQ1/5X+xfS1/p?= +From: =?koi8-r?B?8sXL0sXB1MnXzs/FIMHHxc7U09TXzw==?= +Content-Type: text/plain; charset=koi8-r +Message-Id: <20160614102505.9OFQ19L1C> + +ώτο ταλοε ςελμανξαρ ςασσωμλα? +λΑΛΟΚ ΟΤΛΜΙΛ ΦΔΑΤΨ ΟΤ άΤΟΗΟ ΝΕΤΟΔΑ ΠΟΙΣΛΑ ΛΜΙΕΞΤΟΧ? diff --git a/src/mailman/rest/tests/test_moderation.py b/src/mailman/rest/tests/test_moderation.py index e0c3f1ccf..03d52dfc4 100644 --- a/src/mailman/rest/tests/test_moderation.py +++ b/src/mailman/rest/tests/test_moderation.py @@ -17,8 +17,10 @@ """REST moderation tests.""" +import os import unittest +from email import message_from_binary_file from mailman.app.lifecycle import create_list from mailman.app.moderator import hold_message from mailman.database.transaction import transaction @@ -206,6 +208,23 @@ class TestSubscriptionModeration(unittest.TestCase): emails = set(json['email'] for json in content['entries']) self.assertEqual(emails, {'anne@example.com', 'bart@example.com'}) + def test_view_malformed_held_message(self): + # Opening a bad (i.e. bad structure) email and holding it. + pwd = os.path.dirname(os.path.realpath(__file__)) + email_path = os.path.join(pwd, 'data/bad_email') + msg = message_from_binary_file(open(email_path, 'rb')) + msg.sender = 'bogussender@example.com' + with transaction(): + hold_message(self._mlist, msg) + # Now trying to access held messages from REST API should not give + # 500 server error if one of the messages can't be parsed properly. + content, response = call_api( + 'http://localhost:9001/3.0/lists/ant@example.com/held') + self.assertEqual(response.status, 200) + self.assertEqual(len(content['entries']), 1) + self.assertEqual(content['entries'][0]['msg'], + 'this message is defective') + def test_individual_request(self): # We can view an individual request. with transaction(): -- cgit v1.2.3-70-g09d2