summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorWolfgang Schnerring <wosc@wosc.de>2012-07-04 11:40:11 +0000
committerWolfgang Schnerring <wosc@wosc.de>2012-07-04 11:40:11 +0000
commit8285400aabcc39fb90ebb4ed5bcdedc86525fff3 (patch)
treeb36e3b09c2de9a5c6bc9e3f66a443538e071457b /src
parent07f29a604b703703c315e9da060de5447efb8f9c (diff)
downloadzope-security-8285400aabcc39fb90ebb4ed5bcdedc86525fff3.tar.gz
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.
Diffstat (limited to 'src')
-rw-r--r--src/zope/security/testing.py26
-rw-r--r--src/zope/security/tests/test_testing.py48
2 files changed, 73 insertions, 1 deletions
diff --git a/src/zope/security/testing.py b/src/zope/security/testing.py
index 2f4a6a5..53d1ebe 100644
--- a/src/zope/security/testing.py
+++ b/src/zope/security/testing.py
@@ -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,6 +19,9 @@ This module provides some helper/stub objects for setting up interactions.
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 @@ class Principal:
self.groups = groups
interface.directlyProvides(self, interfaces.IGroupAwarePrincipal)
+
@interface.implementer(interfaces.IParticipation)
class Participation:
@@ -51,3 +55,23 @@ def addCheckerPublic():
)
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
+
+
+@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()
diff --git a/src/zope/security/tests/test_testing.py b/src/zope/security/tests/test_testing.py
new file mode 100644
index 0000000..c04ae44
--- /dev/null
+++ b/src/zope/security/tests/test_testing.py
@@ -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())