[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Security/Grants/Views/Browser/tests - PermissionService.py:1.1 RolePermissionManager.py:1.1 RoleService.py:1.1 __init__.py:1.1 testPrincipalPermissionView.py:1.1 testPrincipalRoleView.py:1.1 testRolePermissionView.py:1.1
Jim Fulton
jim@zope.com
Thu, 20 Jun 2002 11:55:03 -0400
Update of /cvs-repository/Zope3/lib/python/Zope/App/Security/Grants/Views/Browser/tests
In directory cvs.zope.org:/tmp/cvs-serv15462/lib/python/Zope/App/Security/Grants/Views/Browser/tests
Added Files:
PermissionService.py RolePermissionManager.py RoleService.py
__init__.py testPrincipalPermissionView.py
testPrincipalRoleView.py testRolePermissionView.py
Log Message:
implemented:
http://dev.zope.org/Wikis/DevSite/Projects/ComponentArchitecture/MergeSecurityIntoZopeNamespace
While I was at it, I couldn't resist implementing a variation of:
http://dev.zope.org/Wikis/DevSite/Projects/ComponentArchitecture/SecurityPackageReorg
which was a lot more work.
=== Added File Zope3/lib/python/Zope/App/Security/Grants/Views/Browser/tests/PermissionService.py ===
##############################################################################
#
# 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.
#
##############################################################################
"""PermissionService implementation for testing
$Id: PermissionService.py,v 1.1 2002/06/20 15:55:01 jim Exp $
"""
from Zope.App.Security.IPermissionService import IPermissionService
from Zope.App.Security.IPermission import IPermission
class Permission:
__implements__ = IPermission
def __init__(self, id, title): self._id, self._title = id, title
def getId(self): return self._id
def getTitle(self): return self._title
def getDescription(self): return ''
class PermissionService:
__implements__ = IPermissionService
def __init__(self, **kw):
self._permissions = r = {}
for id, title in kw.items(): r[id]=Permission(id, title)
# Implementation methods for interface
# Zope.App.Security.IPermissionService.
def getPermission(self, rid):
'''See interface IPermissionService'''
return self._permissions.get(rid)
def getPermissions(self):
'''See interface IPermissionService'''
return self._permissions.values()
=== Added File Zope3/lib/python/Zope/App/Security/Grants/Views/Browser/tests/RolePermissionManager.py ===
##############################################################################
#
# 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.
#
##############################################################################
"""
Test IRolePermissionManager class.
$Id: RolePermissionManager.py,v 1.1 2002/06/20 15:55:01 jim Exp $
"""
from Zope.App.Security.IRolePermissionManager import IRolePermissionManager
from Zope.App.Security.Settings import Allow, Assign
class RolePermissionManager:
__implements__ = IRolePermissionManager
def __init__(self, **rp):
self._rp = rp
# Implementation methods for interface
# Zope.App.Security.IRolePermissionManager.
def getRolesForPermission(self, permission):
'''See interface IRolePermissionMap'''
r=[]
for role, permissions in self._rp.items():
if permission in permissions: r.append((role, Allow))
return r
def getPermissionAcquired(self, permission):
'''See interface IRolePermissionMap'''
return 1
def getPermissionsForRole(self, role):
'''See interface IRolePermissionMap'''
return [(perm, Allow) for perm in self._rp[role]]
def setPermissionAcquired(self, permission, flag):
'''See interface IRolePermissionManager'''
raise TypeError
def unsetPermissionFromRole(self, permission, role):
'''See interface IRolePermissionManager'''
permissions = self._rp.get(role, ())
if permission in permissions:
permissions.remove(permission)
if not permissions:
# XXX: this del removed by Steve and Casey
# in order to get the PermissionsForRole
# view unit tests to work correctly.
#
# Why is this del here?
#
# It doesn't seem to break anything to remove
# it, like this!
#del self._rp[role]
pass
def grantPermissionToRole(self, permission, role):
'''See interface IRolePermissionManager'''
if role in self._rp:
if permission not in self._rp[role]:
self._rp[role].append(permission)
else:
self._rp[role] = [permission]
=== Added File Zope3/lib/python/Zope/App/Security/Grants/Views/Browser/tests/RoleService.py ===
##############################################################################
#
# 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.
#
##############################################################################
"""RoleService implementation for testing
$Id: RoleService.py,v 1.1 2002/06/20 15:55:01 jim Exp $
"""
from Zope.App.Security.IRoleService import IRoleService
from Zope.App.Security.IRole import IRole
class Role:
__implements__ = IRole
def __init__(self, id, title): self._id, self._title = id, title
def getId(self): return self._id
def getTitle(self): return self._title
def getDescription(self): return ''
class RoleService:
__implements__ = IRoleService
def __init__(self, **kw):
self._roles = r = {}
for id, title in kw.items(): r[id]=Role(id, title)
# Implementation methods for interface
# Zope.App.Security.IRoleService.
def getRole(self, rid):
'''See interface IRoleService'''
return self._roles.get(rid)
def getRoles(self):
'''See interface IRoleService'''
return self._roles.values()
=== Added File Zope3/lib/python/Zope/App/Security/Grants/Views/Browser/tests/__init__.py ===
##############################################################################
#
# Copyright (c) 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.
#
##############################################################################
"""XXX short summary goes here.
XXX longer description goes here.
$Id: __init__.py,v 1.1 2002/06/20 15:55:01 jim Exp $
"""
=== Added File Zope3/lib/python/Zope/App/Security/Grants/Views/Browser/tests/testPrincipalPermissionView.py ===
##############################################################################
#
# 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.
#
##############################################################################
"""
$Id: testPrincipalPermissionView.py,v 1.1 2002/06/20 15:55:01 jim Exp $
"""
import unittest
from Zope.ComponentArchitecture import getService, getServiceManager
from Zope.App.OFS.Annotation.IAttributeAnnotatable import IAttributeAnnotatable
from Zope.App.OFS.Annotation.IAnnotations import IAnnotations
from Zope.App.OFS.Annotation.AttributeAnnotations import AttributeAnnotations
from Zope.App.Security.IPermissionService import IPermissionService
from Zope.App.Security.IAuthenticationService import IAuthenticationService
from Zope.App.Security.IPrincipalPermissionManager \
import IPrincipalPermissionManager
from Zope.App.Security.IPrincipalPermissionMap import IPrincipalPermissionMap
from Zope.App.Security.Settings import Allow, Deny, Unset
from Zope.App.OFS.Services.ServiceManager.tests.PlacefulSetup \
import PlacefulSetup
class DummyContext:
__implements__ = IAttributeAnnotatable
#IPrincipalPermissionManager, IPrincipalPermissionMap
class DummyPermissionService:
__implements__ = IPermissionService
def __init__(self, perm_objs):
perms = {}
for perm_obj in perm_objs:
perms[perm_obj.getId()] = perm_obj
self.perms = perms
def getPermission(self,pr_id):
return self.perms[pr_id]
def getPermissions(self):
return self.perms.keys()
class DummyAuthenticationService:
__implements__ = IAuthenticationService
def __init__(self, principals):
pr = {}
for principal in principals:
pr[principal.getId()] = principal
self.principals = pr
def getPrincipal(self, principal_id):
return self.principals[principal_id]
class DummyAdapter:
__implements__ = IPrincipalPermissionManager, IPrincipalPermissionMap
def __init__(self, context):
self._context = context
if not hasattr(self._context,'principals'):
self._context.principals = {}
def grantPermissionToPrincipal(self, permission, principal):
if not (principal in self._context.principals):
self._context.principals[principal]={}
self._context.principals[principal][permission]=Allow
def denyPermissionToPrincipal(self, permission, principal):
if not (principal in self._context.principals):
self._context.principals[principal]={}
self._context.principals[principal][permission]=Deny
def unsetPermissionForPrincipal(self, permission, principal):
if not (principal in self._context.principals):
return
try:
del self._context.principals[principal][permission]
except KeyError:
pass
def getSetting(self, permission, principal):
try:
setting = self._context.principals[principal][permission]
except KeyError:
setting = Unset
return setting
def getPrincipalsForPermission(self, permission):
ret = []
for principal, permissions in self._context.principals.items():
if permissions in permissions:
ret.append((principal, permissions[permission]))
return ret
def getPermissionsForPrincipal(self, principal):
try:
return self._context.principals[principal].items()
except KeyError:
return []
class DummyObject:
def __init__(self, id, title):
self._id = id
self._title = title
def getId(self):
return self._id
def getTitle(self):
return self._title
class Test(PlacefulSetup, unittest.TestCase):
def setUp(self):
PlacefulSetup.setUp(self)
self._permissions = []
self._permissions.append(DummyObject('qux', 'Qux'))
self._permissions.append(DummyObject('baz', 'Baz'))
defineService=getServiceManager(None).defineService
provideService=getServiceManager(None).provideService
defineService(
'PermissionService', IPermissionService)
provideService('PermissionService',
DummyPermissionService(self._permissions))
defineService('AuthenticationService',
IAuthenticationService)
self._principals = []
self._principals.append(DummyObject('foo', 'Foo'))
self._principals.append(DummyObject('bar', 'Bar'))
provideService('AuthenticationService',
DummyAuthenticationService(principals = self._principals))
provideAdapter=getService(None,'Adapters').provideAdapter
provideAdapter(IAttributeAnnotatable,
IPrincipalPermissionManager, DummyAdapter)
provideAdapter(
IAttributeAnnotatable, IAnnotations, AttributeAnnotations)
def _makeOne(self):
from Zope.App.Security.Grants.Views.Browser.PrincipalPermissionView \
import PrincipalPermissionView
return PrincipalPermissionView(DummyContext(), None)
def testGrantPermissions(self):
view = self._makeOne()
allowed_perms = view.getPermissionsForPrincipal(
self._principals[0].getId(), 'Allow')
denied_perms = view.getPermissionsForPrincipal(
self._principals[0].getId(), 'Deny')
self.assertEqual(len(allowed_perms), 0, 'List not empty')
self.assertEqual(len(denied_perms), 0, 'List not empty')
view.grantPermissions(self._principals[0].getId(),
[self._permissions[0].getId()])
self.assertEqual(view.getPermissionsForPrincipal(
self._principals[0].getId(),'Allow'),
[self._permissions[0]])
self.assertEqual(view.getPermissionsForPrincipal(
self._principals[0].getId(),'Deny'),
[])
view.grantPermissions(self._principals[0].getId(),
[self._permissions[1].getId()])
self.assertEqual(view.getPermissionsForPrincipal(
self._principals[0].getId(),'Allow').sort(),
self._permissions.sort())
self.assertEqual(view.getPermissionsForPrincipal(
self._principals[0].getId(),'Deny'),
[])
view.grantPermissions(self._principals[1].getId(),
[self._permissions[0].getId()])
self.assertEqual(view.getPermissionsForPrincipal(
self._principals[1].getId(),'Allow'),
[self._permissions[0]])
self.assertEqual(view.getPermissionsForPrincipal(
self._principals[1].getId(),'Deny'),
[])
view.grantPermissions(self._principals[1].getId(),
[self._permissions[1].getId()])
self.assertEqual(view.getPermissionsForPrincipal(
self._principals[1].getId(),'Allow').sort(),
self._permissions.sort())
self.assertEqual(view.getPermissionsForPrincipal(
self._principals[1].getId(),'Deny'),
[])
def testDenyPermissions(self):
view = self._makeOne()
allowed_perms = view.getPermissionsForPrincipal(
self._principals[0].getId(), 'Allow')
denied_perms = view.getPermissionsForPrincipal(
self._principals[0].getId(), 'Deny')
self.assertEqual(len(allowed_perms), 0, 'List not empty')
self.assertEqual(len(denied_perms), 0, 'List not empty')
view.denyPermissions(self._principals[0].getId(),
[self._permissions[0].getId()])
self.assertEqual(view.getPermissionsForPrincipal(
self._principals[0].getId(),'Deny'),
[self._permissions[0]])
self.assertEqual(view.getPermissionsForPrincipal(
self._principals[0].getId(),'Allow'),
[])
view.denyPermissions(self._principals[0].getId(),
[self._permissions[1].getId()])
self.assertEqual(view.getPermissionsForPrincipal(
self._principals[0].getId(),'Deny').sort(),
self._permissions.sort())
self.assertEqual(view.getPermissionsForPrincipal(
self._principals[0].getId(),'Allow'),
[])
view.denyPermissions(self._principals[1].getId(), [
self._permissions[0].getId()])
self.assertEqual(view.getPermissionsForPrincipal(
self._principals[1].getId(),'Deny'),
[self._permissions[0]])
self.assertEqual(view.getPermissionsForPrincipal(
self._principals[1].getId(),'Allow'),
[])
view.denyPermissions(self._principals[1].getId(),
[self._permissions[1].getId()])
self.assertEqual(view.getPermissionsForPrincipal(
self._principals[1].getId(),'Deny').sort(),
self._permissions.sort())
self.assertEqual(view.getPermissionsForPrincipal(
self._principals[1].getId(),'Allow'),
[])
def testAllowDenyPermissions(self):
view = self._makeOne()
allowed_perms = view.getPermissionsForPrincipal(
self._principals[0].getId(), 'Allow')
denied_perms = view.getPermissionsForPrincipal(
self._principals[0].getId(), 'Deny')
self.assertEqual(len(allowed_perms), 0, 'List not empty')
self.assertEqual(len(denied_perms), 0, 'List not empty')
view.grantPermissions(self._principals[0].getId(),
[self._permissions[0].getId()])
self.assertEqual(view.getPermissionsForPrincipal(
self._principals[0].getId(),'Allow'),
[self._permissions[0]])
self.assertEqual(view.getPermissionsForPrincipal(
self._principals[0].getId(),'Deny'),
[])
allowed_perms = view.getPermissionsForPrincipal(
self._principals[0].getId(), 'Allow')
self.assertEqual(len(allowed_perms), 1, 'List has wrong length')
# Now change it to deny
view.denyPermissions(self._principals[0].getId(),
[self._permissions[0].getId()])
self.assertEqual(view.getPermissionsForPrincipal(
self._principals[0].getId(),'Deny'),
[self._permissions[0]])
self.assertEqual(view.getPermissionsForPrincipal(
self._principals[0].getId(),'Allow'),
[])
view.grantPermissions(self._principals[0].getId(),
[self._permissions[1].getId()])
self.assertEqual(view.getPermissionsForPrincipal(
self._principals[0].getId(),'Deny'), [self._permissions[0]])
self.assertEqual(view.getPermissionsForPrincipal(
self._principals[0].getId(),'Allow'), [self._permissions[1]])
def testUnsetPermissions(self):
view = self._makeOne()
view.grantPermissions(self._principals[0].getId(),
[self._permissions[0].getId()])
allowed_perms = view.getPermissionsForPrincipal(
self._principals[0].getId(), 'Allow')
self.assertEqual(len(allowed_perms), 1, 'List has wrong length')
view.unsetPermissions(self._principals[0].getId(),
[self._permissions[0].getId()])
allowed_perms = view.getPermissionsForPrincipal(
self._principals[0].getId(), 'Allow')
self.assertEqual(len(allowed_perms), 0, 'Permission not unset')
# Deleting mutiple in one step
view.grantPermissions(self._principals[0].getId(),
[self._permissions[0].getId(),
self._permissions[1].getId()])
allowed_perms = view.getPermissionsForPrincipal(
self._principals[0].getId(), 'Allow')
self.assertEqual(len(allowed_perms), 2, 'List has wrong length')
view.unsetPermissions(self._principals[0].getId(),
[self._permissions[0].getId(),
self._permissions[1].getId()])
allowed_perms = view.getPermissionsForPrincipal(
self._principals[0].getId(), 'Allow')
self.assertEqual(len(allowed_perms), 0, 'Some permissions not unset')
# Deleting in a row
view.grantPermissions(self._principals[0].getId(),
[self._permissions[0].getId(),
self._permissions[1].getId()])
allowed_perms = view.getPermissionsForPrincipal(
self._principals[0].getId(), 'Allow')
self.assertEqual(len(allowed_perms), 2, 'List has wrong length')
view.unsetPermissions(self._principals[0].getId(),
[self._permissions[0].getId()])
allowed_perms = view.getPermissionsForPrincipal(
self._principals[0].getId(), 'Allow')
self.assertEqual(len(allowed_perms), 1, 'Some permissions not unset')
view.unsetPermissions(self._principals[0].getId(),
[self._permissions[1].getId()])
allowed_perms = view.getPermissionsForPrincipal(
self._principals[0].getId(), 'Allow')
self.assertEqual(len(allowed_perms), 0, 'Not all permissions unset')
# Ask for an other way of getting the unset permisssions
unset_perms = view.getUnsetPermissionsForPrincipal(
self._principals[0].getId())
self.assertEqual(len(unset_perms), 2, 'Not all permissions unset')
def test_suite():
loader=unittest.TestLoader()
return loader.loadTestsFromTestCase(Test)
if __name__=='__main__':
unittest.TextTestRunner().run(test_suite())
=== Added File Zope3/lib/python/Zope/App/Security/Grants/Views/Browser/tests/testPrincipalRoleView.py ===
##############################################################################
#
# 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.
#
##############################################################################
"""
$Id: testPrincipalRoleView.py,v 1.1 2002/06/20 15:55:01 jim Exp $
"""
import unittest
from Zope.App.OFS.Services.ServiceManager.tests.PlacefulSetup\
import PlacefulSetup
from Zope.ComponentArchitecture import getServiceManager
from Zope.App.Security.IRoleService import IRoleService
from Zope.App.Security.IAuthenticationService import IAuthenticationService
from Zope.App.Security.IPrincipalRoleManager import IPrincipalRoleManager
from Zope.App.Security.IPrincipalRoleMap import IPrincipalRoleMap
class DummyManager:
__implements__ = IPrincipalRoleManager
def getSetting(self, role, principal):
return '%r:%r' % (role, principal)
class DummyRoleService:
__implements__ = IRoleService
def __init__(self, roles):
self._roles = roles
def getRoles(self):
return self._roles
class DummyObject:
def __init__(self, id, title):
self._id = id
self._title = title
def getId(self):
return self._id
def getTitle(self):
return self._title
class DummyAuthenticationService:
__implements__ = IAuthenticationService
def __init__(self, principals):
self._principals = principals
def getPrincipals(self):
return self._principals
class Test(PlacefulSetup, unittest.TestCase):
def setUp(self):
PlacefulSetup.setUp(self)
self._roles = []
self._roles.append(DummyObject('qux', 'Qux'))
self._roles.append(DummyObject('baz', 'Baz'))
defineService=getServiceManager(None).defineService
provideService=getServiceManager(None).provideService
defineService('RoleService', IRoleService)
provideService('RoleService'
, DummyRoleService(roles = self._roles))
defineService('AuthenticationService', IAuthenticationService)
self._principals = []
self._principals.append(DummyObject('foo', 'Foo'))
self._principals.append(DummyObject('bar', 'Bar'))
provideService('AuthenticationService',
DummyAuthenticationService(principals = self._principals))
def _makeOne(self):
from Zope.App.Security.Grants.Views.Browser.PrincipalRoleView \
import PrincipalRoleView
return PrincipalRoleView(DummyManager(), None)
def testRoles(self):
view = self._makeOne()
roles = list(view.getAllRoles())
self.assertEqual(len(roles), 2)
ids = map(lambda x: x.getId(), self._roles)
titles = map(lambda x: x.getTitle(), self._roles)
for role in roles:
self.failUnless(role.getId() in ids)
self.failUnless(role.getTitle() in titles)
def testPrincipals(self):
view = self._makeOne()
principals = list(view.getAllPrincipals())
self.assertEqual(len(principals), 2)
ids = map(lambda x: x.getId(), self._principals)
titles = map(lambda x: x.getTitle(), self._principals)
for principal in principals:
self.failUnless(principal.getId() in ids)
self.failUnless(principal.getTitle() in titles)
def testPrincipalRoleGrid(self):
view = self._makeOne()
grid = view.createGrid()
p_ids = [p.getId() for p in view.getAllPrincipals()]
r_ids = [r.getId() for r in view.getAllRoles()]
self.failUnless(grid.listAvailableValues())
for id in [p.getId() for p in grid.principals()]:
self.failUnless(id in p_ids)
for id in [r.getId() for r in grid.roles()]:
self.failUnless(id in r_ids)
map = DummyManager()
grid_entries = [(r, p, map.getSetting(r, p))
for r in grid.roles()
for p in grid.principals()]
for r, p, setting in grid_entries:
self.assertEquals(setting, grid.getValue(r, p))
def test_suite():
loader=unittest.TestLoader()
return loader.loadTestsFromTestCase(Test)
if __name__=='__main__':
unittest.TextTestRunner().run(test_suite())
=== Added File Zope3/lib/python/Zope/App/Security/Grants/Views/Browser/tests/testRolePermissionView.py ===
##############################################################################
#
# 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.
#
##############################################################################
import unittest, sys
from Zope.App.OFS.Services.ServiceManager.tests.PlacefulSetup\
import PlacefulSetup
from Zope.ComponentArchitecture import getServiceManager
from Zope.App.Security.IRoleService import IRoleService
from RoleService import RoleService
from PermissionService import PermissionService
from RolePermissionManager import RolePermissionManager
from Zope.App.Security.Grants.Views.Browser.RolePermissionView \
import RolePermissionView
from Zope.App.Security.IPermissionService import IPermissionService
class Test(PlacefulSetup, unittest.TestCase):
def setUp(self):
PlacefulSetup.setUp(self)
defineService=getServiceManager(None).defineService
provideService=getServiceManager(None).provideService
defineService('RoleService', IRoleService)
provideService('RoleService', RoleService(
manager='Manager', member='Member'))
defineService('PermissionService', IPermissionService)
provideService('PermissionService', PermissionService(
read='Read', write='Write'))
self.view = RolePermissionView(RolePermissionManager(), None)
def testRoles(self):
roles = list(self.view.roles())
ids = ['manager', 'member']
titles = ['Manager', 'Member']
for role in roles:
i=ids.index(role.getId())
self.failIf(i < 0)
self.assertEqual(role.getTitle(), titles[i])
del ids[i]
del titles[i]
def testPermisssions(self):
permissions = list(self.view.permissions())
ids = ['read', 'write']
titles = ['Read', 'Write']
for permission in permissions:
i=ids.index(permission.getId())
self.failIf(i < 0)
self.assertEqual(permission.getTitle(), titles[i])
del ids[i]
del titles[i]
def testGrant(self):
roles = self.view.roles()
permissions = self.view.permissions()
self.view.action({
'p0': 'read', 'p1': 'write',
'r0': 'manager', 'r1': 'member',
'p0r0': '1', 'p0r1': '1', 'p1r0': '1',
},
testing=1)
permissionRoles = self.view.permissionRoles()
for ip in range(len(permissionRoles)):
permissionRole = permissionRoles[ip]
rset = permissionRole.roles()
for ir in range(len(rset)):
setting = rset[ir]
if setting is None:
self.failIf(
roles[ir].getId() == 'manager'
or
permissions[ip].getId() == 'read'
)
else:
self.failUnless(
roles[ir].getId() == 'manager'
or
permissions[ip].getId() == 'read'
)
self.view.action({
'p0': 'read', 'p1': 'write',
'r0': 'manager', 'r1': 'member',
'p0r0': '1',
},
testing=1)
permissionRoles = self.view.permissionRoles()
for ip in range(len(permissionRoles)):
permissionRole = permissionRoles[ip]
rset = permissionRole.roles()
for ir in range(len(rset)):
setting = rset[ir]
if setting is None:
self.failIf(
roles[ir].getId() == 'manager'
and
permissions[ip].getId() == 'read'
)
else:
self.failUnless(
roles[ir].getId() == 'manager'
and
permissions[ip].getId() == 'read'
)
self.view.update_permission(REQUEST=None,
permission_id='write',
roles=['member'],
testing=1)
permission = self.view.permissionForID('write')
self.assertEquals(
[r['id']
for r in permission.rolesInfo()
if r['checked']],
['member'])
self.view.update_permission(REQUEST=None,
permission_id='write',
# roles=[], roles attr omitted
testing=1)
permission = self.view.permissionForID('write')
self.assertEquals(
[r['id']
for r in permission.rolesInfo()
if r['checked']],
[])
self.view.update_permission(REQUEST=None,
permission_id='write',
roles=['manager','member'],
testing=1)
permission = self.view.permissionForID('write')
result = [r['id']
for r in permission.rolesInfo()
if r['checked']]
what_result_should_be = ['manager','member']
result.sort()
what_result_should_be.sort()
self.assertEquals(
result,
what_result_should_be
)
self.view.update_role(REQUEST=None,
role_id='member',
permissions=['write','read'],
testing=1)
role = self.view.roleForID('member')
result = [r['id']
for r in role.permissionsInfo()
if r['checked']]
what_result_should_be = ['write','read']
result.sort()
what_result_should_be.sort()
self.assertEquals(
result,
what_result_should_be
)
self.view.update_role(REQUEST=None,
role_id='member',
# omitted attribute permissions,
testing=1)
role = self.view.roleForID('member')
result = [r['id']
for r in role.permissionsInfo()
if r['checked']]
what_result_should_be = []
result.sort()
what_result_should_be.sort()
self.assertEquals(
result,
what_result_should_be
)
def test_suite():
loader=unittest.TestLoader()
return loader.loadTestsFromTestCase(Test)
if __name__=='__main__':
unittest.TextTestRunner().run(test_suite())