[Zope3-checkins] CVS: Zope3/src/zope/products/securitypolicy/tests - test_zopepolicy.py:1.2.12.1

Marius Gedminas marius at pov.lt
Mon Mar 8 13:44:14 EST 2004


Update of /cvs-repository/Zope3/src/zope/products/securitypolicy/tests
In directory cvs.zope.org:/tmp/cvs-serv14991/src/zope/products/securitypolicy/tests

Modified Files:
      Tag: mgedmin-events2-branch
	test_zopepolicy.py 
Log Message:
Replaced security managers and security contexts with interactions.  There is
at most one active interaction per thread, accessible with getInteraction().
Code that used getSecurityManager to get the authenticated principal should now
use the current interaction.  Note that the interaction contains an iterable of
principals instead of just one principal.  Code that used a security manager to
implement user logins should now use newInteraction/ endInteraction pair.  Code
that used a security manager to check whether the authenticated user has a
permission to do something should now ask the security policy directly (there's
a new global function getSecurityPolicy).

Interactions are tied with security policies: ISecurityPolicy has a method
used to create a new interaction for a given request.  This is not necessarily
the best idea, perhaps a global hook (setInteractionFactory) would be better.

Things not done yet:
 - Not all places in the code are ready to cope with more than one principal.
 - The README in zope.security and the sandbox security example need to be
   updated.
 - There was an idea of using a notification method in IInteraction that would
   let it customize the handling of local authentication during traversal.
   It could be e.g. afterLocalAuthentication(old_principal, new_principal, site)
   Currently the ZopePublication code just does
     interaction.remove(old_principal)
     interaction.add(new_principal)
   when request.user is changed during traversal.
 - The interaction API could be polished a bit (perhaps the request argument
   to newInteraction should be optional, perhaps there should be an alternative
   principals argument to newInteraction, perhaps endInteraction should not
   raise an exception when it is called outside of an active interaction).
 - It is not clearly cut when security checks should use the global interaction
   and when they should use the interaction of the security proxy.  Perhaps
   use the global one if interaction stored in the proxy is None?
 - It is not defined explicitly where the interaction argument can safely be
   None (as an argument to ProxyFactory, as an argument to security checkers,
   etc.).
 - Some places that construct security proxies pass None to ProxyFactory.
   Perhaps they should use the current interaction instead.  Or maybe not.





=== Zope3/src/zope/products/securitypolicy/tests/test_zopepolicy.py 1.2 => 1.2.12.1 ===
--- Zope3/src/zope/products/securitypolicy/tests/test_zopepolicy.py:1.2	Wed Jan 14 17:55:35 2004
+++ Zope3/src/zope/products/securitypolicy/tests/test_zopepolicy.py	Mon Mar  8 13:43:43 2004
@@ -17,6 +17,7 @@
 """
 
 import unittest
+from zope.interface.verify import verifyObject
 
 from zope.app.tests import ztapi
 from zope.component.service import serviceManager as services
@@ -52,9 +53,13 @@
 from zope.app.services.tests.placefulsetup import PlacefulSetup
 from zope.products.securitypolicy.zopepolicy import permissionsOfPrincipal
 
-class Context:
-    def __init__(self, user, stack=[]):
-        self.user, self.stack = user, stack
+class Interaction:
+    def __init__(self, user):
+        self.principals = [user]
+
+class RequestStub:
+    def __init__(self, user):
+        self.user = user
 
 class Unprotected:
     pass
@@ -147,25 +152,39 @@
         permissions.sort()
         self.assertEqual(permissions, expected)
 
-    def testImport(self):
+    def testInterfaces(self):
+        from zope.security.interfaces import ISecurityPolicy
         from zope.products.securitypolicy.zopepolicy import ZopeSecurityPolicy
+        verifyObject(ISecurityPolicy, ZopeSecurityPolicy())
+
+    def testCreateInteraction(self):
+        from zope.security.interfaces import IInteraction
+        from zope.products.securitypolicy.zopepolicy import ZopeSecurityPolicy
+        i1 = ZopeSecurityPolicy().createInteraction(None)
+        verifyObject(IInteraction, i1)
+        self.assertEquals(list(i1.principals), [])
+
+        user = object()
+        i2 = ZopeSecurityPolicy().createInteraction(RequestStub(user))
+        verifyObject(IInteraction, i2)
+        self.assertEquals(list(i2.principals), [user])
 
     def testGlobalCheckPermission(self):
-        self.failUnless(
-            self.policy.checkPermission(self.read, None, Context(self.jim)))
-        self.failUnless(
-            self.policy.checkPermission(self.read, None, Context(self.tim)))
-        self.failUnless(
-            self.policy.checkPermission(self.write, None, Context(self.tim)))
+        self.failUnless(self.policy.checkPermission(self.read, None,
+                                                    Interaction(self.jim)))
+        self.failUnless(self.policy.checkPermission(self.read, None,
+                                                    Interaction(self.tim)))
+        self.failUnless(self.policy.checkPermission(self.write, None,
+                                                    Interaction(self.tim)))
 
         self.failIf(self.policy.checkPermission(
-            self.read, None, Context(self.unknown)))
+            self.read, None, Interaction(self.unknown)))
         self.failIf(self.policy.checkPermission(
-            self.write, None, Context(self.unknown)))
+            self.write, None, Interaction(self.unknown)))
 
         self.failIf(
             self.policy.checkPermission(
-            self.read, None, Context(self.unknown)))
+            self.read, None, Interaction(self.unknown)))
 
         self.__assertPermissions(self.jim, ['create', 'read'])
         self.__assertPermissions(self.tim, ['read', 'write'])
@@ -176,28 +195,28 @@
 
         self.failUnless(
             self.policy.checkPermission(
-            self.read, None, Context(self.unknown)))
+            self.read, None, Interaction(self.unknown)))
 
         self.__assertPermissions(self.unknown, ['read'])
 
         principalPermissionManager.grantPermissionToPrincipal(
             self.write, self.jim.getId())
-        self.failUnless(
-            self.policy.checkPermission(self.write, None, Context(self.jim)))
+        self.failUnless(self.policy.checkPermission(self.write, None,
+                                                    Interaction(self.jim)))
 
         self.__assertPermissions(self.jim, ['create', 'read', 'write'])
 
     def testPlaylessPrincipalRole(self):
         self.failIf(self.policy.checkPermission(
-            self.write, None, Context(self.jim)))
+            self.write, None, Interaction(self.jim)))
         principalRoleManager.assignRoleToPrincipal(
             self.manager, self.jim.getId())
         self.failUnless(self.policy.checkPermission(
-            self.write, None, Context(self.jim)))
+            self.write, None, Interaction(self.jim)))
         principalRoleManager.removeRoleFromPrincipal(
             self.manager, self.jim.getId())
         self.failIf(self.policy.checkPermission(
-            self.write, None, Context(self.jim)))
+            self.write, None, Interaction(self.jim)))
 
     def testPlayfulPrincipalRole(self):
         ztapi.provideAdapter(
@@ -209,15 +228,15 @@
         ob3 = TestClass(); ob3.__parent__ = ob2
 
         self.failIf(self.policy.checkPermission(
-            self.write, ob3, Context(self.jim)))
+            self.write, ob3, Interaction(self.jim)))
         AnnotationPrincipalRoleManager(ob3).assignRoleToPrincipal(
             self.manager, self.jim.getId())
         self.failUnless(self.policy.checkPermission(
-            self.write, ob3, Context(self.jim)))
+            self.write, ob3, Interaction(self.jim)))
         AnnotationPrincipalRoleManager(ob3).removeRoleFromPrincipal(
             self.manager, self.jim.getId())
         self.failIf(self.policy.checkPermission(
-            self.write, ob3, Context(self.jim)))
+            self.write, ob3, Interaction(self.jim)))
 
     def testPlayfulRolePermissions(self):
 
@@ -231,21 +250,23 @@
         ob2 = TestClass(); ob2.__parent__ = ob1
         ob3 = TestClass(); ob3.__parent__ = ob2
 
-        self.failIf(self.policy.checkPermission(test, ob3, Context(self.tim)))
+        self.failIf(self.policy.checkPermission(test, ob3,
+                                                Interaction(self.tim)))
         self.__assertPermissions(self.tim, ['read', 'write'], ob3)
 
         ARPM(ob2).grantPermissionToRole(test, self.manager)
         self.failUnless(self.policy.checkPermission(test, ob3,
-                                                    Context(self.tim)))
+                                                    Interaction(self.tim)))
         self.__assertPermissions(self.tim, ['read', 'test', 'write'], ob3)
 
-        self.failIf(self.policy.checkPermission(test, ob3, Context(self.jim)))
+        self.failIf(self.policy.checkPermission(test, ob3,
+                                                Interaction(self.jim)))
         self.__assertPermissions(self.jim, ['create', 'read'], ob3)
 
 
         ARPM(ob3).grantPermissionToRole(test, self.peon)
         self.failUnless(self.policy.checkPermission(
-            test, ob3, Context(self.jim)))
+            test, ob3, Interaction(self.jim)))
         self.__assertPermissions(self.jim, ['create', 'read', 'test'], ob3)
 
 
@@ -253,7 +274,7 @@
         principalPermissionManager.denyPermissionToPrincipal(
             test, self.jim.getId())
         self.failIf(self.policy.checkPermission(
-            test, ob3, Context(self.jim)))
+            test, ob3, Interaction(self.jim)))
         self.__assertPermissions(self.jim, ['create', 'read'], ob3)
 
         principalPermissionManager.unsetPermissionForPrincipal(
@@ -267,11 +288,12 @@
         new = principalRegistry.definePrincipal('new', 'Newbie',
                                                 'Newbie User', 'new', '098')
         principalRoleManager.assignRoleToPrincipal(self.arole, new.getId())
-        self.failUnless(self.policy.checkPermission(test, ob3, Context(new)))
+        self.failUnless(self.policy.checkPermission(test, ob3,
+                                                    Interaction(new)))
         self.__assertPermissions(new, ['test'], ob3)
 
         principalRoleManager.assignRoleToPrincipal(self.peon, new.getId())
-        self.failIf(self.policy.checkPermission(test, ob3, Context(new)))
+        self.failIf(self.policy.checkPermission(test, ob3, Interaction(new)))
         self.__assertPermissions(new, ['read'], ob3)
 
     def testPlayfulPrinciplePermissions(self):
@@ -286,30 +308,31 @@
         test = permissionRegistry.definePermission('test', 'Test', '')
         test = test.getId()
 
-        self.failIf(self.policy.checkPermission(test, ob3, Context(self.tim)))
+        self.failIf(self.policy.checkPermission(test, ob3,
+                                                Interaction(self.tim)))
 
         self.__assertPermissions(self.tim, ['read', 'write'], ob3)
 
         APPM(ob2).grantPermissionToPrincipal(test, self.tim.getId())
         self.failUnless(self.policy.checkPermission(test, ob3,
-                                                    Context(self.tim)))
+                                                    Interaction(self.tim)))
         self.__assertPermissions(self.tim, ['read', 'test', 'write'], ob3)
 
         APPM(ob3).denyPermissionToPrincipal(test, self.tim.getId())
         self.failIf(self.policy.checkPermission(test, ob3,
-                                                Context(self.tim)))
+                                                Interaction(self.tim)))
         self.__assertPermissions(self.tim, ['read', 'write'], ob3)
 
         APPM(ob1).denyPermissionToPrincipal(test, self.jim.getId())
         APPM(ob3).grantPermissionToPrincipal(test, self.jim.getId())
         self.failUnless(self.policy.checkPermission(test, ob3,
-                                                    Context(self.jim)))
+                                                    Interaction(self.jim)))
         self.__assertPermissions(self.jim, ['create', 'read', 'test'], ob3)
 
 
         APPM(ob3).unsetPermissionForPrincipal(test, self.jim.getId())
         self.failIf(self.policy.checkPermission(test, ob3,
-                                                Context(self.jim)))
+                                                Interaction(self.jim)))
         self.__assertPermissions(self.jim, ['create', 'read'], ob3)
 
         # make sure placeless principal permissions override placeful ones
@@ -317,7 +340,7 @@
         principalPermissionManager.denyPermissionToPrincipal(
             test, self.tim.getId())
         self.failIf(self.policy.checkPermission(test, ob3,
-                                                Context(self.tim)))
+                                                Interaction(self.tim)))
 
         self.__assertPermissions(self.tim, ['read', 'write'], ob3)
 




More information about the Zope3-Checkins mailing list