summaryrefslogtreecommitdiff
path: root/src/mailman/plugins/testing
diff options
context:
space:
mode:
authorBarry Warsaw2017-08-29 14:07:55 +0000
committerBarry Warsaw2017-08-29 14:07:55 +0000
commitde8c204fa40f0c4677a1b73b2ee7e3e86ce93a9c (patch)
tree6fd2038427fbb36d8173fe338d277351cd19727b /src/mailman/plugins/testing
parentf847e15407bfbf824236547bdf728a1ae00bd405 (diff)
parentae0042a90220119414f61aeb20c6b58bfacb8af2 (diff)
downloadmailman-de8c204fa40f0c4677a1b73b2ee7e3e86ce93a9c.tar.gz
mailman-de8c204fa40f0c4677a1b73b2ee7e3e86ce93a9c.tar.zst
mailman-de8c204fa40f0c4677a1b73b2ee7e3e86ce93a9c.zip
Diffstat (limited to 'src/mailman/plugins/testing')
-rw-r--r--src/mailman/plugins/testing/__init__.py0
-rw-r--r--src/mailman/plugins/testing/alternate.cfg7
-rw-r--r--src/mailman/plugins/testing/alternate/__init__.py0
-rw-r--r--src/mailman/plugins/testing/alternate/rules/__init__.py0
-rw-r--r--src/mailman/plugins/testing/alternate/rules/rules.py14
-rw-r--r--src/mailman/plugins/testing/example/__init__.py0
-rw-r--r--src/mailman/plugins/testing/example/hooks.py21
-rw-r--r--src/mailman/plugins/testing/example/rest.py79
-rw-r--r--src/mailman/plugins/testing/example/rules/__init__.py0
-rw-r--r--src/mailman/plugins/testing/example/rules/rules.py14
-rw-r--r--src/mailman/plugins/testing/hooks.cfg3
-rw-r--r--src/mailman/plugins/testing/layer.py53
-rw-r--r--src/mailman/plugins/testing/rest.cfg6
-rw-r--r--src/mailman/plugins/testing/showrules.py6
14 files changed, 203 insertions, 0 deletions
diff --git a/src/mailman/plugins/testing/__init__.py b/src/mailman/plugins/testing/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/src/mailman/plugins/testing/__init__.py
diff --git a/src/mailman/plugins/testing/alternate.cfg b/src/mailman/plugins/testing/alternate.cfg
new file mode 100644
index 000000000..6cf6ea4f3
--- /dev/null
+++ b/src/mailman/plugins/testing/alternate.cfg
@@ -0,0 +1,7 @@
+[plugin.example]
+class: example.hooks.ExamplePlugin
+enabled: yes
+component_package: alternate
+
+[logging.plugins]
+propagate: yes
diff --git a/src/mailman/plugins/testing/alternate/__init__.py b/src/mailman/plugins/testing/alternate/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/src/mailman/plugins/testing/alternate/__init__.py
diff --git a/src/mailman/plugins/testing/alternate/rules/__init__.py b/src/mailman/plugins/testing/alternate/rules/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/src/mailman/plugins/testing/alternate/rules/__init__.py
diff --git a/src/mailman/plugins/testing/alternate/rules/rules.py b/src/mailman/plugins/testing/alternate/rules/rules.py
new file mode 100644
index 000000000..f8b8d725c
--- /dev/null
+++ b/src/mailman/plugins/testing/alternate/rules/rules.py
@@ -0,0 +1,14 @@
+from mailman.interfaces.rules import IRule
+from public import public
+from zope.interface import implementer
+
+
+@public
+@implementer(IRule)
+class AlternateRule:
+ name = 'alternate-rule'
+ description = 'An alternate rule.'
+ record = True
+
+ def check(self, mlist, msg, msgdata):
+ return 'alternate' in msgdata
diff --git a/src/mailman/plugins/testing/example/__init__.py b/src/mailman/plugins/testing/example/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/src/mailman/plugins/testing/example/__init__.py
diff --git a/src/mailman/plugins/testing/example/hooks.py b/src/mailman/plugins/testing/example/hooks.py
new file mode 100644
index 000000000..55c09c4a5
--- /dev/null
+++ b/src/mailman/plugins/testing/example/hooks.py
@@ -0,0 +1,21 @@
+import os
+
+from mailman.interfaces.plugin import IPlugin
+from public import public
+from zope.interface import implementer
+
+
+@public
+@implementer(IPlugin)
+class ExamplePlugin:
+ def pre_hook(self):
+ if os.environ.get('DEBUG_HOOKS'):
+ print("I'm in my pre-hook")
+
+ def post_hook(self):
+ if os.environ.get('DEBUG_HOOKS'):
+ print("I'm in my post-hook")
+
+ @property
+ def resource(self):
+ return None
diff --git a/src/mailman/plugins/testing/example/rest.py b/src/mailman/plugins/testing/example/rest.py
new file mode 100644
index 000000000..1d83ce2cd
--- /dev/null
+++ b/src/mailman/plugins/testing/example/rest.py
@@ -0,0 +1,79 @@
+from mailman.config import config
+from mailman.interfaces.plugin import IPlugin
+from mailman.rest.helpers import bad_request, child, etag, no_content, okay
+from mailman.rest.validator import Validator
+from public import public
+from zope.interface import implementer
+
+
+@public
+class Yes:
+ def on_get(self, request, response):
+ okay(response, etag(dict(yes=True)))
+
+
+@public
+class No:
+ def on_get(self, request, response):
+ bad_request(response, etag(dict(no=False)))
+
+
+@public
+class NumberEcho:
+ def __init__(self):
+ self._plugin = config.plugins['example']
+
+ def on_get(self, request, response):
+ okay(response, etag(dict(number=self._plugin.number)))
+
+ def on_post(self, request, response):
+ try:
+ resource = Validator(number=int)(request)
+ self._plugin.number = resource['number']
+ except ValueError as error:
+ bad_request(response, str(error))
+ else:
+ no_content(response)
+
+ def on_delete(self, request, response):
+ self._plugin.number = 0
+ no_content(response)
+
+
+@public
+class RESTExample:
+ def on_get(self, request, response):
+ resource = {
+ 'my-name': 'example-plugin',
+ 'my-child-resources': 'yes, no, echo',
+ }
+ okay(response, etag(resource))
+
+ @child()
+ def yes(self, context, segments):
+ return Yes(), []
+
+ @child()
+ def no(self, context, segments):
+ return No(), []
+
+ @child()
+ def echo(self, context, segments):
+ return NumberEcho(), []
+
+
+@public
+@implementer(IPlugin)
+class ExamplePlugin:
+ def __init__(self):
+ self.number = 0
+
+ def pre_hook(self):
+ pass
+
+ def post_hook(self):
+ pass
+
+ @property
+ def resource(self):
+ return RESTExample()
diff --git a/src/mailman/plugins/testing/example/rules/__init__.py b/src/mailman/plugins/testing/example/rules/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/src/mailman/plugins/testing/example/rules/__init__.py
diff --git a/src/mailman/plugins/testing/example/rules/rules.py b/src/mailman/plugins/testing/example/rules/rules.py
new file mode 100644
index 000000000..7e9ba6ce7
--- /dev/null
+++ b/src/mailman/plugins/testing/example/rules/rules.py
@@ -0,0 +1,14 @@
+from mailman.interfaces.rules import IRule
+from public import public
+from zope.interface import implementer
+
+
+@public
+@implementer(IRule)
+class ExampleRule:
+ name = 'example-rule'
+ description = 'An example rule.'
+ record = True
+
+ def check(self, mlist, msg, msgdata):
+ return 'example' in msgdata
diff --git a/src/mailman/plugins/testing/hooks.cfg b/src/mailman/plugins/testing/hooks.cfg
new file mode 100644
index 000000000..15fb31815
--- /dev/null
+++ b/src/mailman/plugins/testing/hooks.cfg
@@ -0,0 +1,3 @@
+[plugin.example]
+class: example.hooks.ExamplePlugin
+enabled: yes
diff --git a/src/mailman/plugins/testing/layer.py b/src/mailman/plugins/testing/layer.py
new file mode 100644
index 000000000..311a419f4
--- /dev/null
+++ b/src/mailman/plugins/testing/layer.py
@@ -0,0 +1,53 @@
+# Copyright (C) 2017 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 <http://www.gnu.org/licenses/>.
+
+"""REST layer for plugins."""
+
+import os
+
+from contextlib import ExitStack
+from mailman.testing.helpers import (
+ TestableMaster, hackenv, wait_for_webservice)
+from mailman.testing.layers import SMTPLayer
+from pkg_resources import resource_filename
+from public import public
+
+
+# Don't inherit from RESTLayer since layers get run in bottom up order,
+# meaning RESTLayer will get setUp() before this layer does, and that won't
+# use the configuration file we need it to use.
+@public
+class PluginRESTLayer(SMTPLayer):
+ @classmethod
+ def setUp(cls):
+ cls.resources = ExitStack()
+ plugin_path = os.path.join(
+ os.path.dirname(
+ resource_filename('mailman.plugins', '__init__.py')),
+ 'testing')
+ config_file = resource_filename('mailman.plugins.testing', 'rest.cfg')
+ cls.resources.enter_context(
+ hackenv('MAILMAN_CONFIG_FILE', config_file))
+ cls.resources.enter_context(
+ hackenv('PYTHONPATH', plugin_path))
+ cls.server = TestableMaster(wait_for_webservice)
+ cls.server.start('rest')
+ cls.resources.callback(cls.server.stop)
+
+ @classmethod
+ def tearDown(cls):
+ cls.resources.close()
diff --git a/src/mailman/plugins/testing/rest.cfg b/src/mailman/plugins/testing/rest.cfg
new file mode 100644
index 000000000..a9b4b9bd9
--- /dev/null
+++ b/src/mailman/plugins/testing/rest.cfg
@@ -0,0 +1,6 @@
+[plugin.example]
+class: example.rest.ExamplePlugin
+enabled: yes
+
+[webservice]
+port: 9001
diff --git a/src/mailman/plugins/testing/showrules.py b/src/mailman/plugins/testing/showrules.py
new file mode 100644
index 000000000..926ef0f70
--- /dev/null
+++ b/src/mailman/plugins/testing/showrules.py
@@ -0,0 +1,6 @@
+from mailman.config import config
+
+
+def showrules():
+ for name in sorted(config.rules):
+ print(name)