aboutsummaryrefslogtreecommitdiff
path: root/src/mailman_pgp/runners
diff options
context:
space:
mode:
Diffstat (limited to 'src/mailman_pgp/runners')
-rw-r--r--src/mailman_pgp/runners/tests/__init__.py0
-rw-r--r--src/mailman_pgp/runners/tests/test_incoming.py126
2 files changed, 126 insertions, 0 deletions
diff --git a/src/mailman_pgp/runners/tests/__init__.py b/src/mailman_pgp/runners/tests/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/mailman_pgp/runners/tests/__init__.py
diff --git a/src/mailman_pgp/runners/tests/test_incoming.py b/src/mailman_pgp/runners/tests/test_incoming.py
new file mode 100644
index 0000000..d51dbb8
--- /dev/null
+++ b/src/mailman_pgp/runners/tests/test_incoming.py
@@ -0,0 +1,126 @@
+# Copyright (C) 2017 Jan Jancar
+#
+# This file is a part of the Mailman PGP plugin.
+#
+# This program 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.
+#
+# This program 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
+# this program. If not, see <http://www.gnu.org/licenses/>.
+from time import sleep
+from unittest import TestCase
+
+from mailman.app.lifecycle import create_list
+from mailman.interfaces.action import Action
+from mailman.interfaces.member import MemberRole
+from mailman.interfaces.usermanager import IUserManager
+from mailman.testing.helpers import (make_testable_runner,
+ specialized_message_from_string as mfs,
+ get_queue_messages, set_preferred)
+from pgpy import PGPMessage
+from zope.component import getUtility
+
+from mailman_pgp.config import mm_config
+from mailman_pgp.database import mm_transaction, transaction
+from mailman_pgp.model.address import PGPAddress
+from mailman_pgp.model.list import PGPMailingList
+from mailman_pgp.pgp.tests.base import load_key, load_message
+from mailman_pgp.runners.incoming import IncomingRunner
+from mailman_pgp.testing.layers import PGPConfigLayer
+
+
+class TestIncoming(TestCase):
+ layer = PGPConfigLayer
+
+ def setUp(self):
+ user_manager = getUtility(IUserManager)
+ with mm_transaction():
+ self.mlist = create_list('test@example.com',
+ style_name='pgp-default')
+ self.sender = user_manager.create_user('RSA-1024b@example.org')
+ set_preferred(self.sender)
+ self.mlist.subscribe(self.sender, MemberRole.member)
+
+ self.pgp_list = PGPMailingList.for_list(self.mlist)
+
+ sender_key = load_key('data/rsa_1024.pub.asc')
+ with transaction() as t:
+ self.pgp_sender = PGPAddress(self.sender.preferred_address)
+ self.pgp_sender.key = sender_key
+ t.add(self.pgp_sender)
+
+ self.msg_clear = load_message('data/clear.eml')
+ self.msg_inline_encrypted = load_message('data/inline_encrypted.eml')
+
+ self.runner = make_testable_runner(IncomingRunner, 'in')
+
+ def test_pass_default(self):
+ with mm_transaction():
+ ordinary_list = create_list('ordinary@example.com')
+
+ msg = mfs("""\
+From: anne@example.com
+To: ordinary@example.com
+
+""")
+
+ msgdata = dict(listid='ordinary.example.com')
+ mm_config.switchboards['in'].enqueue(msg, msgdata)
+ self.runner.run()
+ items = get_queue_messages('in_default', expected_count=1)
+ self.assertEqual(items[0].msg.sender, 'anne@example.com')
+
+ def test_nonencrypted_action(self):
+ with transaction():
+ self.pgp_list.nonencrypted_msg_action = Action.hold
+
+ msgdata = dict(listid='test.example.com')
+ mm_config.switchboards['in'].enqueue(self.msg_clear, msgdata)
+ self.runner.run()
+ items = get_queue_messages('in_default', expected_count=1)
+ self.assertEqual(items[0].msgdata['moderation_action'],
+ Action.hold.name)
+ self.assertEqual(items[0].msgdata['moderation_sender'],
+ self.msg_clear.sender)
+ self.assertEqual(items[0].msgdata['moderation_reason'],
+ 'Message was not encrypted.')
+ self.assertTrue(items[0].msgdata['pgp_moderate'])
+
+ with transaction():
+ self.pgp_list.nonencrypted_msg_action = Action.defer
+
+ msgdata = dict(listid='test.example.com')
+ mm_config.switchboards['in'].enqueue(self.msg_clear, msgdata)
+ self.runner.run()
+ get_queue_messages('in_default', expected_count=1)
+
+ def test_decrypt(self):
+ for i in range(15): # pragma: no cover
+ if self.pgp_list.pubkey is not None:
+ break
+ sleep(1)
+
+ payload = 'Some encrypted text.'
+ pmsg = PGPMessage.new(payload)
+ emsg = self.pgp_list.pubkey.encrypt(pmsg)
+ msg = mfs("""
+From: RSA-1024b@example.org
+To: test@example.com
+
+{}
+""".format(str(emsg)))
+
+ msgdata = dict(listid='test.example.com')
+ mm_config.switchboards['in'].enqueue(msg,
+ msgdata)
+ self.runner.run()
+ items = get_queue_messages('in_default', expected_count=1)
+ out_msg = items[0].msg
+ self.assertEqual(out_msg.get_payload(), payload)