Back to index

plone3  3.1.7
test_MembershipTool.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2002 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 """ Unit tests for MembershipTool module.
00014 
00015 $Id: test_MembershipTool.py 77113 2007-06-26 20:36:26Z yuppie $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from AccessControl.SecurityManagement import newSecurityManager
00022 from OFS.Folder import Folder
00023 from zope.component import getSiteManager
00024 from zope.testing.cleanup import cleanUp
00025 
00026 from Products.CMFCore.CMFBTreeFolder import CMFBTreeFolder
00027 from Products.CMFCore.interfaces import ISiteRoot
00028 from Products.CMFCore.MemberDataTool import MemberDataTool
00029 from Products.CMFCore.PortalFolder import PortalFolder
00030 from Products.CMFCore.tests.base.dummy import DummySite
00031 from Products.CMFCore.tests.base.dummy import DummyTool
00032 from Products.CMFCore.tests.base.dummy import DummyUserFolder
00033 from Products.CMFCore.tests.base.testcase import SecurityTest
00034 
00035 
00036 class MembershipToolTests(unittest.TestCase):
00037 
00038     def test_z2interfaces(self):
00039         from Interface.Verify import verifyClass
00040         from Products.CMFCore.interfaces.portal_membership \
00041                 import portal_membership as IMembershipTool
00042         from Products.CMFCore.MembershipTool import MembershipTool
00043 
00044         verifyClass(IMembershipTool, MembershipTool)
00045 
00046     def test_z3interfaces(self):
00047         from zope.interface.verify import verifyClass
00048         from Products.CMFCore.interfaces import IMembershipTool
00049         from Products.CMFCore.MembershipTool import MembershipTool
00050 
00051         verifyClass(IMembershipTool, MembershipTool)
00052 
00053 
00054 class MembershipToolSecurityTests(SecurityTest):
00055 
00056     def _makeOne(self, *args, **kw):
00057         from Products.CMFCore.MembershipTool import MembershipTool
00058 
00059         return MembershipTool(*args, **kw)
00060 
00061     def _makeSite(self, parent=None):
00062         if parent is None:
00063             parent = self.root
00064         site = DummySite( 'site' ).__of__( parent )
00065         site._setObject( 'portal_membership', self._makeOne() )
00066         return site
00067 
00068     def test_getCandidateLocalRoles(self):
00069         site = self._makeSite()
00070         mtool = site.portal_membership
00071         acl_users = site._setObject( 'acl_users', DummyUserFolder() )
00072 
00073         newSecurityManager(None, acl_users.user_foo)
00074         rval = mtool.getCandidateLocalRoles(mtool)
00075         self.assertEqual( rval, ('Dummy',) )
00076         newSecurityManager(None, acl_users.all_powerful_Oz)
00077         rval = mtool.getCandidateLocalRoles(mtool)
00078         self.assertEqual( rval, ('Manager', 'Member', 'Owner', 'Reviewer') )
00079 
00080     def test_createMemberArea(self):
00081         site = self._makeSite()
00082         mtool = site.portal_membership
00083         members = site._setObject( 'Members', PortalFolder('Members') )
00084         acl_users = site._setObject( 'acl_users', DummyUserFolder() )
00085         wtool = site._setObject( 'portal_workflow', DummyTool() )
00086 
00087         # permission
00088         mtool.createMemberArea('user_foo')
00089         self.failIf( hasattr(members.aq_self, 'user_foo') )
00090         newSecurityManager(None, acl_users.user_bar)
00091         mtool.createMemberArea('user_foo')
00092         self.failIf( hasattr(members.aq_self, 'user_foo') )
00093         newSecurityManager(None, acl_users.user_foo)
00094         mtool.setMemberareaCreationFlag()
00095         mtool.createMemberArea('user_foo')
00096         self.failIf( hasattr(members.aq_self, 'user_foo') )
00097         newSecurityManager(None, acl_users.all_powerful_Oz)
00098         mtool.setMemberareaCreationFlag()
00099         mtool.createMemberArea('user_foo')
00100         self.failUnless( hasattr(members.aq_self, 'user_foo') )
00101 
00102         # default content
00103         f = members.user_foo
00104         ownership = acl_users.user_foo
00105         localroles = ( ( 'user_foo', ('Owner',) ), )
00106         self.assertEqual( f.getOwner(), ownership )
00107         self.assertEqual( f.get_local_roles(), localroles,
00108                           'CMF Collector issue #162 (LocalRoles broken): %s'
00109                           % str( f.get_local_roles() ) )
00110 
00111     def test_createMemberAreaCMFBTreeFolder(self):
00112         # Test member area creation if the toplevel "Members" folder is
00113         # a CMFBTreeFolder (http://www.zope.org/Collectors/CMF/441
00114         site = self._makeSite()
00115         mtool = site.portal_membership
00116         members = site._setObject( 'Members', CMFBTreeFolder('Members') )
00117         acl_users = site._setObject( 'acl_users', DummyUserFolder() )
00118         wtool = site._setObject( 'portal_workflow', DummyTool() )
00119 
00120         # permission
00121         mtool.createMemberArea('user_foo')
00122         self.failIf( hasattr(members.aq_self, 'user_foo') )
00123         newSecurityManager(None, acl_users.user_bar)
00124         mtool.createMemberArea('user_foo')
00125         self.failIf( hasattr(members.aq_self, 'user_foo') )
00126         newSecurityManager(None, acl_users.user_foo)
00127         mtool.setMemberareaCreationFlag()
00128         mtool.createMemberArea('user_foo')
00129         self.failIf( hasattr(members.aq_self, 'user_foo') )
00130         newSecurityManager(None, acl_users.all_powerful_Oz)
00131         mtool.setMemberareaCreationFlag()
00132         mtool.createMemberArea('user_foo')
00133         self.failUnless( hasattr(members.aq_self, 'user_foo') )
00134 
00135         # default content
00136         f = members.user_foo
00137         ownership = acl_users.user_foo
00138         localroles = ( ( 'user_foo', ('Owner',) ), )
00139         self.assertEqual( f.getOwner(), ownership )
00140         self.assertEqual( f.get_local_roles(), localroles,
00141                           'CMF Collector issue #162 (LocalRoles broken): %s'
00142                           % str( f.get_local_roles() ) )
00143 
00144     def test_createMemberArea_chained(self):
00145         LOCAL_USER_ID = 'user_foo'
00146         NONLOCAL_USER_ID = 'user_bar'
00147 
00148         self.root._setObject( 'folder', Folder('folder') )
00149         site = self._makeSite( self.root.folder )
00150         mtool = site.portal_membership
00151         members = site._setObject( 'Members', PortalFolder('Members') )
00152         wtool = site._setObject( 'portal_workflow', DummyTool() )
00153 
00154         local_uf = DummyUserFolder()
00155         delattr( local_uf, NONLOCAL_USER_ID )
00156         acl_users = site._setObject('acl_users', local_uf)
00157 
00158         nonlocal_uf = DummyUserFolder()
00159         delattr( nonlocal_uf, LOCAL_USER_ID )
00160         self.root.folder._setObject('acl_users', nonlocal_uf)
00161 
00162         newSecurityManager(None, acl_users.all_powerful_Oz)
00163         mtool.createMemberArea( NONLOCAL_USER_ID )
00164         self.failUnless( hasattr(members.aq_self, NONLOCAL_USER_ID ) )
00165         mtool.createMemberArea( LOCAL_USER_ID )
00166         self.failUnless( hasattr(members.aq_self, LOCAL_USER_ID ) )
00167 
00168     def test_deleteMembers(self):
00169         site = self._makeSite()
00170         sm = getSiteManager()
00171         sm.registerUtility(site, ISiteRoot)
00172         mtool = site.portal_membership
00173         members = site._setObject( 'Members', PortalFolder('Members') )
00174         acl_users = site._setObject( 'acl_users', DummyUserFolder() )
00175         mdtool = site._setObject( 'portal_memberdata', MemberDataTool() )
00176         newSecurityManager(None, acl_users.all_powerful_Oz)
00177 
00178         self.assertEqual( acl_users.getUserById('user_foo'),
00179                           acl_users.user_foo )
00180         mtool.createMemberArea('user_foo')
00181         self.failUnless( hasattr(members.aq_self, 'user_foo') )
00182         mdtool.registerMemberData('Dummy', 'user_foo')
00183         self.failUnless( mdtool._members.has_key('user_foo') )
00184 
00185         rval = mtool.deleteMembers( ('user_foo', 'user_baz') )
00186         self.assertEqual( rval, ('user_foo',) )
00187         self.failIf( acl_users.getUserById('user_foo', None) )
00188         self.failIf( mdtool._members.has_key('user_foo') )
00189         self.failIf( hasattr(members.aq_self, 'user_foo') )
00190 
00191         cleanUp()
00192 
00193     def test_deleteMembersUnsupported(self):
00194         # Quite a few user folders do not support the deletion API
00195         # http://www.zope.org/Collectors/CMF/481
00196         # Make sure we get the right exception
00197         site = self._makeSite()
00198         mtool = site.portal_membership
00199         members = site._setObject( 'Members', PortalFolder('Members') )
00200         acl_users = site._setObject( 'acl_users', DummyUserFolder() )
00201         utool = site._setObject( 'portal_url', DummyTool() )
00202         wtool = site._setObject( 'portal_workflow', DummyTool() )
00203         mdtool = site._setObject( 'portal_memberdata', MemberDataTool() )
00204         newSecurityManager(None, acl_users.all_powerful_Oz)
00205 
00206         self.assertEqual( acl_users.getUserById('user_foo'),
00207                           acl_users.user_foo )
00208         mtool.createMemberArea('user_foo')
00209         self.failUnless( hasattr(members.aq_self, 'user_foo') )
00210         mdtool.registerMemberData('Dummy', 'user_foo')
00211         self.failUnless( mdtool._members.has_key('user_foo') )
00212 
00213         # Fake an incompatible user folder by deleting the class method
00214         deletion_method = DummyUserFolder.userFolderDelUsers
00215         del DummyUserFolder.userFolderDelUsers
00216         self.assertRaises( NotImplementedError
00217                          , mtool.deleteMembers
00218                          , ('user_foo',)
00219                          )
00220         self.failUnless( acl_users.getUserById('user_foo', None) )
00221         self.failUnless( mdtool._members.has_key('user_foo') )
00222         self.failUnless( hasattr(members.aq_self, 'user_foo') )
00223 
00224         # Cleanup
00225         DummyUserFolder.userFolderDelUsers = deletion_method
00226 
00227     def test_getMemberById_nonesuch(self):
00228         INVALID_USER_ID = 'nonesuch'
00229 
00230         self.root._setObject( 'folder', Folder( 'folder' ) )
00231         site = self._makeSite( self.root.folder )
00232         tool = site.portal_membership
00233         site.acl_users = DummyUserFolder()
00234         self.assertEqual( None, tool.getMemberById( INVALID_USER_ID ) )
00235 
00236     def test_getMemberById_local(self):
00237         LOCAL_USER_ID = 'user_foo'
00238 
00239         self.root._setObject( 'folder', Folder('folder') )
00240         site = self._makeSite( self.root.folder )
00241         site._setObject( 'acl_users', DummyUserFolder() )
00242         tool = site.portal_membership
00243         member = tool.getMemberById( LOCAL_USER_ID)
00244         self.assertEqual( member.getId(), LOCAL_USER_ID )
00245 
00246     def test_getMemberById_nonlocal(self):
00247         NONLOCAL_USER_ID = 'user_bar'
00248 
00249         self.root._setObject( 'folder', Folder( 'folder' ) )
00250         site = self._makeSite( self.root.folder )
00251         self.root.folder._setObject( 'acl_users', DummyUserFolder() )
00252         tool = site.portal_membership
00253         member = tool.getMemberById( NONLOCAL_USER_ID )
00254         self.assertEqual( member.getId(), NONLOCAL_USER_ID )
00255 
00256     def test_getMemberById_chained(self):
00257         LOCAL_USER_ID = 'user_foo'
00258         NONLOCAL_USER_ID = 'user_bar'
00259 
00260         self.root._setObject( 'folder', Folder( 'folder' ) )
00261         site = self._makeSite( self.root.folder )
00262         tool = site.portal_membership
00263 
00264         local_uf = DummyUserFolder()
00265         delattr( local_uf, NONLOCAL_USER_ID )
00266         site._setObject('acl_users', local_uf)
00267 
00268         nonlocal_uf = DummyUserFolder()
00269         delattr( nonlocal_uf, LOCAL_USER_ID )
00270         self.root.folder._setObject('acl_users', nonlocal_uf)
00271 
00272         local_member = tool.getMemberById(LOCAL_USER_ID)
00273         self.assertEqual(local_member.getId(), LOCAL_USER_ID)
00274 
00275         nonlocal_member = tool.getMemberById(NONLOCAL_USER_ID)
00276         self.assertEqual(nonlocal_member.getId(), NONLOCAL_USER_ID)
00277 
00278 
00279 def test_suite():
00280     return unittest.TestSuite((
00281         unittest.makeSuite(MembershipToolTests),
00282         unittest.makeSuite(MembershipToolSecurityTests),
00283         ))
00284 
00285 if __name__ == '__main__':
00286     unittest.main(defaultTest='test_suite')