vendor/CMF/1.6.3/CMFCore

view tests/test_utils.py @ 0:587011552858

import CMF 1.6.3
author bdelbosc
date Mon, 23 Apr 2007 13:58:01 +0000
parents
children
line source
1 import unittest
2 import Testing
4 from Products.CMFCore.tests.base.testcase import SecurityTest
7 class CoreUtilsTests(unittest.TestCase):
9 def test_normalize(self):
10 from Products.CMFCore.utils import normalize
12 self.assertEqual( normalize('foo/bar'), 'foo/bar' )
13 self.assertEqual( normalize('foo\\bar'), 'foo/bar' )
15 def test_keywordsplitter_empty(self):
16 from Products.CMFCore.utils import keywordsplitter
18 for x in [ '', ' ', ',', ',,', ';', ';;' ]:
19 self.assertEqual( keywordsplitter({'Keywords': x}),
20 [] )
22 def test_keywordsplitter_single(self):
23 from Products.CMFCore.utils import keywordsplitter
25 for x in [ 'foo', ' foo ', 'foo,', 'foo ,,', 'foo;', 'foo ;;' ]:
26 self.assertEqual( keywordsplitter({'Keywords': x}),
27 ['foo'] )
29 def test_keywordsplitter_multi(self):
30 from Products.CMFCore.utils import keywordsplitter
32 for x in [ 'foo, bar, baz'
33 , 'foo, bar , baz'
34 , 'foo, bar,, baz'
35 , 'foo; bar; baz'
36 ]:
37 self.assertEqual( keywordsplitter({'Keywords': x}),
38 ['foo', 'bar', 'baz'] )
40 def test_contributorsplitter_emtpy(self):
41 from Products.CMFCore.utils import contributorsplitter
43 for x in [ '', ' ', ';', ';;' ]:
44 self.assertEqual( contributorsplitter({'Contributors': x}),
45 [] )
47 def test_contributorsplitter_single(self):
48 from Products.CMFCore.utils import contributorsplitter
50 for x in [ 'foo', ' foo ', 'foo;', 'foo ;;' ]:
51 self.assertEqual( contributorsplitter({'Contributors': x}),
52 ['foo'] )
54 def test_contributorsplitter_multi(self):
55 from Products.CMFCore.utils import contributorsplitter
57 for x in [ 'foo; bar; baz'
58 , 'foo; bar ; baz'
59 , 'foo; bar;; baz'
60 ]:
61 self.assertEqual( contributorsplitter({'Contributors': x}),
62 ['foo', 'bar', 'baz'] )
65 class CoreUtilsSecurityTests(SecurityTest):
67 def _makeSite(self):
68 from AccessControl.Owned import Owned
69 from Products.CMFCore.tests.base.dummy import DummySite
70 from Products.CMFCore.tests.base.dummy import DummyUserFolder
71 from Products.CMFCore.tests.base.dummy import DummyObject
73 class _DummyObject(Owned, DummyObject):
74 pass
76 site = DummySite('site').__of__(self.root)
77 site._setObject( 'acl_users', DummyUserFolder() )
78 site._setObject('foo_dummy', _DummyObject(id='foo_dummy'))
79 site._setObject('bar_dummy', _DummyObject(id='bar_dummy'))
81 return site
83 def test__checkPermission(self):
84 from AccessControl import getSecurityManager
85 from AccessControl.ImplPython import ZopeSecurityPolicy
86 from AccessControl.Permission import Permission
87 from AccessControl.SecurityManagement import newSecurityManager
88 from AccessControl.SecurityManager import setSecurityPolicy
89 from Products.CMFCore.utils import _checkPermission
91 setSecurityPolicy(ZopeSecurityPolicy())
92 site = self._makeSite()
93 newSecurityManager(None, site.acl_users.user_foo)
94 o = site.bar_dummy
95 Permission('View',(),o).setRoles(('Anonymous',))
96 Permission('WebDAV access',(),o).setRoles(('Authenticated',))
97 Permission('Manage users',(),o).setRoles(('Manager',))
98 eo = site.foo_dummy
99 eo._owner = (['acl_users'], 'all_powerful_Oz')
100 getSecurityManager().addContext(eo)
101 self.failUnless( _checkPermission('View', o) )
102 self.failUnless( _checkPermission('WebDAV access', o) )
103 self.failIf( _checkPermission('Manage users', o) )
105 eo._proxy_roles = ('Authenticated',)
106 self.failIf( _checkPermission('View', o) )
107 self.failUnless( _checkPermission('WebDAV access', o) )
108 self.failIf( _checkPermission('Manage users', o) )
110 eo._proxy_roles = ('Manager',)
111 self.failIf( _checkPermission('View', o) )
112 self.failIf( _checkPermission('WebDAV access', o) )
113 self.failUnless( _checkPermission('Manage users', o) )
115 def test_mergedLocalRolesManipulation(self):
116 # The _mergedLocalRoles function used to return references to
117 # actual local role settings and it was possible to manipulate them
118 # by changing the return value. http://www.zope.org/Collectors/CMF/376
119 from Products.CMFCore.tests.base.dummy import DummyContent
120 from Products.CMFCore.utils import _mergedLocalRoles
121 obj = DummyContent()
122 obj.manage_addLocalRoles('dummyuser1', ['Manager', 'Owner'])
123 self.assertEqual(len(obj.get_local_roles_for_userid('dummyuser1')), 2)
125 merged_roles = _mergedLocalRoles(obj)
126 merged_roles['dummyuser1'].append('FOO')
128 # The values on the object itself should still the the same
129 self.assertEqual(len(obj.get_local_roles_for_userid('dummyuser1')), 2)
132 def test_suite():
133 return unittest.TestSuite((
134 unittest.makeSuite(CoreUtilsTests),
135 unittest.makeSuite(CoreUtilsSecurityTests),
136 ))
138 if __name__ == '__main__':
139 unittest.main(defaultTest='test_suite')