[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Security/Registries/tests - __init__.py:1.1 testPermissionRegistry.py:1.1 testPrincipalRegistry.py:1.1 testRoleRegistry.py:1.1
Jim Fulton
jim@zope.com
Thu, 20 Jun 2002 11:55:04 -0400
Update of /cvs-repository/Zope3/lib/python/Zope/App/Security/Registries/tests
In directory cvs.zope.org:/tmp/cvs-serv15462/lib/python/Zope/App/Security/Registries/tests
Added Files:
__init__.py testPermissionRegistry.py testPrincipalRegistry.py
testRoleRegistry.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/Registries/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:02 jim Exp $
"""
=== Added File Zope3/lib/python/Zope/App/Security/Registries/tests/testPermissionRegistry.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 handler for 'definePermission' directive
$Id: testPermissionRegistry.py,v 1.1 2002/06/20 15:55:02 jim Exp $
"""
import unittest, sys
from Zope.App.Security.Registries.PermissionRegistry import permissionRegistry
from Zope.App.Security.IPermission import IPermission
from Interface.Verify import verifyObject
from Zope.Testing.CleanUp import CleanUp # Base class w registry cleanup
class Test(CleanUp, unittest.TestCase):
def testEmptyPermissions(self):
self.assertEqual(None, permissionRegistry.getPermission('Foo'))
self.failIf(permissionRegistry.definedPermission('Foo'))
def testPermissionStartsWithDot(self):
self.assertRaises(ValueError, permissionRegistry.definePermission,
'.Foo', 'dot foo title')
def testPermissionIsAnIPermission(self):
permissionRegistry.definePermission('Foo', 'foo title')
permission = permissionRegistry.getPermission('Foo')
self.assertEqual(verifyObject(IPermission, permission), 1)
def testDefinePermission(self):
perm = permissionRegistry.definePermission('Foo', 'foo title')
self.failUnless(verifyObject(IPermission, perm))
self.failUnless(permissionRegistry.definedPermission('Foo'))
permission = permissionRegistry.getPermission('Foo')
self.assertEquals(permission.getTitle(), 'foo title')
def testDefinePermissionWithTitle(self):
eq = self.assertEqual
permissionRegistry.definePermission('Foo', 'Foo-able')
permission = permissionRegistry.getPermission('Foo')
eq(permission.getTitle(), 'Foo-able')
eq(permission.getDescription(), '')
def testDefinePermissionWithTitleAndDescription(self):
eq = self.assertEqual
permissionRegistry.definePermission('Foo', 'Foo-able',
'A foo-worthy permission')
permission = permissionRegistry.getPermission('Foo')
eq(permission.getTitle(), 'Foo-able')
eq(permission.getDescription(), 'A foo-worthy permission')
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/Registries/tests/testPrincipalRegistry.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.Security.Registries.PrincipalRegistry import PrincipalRegistry
from Zope.App.Security.Registries.PrincipalRegistry \
import DuplicateLogin, DuplicateId
from Zope.Exceptions import NotFoundError
from Zope.Publisher.HTTP.IHTTPCredentials import IHTTPCredentials
from Zope.App.OFS.Services.ServiceManager.tests.PlacefulSetup \
import PlacefulSetup
class Request:
__implements__ = IHTTPCredentials
def __init__(self, lpw):
self.__lpw = lpw
def _authUserPW(self):
return self.__lpw
challenge = None
def unauthorized(self, challenge):
self.challenge = challenge
class Test(PlacefulSetup, unittest.TestCase):
def setUp(self):
PlacefulSetup.setUp(self)
from Zope.ComponentArchitecture import getService
from Zope.App.Security.BasicAuthAdapter import BasicAuthAdapter
from Zope.App.Security.ILoginPassword import ILoginPassword
getService(None,"Adapters").provideAdapter(
IHTTPCredentials, ILoginPassword, BasicAuthAdapter)
self.reg = PrincipalRegistry()
self.reg.definePrincipal('1', 'Tim Peters', 'Sir Tim Peters',
'tim', '123')
self.reg.definePrincipal('2', 'Jim Fulton', 'Sir Jim Fulton',
'jim', '456')
def testRegistered(self):
p = self.reg.getPrincipal('1')
self.assertEqual(p.getId(), '1')
self.assertEqual(p.getTitle(), 'Tim Peters')
self.assertEqual(p.getDescription(), 'Sir Tim Peters')
p = self.reg.getPrincipal('2')
self.assertEqual(p.getId(), '2')
self.assertEqual(p.getTitle(), 'Jim Fulton')
self.assertEqual(p.getDescription(), 'Sir Jim Fulton')
self.assertEqual(len(self.reg.getPrincipals('')), 2)
def testUnRegistered(self):
self.assertRaises(NotFoundError, self.reg.getPrincipal, '3')
def testDup(self):
self.assertRaises(DuplicateId,
self.reg.definePrincipal,
'1', 'Tim Peters', 'Sir Tim Peters',
'tim2', '123')
self.assertRaises(DuplicateLogin,
self.reg.definePrincipal,
'3', 'Tim Peters', 'Sir Tim Peters',
'tim', '123')
self.assertRaises(NotFoundError, self.reg.getPrincipal, '3')
self.assertEqual(len(self.reg.getPrincipals('')), 2)
def testSearch(self):
r = self.reg.getPrincipals('J')
self.assertEquals(len(r), 1)
self.failUnless(r[0] is self.reg.getPrincipal('2'))
def testByLogin(self):
tim = self.reg.getPrincipalByLogin('tim')
self.assertEquals(tim.getLogin(), 'tim')
jim = self.reg.getPrincipalByLogin('jim')
self.assertEquals(jim.getLogin(), 'jim')
self.assertRaises(NotFoundError,
self.reg.getPrincipalByLogin, 'kim')
def testValidation(self):
tim = self.reg.getPrincipalByLogin('tim')
self.assert_(tim.validate('123'))
self.failIf(tim.validate('456'))
self.failIf(tim.validate(''))
self.failIf(tim.validate('1234'))
self.failIf(tim.validate('12'))
def testAuthenticate(self):
req = Request(('tim', '123'))
pid = self.reg.authenticate(req)
self.assertEquals(pid, '1')
req = Request(('tim', '1234'))
pid = self.reg.authenticate(req)
self.assertEquals(pid, None)
req = Request(('kim', '123'))
pid = self.reg.authenticate(req)
self.assertEquals(pid, None)
def testUnauthorized(self):
request = Request(None)
self.reg.unauthorized(self.reg.unauthenticatedPrincipal(), request)
self.assertEquals(request.challenge, "basic realm=zope")
request = Request(None)
self.reg.unauthorized(None, request)
self.assertEquals(request.challenge, "basic realm=zope")
request = Request(None)
self.reg.unauthorized("1", request)
self.assertEquals(request.challenge, None)
def testDefaultPrincipal(self):
self.assertEquals(self.reg.unauthenticatedPrincipal(), None)
self.assertRaises(DuplicateId, self.reg.defineDefaultPrincipal,
"1", "tim")
self.reg.defineDefaultPrincipal("everybody", "Default Principal")
self.assertEquals(self.reg.unauthenticatedPrincipal(), "everybody")
self.reg.defineDefaultPrincipal("anybody", "Default Principal",
"This is the default headmaster")
self.assertEquals(self.reg.unauthenticatedPrincipal(), "anybody")
self.assertRaises(NotFoundError, self.reg.getPrincipal, "everybody")
p = self.reg.getPrincipal("anybody")
self.assertEquals(p.getId(), "anybody")
self.assertEquals(p.getTitle(), "Default Principal")
self.assertRaises(DuplicateId, self.reg.definePrincipal,
"anybody", "title")
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/Registries/tests/testRoleRegistry.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 handler for 'defineRole' directive """
import unittest, sys
from Zope.App.Security.Registries.RoleRegistry import roleRegistry
from Zope.App.Security.IRole import IRole
from Interface.Verify import verifyObject
from Zope.Testing.CleanUp import CleanUp # Base class w registry cleanup
class Test(CleanUp, unittest.TestCase):
def testEmptyRoles(self):
self.assertEqual(None, roleRegistry.getRole('Foo'))
self.failIf(roleRegistry.definedRole('Foo'))
def testRoleIsAnIRole(self):
r = roleRegistry.defineRole('Foo', 'Foo role')
role = roleRegistry.getRole(r.getId())
self.assertEqual(verifyObject(IRole, role), 1)
def testDefineRole(self):
role = roleRegistry.defineRole('Foo', 'foo role')
self.failUnless(verifyObject(IRole, role))
self.failUnless(roleRegistry.definedRole(role.getId()))
role = roleRegistry.getRole(role.getId())
self.assertEquals(role.getTitle(), 'foo role')
def testDefineRoleWithTitle(self):
eq = self.assertEqual
r = roleRegistry.defineRole('Foo', 'Foo-able')
role = roleRegistry.getRole(r.getId())
eq(role.getTitle(), 'Foo-able')
eq(role.getDescription(), '')
def testDefineRoleWithTitleAndDescription(self):
eq = self.assertEqual
r = roleRegistry.defineRole('Foo', 'Foo-able', 'A foo-worthy role')
role = roleRegistry.getRole(r.getId())
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())