[Checkins] SVN: zope.publisher/trunk/ Add test convenience helper ``create_interaction`` and ``with interaction()``.

Wolfgang Schnerring wosc at wosc.de
Wed Nov 16 14:29:27 UTC 2011


Log message for revision 123380:
  Add test convenience helper ``create_interaction`` and ``with interaction()``.
  
  

Changed:
  U   zope.publisher/trunk/CHANGES.txt
  A   zope.publisher/trunk/src/zope/publisher/testing.py
  A   zope.publisher/trunk/src/zope/publisher/tests/test_testing.py

-=-
Modified: zope.publisher/trunk/CHANGES.txt
===================================================================
--- zope.publisher/trunk/CHANGES.txt	2011-11-16 12:02:49 UTC (rev 123379)
+++ zope.publisher/trunk/CHANGES.txt	2011-11-16 14:29:26 UTC (rev 123380)
@@ -5,6 +5,7 @@
 -------------------
 
 - Fix error when no charset matches form data and HTTP_ACCEPT_CHARSET contains a *.
+- Add test convenience helper ``create_interaction`` and ``with interaction()``.
 
 
 3.12.6 (2010-12-17)

Added: zope.publisher/trunk/src/zope/publisher/testing.py
===================================================================
--- zope.publisher/trunk/src/zope/publisher/testing.py	                        (rev 0)
+++ zope.publisher/trunk/src/zope/publisher/testing.py	2011-11-16 14:29:26 UTC (rev 123380)
@@ -0,0 +1,41 @@
+#############################################################################
+#
+# Copyright (c) 2011 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+import contextlib
+import zope.publisher.browser
+import zope.security.management
+import zope.security.testing
+
+
+# The interaction helpers conceptually rather belong to zope.security, but
+# since that doesn't (and shouldn't) depend on zope.publisher, we have to put
+# them here, unfortunately.
+
+def create_interaction(principal_id, **kw):
+    principal = zope.security.testing.Principal(principal_id, **kw)
+    request = zope.publisher.browser.TestRequest()
+    request.setPrincipal(principal)
+    zope.security.management.newInteraction(request)
+    return principal
+
+
+ at contextlib.contextmanager
+def interaction(principal_id, **kw):
+    if zope.security.management.queryInteraction():
+        # There already is an interaction. Great. Leave it alone.
+        yield
+    else:
+        principal = create_interaction(principal_id)
+        yield principal
+        zope.security.management.endInteraction()

Added: zope.publisher/trunk/src/zope/publisher/tests/test_testing.py
===================================================================
--- zope.publisher/trunk/src/zope/publisher/tests/test_testing.py	                        (rev 0)
+++ zope.publisher/trunk/src/zope/publisher/tests/test_testing.py	2011-11-16 14:29:26 UTC (rev 123380)
@@ -0,0 +1,36 @@
+#############################################################################
+#
+# Copyright (c) 2011 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+from __future__ import with_statement
+import unittest
+import zope.publisher.testing
+import zope.security.management
+
+
+class InteractionHelperTest(unittest.TestCase):
+
+    def test_create_interaction_should_return_principal(self):
+        principal = zope.publisher.testing.create_interaction(
+            'foo', groups=['bar'], description='desc')
+        interaction = zope.security.management.getInteraction()
+        request = interaction.participations[0]
+        self.assertEqual('foo', request.principal.id)
+        self.assertEqual(principal.groups, request.principal.groups)
+        self.assertEqual('desc', request.principal.description)
+
+    def test_usable_as_contextmanager(self):
+        with zope.publisher.testing.interaction('foo'):
+            interaction = zope.security.management.getInteraction()
+            request = interaction.participations[0]
+            self.assertEqual('foo', request.principal.id)



More information about the checkins mailing list