[Checkins] SVN: zope.security/trunk/ Add test convenience helpers for interactions
Wolfgang Schnerring
cvs-admin at zope.org
Wed Jul 4 11:40:15 UTC 2012
Log message for revision 127250:
Add test convenience helpers for interactions
This is the exact same idea as in zope.publisher, only we use a fake Participation
instead of a TestRequest, so it works with zope.security by itself.
Changed:
U zope.security/trunk/CHANGES.txt
U zope.security/trunk/src/zope/security/testing.py
A zope.security/trunk/src/zope/security/tests/test_testing.py
-=-
Modified: zope.security/trunk/CHANGES.txt
===================================================================
--- zope.security/trunk/CHANGES.txt 2012-07-04 11:39:12 UTC (rev 127249)
+++ zope.security/trunk/CHANGES.txt 2012-07-04 11:40:11 UTC (rev 127250)
@@ -16,7 +16,10 @@
- Dropped support for Python 2.4 and 2.5.
+- Added test convenience helper ``create_interaction`` and
+ ``with interaction()``.
+
3.8.3 (2011-09-24)
------------------
Modified: zope.security/trunk/src/zope/security/testing.py
===================================================================
--- zope.security/trunk/src/zope/security/testing.py 2012-07-04 11:39:12 UTC (rev 127249)
+++ zope.security/trunk/src/zope/security/testing.py 2012-07-04 11:40:11 UTC (rev 127250)
@@ -1,6 +1,6 @@
##############################################################################
#
-# Copyright (c) 2004 Zope Foundation and Contributors.
+# Copyright (c) 2004-2011 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
@@ -19,7 +19,10 @@
from zope import interface, component
from zope.security import interfaces
from zope.security.permission import Permission
+import contextlib
+import zope.security.management
+
@interface.implementer(interfaces.IPrincipal)
class Principal:
@@ -31,6 +34,7 @@
self.groups = groups
interface.directlyProvides(self, interfaces.IGroupAwarePrincipal)
+
@interface.implementer(interfaces.IParticipation)
class Participation:
@@ -51,3 +55,23 @@
)
gsm = component.getGlobalSiteManager()
gsm.registerUtility(perm, interfaces.IPermission, perm.id)
+
+
+def create_interaction(principal_id, **kw):
+ principal = Principal(principal_id, **kw)
+ participation = Participation(principal)
+ zope.security.management.newInteraction(participation)
+ 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, **kw)
+ try:
+ yield principal
+ finally:
+ zope.security.management.endInteraction()
Added: zope.security/trunk/src/zope/security/tests/test_testing.py
===================================================================
--- zope.security/trunk/src/zope/security/tests/test_testing.py (rev 0)
+++ zope.security/trunk/src/zope/security/tests/test_testing.py 2012-07-04 11:40:11 UTC (rev 127250)
@@ -0,0 +1,48 @@
+#############################################################################
+#
+# 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.security.management
+import zope.security.testing
+
+
+class InteractionHelperTest(unittest.TestCase):
+
+ def tearDown(self):
+ zope.security.management.endInteraction()
+
+ def test_create_interaction_should_return_principal(self):
+ principal = zope.security.testing.create_interaction(
+ 'foo', groups=['bar'], description='desc')
+ interaction = zope.security.management.getInteraction()
+ participation = interaction.participations[0]
+ self.assertEqual('foo', participation.principal.id)
+ self.assertEqual(principal.groups, participation.principal.groups)
+ self.assertEqual('desc', participation.principal.description)
+
+ def test_usable_as_contextmanager(self):
+ with zope.security.testing.interaction('foo'):
+ interaction = zope.security.management.getInteraction()
+ participation = interaction.participations[0]
+ self.assertEqual('foo', participation.principal.id)
+ self.assertFalse(zope.security.management.queryInteraction())
+
+ def test_contextmanager_ends_interaction_on_exception(self):
+ try:
+ with zope.security.testing.interaction('foo'):
+ raise RuntimeError()
+ except RuntimeError:
+ pass
+ self.assertFalse(zope.security.management.queryInteraction())
More information about the checkins
mailing list