[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Security/tests - testRolePermissionMap.py:1.1.2.1 testRoleRegistry.py:1.1.2.1
Barry Warsaw
barry@wooz.org
Wed, 12 Dec 2001 17:38:47 -0500
Update of /cvs-repository/Zope3/lib/python/Zope/App/Security/tests
In directory cvs.zope.org:/tmp/cvs-serv17705/lib/python/Zope/App/Security/tests
Added Files:
Tag: Zope-3x-branch
testRolePermissionMap.py testRoleRegistry.py
Log Message:
New tests for PermissionRegistry refactoring.
=== Added File Zope3/lib/python/Zope/App/Security/tests/testRolePermissionMap.py ===
# Copyright (c) 2001 Zope Coporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 1.1 (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 handler for RolePermissionMap module."""
import sys
import unittest
from Zope.App.Security import PermissionRegistry
from Zope.App.Security import RoleRegistry
from Zope.App.Security import RolePermissionMap
class Test(unittest.TestCase):
def tearDown(self):
from Zope.App.Security.RolePermissionMap import _clear
from Zope.App.Security.PermissionRegistry import _clear as _pclear
from Zope.App.Security.RoleRegistry import _clear as _rclear
_clear()
_pclear()
_rclear()
def testUnboundRolePermission(self):
permission = PermissionRegistry.definePermission('APerm')
role = RoleRegistry.defineRole('ARole')
self.assertEqual(RolePermissionMap.getRolesForPermission(permission),
[])
self.assertEqual(RolePermissionMap.getPermissionsForRole(role),
[])
def testRolePermission(self):
permission = PermissionRegistry.definePermission('APerm')
role = RoleRegistry.defineRole('ARole')
RolePermissionMap.grantPermissionToRole(permission, role)
self.assertEqual(RolePermissionMap.getRolesForPermission(permission),
[role])
self.assertEqual(RolePermissionMap.getPermissionsForRole(role),
[permission])
def testManyPermissionsOneRole(self):
perm1 = PermissionRegistry.definePermission('Perm One')
perm2 = PermissionRegistry.definePermission('Perm Two')
role1 = RoleRegistry.defineRole('Role One')
RolePermissionMap.grantPermissionToRole(perm1, role1)
RolePermissionMap.grantPermissionToRole(perm2, role1)
perms = RolePermissionMap.getPermissionsForRole(role1)
self.assertEqual(len(perms), 2)
self.failUnless(perm1 in perms)
self.failUnless(perm2 in perms)
def testManyRolesOnePermission(self):
perm1 = PermissionRegistry.definePermission('Perm One')
role1 = RoleRegistry.defineRole('Role One')
role2 = RoleRegistry.defineRole('Role Two')
RolePermissionMap.grantPermissionToRole(perm1, role1)
RolePermissionMap.grantPermissionToRole(perm1, role2)
roles = RolePermissionMap.getRolesForPermission(perm1)
self.assertEqual(len(roles), 2)
self.failUnless(role1 in roles)
self.failUnless(role2 in roles)
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/tests/testRoleRegistry.py ===
# Copyright (c) 2001 Zope Coporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 1.1 (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 handler for 'defineRole' directive """
import unittest, sys
from Zope.App.Security.RoleRegistry import defineRole
from Zope.App.Security.RoleRegistry import definedRole
from Zope.App.Security.RoleRegistry import getRole
from Zope.App.Security.IRole import IRole
from Interface.verify import verify
class Test(unittest.TestCase):
def tearDown(self):
from Zope.App.Security.RoleRegistry import _clear
_clear()
def testEmptyRoles(self):
self.assertRaises(LookupError, getRole, 'Foo')
self.failIf(definedRole('Foo'))
self.assertEqual(getRole('Foo', None), None)
def testRoleIsAnIRole(self):
defineRole('Foo')
role = getRole('Foo')
self.assertEqual(verify(IRole, role), 1)
def testDefineRole(self):
role = defineRole('Foo')
self.failUnless(verify(IRole, role))
self.failUnless(definedRole('Foo'))
role = getRole('Foo')
self.assertEquals(role.getTitle(), 'Foo')
def testDefineRoleWithTitle(self):
eq = self.assertEqual
defineRole('Foo', 'Foo-able')
role = getRole('Foo')
eq(role.getTitle(), 'Foo-able')
eq(role.getDescription(), '')
def testDefineRoleWithTitleAndDescription(self):
eq = self.assertEqual
defineRole('Foo', 'Foo-able', 'A foo-worthy role')
role = getRole('Foo')
eq(role.getTitle(), 'Foo-able')
eq(role.getDescription(), 'A foo-worthy role')
def test_suite():
loader=unittest.TestLoader()
return loader.loadTestsFromTestCase(Test)
if __name__=='__main__':
unittest.TextTestRunner().run(test_suite())