[Zope3-checkins] CVS: Zope3/src/zope/app/security/tests - test_zopepolicy.py:1.7

Albertas Agejevas alga@codeworks.lt
Thu, 13 Mar 2003 11:28:46 -0500


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

Modified Files:
	test_zopepolicy.py 
Log Message:
Made SecurityManager.getPrincipal() return a Principal object wrapped
in a context of its AuthenticationService.


=== Zope3/src/zope/app/security/tests/test_zopepolicy.py 1.6 => 1.7 ===
--- Zope3/src/zope/app/security/tests/test_zopepolicy.py:1.6	Tue Mar 11 11:11:20 2003
+++ Zope3/src/zope/app/security/tests/test_zopepolicy.py	Thu Mar 13 11:28:15 2003
@@ -64,27 +64,19 @@
 class Principal(PrincipalBase):
     def getRoles(self): return ['Manager']
 
-class AuthService:
-    """we only implement what we need for testing here"""
-    __implements__ = IAuthenticationService, ISimpleService
-    def __init__(self):
-        self.data = {'jim': Principal('jim','Jim','Jim Fulton')}
-    def getPrincipal(self, id):
-        return self.data[id]
-
 
 class Test(PlacefulSetup, unittest.TestCase):
 
     def setUp(self):
         PlacefulSetup.setUp(self)
 
-    
+
         services.defineService(Permissions, IPermissionService)
         services.provideService(Permissions, permissionRegistry)
-        
+
         services.defineService(Roles, IRoleService)
         services.provideService(Roles, roleRegistry)
-        
+
         services.defineService(Authentication, IAuthenticationService)
         services.provideService(Authentication, principalRegistry)
 
@@ -95,17 +87,14 @@
                        AttributeAnnotations)
 
         # set up some principals
-        jim = principalRegistry.definePrincipal('jim', 'Jim', 'Jim Fulton',
-                                                'jim', '123')
-        self.jim = jim.getId()
-
-        tim = principalRegistry.definePrincipal('tim', 'Tim', 'Tim Peters',
-                                                'tim', '456')
-        self.tim = tim.getId()
+        self.jim = principalRegistry.definePrincipal('jim', 'Jim', 'Jim Fulton',
+                                                     'jim', '123')
 
-        unknown = principalRegistry.defineDefaultPrincipal('unknown',
+        self.tim = principalRegistry.definePrincipal('tim', 'Tim', 'Tim Peters',
+                                                     'tim', '456')
+
+        self.unknown = principalRegistry.defineDefaultPrincipal('unknown',
                     'Unknown', 'Nothing is known about this principal')
-        self.unknown = unknown.getId()
 
         # set up some permissions
         read = permissionRegistry.definePermission('read', 'Read',
@@ -132,8 +121,10 @@
         self.arole = arole.getId()
 
         # grant and deny some permissions to a principal
-        principalPermissionManager.grantPermissionToPrincipal(self.create, self.jim)
-        principalPermissionManager.denyPermissionToPrincipal(self.update, self.jim)
+        principalPermissionManager.grantPermissionToPrincipal(self.create,
+                                                              self.jim.getId())
+        principalPermissionManager.denyPermissionToPrincipal(self.update,
+                                                             self.jim.getId())
 
         # grant and deny some permissions to the roles
         rolePermissionManager.grantPermissionToRole(self.read, self.peon)
@@ -142,8 +133,8 @@
         rolePermissionManager.grantPermissionToRole(self.write, self.manager)
 
         # ... and assign roles to principals
-        principalRoleManager.assignRoleToPrincipal(self.peon, self.jim)
-        principalRoleManager.assignRoleToPrincipal(self.manager, self.tim)
+        principalRoleManager.assignRoleToPrincipal(self.peon, self.jim.getId())
+        principalRoleManager.assignRoleToPrincipal(self.manager, self.tim.getId())
 
         self.policy = self._makePolicy()
 
@@ -190,7 +181,7 @@
         self.__assertPermissions(self.unknown, ['read'])
 
         principalPermissionManager.grantPermissionToPrincipal(
-            self.write, self.jim)
+            self.write, self.jim.getId())
         self.failUnless(
             self.policy.checkPermission(self.write, None, Context(self.jim)))
 
@@ -198,10 +189,10 @@
 
 
     def testUserWithRoles(self):
-        services.provideService(Authentication, AuthService(),force=True)
+        jim = Principal('jim','Jim','Jim Fulton')
         self.failUnless(
-            self.policy.checkPermission(self.write, None, Context(self.jim)))
-        self.__assertPermissions(self.jim, ['create', 'read', 'write'])
+            self.policy.checkPermission(self.write, None, Context(jim)))
+        self.__assertPermissions(jim, ['create', 'read', 'write'])
 
 
     def testPlayfulPrincipalRole(self):
@@ -216,7 +207,7 @@
         self.failIf(self.policy.checkPermission(
             self.write, ob, Context(self.jim)))
         AnnotationPrincipalRoleManager(ob).assignRoleToPrincipal(
-            self.manager, self.jim)
+            self.manager, self.jim.getId())
         self.failUnless(self.policy.checkPermission(
             self.write, ob, Context(self.jim)))
 
@@ -255,13 +246,13 @@
 
 
         principalPermissionManager.denyPermissionToPrincipal(
-            test, self.jim)
+            test, self.jim.getId())
         self.failIf(self.policy.checkPermission(
             test, ob, Context(self.jim)))
         self.__assertPermissions(self.jim, ['create', 'read'], ob)
 
         principalPermissionManager.unsetPermissionForPrincipal(
-            test, self.jim)
+            test, self.jim.getId())
 
         # Make sure multiple conflicting role permissions resolve correctly
         ARPM(ob2).grantPermissionToRole(test, 'Anonymous')
@@ -270,12 +261,11 @@
 
         new = principalRegistry.definePrincipal('new', 'Newbie',
                                                 'Newbie User', 'new', '098')
-        new = new.getId()
-        principalRoleManager.assignRoleToPrincipal(self.arole, new)
+        principalRoleManager.assignRoleToPrincipal(self.arole, new.getId())
         self.failUnless(self.policy.checkPermission(test, ob, Context(new)))
         self.__assertPermissions(new, ['test'], ob)
 
-        principalRoleManager.assignRoleToPrincipal(self.peon, new)
+        principalRoleManager.assignRoleToPrincipal(self.peon, new.getId())
         self.failIf(self.policy.checkPermission(test, ob, Context(new)))
         self.__assertPermissions(new, ['read'], ob)
 
@@ -296,32 +286,32 @@
 
         self.__assertPermissions(self.tim, ['read', 'write'], ob)
 
-        APPM(ob2).grantPermissionToPrincipal(test, self.tim)
+        APPM(ob2).grantPermissionToPrincipal(test, self.tim.getId())
         self.failUnless(self.policy.checkPermission(test, ob,
                                                     Context(self.tim)))
         self.__assertPermissions(self.tim, ['read', 'test', 'write'], ob)
 
-        APPM(ob3).denyPermissionToPrincipal(test, self.tim)
+        APPM(ob3).denyPermissionToPrincipal(test, self.tim.getId())
         self.failIf(self.policy.checkPermission(test, ob,
                                                 Context(self.tim)))
         self.__assertPermissions(self.tim, ['read', 'write'], ob)
 
-        APPM(ob1).denyPermissionToPrincipal(test, self.jim)
-        APPM(ob3).grantPermissionToPrincipal(test, self.jim)
+        APPM(ob1).denyPermissionToPrincipal(test, self.jim.getId())
+        APPM(ob3).grantPermissionToPrincipal(test, self.jim.getId())
         self.failUnless(self.policy.checkPermission(test, ob,
                                                     Context(self.jim)))
         self.__assertPermissions(self.jim, ['create', 'read', 'test'], ob)
 
 
-        APPM(ob3).unsetPermissionForPrincipal(test, self.jim)
+        APPM(ob3).unsetPermissionForPrincipal(test, self.jim.getId())
         self.failIf(self.policy.checkPermission(test, ob,
                                                 Context(self.jim)))
         self.__assertPermissions(self.jim, ['create', 'read'], ob)
 
         # make sure placeless principal permissions override placeful ones
-        APPM(ob).grantPermissionToPrincipal(test, self.tim)
+        APPM(ob).grantPermissionToPrincipal(test, self.tim.getId())
         principalPermissionManager.denyPermissionToPrincipal(
-            test, self.tim)
+            test, self.tim.getId())
         self.failIf(self.policy.checkPermission(test, ob,
                                                 Context(self.tim)))