vendor/CMF/1.6.3/CMFCore

view tests/test_MembershipTool.py @ 0:587011552858

import CMF 1.6.3
author bdelbosc
date Mon, 23 Apr 2007 13:58:01 +0000
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2002 Zope Corporation and Contributors. All Rights Reserved.
4 #
5 # This software is subject to the provisions of the Zope Public License,
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10 # FOR A PARTICULAR PURPOSE.
11 #
12 ##############################################################################
13 """ Unit tests for MembershipTool module.
15 $Id$
16 """
18 from unittest import TestCase, TestSuite, makeSuite, main
19 import Testing
20 try:
21 import Zope2
22 except ImportError: # BBB: for Zope 2.7
23 import Zope as Zope2
24 Zope2.startup()
26 from AccessControl.SecurityManagement import newSecurityManager
27 from OFS.Folder import Folder
29 from Products.CMFCore.MemberDataTool import MemberDataTool
30 from Products.CMFCore.PortalFolder import PortalFolder
31 from Products.CMFCore.tests.base.dummy import DummySite
32 from Products.CMFCore.tests.base.dummy import DummyTool
33 from Products.CMFCore.tests.base.dummy import DummyUserFolder
34 from Products.CMFCore.tests.base.testcase import SecurityTest
37 class MembershipToolTests(TestCase):
39 def test_z2interfaces(self):
40 from Interface.Verify import verifyClass
41 from Products.CMFCore.interfaces.portal_actions \
42 import ActionProvider as IActionProvider
43 from Products.CMFCore.interfaces.portal_membership \
44 import portal_membership as IMembershipTool
45 from Products.CMFCore.MembershipTool import MembershipTool
47 verifyClass(IActionProvider, MembershipTool)
48 verifyClass(IMembershipTool, MembershipTool)
50 def test_z3interfaces(self):
51 try:
52 from zope.interface.verify import verifyClass
53 from Products.CMFCore.interfaces import IActionProvider
54 from Products.CMFCore.interfaces import IMembershipTool
55 except ImportError:
56 # BBB: for Zope 2.7
57 return
58 from Products.CMFCore.MembershipTool import MembershipTool
60 verifyClass(IActionProvider, MembershipTool)
61 verifyClass(IMembershipTool, MembershipTool)
64 class MembershipToolSecurityTests(SecurityTest):
66 def _makeOne(self, *args, **kw):
67 from Products.CMFCore.MembershipTool import MembershipTool
69 return MembershipTool(*args, **kw)
71 def _makeSite(self, parent=None):
72 if parent is None:
73 parent = self.root
74 site = DummySite( 'site' ).__of__( parent )
75 site._setObject( 'portal_membership', self._makeOne() )
76 return site
78 def test_getCandidateLocalRoles(self):
79 site = self._makeSite()
80 mtool = site.portal_membership
81 acl_users = site._setObject( 'acl_users', DummyUserFolder() )
83 newSecurityManager(None, acl_users.user_foo)
84 rval = mtool.getCandidateLocalRoles(mtool)
85 self.assertEqual( rval, ('Dummy',) )
86 newSecurityManager(None, acl_users.all_powerful_Oz)
87 rval = mtool.getCandidateLocalRoles(mtool)
88 self.assertEqual( rval, ('Manager', 'Member', 'Owner', 'Reviewer') )
90 def test_createMemberArea(self):
91 site = self._makeSite()
92 mtool = site.portal_membership
93 members = site._setObject( 'Members', PortalFolder('Members') )
94 acl_users = site._setObject( 'acl_users', DummyUserFolder() )
95 wtool = site._setObject( 'portal_workflow', DummyTool() )
97 # permission
98 mtool.createMemberArea('user_foo')
99 self.failIf( hasattr(members.aq_self, 'user_foo') )
100 newSecurityManager(None, acl_users.user_bar)
101 mtool.createMemberArea('user_foo')
102 self.failIf( hasattr(members.aq_self, 'user_foo') )
103 newSecurityManager(None, acl_users.user_foo)
104 mtool.setMemberareaCreationFlag()
105 mtool.createMemberArea('user_foo')
106 self.failIf( hasattr(members.aq_self, 'user_foo') )
107 newSecurityManager(None, acl_users.all_powerful_Oz)
108 mtool.setMemberareaCreationFlag()
109 mtool.createMemberArea('user_foo')
110 self.failUnless( hasattr(members.aq_self, 'user_foo') )
112 # default content
113 f = members.user_foo
114 ownership = acl_users.user_foo
115 localroles = ( ( 'user_foo', ('Owner',) ), )
116 self.assertEqual( f.getOwner(), ownership )
117 self.assertEqual( f.get_local_roles(), localroles,
118 'CMF Collector issue #162 (LocalRoles broken): %s'
119 % str( f.get_local_roles() ) )
121 def test_deleteMembers(self):
122 site = self._makeSite()
123 mtool = site.portal_membership
124 members = site._setObject( 'Members', PortalFolder('Members') )
125 acl_users = site._setObject( 'acl_users', DummyUserFolder() )
126 utool = site._setObject( 'portal_url', DummyTool() )
127 wtool = site._setObject( 'portal_workflow', DummyTool() )
128 mdtool = site._setObject( 'portal_memberdata', MemberDataTool() )
129 newSecurityManager(None, acl_users.all_powerful_Oz)
131 self.assertEqual( acl_users.getUserById('user_foo'),
132 acl_users.user_foo )
133 mtool.createMemberArea('user_foo')
134 self.failUnless( hasattr(members.aq_self, 'user_foo') )
135 mdtool.registerMemberData('Dummy', 'user_foo')
136 self.failUnless( mdtool._members.has_key('user_foo') )
138 rval = mtool.deleteMembers( ('user_foo', 'user_baz') )
139 self.assertEqual( rval, ('user_foo',) )
140 self.failIf( acl_users.getUserById('user_foo', None) )
141 self.failIf( mdtool._members.has_key('user_foo') )
142 self.failIf( hasattr(members.aq_self, 'user_foo') )
144 def test_getMemberById_nonesuch(self):
145 INVALID_USER_ID = 'nonesuch'
147 self.root._setObject( 'folder', Folder( 'folder' ) )
148 site = self._makeSite( self.root.folder )
149 tool = site.portal_membership
150 site.acl_users = DummyUserFolder()
151 self.assertEqual( None, tool.getMemberById( INVALID_USER_ID ) )
153 def test_getMemberById_local(self):
154 LOCAL_USER_ID = 'user_foo'
156 self.root._setObject( 'folder', Folder('folder') )
157 site = self._makeSite( self.root.folder )
158 site._setObject( 'acl_users', DummyUserFolder() )
159 tool = site.portal_membership
160 member = tool.getMemberById( LOCAL_USER_ID)
161 self.assertEqual( member.getId(), LOCAL_USER_ID )
163 def test_getMemberById_nonlocal(self):
164 NONLOCAL_USER_ID = 'user_bar'
166 self.root._setObject( 'folder', Folder( 'folder' ) )
167 site = self._makeSite( self.root.folder )
168 self.root.folder._setObject( 'acl_users', DummyUserFolder() )
169 tool = site.portal_membership
170 member = tool.getMemberById( NONLOCAL_USER_ID )
171 self.assertEqual( member.getId(), NONLOCAL_USER_ID )
173 def test_getMemberById_chained(self):
174 LOCAL_USER_ID = 'user_foo'
175 NONLOCAL_USER_ID = 'user_bar'
177 self.root._setObject( 'folder', Folder( 'folder' ) )
178 site = self._makeSite( self.root.folder )
179 tool = site.portal_membership
181 local_uf = DummyUserFolder()
182 delattr( local_uf, NONLOCAL_USER_ID )
183 site._setObject('acl_users', local_uf)
185 nonlocal_uf = DummyUserFolder()
186 delattr( nonlocal_uf, LOCAL_USER_ID )
187 self.root.folder._setObject('acl_users', nonlocal_uf)
189 local_member = tool.getMemberById(LOCAL_USER_ID)
190 self.assertEqual(local_member.getId(), LOCAL_USER_ID)
192 nonlocal_member = tool.getMemberById(NONLOCAL_USER_ID)
193 self.assertEqual(nonlocal_member.getId(), NONLOCAL_USER_ID)
195 def test_suite():
196 return TestSuite((
197 makeSuite( MembershipToolTests ),
198 makeSuite( MembershipToolSecurityTests )
199 ))
201 if __name__ == '__main__':
202 main(defaultTest='test_suite')