Back to index

plone3  3.1.7
testPloneInterface.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: testPloneInterface.py 30098 2006-09-08 12:35:01Z encolpe $
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 from Products.PloneTestCase import PloneTestCase
00035 PloneTestCase.setupPloneSite()
00036 
00037 # Permissions / security
00038 from AccessControl.Permissions import access_contents_information, view, add_documents_images_and_files, change_images_and_files, view_management_screens
00039 from AccessControl.SecurityManagement import newSecurityManager, noSecurityManager, getSecurityManager
00040 from AccessControl import Unauthorized
00041 from AccessControl.User import UnrestrictedUser
00042 
00043 import urllib
00044 
00045 # Create the error_log object
00046 app = ZopeTestCase.app()
00047 ZopeTestCase.utils.setupSiteErrorLog(app)
00048 ZopeTestCase.close(app)
00049 
00050 # Start the web server
00051 host, port = ZopeTestCase.utils.startZServer(4)
00052 base = 'http://%s:%d/%s' %(host, port, ZopeTestCase.folder_name)
00053 
00054 # Install our product
00055 ZopeTestCase.installProduct('GroupUserFolder')
00056 
00057 
00058 # Set log options if Log module is available
00059 # This is done to set LOG_PROCESSORs to file logs instead of Zope logs
00060 try:
00061     import Log
00062 
00063     Log.LOG_LEVEL = Log.LOG_DEBUG
00064 
00065     Log.LOG_PROCESSOR = {
00066         Log.LOG_NONE: Log.logFile,
00067         Log.LOG_CRITICAL: Log.logFile,
00068         Log.LOG_ERROR: Log.logFile,
00069         Log.LOG_WARNING: Log.logFile,
00070         Log.LOG_NOTICE: Log.logFile,
00071         Log.LOG_DEBUG: Log.logFile,
00072         }
00073 
00074     from Log import *
00075     Log(LOG_NOTICE, "Starting %s at %d debug level" % (os.path.dirname(__file__), LOG_LEVEL, ))
00076 
00077 except:
00078     print "Log module not available"
00079     LOG_DEBUG = None
00080     LOG_NOTICE = None
00081     LOG_WARNING = None
00082     LOG_ERROR = None
00083     LOG_CRITICAL = None
00084     def Log(*args, **kw):
00085         pass
00086     raise
00087 
00088 
00089 
00090 class ManagementOpener(urllib.FancyURLopener):
00091     def prompt_user_passwd(self, host, realm):
00092         return ('manager', 'secret')
00093 
00094 class UnauthorizedOpener(urllib.FancyURLopener):
00095     def prompt_user_passwd(self, host, realm):
00096         raise Unauthorized, 'The URLopener was asked for authentication'
00097 
00098 
00099 class TestPloneInterface(PloneTestCase.PloneTestCase):
00100 
00101     def afterSetUp(self,):
00102         self.loginPortalOwner()
00103         self.qi = self.portal.portal_quickinstaller
00104         self.qi.installProduct('GroupUserFolder')
00105         self.mt = self.portal.portal_membership
00106         self.gt = self.portal.portal_groups
00107         self.acl_users = self.portal.acl_users
00108 
00109     def testUserCreation(self,):
00110         """
00111         test user creation with plone
00112         """
00113         # Create a non-group-related user
00114         self.mt.addMember("member1", "secret", ['Member',], None)
00115         self.failUnless("member1" in self.acl_users.getUserNames())
00116         self.failUnless("Member" in self.acl_users.getUser("member1").getRoles())
00117 
00118     def testGroupCreation(self,):
00119         """
00120         test group creation with plone
00121         """
00122         # Group without roles
00123         self.gt.addGroup("group1", roles = ['Member',], )
00124         self.failUnless("group_group1" in self.acl_users.getGroupNames())
00125         self.failUnless("Member" in self.acl_users.getGroup("group_group1").getRoles())
00126 
00127         # Group with valid roles
00128         self.portal._addRole('SampleRole')
00129         self.gt.addGroup("group2", roles = ['SampleRole',], )
00130         self.failUnless("SampleRole" in self.acl_users.getGroup("group_group2").getRoles())
00131         self.failUnless("Member" not in self.acl_users.getGroup("group_group2").getRoles())
00132 
00133     def testUserToGroup(self,):
00134         """
00135         test user and group interaction with Plone API
00136         """
00137         # Add a user and a group with valid roles
00138         self.portal._addRole('SampleRole')
00139         self.gt.addGroup("group2", roles = ['SampleRole',], )
00140         self.mt.addMember("member1", "secret", ['Member',], None)
00141 
00142         # test group affectation
00143         group = self.gt.getGroupById("group2")
00144         group.addMember("member1")
00145         Log(LOG_DEBUG, group.getGroupMemberIds())
00146         self.failUnless("member1" in group.getGroupMemberIds())
00147 
00148     def testUserToGroupRoles(self,):
00149         # Add a user and a group with valid roles
00150         self.portal._addRole('SampleRole')
00151         self.gt.addGroup("group2", roles = ['SampleRole',], )
00152         self.mt.addMember("member1", "secret", ['Member',], None)
00153         group = self.gt.getGroupById("group2")
00154         group.addMember("member1")
00155 
00156         # test roles
00157         self.failUnless("SampleRole" in self.acl_users.getUser("member1").getRoles())
00158         self.failUnless("Member" in self.acl_users.getUser("member1").getRoles())
00159 
00160         # Ensure that "group-acquired" role is not affected to the user directly
00161         self.failUnless("SampleRole" not in self.acl_users.getUser("member1").getUserRoles())
00162 
00163 
00164     def testUserToGroupRolesBug(self,):
00165         # Try to reproduce a bug happening when you affect twice the same user
00166         # to the same group: in this case, the user will get group's role affected
00167         # to him directly... which should not happend.
00168         # Add a user and a group with valid roles
00169         self.portal._addRole('SampleRole')
00170         self.gt.addGroup("group2", roles = ['SampleRole',], )
00171         self.mt.addMember("member1", "secret", ['Member',], None)
00172         group = self.gt.getGroupById("group2")
00173         group.addMember("member1")
00174 
00175         # Do it once again
00176         group.addMember("member1")
00177 
00178         # Ensure that "group-acquired" role is not affected to the user directly
00179         self.failIf("SampleRole" in self.acl_users.getUser("member1").getUserRoles())
00180 
00181     def testUserToGroupRemoving(self,):
00182         # Add a user and a group with valid roles
00183         self.portal._addRole('SampleRole')
00184         self.gt.addGroup("group2", roles = ['SampleRole',], )
00185         self.mt.addMember("member1", "secret", ['Member',], None)
00186         group = self.gt.getGroupById("group2")
00187         group.addMember("member1")
00188 
00189         # Remove user from the group and check if everything still works
00190         group.removeMember("member1")
00191         self.failUnless("member1" not in group.getGroupMembers())
00192         self.failUnless("SampleRole" not in self.acl_users.getUser("member1").getRoles())
00193         self.failUnless("Member" in self.acl_users.getUser("member1").getRoles())
00194 
00195 
00196 if __name__ == '__main__':
00197     framework(descriptions=1, verbosity=1)
00198 else:
00199     import unittest
00200     def test_suite():
00201         suite = unittest.TestSuite()
00202 ##        suite.addTest(unittest.makeSuite(TestPloneInterface))
00203         return suite