[Zope3-checkins]
SVN: Zope3/branches/mgedmin-security/src/zope/security/
Added missing unit tests for IInteractionManagement and made
them run.
Marius Gedminas
marius at pov.lt
Wed May 12 14:59:20 EDT 2004
Log message for revision 24600:
Added missing unit tests for IInteractionManagement and made them run.
Added a simple IInteraction implementation. Added createInteraction to
ISecurityPolicy.
-=-
Modified: Zope3/branches/mgedmin-security/src/zope/security/interfaces.py
===================================================================
--- Zope3/branches/mgedmin-security/src/zope/security/interfaces.py 2004-05-12 18:48:08 UTC (rev 24599)
+++ Zope3/branches/mgedmin-security/src/zope/security/interfaces.py 2004-05-12 18:59:20 UTC (rev 24600)
@@ -189,6 +189,16 @@
class ISecurityPolicy(Interface): # XXX: will change
+ def createInteraction(participation=None):
+ """Creates a new interaction for a given request.
+
+ If participation is not None, it is added to the new interaction.
+
+ XXX perhaps this should be a separate interface IInteractionFactory,
+ and the factory registered by calling
+ ISecurityManagement.global setInteractionFactory(factory).
+ """
+
def checkPermission(permission, object, context):
"""Return whether security context allows permission on object.
Modified: Zope3/branches/mgedmin-security/src/zope/security/management.py
===================================================================
--- Zope3/branches/mgedmin-security/src/zope/security/management.py 2004-05-12 18:48:08 UTC (rev 24599)
+++ Zope3/branches/mgedmin-security/src/zope/security/management.py 2004-05-12 18:59:20 UTC (rev 24600)
@@ -19,6 +19,8 @@
# zope.security.manager needs it
system_user = object()
+import traceback
+
from zope.interface import moduleProvides
from zope.security.interfaces import ISecurityManagement
from zope.security.interfaces import ISecurityManagementSetup
@@ -115,7 +117,7 @@
raise AssertionError("newInteraction called"
" while another interaction is active:\n%s"
% "".join(traceback.format_list(stack)))
- interaction = _defaultPolicy.createInteraction(participation)
+ interaction = getSecurityPolicy().createInteraction(participation)
interaction._newInteraction_called_from = traceback.extract_stack()
thread_globals(_thread).interaction = interaction
Added: Zope3/branches/mgedmin-security/src/zope/security/simpleinteraction.py
===================================================================
--- Zope3/branches/mgedmin-security/src/zope/security/simpleinteraction.py 2004-05-12 18:48:08 UTC (rev 24599)
+++ Zope3/branches/mgedmin-security/src/zope/security/simpleinteraction.py 2004-05-12 18:59:20 UTC (rev 24600)
@@ -0,0 +1,54 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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.
+#
+##############################################################################
+""" Define Zope\'s default interaction class
+
+$Id: simpleinteraction.py,v 1.1.2.2 2004/03/19 18:50:56 mgedmin Exp $
+"""
+
+import sets
+
+from zope.interface import implements
+from zope.security.interfaces import IInteraction
+
+__metaclass__ = type
+
+
+class Interaction:
+ implements(IInteraction)
+
+ def __init__(self):
+ self.participations = []
+
+ def add(self, participation):
+ if participation.interaction is not None:
+ raise ValueError("%r already belongs to an interaction"
+ % participation)
+ participation.interaction = self
+ self.participations.append(participation)
+
+ def remove(self, participation):
+ if participation.interaction is not self:
+ raise ValueError("%r does not belong to this interaction"
+ % participation)
+ self.participations.remove(participation)
+ participation.interaction = None
+
+
+def createInteraction(participation=None):
+ """A helper for implementing ISecurityPolicy.createInteraction"""
+ interaction = Interaction()
+ if participation is not None:
+ interaction.add(participation)
+ return interaction
+
Property changes on: Zope3/branches/mgedmin-security/src/zope/security/simpleinteraction.py
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: Zope3/branches/mgedmin-security/src/zope/security/simplepolicies.py
===================================================================
--- Zope3/branches/mgedmin-security/src/zope/security/simplepolicies.py 2004-05-12 18:48:08 UTC (rev 24599)
+++ Zope3/branches/mgedmin-security/src/zope/security/simplepolicies.py 2004-05-12 18:59:20 UTC (rev 24600)
@@ -18,6 +18,8 @@
from zope.security.interfaces import ISecurityPolicy
from zope.security.management import system_user
+from zope.security.simpleinteraction import createInteraction \
+ as _createInteraction
import zope.security.checker
from zope.interface import implements
@@ -25,6 +27,8 @@
"""Deny all access."""
implements(ISecurityPolicy)
+ createInteraction = staticmethod(_createInteraction)
+
def checkPermission(self, permission, object, context):
if permission is zope.security.checker.CheckerPublic:
return True
@@ -39,5 +43,7 @@
"""Allow all access."""
implements(ISecurityPolicy)
+ createInteraction = staticmethod(_createInteraction)
+
def checkPermission(self, permission, object, context):
return True
Modified: Zope3/branches/mgedmin-security/src/zope/security/tests/test_management.py
===================================================================
--- Zope3/branches/mgedmin-security/src/zope/security/tests/test_management.py 2004-05-12 18:48:08 UTC (rev 24599)
+++ Zope3/branches/mgedmin-security/src/zope/security/tests/test_management.py 2004-05-12 18:59:20 UTC (rev 24600)
@@ -110,7 +110,60 @@
mgr = getSecurityManager()
self.failIf(mgr.checkPermission(None, None))
+ def test_securityPolicy(self):
+ from zope.security.management import setSecurityPolicy
+ from zope.security.management import getSecurityPolicy
+ from zope.security.simplepolicies import PermissiveSecurityPolicy
+ policy = PermissiveSecurityPolicy()
+ setSecurityPolicy(policy)
+ self.assert_(getSecurityPolicy() is policy)
+
+ def test_getInteraction(self):
+ # XXX this test is a bit obfuscated
+ from zope.security.management import getInteraction
+
+ marker = object()
+ class ThreadVars:
+ interaction = marker
+ class ThreadStub:
+ __zope3_thread_globals__ = ThreadVars()
+
+ self.assert_(getInteraction(_thread=ThreadStub()) is marker)
+
+ def test_newInteraction(self):
+ # XXX this test is a bit obfuscated
+ from zope.security.management import newInteraction
+
+ class ThreadVars:
+ interaction = None
+ class ThreadStub:
+ __zope3_thread_globals__ = ThreadVars()
+
+ rq = None
+ thread = ThreadStub()
+ newInteraction(rq, _thread=thread)
+ self.assert_(thread.__zope3_thread_globals__.interaction is not None)
+
+ self.assertRaises(AssertionError, newInteraction, rq, _thread=thread)
+
+ def test_endInteraction(self):
+ # XXX this test is a bit obfuscated
+ from zope.security.management import endInteraction
+
+ marker = object()
+ class ThreadVars:
+ interaction = marker
+ class ThreadStub:
+ __zope3_thread_globals__ = ThreadVars()
+
+ thread = ThreadStub()
+ endInteraction(_thread=thread)
+ self.assert_(thread.__zope3_thread_globals__.interaction is None)
+
+ self.assertRaises(AssertionError, endInteraction, _thread=thread)
+
+
def test_suite():
loader=unittest.TestLoader()
return loader.loadTestsFromTestCase(Test)
Added: Zope3/branches/mgedmin-security/src/zope/security/tests/test_simpleinteraction.py
===================================================================
--- Zope3/branches/mgedmin-security/src/zope/security/tests/test_simpleinteraction.py 2004-05-12 18:48:08 UTC (rev 24599)
+++ Zope3/branches/mgedmin-security/src/zope/security/tests/test_simpleinteraction.py 2004-05-12 18:59:20 UTC (rev 24600)
@@ -0,0 +1,84 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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.
+#
+##############################################################################
+"""Unit tests for zope.security.simpleinteraction."""
+
+import unittest
+
+from zope.interface.verify import verifyObject
+
+
+class RequestStub:
+
+ def __init__(self, principal=None):
+ self.principal = principal
+ self.interaction = None
+
+
+class TestInteraction(unittest.TestCase):
+
+ def test(self):
+ from zope.security.interfaces import IInteraction
+ from zope.security.simpleinteraction import Interaction
+ interaction = Interaction()
+ verifyObject(IInteraction, interaction)
+
+ def test_add(self):
+ from zope.security.simpleinteraction import Interaction
+ rq = RequestStub()
+ interaction = Interaction()
+ interaction.add(rq)
+ self.assert_(rq in interaction.participations)
+ self.assert_(rq.interaction is interaction)
+
+ # rq already added
+ self.assertRaises(ValueError, interaction.add, rq)
+
+ interaction2 = Interaction()
+ self.assertRaises(ValueError, interaction2.add, rq)
+
+ def test_remove(self):
+ from zope.security.simpleinteraction import Interaction
+ rq = RequestStub()
+ interaction = Interaction()
+
+ self.assertRaises(ValueError, interaction.remove, rq)
+
+ interaction.add(rq)
+
+ interaction.remove(rq)
+ self.assert_(rq not in interaction.participations)
+ self.assert_(rq.interaction is None)
+
+ def testCreateInteraction(self):
+ from zope.security.interfaces import IInteraction
+ from zope.security.simpleinteraction import createInteraction
+ i1 = createInteraction()
+ verifyObject(IInteraction, i1)
+ self.assertEquals(list(i1.participations), [])
+
+ user = object()
+ request = RequestStub(user)
+ i2 = createInteraction(request)
+ verifyObject(IInteraction, i2)
+ self.assertEquals(list(i2.participations), [request])
+
+
+def test_suite():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(TestInteraction))
+ return suite
+
+
+if __name__ == '__main__':
+ unittest.main()
Property changes on: Zope3/branches/mgedmin-security/src/zope/security/tests/test_simpleinteraction.py
___________________________________________________________________
Name: svn:eol-style
+ native
More information about the Zope3-Checkins
mailing list