Back to index

plone3  3.1.7
testMultipleSources.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 ## GroupUserFolder
00003 ## Copyright (C)2006 Ingeniweb
00004 
00005 ## This program is free software; you can redistribute it and/or modify
00006 ## it under the terms of the GNU General Public License as published by
00007 ## the Free Software Foundation; either version 2 of the License, or
00008 ## (at your option) any later version.
00009 
00010 ## This program is distributed in the hope that it will be useful,
00011 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 ## GNU General Public License for more details.
00014 
00015 ## You should have received a copy of the GNU General Public License
00016 ## along with this program; see the file COPYING. If not, write to the
00017 ## Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 """
00019 
00020 """
00021 __version__ = "$Revision:  $"
00022 # $Source:  $
00023 # $Id: testMultipleSources.py 30324 2006-09-13 09:43:56Z jvloothuis $
00024 __docformat__ = 'restructuredtext'
00025 
00026 import os, sys
00027 if __name__ == '__main__':
00028     execfile(os.path.join(sys.path[0], 'framework.py'))
00029 
00030 
00031 
00032 # Load fixture
00033 from Testing import ZopeTestCase
00034 
00035 # Permissions / security
00036 from AccessControl.Permissions import access_contents_information, view, add_documents_images_and_files, change_images_and_files, view_management_screens
00037 from AccessControl.SecurityManagement import newSecurityManager, noSecurityManager, getSecurityManager
00038 from AccessControl import Unauthorized
00039 from AccessControl.User import UnrestrictedUser
00040 
00041 import urllib
00042 
00043 # Create the error_log object
00044 app = ZopeTestCase.app()
00045 ZopeTestCase.utils.setupSiteErrorLog(app)
00046 ZopeTestCase.close(app)
00047 
00048 # Start the web server
00049 host, port = ZopeTestCase.utils.startZServer(4)
00050 base = 'http://%s:%d/%s' %(host, port, ZopeTestCase.folder_name)
00051 
00052 from AccessControl import getSecurityManager
00053 
00054 # Get global vars
00055 from Products.GroupUserFolder.global_symbols import *
00056 
00057 
00058 # Install our product
00059 ZopeTestCase.installProduct('GroupUserFolder')
00060 
00061 ZopeTestCase.installProduct('LDAPUserFolder')
00062 
00063 
00064 # Set log options if Log module is available
00065 # This is done to set LOG_PROCESSORs to file logs instead of Zope logs
00066 try:
00067     import Log
00068 
00069     Log.LOG_LEVEL = Log.LOG_DEBUG
00070 
00071     Log.LOG_PROCESSOR = {
00072         Log.LOG_NONE: Log.logFile,
00073         Log.LOG_CRITICAL: Log.logFile,
00074         Log.LOG_ERROR: Log.logFile,
00075         Log.LOG_WARNING: Log.logFile,
00076         Log.LOG_NOTICE: Log.logFile,
00077         Log.LOG_DEBUG: Log.logFile,
00078         }
00079 
00080     from Log import *
00081     Log(LOG_NOTICE, "Starting %s at %d debug level" % (os.path.dirname(__file__), LOG_LEVEL, ))
00082 
00083 except:
00084     print "Log module not available"
00085     LOG_DEBUG = None
00086     LOG_NOTICE = None
00087     LOG_WARNING = None
00088     LOG_ERROR = None
00089     LOG_CRITICAL = None
00090     def Log(*args, **kw):
00091         pass
00092     raise
00093 
00094 
00095 
00096 class ManagementOpener(urllib.FancyURLopener):
00097     def prompt_user_passwd(self, host, realm):
00098         return ('manager', 'secret')
00099 
00100 class UnauthorizedOpener(urllib.FancyURLopener):
00101     def prompt_user_passwd(self, host, realm):
00102         raise Unauthorized, 'The URLopener was asked for authentication'
00103 
00104 
00105 class TestMultipleSources(ZopeTestCase.ZopeTestCase):
00106 
00107     def afterSetUp(self,):
00108         """
00109         afterSetUp(self) => This method is called to create Folder with a GRUF inside.
00110 
00111         """
00112         # Replace default acl_user by a GRUF one
00113         self.folder.manage_delObjects(['acl_users'])
00114         self.folder.manage_addProduct['GroupUserFolder'].manage_addGroupUserFolder()
00115 
00116         # Put back the default user inside
00117         self._setupUser()
00118 
00119         # Create a few roles
00120         self.folder._addRole("r1")
00121         self.folder._addRole("r2")
00122         self.folder._addRole("r3")
00123 
00124         # Create a few groups
00125         self.folder.acl_users._doAddGroup('g1', ())
00126         self.folder.acl_users._doAddGroup('g2', ('r1', ))
00127         self.folder.acl_users._doAddGroup('g3', ('r2', ))
00128         self.folder.acl_users._doAddGroup('g4', ('r2', 'r3', ))
00129 
00130         # Create nested groups
00131         self.folder.acl_users._doAddGroup('ng1', (), ('g1', ))
00132         self.folder.acl_users._doAddGroup('ng2', (), ('g2', 'g3', ))
00133         self.folder.acl_users._doAddGroup('ng3', (), ('g2', 'ng2', ))
00134         self.folder.acl_users._doAddGroup('ng4', ('r3', ), ('g2', 'ng2', ))
00135         self.folder.acl_users._doAddGroup('ng5', (), ('g2', 'ng4', ))
00136 ##        self.folder.acl_users._doAddGroup('ng6', (), ('ng5', 'ng6', ))
00137 
00138         # Create a manager and a few users
00139         self.folder.acl_users._doAddUser('manager', 'secret', ('Manager',), (), (), )
00140         self.folder.acl_users._doAddUser('u1', 'secret', (), (), (), )
00141         self.folder.acl_users._doAddUser('u2', 'secret', (), (), ('g1', ), )
00142         self.folder.acl_users._doAddUser('u3', 'secret', (), (), ('g1', 'g2'), )
00143         self.folder.acl_users._doAddUser('u4', 'secret', (), (), ('g1', 'g2', 'g3'), )
00144         self.folder.acl_users._doAddUser('u5', 'secret', ('r1', ), (), ('g2', 'g3'), )
00145         self.folder.acl_users._doAddUser('u6', 'secret', ('r1', ), (), ('g3', ), )
00146         self.folder.acl_users._doAddUser('u7', 'secret', ('r1', ), (), ('g4', ), )
00147 
00148         # Create nested-groups users
00149         self.folder.acl_users._doAddUser('u8', 'secret', (), (), ('ng1', ), )
00150         self.folder.acl_users._doAddUser('u9', 'secret', (), (), ('g1', 'ng2', ), )
00151         self.folder.acl_users._doAddUser('u10', 'secret', (), (), ('ng2', 'ng3', ), )
00152         self.folder.acl_users._doAddUser('u11', 'secret', ('r3', ), (), ('ng2', 'ng3', ), )
00153 ##        self.folder.acl_users._doAddUser('u12', 'secret', (), (), ('ng5', 'ng6', ), )
00154 
00155 ##        # Create a few folders to play with
00156         self.folder.manage_addProduct['OFSP'].manage_addFolder('lr')
00157         self.folder.lr.manage_addLocalRoles("group_g1", ("r3", ))
00158         self.folder.lr.manage_addLocalRoles("u3", ("r3", ))
00159         self.folder.lr.manage_addLocalRoles("u6", ("r3", ))
00160 
00161         # Special case of nesting
00162         self.folder.acl_users._doAddGroup('extranet', (), ())
00163         self.folder.acl_users._doAddGroup('intranet', (), ('extranet', ))
00164         self.folder.acl_users._doAddGroup('compta', (), ('intranet', 'extranet' ))
00165 
00166 
00167     #                                                   #
00168     #           Basic Multi-Sources API                 #
00169     #                                                   #
00170 
00171     def build(self,):
00172         self.folder.acl_users.addUserSource("manage_addProduct/OFSP/manage_addUserFolder")
00173 
00174     def test01addSources(self,):
00175         """Add a few user sources"""
00176         self.folder.acl_users.addUserSource("manage_addProduct/OFSP/manage_addUserFolder")
00177         self.failUnless('Users01' in self.folder.acl_users.objectIds())
00178         self.failUnless(self.folder.acl_users.Users01.getId() == 'Users01')
00179         self.failUnless('acl_users' in self.folder.acl_users.Users01.objectIds())
00180         self.failUnless(not self.folder.acl_users.listUserSources()[-1].getUserNames())
00181 
00182     def test02getDefaultSource(self,):
00183         "Test default source retreiving"
00184         self.build()
00185         #Log(LOG_DEBUG, self.getDefaultUserSource().getId())
00186         self.failUnless(self.folder.acl_users.getDefaultUserSource().getId() == 'acl_users')
00187 
00188     def test03userFetching(self,):
00189         "Test user creation & fetching"
00190         self.build()
00191         self.failUnless(self.folder.acl_users.listUserSources()[-1].aq_parent.id == 'Users01')
00192         self.failUnless(self.folder.acl_users.listUserSources()[-1].meta_type == 'User Folder')
00193         self.folder.acl_users.listUserSources()[-1]._doAddUser('U1_01', 'secret', (), (), )
00194         self.failUnless(self.folder.acl_users.listUserSources()[-1].getUserNames() == ['U1_01'])
00195         U1 = self.folder.acl_users.getUser('U1_01')
00196         self.folder.acl_users.getUser('u1')
00197         self.failUnless(self.folder.acl_users.getUser('U1_01').getUserSourceId() == 'Users01')
00198         self.failUnless(self.folder.acl_users.getUnwrappedUser("U1_01").name == 'U1_01')
00199 
00200     def test04ChangeUserRole(self,):
00201         "Test if it possible to change a user's role using the sources API"
00202         self.setRoles(["Manager"])
00203         user = getSecurityManager().getUser()
00204         Log(LOG_DEBUG, user.getRoles())
00205         self.failUnless(user.has_role("Manager"))
00206 
00207     def test05AvailableTypes(self,):
00208         "test available user folders"
00209         self.setRoles(["Manager", ])
00210         self.failUnless(getSecurityManager().getUser().has_role("Manager"))
00211         sources = self.folder.acl_users.listAvailableUserSources(filter_permissions = 0, filter_classes = 1)
00212         self.failUnless(
00213             ('User Folder', "manage_addProduct/OFSP/manage_addUserFolder") in sources,
00214             "There should only have one UF, not %s" % (sources, ))
00215 
00216     def test06LUFSource(self):
00217         # Make sure this method returns only one LDAP User Folder
00218         gruf = self.folder.acl_users
00219 
00220         # Make sure the default returns None
00221         self.failUnlessEqual(gruf.getLUFSource(), None) 
00222 
00223         # Now try to test with an LDAP User Folder
00224         try:
00225             gruf.replaceUserSource(
00226                 "Users",
00227                 "manage_addProduct/LDAPUserFolder/manage_addLDAPUserFolder",
00228                 )
00229         except AttributeError: # No LDAP user folder installed so bail
00230             return
00231 
00232         # Make sure it now returns an LDAP user folder
00233         self.failUnlessEqual(gruf.getLUFSource().meta_type, 'LDAPUserFolder') 
00234 
00235         # Now add another and make sure it still picks the LDAP user folder
00236         gruf.addUserSource("manage_addProduct/OFSP/manage_addUserFolder")
00237         self.failUnlessEqual(gruf.getLUFSource().meta_type, 'LDAPUserFolder') 
00238 
00239 
00240 
00241 if __name__ == '__main__':
00242     framework(descriptions=1, verbosity=1)
00243 else:
00244     import unittest
00245     def test_suite():
00246         suite = unittest.TestSuite()
00247         suite.addTest(unittest.makeSuite(TestMultipleSources))
00248         return suite