Back to index

plone3  3.1.7
test_guard.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2005 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ Guard tests.
00014 
00015 $Id: test_guard.py 73035 2007-03-07 16:41:03Z jens $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 import warnings
00021 
00022 from AccessControl import getSecurityManager
00023 warnings.filterwarnings('ignore', "Products.PageTemplates.TALES has moved")
00024 from Products.PageTemplates.TALES import CompilerError
00025 from Products.CMFCore.tests.base.dummy import DummyContent
00026 from Products.CMFCore.tests.base.dummy import DummySite
00027 from Products.CMFCore.tests.base.dummy import DummyTool
00028 from Products.CMFCore.WorkflowTool import WorkflowTool
00029 
00030 from Products.DCWorkflow.Guard import Guard
00031 from Products.DCWorkflow.DCWorkflow import DCWorkflowDefinition
00032 
00033 
00034 class TestGuard(unittest.TestCase):
00035 
00036     def setUp(self):
00037         self.site = DummySite('site')
00038         self.site._setObject( 'portal_types', DummyTool() )
00039         self.site._setObject( 'portal_workflow', WorkflowTool() )
00040 
00041         # Construct a workflow
00042         wftool = self.site.portal_workflow
00043         wftool._setObject('wf', DCWorkflowDefinition('wf'))
00044         wftool.setDefaultChain('wf')
00045 
00046     def _getDummyWorkflow(self):
00047         return self.site.portal_workflow['wf']
00048 
00049     def test_BaseGuardAPI(self):
00050 
00051         #
00052         # Test guard basic API
00053         #
00054 
00055         guard = Guard()
00056         self.assertNotEqual(guard, None)
00057 
00058         # Test default values
00059         self.assertEqual(guard.getPermissionsText(), '')
00060         self.assertEqual(guard.getRolesText(), '')
00061         self.assertEqual(guard.getExprText(), '')
00062 
00063         # Initialize the guard with empty values
00064         # not initialization
00065         guard_props = {'guard_permissions':'',
00066                        'guard_roles':'',
00067                        'guard_expr' :''}
00068         res = guard.changeFromProperties(guard_props)
00069         self.assert_(res==0)
00070 
00071         # Test default values
00072         self.assertEqual(guard.getPermissionsText(), '')
00073         self.assertEqual(guard.getRolesText(), '')
00074         self.assertEqual(guard.getExprText(), '')
00075 
00076         # Change guard
00077         guard_props = {'guard_roles':'Manager',
00078                        'guard_permissions':'',
00079                        'guard_expr' :''}
00080         res = guard.changeFromProperties(guard_props)
00081         self.assert_(res==1)
00082         self.assertEqual(guard.getRolesText(), 'Manager')
00083         self.assertEqual(guard.getPermissionsText(), '')
00084         self.assertEqual(guard.getExprText(), '')
00085 
00086         # Change guard
00087         guard_props = {'guard_roles':'Manager;',
00088                        'guard_permissions':'',
00089                        'guard_expr' :''}
00090         res = guard.changeFromProperties(guard_props)
00091         self.assert_(res==1)
00092         # With one space after the ';'
00093         self.assertEqual(guard.getRolesText(), 'Manager; ')
00094         self.assertEqual(guard.getPermissionsText(), '')
00095         self.assertEqual(guard.getExprText(), '')
00096 
00097         # Change guard
00098         guard_props = {'guard_roles':'Manager;Member',
00099                        'guard_permissions':'',
00100                        'guard_expr' :''}
00101         res = guard.changeFromProperties(guard_props)
00102         self.assert_(res==1)
00103         # With one space after the ';'
00104         self.assertEqual(guard.getRolesText(), 'Manager; Member')
00105         self.assertEqual(guard.getPermissionsText(), '')
00106         self.assertEqual(guard.getExprText(), '')
00107 
00108         # Change guard
00109         guard_props = {'guard_roles':'Manager;Member',
00110                        'guard_permissions':'',
00111                        'guard_expr' :''}
00112         res = guard.changeFromProperties(guard_props)
00113         self.assert_(res==1)
00114         # With one space after the ';'
00115         self.assertEqual(guard.getRolesText(), 'Manager; Member')
00116         self.assertEqual(guard.getPermissionsText(), '')
00117         self.assertEqual(guard.getExprText(), '')
00118 
00119         # Change guard
00120         guard_props = {'guard_roles':'Manager',
00121                        'guard_permissions':'',
00122                        'guard_expr' :''}
00123         res = guard.changeFromProperties(guard_props)
00124         self.assert_(res==1)
00125         self.assertEqual(guard.getRolesText(), 'Manager')
00126         self.assertEqual(guard.getPermissionsText(), '')
00127         self.assertEqual(guard.getExprText(), '')
00128 
00129         # Change guard
00130         guard_props = {'guard_roles':'Manager',
00131                        'guard_permissions':'ManagePortal;',
00132                        'guard_expr' :''}
00133         res = guard.changeFromProperties(guard_props)
00134         self.assert_(res==1)
00135         self.assertEqual(guard.getRolesText(), 'Manager')
00136         self.assertEqual(guard.getPermissionsText(), 'ManagePortal; ')
00137         self.assertEqual(guard.getExprText(), '')
00138 
00139         # Change guard
00140         guard_props = {'guard_roles':'Manager',
00141                        'guard_permissions':'ManagePortal',
00142                        'guard_expr' :''}
00143         res = guard.changeFromProperties(guard_props)
00144         self.assert_(res==1)
00145         self.assertEqual(guard.getRolesText(), 'Manager')
00146         self.assertEqual(guard.getPermissionsText(), 'ManagePortal')
00147         self.assertEqual(guard.getExprText(), '')
00148 
00149         # Change guard
00150         guard_props = {'guard_roles':'Manager',
00151                        'guard_permissions':'ManagePortal',
00152                        'guard_expr' :'python:1'}
00153         res = guard.changeFromProperties(guard_props)
00154         self.assert_(res==1)
00155         self.assertEqual(guard.getRolesText(), 'Manager')
00156         self.assertEqual(guard.getPermissionsText(), 'ManagePortal')
00157         self.assertEqual(guard.getExprText(), 'python:1')
00158 
00159         # Change guard
00160         guard_props = {'guard_roles':'Manager',
00161                        'guard_permissions':'ManagePortal',
00162                        'guard_expr' :'string:'}
00163         res = guard.changeFromProperties(guard_props)
00164         self.assert_(res==1)
00165         self.assertEqual(guard.getRolesText(), 'Manager')
00166         self.assertEqual(guard.getPermissionsText(), 'ManagePortal')
00167         self.assertEqual(guard.getExprText(), 'string:')
00168 
00169         # Change guard with wrong TALES
00170         guard_props = {'guard_roles':'Manager',
00171                        'guard_permissions':'ManagePortal',
00172                        'guard_expr' :'python:'}
00173         self.assertRaises(CompilerError,
00174                           guard.changeFromProperties, guard_props)
00175 
00176         self.assertEqual(guard.getRolesText(), 'Manager')
00177         self.assertEqual(guard.getPermissionsText(), 'ManagePortal')
00178         self.assertEqual(guard.getExprText(), 'string:')
00179 
00180         # reinit the guard
00181         guard_props = {'guard_permissions':'',
00182                        'guard_roles':'',
00183                        'guard_expr' :''}
00184         res = guard.changeFromProperties(guard_props)
00185         self.assert_(res==0)
00186 
00187         # No API on DCWorkflow guard to reset properly....
00188         guard.permissions = ''
00189         guard.roles = ''
00190         guard.expr = None
00191 
00192         # Test default values
00193         self.assertEqual(guard.getPermissionsText(), '')
00194         self.assertEqual(guard.getRolesText(), '')
00195         self.assertEqual(guard.getExprText(), '')
00196 
00197         # XXX more tests with permissions and roles
00198 
00199     def test_checkGuardExpr(self):
00200 
00201         #
00202         # Basic checks.
00203         #
00204 
00205         guard = Guard()
00206 
00207         # Create compulsory context elements
00208         sm = getSecurityManager()
00209         self.site._setObject('dummy', DummyContent('dummy'))
00210         ob = self.site.dummy
00211         wf_def = self._getDummyWorkflow()
00212 
00213         # Initialize the guard with an ok guard
00214         guard_props = {'guard_permissions':'',
00215                        'guard_roles':'',
00216                        'guard_expr' :'python:1'}
00217 
00218         res = guard.changeFromProperties(guard_props)
00219         self.assert_(res)
00220         self.assert_(guard.check(sm, wf_def, ob))
00221 
00222         # Initialize the guard with a not ok guard
00223         guard_props = {'guard_permissions':'',
00224                        'guard_roles':'',
00225                        'guard_expr' :'python:0'}
00226         res = guard.changeFromProperties(guard_props)
00227         self.assert_(res)
00228         self.assert_(not guard.check(sm, wf_def, ob))
00229 
00230         # XXX more tests with permissions and roles
00231 
00232     def test_checkWithKwargs(self):
00233 
00234         #
00235         # Checks with kwargs
00236         #
00237 
00238         guard = Guard()
00239 
00240         # Create compulsory context elements
00241         sm = getSecurityManager()
00242         self.site._setObject('dummy', DummyContent('dummy'))
00243         ob = self.site.dummy
00244         wf_def = self._getDummyWorkflow()
00245 
00246         # Initialize the guard with an ok guard
00247         guard_props = {'guard_permissions':'',
00248                        'guard_roles':'',
00249                        'guard_expr' :'python:1'}
00250 
00251         res = guard.changeFromProperties(guard_props)
00252         self.assert_(res)
00253         self.assert_(guard.check(sm, wf_def, ob, arg1=1, arg2=2))
00254 
00255         # Initialize the guard with a not ok guard
00256         guard_props = {'guard_permissions':'',
00257                        'guard_roles':'',
00258                        'guard_expr' :'python:0'}
00259         res = guard.changeFromProperties(guard_props)
00260         self.assert_(res)
00261         self.assert_(not guard.check(sm, wf_def, ob, arg1=1, arg2=2))
00262 
00263         # XXX more tests with permissions and roles
00264 
00265 
00266 def test_suite():
00267     return unittest.TestSuite((
00268         unittest.makeSuite(TestGuard),
00269         ))
00270 
00271 if __name__ == '__main__':
00272     unittest.main(defaultTest='test_suite')