def test_preserving_disposition(self): # Preserving a message keeps it in the store. request_id = hold_message(self._mlist, self._msg) handle_message(self._mlist, request_id, Action.discard, preserve=True) message_store = getUtility(IMessageStore) preserved_message = message_store.get_message_by_id('<alpha>') self.assertEqual(preserved_message['message-id'], '<alpha>')
def test_accepted_message_gets_posted(self): # A message that is accepted by the moderator should get posted to the # mailing list. LP: #827697 msgdata = dict(listname='*****@*****.**', recipients=['*****@*****.**']) request_id = hold_message(self._mlist, self._msg, msgdata) handle_message(self._mlist, request_id, Action.accept) self._in.run() self._pipeline.run() self._out.run() messages = list(SMTPLayer.smtpd.messages) self.assertEqual(len(messages), 1) message = messages[0] # We don't need to test the entire posted message, just the bits that # prove it got sent out. self.assertTrue('x-mailman-version' in message) self.assertTrue('x-peer' in message) # The X-Mailman-Approved-At header has local timezone information in # it, so test that separately. self.assertEqual(message['x-mailman-approved-at'][:-5], 'Mon, 01 Aug 2005 07:49:23 ') del message['x-mailman-approved-at'] # The Message-ID matches the original. self.assertEqual(message['message-id'], '<alpha>') # Anne sent the message and the mailing list received it. self.assertEqual(message['from'], '*****@*****.**') self.assertEqual(message['to'], '*****@*****.**') # The Subject header has the list's prefix. self.assertEqual(message['subject'], '[Test] hold me') # The list's -bounce address is the actual sender, and Bart is the # only actual recipient. These headers are added by the testing # framework and don't show up in production. They match the RFC 5321 # envelope. self.assertEqual(message['x-mailfrom'], '*****@*****.**') self.assertEqual(message['x-rcptto'], '*****@*****.**')
def test_handled_message_stays_in_store(self): # The message is still available in the store, even when it's been # disposed of. request_id = hold_message(self._mlist, self._msg) handle_message(self._mlist, request_id, Action.discard) self.assertEqual(self._request_db.count, 0) message = getUtility(IMessageStore).get_message_by_id('<alpha>') self.assertEqual(message['subject'], 'hold me')
def test_survive_a_deleted_message(self): # When the message that should be deleted is not found in the store, # no error is raised. request_id = hold_message(self._mlist, self._msg) message_store = getUtility(IMessageStore) message_store.delete_message('<alpha>') handle_message(self._mlist, request_id, Action.discard) self.assertEqual(self._request_db.count, 0)
def test_hold_action_alias_for_defer(self): # In handle_message(), the 'hold' action is the same as 'defer' for # purposes of this API. request_id = hold_message(self._mlist, self._msg) handle_message(self._mlist, request_id, Action.defer) # The message is still in the pending requests. key, data = self._request_db.get_request(request_id) self.assertEqual(key, '<alpha>') handle_message(self._mlist, request_id, Action.hold) key, data = self._request_db.get_request(request_id) self.assertEqual(key, '<alpha>')
def test_forward(self): # We can forward the message to an email address. request_id = hold_message(self._mlist, self._msg) handle_message(self._mlist, request_id, Action.discard, forward=['*****@*****.**']) # The forwarded message lives in the virgin queue. items = get_queue_messages('virgin', expected_count=1) self.assertEqual(str(items[0].msg['subject']), 'Forward of moderated message') self.assertEqual(items[0].msgdata['recipients'], ['*****@*****.**'])
def test_preserve_and_forward(self): # We can both preserve and forward the message. request_id = hold_message(self._mlist, self._msg) handle_message(self._mlist, request_id, Action.discard, preserve=True, forward=['*****@*****.**']) # The message is preserved in the store. message_store = getUtility(IMessageStore) preserved_message = message_store.get_message_by_id('<alpha>') self.assertEqual(preserved_message['message-id'], '<alpha>') # And the forwarded message lives in the virgin queue. messages = get_queue_messages('virgin') self.assertEqual(len(messages), 1) self.assertEqual(str(messages[0].msg['subject']), 'Forward of moderated message') self.assertEqual(messages[0].msgdata['recipients'], ['*****@*****.**'])
def moderate(self, request): try: validator = Validator(action=enum_validator(Action)) arguments = validator(request) except ValueError as error: return http.bad_request([], str(error)) requests = IListRequests(self._mlist) try: request_id = int(self._request_id) except ValueError: return http.bad_request() results = requests.get_request(request_id, RequestType.held_message) if results is None: return http.not_found() handle_message(self._mlist, request_id, **arguments) return no_content()
def on_post(self, request, response): try: validator = Validator(action=enum_validator(Action)) arguments = validator(request) except ValueError as error: bad_request(response, str(error)) return requests = IListRequests(self._mlist) try: request_id = int(self._request_id) except ValueError: bad_request(response) return results = requests.get_request(request_id, RequestType.held_message) if results is None: not_found(response) else: handle_message(self._mlist, request_id, **arguments) no_content(response)
def on_post(self, request, response): try: validator = Validator(action=enum_validator(Action)) arguments = validator(request) except ValueError as error: bad_request(response, str(error)) return requests = IListRequests(self._mlist) try: request_id = int(self._request_id) except ValueError: not_found(response) return results = requests.get_request(request_id, RequestType.held_message) if results is None: not_found(response) else: handle_message(self._mlist, request_id, **arguments) no_content(response)
def test_non_preserving_disposition(self): # By default, disposed messages are not preserved. request_id = hold_message(self._mlist, self._msg) handle_message(self._mlist, request_id, Action.discard) message_store = getUtility(IMessageStore) self.assertIsNone(message_store.get_message_by_id('<alpha>'))