[CMF-checkins] CVS: CMF/CMFCore/tests - test_TypesTool.py:1.6
Tres Seaver
tseaver@zope.com
Thu, 29 Nov 2001 22:09:11 -0500
Update of /cvs-repository/CMF/CMFCore/tests
In directory cvs.zope.org:/tmp/cvs-serv3916/CMFCore/tests
Modified Files:
test_TypesTool.py
Log Message:
- Flesh out construction tests for FTI.
=== CMF/CMFCore/tests/test_TypesTool.py 1.5 => 1.6 ===
import Acquisition
from AccessControl.SecurityManagement import newSecurityManager
+from AccessControl.SecurityManagement import noSecurityManager
from AccessControl import SecurityManager
from Products.CMFCore.TypesTool import *
from Products.CMFCore.PortalContent import PortalContent
@@ -12,7 +13,7 @@
from Products.CMFCore import utils
import ZPublisher.HTTPRequest
-class UnitTestSecurityPolicy:
+class PermissiveSecurityPolicy:
"""
Stub out the existing security policy for unit testing purposes.
"""
@@ -35,6 +36,36 @@
return 0
return 1
+class OmnipotentUser( Acquisition.Implicit ):
+ """
+ Stubbed out manager for unit testing purposes.
+ """
+ def getId( self ):
+ return 'all_powerful_Oz'
+
+ getUserName = getId
+
+ def allowed( self, object, object_roles=None ):
+ return 1
+
+class UserWithRoles( Acquisition.Implicit ):
+ """
+ Stubbed out manager 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 UnitTestUser( Acquisition.Implicit ):
"""
Stubbed out manager for unit testing purposes.
@@ -78,8 +109,8 @@
def setUp( self ):
get_transaction().begin()
- self._policy = UnitTestSecurityPolicy()
- SecurityManager.setSecurityPolicy(self._policy)
+ self._policy = PermissiveSecurityPolicy()
+ self._oldPolicy = SecurityManager.setSecurityPolicy(self._policy)
self.connection = Zope.DB.open()
root = self.root = self.connection.root()[ 'Application' ]
newSecurityManager( None, UnitTestUser().__of__( self.root ) )
@@ -116,6 +147,8 @@
def tearDown( self ):
get_transaction().abort()
self.connection.close()
+ noSecurityManager()
+ SecurityManager.setSecurityPolicy(self._oldPolicy)
def off_test_otherFolderTypes( self ):
"""
@@ -167,6 +200,12 @@
self.assertEqual( ti.Description(), 'Description' )
self.assertEqual( ti.Metatype(), 'Foo' )
self.assertEqual( ti.getIcon(), 'foo.gif' )
+ self.assertEqual( ti.immediate_view, '' )
+
+ ti = self._makeInstance( 'Foo'
+ , immediate_view='foo_view'
+ )
+ self.assertEqual( ti.immediate_view, 'foo_view' )
def test_allowType( self ):
ti = self._makeInstance( 'Foo' )
@@ -186,43 +225,43 @@
ti = self._makeInstance( 'Foo', allow_discussion=1 )
self.failUnless( ti.allowDiscussion() )
+ ACTION_LIST = \
+ ( { 'id' : 'view'
+ , 'name' : 'View'
+ , 'action' : 'foo_view'
+ , 'permissions' : ( 'View', )
+ , 'category' : 'object'
+ , 'visible' : 1
+ }
+ , { 'name' : 'Edit' # Note: No ID passed
+ , 'action' : 'foo_edit'
+ , 'permissions' : ( 'Modify', )
+ , 'category' : 'object'
+ , 'visible' : 1
+ }
+ , { 'name' : 'Object Properties' # Note: No ID passed
+ , 'action' : 'foo_properties'
+ , 'permissions' : ( 'Modify', )
+ , 'category' : 'object'
+ , 'visible' : 1
+ }
+ , { 'id' : 'slot'
+ , 'action' : 'foo_slot'
+ , 'category' : 'object'
+ , 'visible' : 0
+ }
+ )
+
def _ripActionValues( self, key, actions ):
return filter( None, map( lambda x, key=key: x.get( key, None )
, actions
) )
- def testActions( self ):
+ def test_listActions( self ):
ti = self._makeInstance( 'Foo' )
self.failIf( ti.getActions() )
- action_list = \
- ( { 'id' : 'view'
- , 'name' : 'View'
- , 'action' : 'foo_view'
- , 'permissions' : ( 'View', )
- , 'category' : 'object'
- , 'visible' : 1
- }
- , { 'name' : 'Edit' # Note: No ID passed
- , 'action' : 'foo_edit'
- , 'permissions' : ( 'Modify', )
- , 'category' : 'object'
- , 'visible' : 1
- }
- , { 'name' : 'Object Properties' # Note: No ID passed
- , 'action' : 'foo_properties'
- , 'permissions' : ( 'Modify', )
- , 'category' : 'object'
- , 'visible' : 1
- }
- , { 'id' : 'slot'
- , 'action' : 'foo_slot'
- , 'category' : 'object'
- , 'visible' : 0
- }
- )
-
- ti = self._makeInstance( 'Foo', actions=action_list )
+ ti = self._makeInstance( 'Foo', actions=self.ACTION_LIST )
actions = ti.getActions()
self.failUnless( actions )
@@ -248,24 +287,242 @@
self.failUnless( 'objectproperties' in visible )
self.failIf( 'slot' in visible )
-class FTITests( TypeInfoTests ):
+ def test_getActionById( self ):
+
+ ti = self._makeInstance( 'Foo' )
+ marker = []
+ self.assertEqual( id( ti.getActionById( 'view', marker ) )
+ , id( marker ) )
+ self.assertRaises( TypeError, ti.getActionById, 'view' )
+
+ ti = self._makeInstance( 'Foo', actions=self.ACTION_LIST )
+ self.assertEqual( id( ti.getActionById( 'foo', marker ) )
+ , id( marker ) )
+ self.assertRaises( TypeError, ti.getActionById, 'foo' )
+
+ action = ti.getActionById( 'view' )
+ self.assertEqual( action, 'foo_view' )
+
+ action = ti.getActionById( 'edit' )
+ self.assertEqual( action, 'foo_edit' )
+
+ action = ti.getActionById( 'objectproperties' )
+ self.assertEqual( action, 'foo_properties' )
+
+ action = ti.getActionById( 'slot' )
+ self.assertEqual( action, 'foo_slot' )
+
+
+class FTIDataTests( TypeInfoTests ):
def _makeInstance( self, id, **kw ):
return apply( FactoryTypeInformation, ( id, ), kw )
- def test_ConstructInstance( self ):
- pass
+ def test_properties( self ):
+ ti = self._makeInstance( 'Foo' )
+ self.assertEqual( ti.product, '' )
+ self.assertEqual( ti.factory, '' )
+
+ ti = self._makeInstance( 'Foo'
+ , product='FooProduct'
+ , factory='addFoo'
+ )
+ self.assertEqual( ti.product, 'FooProduct' )
+ self.assertEqual( ti.factory, 'addFoo' )
-class STITests( TypeInfoTests ):
+
+class STIDataTests( TypeInfoTests ):
def _makeInstance( self, id, **kw ):
return apply( ScriptableTypeInformation, ( id, ), kw )
+ def test_properties( self ):
+ ti = self._makeInstance( 'Foo' )
+ self.assertEqual( ti.permission, '' )
+ self.assertEqual( ti.constructor_path, '' )
+
+ ti = self._makeInstance( 'Foo'
+ , permission='Add Foos'
+ , constructor_path='foo_add'
+ )
+ self.assertEqual( ti.permission, 'Add Foos' )
+ self.assertEqual( ti.constructor_path, 'foo_add' )
+
+
+class Foo:
+ """
+ Shim content object.
+ """
+ def __init__( self, id, *args, **kw ):
+ self.id = id
+ self._args = args
+ self._kw = {}
+ self._kw.update( kw )
+
+class FauxFactory:
+ """
+ Shim product factory.
+ """
+ def __init__( self, folder ):
+ self._folder = folder
+
+ def addFoo( self, id, *args, **kw ):
+ foo = apply( Foo, ( id, ) + args, kw )
+ self._folder._setOb( id, foo )
+
+ __roles__ = ( 'FooAdder', )
+ __allow_access_to_unprotected_subobjects__ = { 'addFoo' : 1 }
+
+class FauxFolder( Acquisition.Implicit ):
+ """
+ Shim container
+ """
+ def __init__( self, fake_product=0 ):
+ if fake_product:
+ self.manage_addProduct = { 'FooProduct' : FauxFactory( self ) }
+
+ self._objects = []
+
+ def _setOb( self, id, obj ):
+ self._objects[id] = obj
+
+ def _getOb( self, id ):
+ return self._objects[id]
+
+class FTIConstructionTests( unittest.TestCase ):
+
+ def setUp( self ):
+ noSecurityManager()
+
+ def _makeInstance( self, id, **kw ):
+ return apply( FactoryTypeInformation, ( id, ), kw )
+
+ def _makeFolder( self, fake_product=0 ):
+ return FauxFolder( fake_product )
+
+ def test_isConstructionAllowed_wo_Container( self ):
+
+ ti = self._makeInstance( 'foo' )
+
+ self.failIf( ti.isConstructionAllowed( None ) )
+ self.failIf( ti.isConstructionAllowed( None, raise_exc=1 ) )
+
+ ti = self._makeInstance( 'Foo'
+ , product='FooProduct'
+ , factory='addFoo'
+ )
+
+ self.failIf( ti.isConstructionAllowed( None ) )
+ self.assertRaises( Exception, ti.isConstructionAllowed
+ , None, raise_exc=1 )
+
+ def test_isConstructionAllowed_wo_ProductFactory( self ):
+
+ ti = self._makeInstance( 'foo' )
+
+ folder = self._makeFolder()
+ self.failIf( ti.isConstructionAllowed( folder ) )
+ self.failIf( ti.isConstructionAllowed( folder, raise_exc=1 ) )
+
+ folder = self._makeFolder( fake_product=1 )
+ self.failIf( ti.isConstructionAllowed( folder ) )
+ self.failIf( ti.isConstructionAllowed( folder, raise_exc=1 ) )
+
+ def test_isConstructionAllowed_wo_Security( self ):
+
+ ti = self._makeInstance( 'Foo'
+ , product='FooProduct'
+ , factory='addFoo'
+ )
+ folder = self._makeFolder( fake_product=1 )
+
+ self.failIf( ti.isConstructionAllowed( folder ) )
+ self.assertRaises( 'Unauthorized', ti.isConstructionAllowed
+ , folder, raise_exc=1 )
+
+class FTIConstructionTests_w_Roles( unittest.TestCase ):
+
+ def tearDown( self ):
+ noSecurityManager()
+
+ def _makeStuff( self ):
+
+ ti = FactoryTypeInformation( 'Foo'
+ , product='FooProduct'
+ , factory='addFoo'
+ )
+ folder = FauxFolder( fake_product=1 )
+
+ return ti, folder
+
+ def test_isConstructionAllowed_for_Omnipotent( self ):
+
+ ti, folder = self._makeStuff()
+ newSecurityManager( None
+ , OmnipotentUser().__of__( folder ) )
+ self.failUnless( ti.isConstructionAllowed( folder ) )
+
+ def test_isConstructionAllowed_w_Role( self ):
+
+ ti, folder = self._makeStuff()
+
+ newSecurityManager( None
+ , UserWithRoles( 'FooAdder' ).__of__( folder ) )
+ self.failUnless( ti.isConstructionAllowed( folder ) )
+
+ def test_isConstructionAllowed_wo_Role( self ):
+
+ ti, folder = self._makeStuff()
+
+ newSecurityManager( None
+ , UserWithRoles( 'FooViewer' ).__of__( folder ) )
+ self.assertRaises( 'Unauthorized', ti.isConstructionAllowed
+ , folder, raise_exc=1 )
+
+ def _test_constructInstance_wo_Roles( self ):
+
+ ti, folder = self._makeStuff()
+
+ newSecurityManager( None
+ , UserWithRoles( 'FooViewer' ).__of__( folder ) )
+
+ self.assertRaises( 'Unauthorized'
+ , ti.constructInstance, folder, 'foo' )
+
+ def _test_constructInstance( self ):
+
+ ti, folder = self._makeStuff()
+
+ newSecurityManager( None
+ , UserWithRoles( 'FooAdder' ).__of__( folder ) )
+
+ ti.constructInstance( folder, 'foo' )
+ foo = folder._getOb( 'foo' )
+ self.assertEqual( foo.id, 'foo' )
+
+ ti.constructInstance( folder, 'bar', 0, 1 )
+ bar = folder._getOb( 'bar' )
+ self.assertEqual( bar.id, 'bar' )
+ self.assertEqual( bar._args, ( 0, 1 ) )
+
+ ti.constructInstance( folder, 'baz', frickle='natz' )
+ baz = folder._getOb( 'baz' )
+ self.assertEqual( baz.id, 'baz' )
+ self.assertEqual( baz._kw[ 'frickle' ], 'natz' )
+
+ ti.constructInstance( folder, 'bam', 0, 1, frickle='natz' )
+ bam = folder._getOb( 'bam' )
+ self.assertEqual( bam.id, 'bam' )
+ self.assertEqual( bam._args, ( 0, 1 ) )
+ self.assertEqual( bam._kw[ 'frickle' ], 'natz' )
+
def test_suite():
suite = unittest.TestSuite()
suite.addTest( unittest.makeSuite( TypesToolTests ) )
- suite.addTest( unittest.makeSuite( FTITests ) )
- suite.addTest( unittest.makeSuite( STITests ) )
+ suite.addTest( unittest.makeSuite( FTIDataTests ) )
+ suite.addTest( unittest.makeSuite( STIDataTests ) )
+ suite.addTest( unittest.makeSuite( FTIConstructionTests ) )
+ suite.addTest( unittest.makeSuite( FTIConstructionTests_w_Roles ) )
return suite
def run():