[Zope3-checkins] CVS: Zope3/src/zope/app/security/tests - test_zopesecuritypolicy.py:1.2

R. David Murray bitz@bitdance.com
Fri, 27 Dec 2002 20:39:55 -0500


Update of /cvs-repository/Zope3/src/zope/app/security/tests
In directory cvs.zope.org:/tmp/cvs-serv31808

Modified Files:
	test_zopesecuritypolicy.py 
Log Message:
Add test (and test scaffolding) to make sure checkPermissions and
permissionsOfPrincipal use roles returned by a principal's
getRoles method.


=== Zope3/src/zope/app/security/tests/test_zopesecuritypolicy.py 1.1 => 1.2 ===
--- Zope3/src/zope/app/security/tests/test_zopesecuritypolicy.py:1.1	Fri Dec 27 17:30:11 2002
+++ Zope3/src/zope/app/security/tests/test_zopesecuritypolicy.py	Fri Dec 27 20:39:55 2002
@@ -30,6 +30,7 @@
 from zope.app.interfaces.security import IRolePermissionManager
 from zope.app.security.registries.permissionregistry import permissionRegistry
 from zope.app.security.registries.principalregistry import principalRegistry
+from zope.app.security.registries.principalregistry import PrincipalBase
 from zope.app.security.registries.roleregistry import roleRegistry
 from zope.app.security.grants.principalpermission \
      import principalPermissionManager
@@ -57,6 +58,17 @@
 class Unprotected:
     pass
 
+class Principal(PrincipalBase):
+    def getRoles(self): return ['Manager']
+
+class AuthService:
+    """we only implement what we need for testing here"""
+    __implements__ = IAuthenticationService
+    def __init__(self):
+        self.data = {'jim': Principal('jim','Jim','Jim Fulton')}
+    def getPrincipal(self, id):
+        return self.data[id]
+
 
 class Test(PlacefulSetup, unittest.TestCase):
 
@@ -137,8 +149,12 @@
         from zope.app.security.zopesecuritypolicy import ZopeSecurityPolicy
         return ZopeSecurityPolicy()
 
-    def testImport( self ):
+    def __assertPermissions(self, user, expected, object=None):
+        permissions = list(permissionsOfPrincipal(user, object))
+        permissions.sort()
+        self.assertEqual(permissions, expected)
 
+    def testImport( self ):
         from zope.app.security.zopesecuritypolicy import ZopeSecurityPolicy
 
     def testGlobalCheckPermission(self):
@@ -177,10 +193,12 @@
 
         self.__assertPermissions(self.jim, ['create', 'read', 'write'])
 
-    def __assertPermissions(self, user, expected, object=None):
-        permissions = list(permissionsOfPrincipal(user, object))
-        permissions.sort()
-        self.assertEqual(permissions, expected)
+
+    def testUserWithRoles(self):
+        services.provideService('Authentication', AuthService(),force=True)
+        self.failUnless(
+            self.policy.checkPermission(self.write, None, Context(self.jim)))
+        self.__assertPermissions(self.jim, ['create', 'read', 'write'])
 
 
     def testPlayfulPrincipalRole(self):