[CMF-checkins] CVS: CMF/CMFCore/tests/base - security.py:1.1.2.1 testcase.py:1.1.2.1
Chris Withers
chrisw@nipltd.com
Tue, 12 Feb 2002 14:02:08 -0500
Update of /cvs-repository/CMF/CMFCore/tests/base
In directory cvs.zope.org:/tmp/cvs-serv20982/base
Added Files:
Tag: ChrisW-refactor_tests-branch
security.py testcase.py
Log Message:
Going home.
=== Added File CMF/CMFCore/tests/base/security.py ===
from Acquisition import Implicit
class PermissiveSecurityPolicy:
"""
Very permissive security policy for unit testing purposes.
"""
#
# Standard SecurityPolicy interface
#
def validate( self
, accessed=None
, container=None
, name=None
, value=None
, context=None
, roles=None
, *args
, **kw):
return 1
def checkPermission( self, permission, object, context) :
if permission == 'forbidden permission':
return 0
return 1
class OmnipotentUser( Implicit ):
"""
Omnipotent User for unit testing purposes.
"""
def getId( self ):
return 'all_powerful_Oz'
getUserName = getId
def allowed( self, object, object_roles=None ):
return 1
class UserWithRoles( Implicit ):
"""
User with roles specified in constructor
for unit testing purposes.
"""
def __init__( self, *roles ):
self._roles = roles
def getId( self ):
return 'high_roller'
getUserName = getId
def allowed( self, object, object_roles=None ):
for orole in object_roles:
if orole in self._roles:
return 1
return 0
class AnonymousUser( Implicit ):
"""
Anonymous USer for unit testing purposes.
"""
def getId( self ):
return 'unit_tester'
getUserName = getId
def has_permission(self, permission, obj):
# For types tool tests dealing with filtered_meta_types
return 1
def allowed( self, object, object_roles=None ):
# for testing permissions on actions
if object.getId() == 'actions_dummy':
if 'Anonymous' in object_roles:
return 1
else:
return 0
return 1
=== Added File CMF/CMFCore/tests/base/testcase.py ===
import Zope
from unittest import TestCase
from AccessControl.SecurityManagement import newSecurityManager
from AccessControl.SecurityManagement import noSecurityManager
from AccessControl.SecurityManager import setSecurityPolicy
from Testing.makerequest import makerequest
from security import PermissiveSecurityPolicy, AnonymousUser
class TransactionalTest( TestCase ):
def setUp( self ):
get_transaction().begin()
self.connection = Zope.DB.open()
self.root = self.connection.root()[ 'Application' ]
def tearDown( self ):
get_transaction().abort()
self.connection.close()
class RequestTest( TransactionalTest ):
def setUp(self):
TransactionalTest.setUp(self)
self.root = makerequest(self.root)
class SecurityTest( TestCase ):
def setUp(self):
get_transaction().begin()
self._policy = PermissiveSecurityPolicy()
self._oldPolicy = setSecurityPolicy(self._policy)
self.connection = Zope.DB.open()
self.root = self.connection.root()[ 'Application' ]
newSecurityManager( None, AnonymousUser().__of__( self.root ) )
def tearDown( self ):
get_transaction().abort()
self.connection.close()
noSecurityManager()
setSecurityPolicy(self._oldPolicy)
class SecurityRequestTest( SecurityTest ):
def setUp(self):
SecurityTest.setUp(self)
self.root = makerequest(self.root)