Back to index

plone3  3.1.7
test_utils.py
Go to the documentation of this file.
00001 import unittest
00002 import Testing
00003 
00004 from Products.CMFCore.tests.base.testcase import SecurityTest
00005 
00006 
00007 class CoreUtilsTests(unittest.TestCase):
00008 
00009     def test_normalize(self):
00010         from Products.CMFCore.utils import normalize
00011 
00012         self.assertEqual( normalize('foo/bar'), 'foo/bar' )
00013         self.assertEqual( normalize('foo\\bar'), 'foo/bar' )
00014 
00015     def test_keywordsplitter_empty(self):
00016         from Products.CMFCore.utils import keywordsplitter
00017 
00018         for x in [ '', ' ', ',', ',,', ';', ';;' ]:
00019             self.assertEqual( keywordsplitter({'Keywords': x}), 
00020                               [] )
00021 
00022     def test_keywordsplitter_single(self):
00023         from Products.CMFCore.utils import keywordsplitter
00024 
00025         for x in [ 'foo', ' foo ', 'foo,', 'foo ,,', 'foo;', 'foo ;;' ]:
00026             self.assertEqual( keywordsplitter({'Keywords': x}), 
00027                               ['foo'] )
00028 
00029     def test_keywordsplitter_multi(self):
00030         from Products.CMFCore.utils import keywordsplitter
00031 
00032         for x in [ 'foo, bar, baz'
00033                  , 'foo, bar , baz'
00034                  , 'foo, bar,, baz'
00035                  , 'foo; bar; baz'
00036                  ]:
00037             self.assertEqual( keywordsplitter({'Keywords': x}), 
00038                               ['foo', 'bar', 'baz'] )
00039 
00040     def test_contributorsplitter_emtpy(self):
00041         from Products.CMFCore.utils import contributorsplitter
00042 
00043         for x in [ '', ' ', ';', ';;' ]:
00044             self.assertEqual( contributorsplitter({'Contributors': x}), 
00045                               [] )
00046 
00047     def test_contributorsplitter_single(self):
00048         from Products.CMFCore.utils import contributorsplitter
00049 
00050         for x in [ 'foo', ' foo ', 'foo;', 'foo ;;' ]:
00051             self.assertEqual( contributorsplitter({'Contributors': x}), 
00052                               ['foo'] )
00053 
00054     def test_contributorsplitter_multi(self):
00055         from Products.CMFCore.utils import contributorsplitter
00056 
00057         for x in [ 'foo; bar; baz'
00058                  , 'foo; bar ; baz'
00059                  , 'foo; bar;; baz'
00060                  ]:
00061             self.assertEqual( contributorsplitter({'Contributors': x}), 
00062                               ['foo', 'bar', 'baz'] )
00063 
00064     def test_getPackageName(self):
00065         from Products.CMFCore.utils import getPackageName
00066         from Products.CMFCore.utils import _globals
00067 
00068         self.assertEqual(getPackageName(globals()), 'Products.CMFCore.tests')
00069         self.assertEqual(getPackageName(_globals), 'Products.CMFCore')
00070 
00071     def test_getContainingPackage(self):
00072         from Products.CMFCore.utils import getContainingPackage
00073 
00074         self.assertEqual(getContainingPackage('Products.CMFCore.exceptions'),
00075                 'Products.CMFCore')
00076         self.assertEqual(getContainingPackage('Products.CMFCore'),
00077                 'Products.CMFCore')
00078         self.assertEqual(getContainingPackage('zope.interface.verify'),
00079                 'zope.interface')
00080 
00081 
00082 class CoreUtilsSecurityTests(SecurityTest):
00083 
00084     def _makeSite(self):
00085         from AccessControl.Owned import Owned
00086         from Products.CMFCore.tests.base.dummy import DummySite
00087         from Products.CMFCore.tests.base.dummy import DummyUserFolder
00088         from Products.CMFCore.tests.base.dummy import DummyObject
00089 
00090         class _DummyObject(Owned, DummyObject):
00091             pass
00092 
00093         site = DummySite('site').__of__(self.root)
00094         site._setObject( 'acl_users', DummyUserFolder() )
00095         site._setObject('foo_dummy', _DummyObject(id='foo_dummy'))
00096         site._setObject('bar_dummy', _DummyObject(id='bar_dummy'))
00097 
00098         return site
00099 
00100     def test__checkPermission(self):
00101         from AccessControl import getSecurityManager
00102         from AccessControl.ImplPython import ZopeSecurityPolicy
00103         from AccessControl.Permission import Permission
00104         from AccessControl.SecurityManagement import newSecurityManager
00105         from AccessControl.SecurityManager import setSecurityPolicy
00106         from Products.CMFCore.utils import _checkPermission
00107 
00108         setSecurityPolicy(ZopeSecurityPolicy())
00109         site = self._makeSite()
00110         newSecurityManager(None, site.acl_users.user_foo)
00111         o = site.bar_dummy
00112         Permission('View',(),o).setRoles(('Anonymous',))
00113         Permission('WebDAV access',(),o).setRoles(('Authenticated',))
00114         Permission('Manage users',(),o).setRoles(('Manager',))
00115         eo = site.foo_dummy
00116         eo._owner = (['acl_users'], 'all_powerful_Oz')
00117         getSecurityManager().addContext(eo)
00118         self.failUnless( _checkPermission('View', o) )
00119         self.failUnless( _checkPermission('WebDAV access', o) )
00120         self.failIf( _checkPermission('Manage users', o) )
00121 
00122         eo._proxy_roles = ('Authenticated',)
00123         self.failIf( _checkPermission('View', o) )
00124         self.failUnless( _checkPermission('WebDAV access', o) )
00125         self.failIf( _checkPermission('Manage users', o) )
00126 
00127         eo._proxy_roles = ('Manager',)
00128         self.failIf( _checkPermission('View', o) )
00129         self.failIf( _checkPermission('WebDAV access', o) )
00130         self.failUnless( _checkPermission('Manage users', o) )
00131 
00132     def test_mergedLocalRolesManipulation(self):
00133         # The _mergedLocalRoles function used to return references to
00134         # actual local role settings and it was possible to manipulate them
00135         # by changing the return value. http://www.zope.org/Collectors/CMF/376
00136         from Products.CMFCore.tests.base.dummy import DummyContent
00137         from Products.CMFCore.utils import _mergedLocalRoles
00138         obj = DummyContent()
00139         obj.manage_addLocalRoles('dummyuser1', ['Manager', 'Owner'])
00140         self.assertEqual(len(obj.get_local_roles_for_userid('dummyuser1')), 2)
00141 
00142         merged_roles = _mergedLocalRoles(obj)
00143         merged_roles['dummyuser1'].append('FOO')
00144 
00145         # The values on the object itself should still the the same
00146         self.assertEqual(len(obj.get_local_roles_for_userid('dummyuser1')), 2)
00147 
00148 
00149 def test_suite():
00150     # reimport to make sure tests are run from Products
00151     from Products.CMFCore.tests.test_utils import CoreUtilsTests
00152 
00153     return unittest.TestSuite((
00154         unittest.makeSuite(CoreUtilsTests),
00155         unittest.makeSuite(CoreUtilsSecurityTests),
00156         ))
00157 
00158 if __name__ == '__main__':
00159     unittest.main(defaultTest='test_suite')