Back to index

plone3  3.1.7
testPloneTools.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: testPloneTools.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 # Load fixture
00031 from Testing import ZopeTestCase
00032 from Products.PloneTestCase import PloneTestCase
00033 PloneTestCase.setupPloneSite()
00034 
00035 # Permissions / security
00036 from AccessControl.Permissions import access_contents_information, view, \
00037      add_documents_images_and_files, change_images_and_files, view_management_screens
00038 from AccessControl.SecurityManagement import newSecurityManager, \
00039      noSecurityManager, getSecurityManager
00040 from AccessControl import Unauthorized
00041 from AccessControl.User import UnrestrictedUser
00042 
00043 import urllib
00044 import string
00045 
00046 # Create the error_log object
00047 app = ZopeTestCase.app()
00048 ZopeTestCase.utils.setupSiteErrorLog(app)
00049 ZopeTestCase.close(app)
00050     
00051 from Products.CMFCore.ActionProviderBase import ActionProviderBase
00052 from Products.GroupUserFolder.interfaces import IUserFolder
00053 from Products.GroupUserFolder import GroupUserFolder
00054 from Products.GroupUserFolder import GroupsTool
00055 from Products.GroupUserFolder import GroupDataTool
00056 from Interface import Verify
00057 
00058 # Install our product
00059 ZopeTestCase.installProduct('GroupUserFolder')
00060 
00061 import GRUFTestCase 
00062 import testInterface
00063 from Log import *
00064 
00065 class GroupTestCase(PloneTestCase.PloneTestCase, ):
00066     
00067     def afterSetUp(self):
00068         self.setRoles(['Manager'])
00069 
00070         # Basic assignements
00071         self.membership = self.portal.portal_membership
00072         self.gruf = self.portal.acl_users
00073         self.groups = self.portal.portal_groups
00074         self.prefix = self.gruf.getGroupPrefix()
00075         self.groups.groupWorkspacesCreationFlag = 0
00076 
00077         # Nuke Administators and Reviewers groups added in 2.1a2 migrations
00078         # (and any other migrated-in groups) to avoid test confusion
00079         self.groups.removeGroups(self.groups.listGroupIds())
00080 
00081         # Intial data
00082         self.groups.addGroup("grp")
00083         self.groups.addGroup("g1")
00084         self.groups.addGroup("g2")
00085         self.groups.addGroup("g3")
00086         self.groups.addGroup("g4")
00087         self.membership.addMember("u1", "secret", [], [])
00088         self.membership.addMember("u2", "secret", [], [])
00089         self.membership.addMember("u3", "secret", [], [])
00090         self.membership.addMember("u4", "secret", [], [])
00091         self.membership.addMember("u5", "secret", [], [])
00092         self.membership.addMember("u6", "secret", [], [])
00093         self.membership.addMember("u7", "secret", [], [])
00094         self.gruf.addMember("g1", "u2")
00095         self.gruf.addMember("g1", "u3")
00096         self.gruf.addMember("g1", "u4")
00097         self.gruf.addMember("g2", "u3")
00098         self.gruf.addMember("g2", "u4")
00099         self.gruf.addMember("g2", "u5")
00100         self.gruf.addMember("g3", "u4")
00101         self.gruf.addMember("g3", "u5")
00102         self.gruf.addMember("g3", "u6")
00103         self.gruf.addMember("g4", "u7")
00104 
00105 
00106 class TestGroupsTool(GroupTestCase, testInterface.TestInterface):
00107     klasses = (        # tell which classes to check
00108         GroupsTool.GroupsTool,
00109         )
00110     ignore_interfaces = (
00111         ActionProviderBase.__implements__,
00112     )
00113 
00114     def test_isGroup(self,):        
00115         g1 = self.groups.getGroupById("g1")
00116         g1.addMember("g2")
00117         ngroups = 0
00118         nusers = 0
00119         for u in g1.getGroupMembers():
00120             if self.groups.isGroup(u):
00121                 ngroups += 1
00122             else:
00123                 nusers += 1
00124         self.failUnlessEqual(ngroups, 1)
00125         self.failUnlessEqual(nusers, 3)
00126 
00127     def test_getGroupById(self, ):
00128         """Returns the portal_groupdata-ish object for a group corresponding
00129         to this id."""
00130         # Create a dummy group
00131         self.portal.portal_groups.addGroup("mygroup",)
00132 
00133         # Get group
00134         g = self.portal.portal_groups.getGroupById("mygroup")
00135         self.failUnless(g, "Emtpy or invalid group")
00136 
00137     def test_getGroupsByUserId(self, ):
00138         """Returns a list of the groups the user corresponding to 'userid' belongs to."""
00139         grps = self.groups.getGroupsByUserId("u3")
00140         ids = map(lambda x: x.getGroupName(), grps)
00141         ids.sort()
00142         self.failUnless(ids == ["g1", "g2", ], "Invalid groups: '%s'" % (ids, ))
00143 
00144     def test_listGroups(self, ):
00145         """Returns a list of the available portal_groupdata-ish objects."""
00146         grps = self.groups.listGroups()
00147         ids = map(lambda x: x.getGroupName(), grps)
00148         self.failUnless(ids == ["g1", "g2", "g3", "g4", "grp", ], "Invalid groups list: '%s'" % (ids, ))
00149 
00150     def test_listGroupIds(self, ):
00151         """Returns a list of the available groups' ids."""
00152         ids = self.groups.listGroupIds()
00153         ids.sort()
00154         self.failUnless(ids == ["g1", "g2", "g3", "g4", "grp", ], "Invalid groups list: '%s'" % (ids, ))
00155 
00156     def test_listGroupNames(self, ):
00157         """Returns a list of the available groups' ids."""
00158         ids = self.groups.listGroupNames()
00159         ids.sort()
00160         self.failUnless(ids == ["g1", "g2", "g3", "g4", "grp", ], "Invalid groups list: '%s'" % (ids, ))
00161 
00162     def test_searchForGroups(self, ):    # maybe searchGroups()?
00163         """Return a list of groups meeting certain conditions. """
00164 ##        grps = searchForGroups(name = "toto")
00165 
00166     def test_addGroup(self, ):
00167         """Create a group with the supplied id, roles, and domains.
00168 
00169         Underlying user folder must support adding users via the usual Zope API.
00170         Passwords for groups seem to be currently irrelevant in GRUF."""
00171         # Create a sample group
00172         self.groups.addGroup("grptest1",)
00173         self.failUnless("grptest1" in self.groups.listGroupIds())
00174 
00175         # Re-create that group to ensure it's forbidden
00176         self.failUnlessRaises(ValueError, self.groups.addGroup, "grptest1")
00177 
00178         # Create group with additional roles, groups and properties (if possible)
00179         self.groups.addGroup("grptest2", ["Reviewer", ], ["grptest1", ], description = "Sample group")
00180         g = self.groups.getGroupById("grptest2")
00181         g1 = self.groups.getGroupById("grptest1")
00182         self.failUnlessEqual(g.getProperty("description"), "Sample group")
00183         self.failUnless("grptest2" in g1.getGroupMemberIds(), g1.getGroupMemberIds(), )
00184         self.failUnless(self.gruf.getGroupByName("grptest2").has_role("Reviewer"))
00185 
00186     def test_editGroup(self, ):
00187         """Edit the given group with the supplied password, roles, and domains.
00188 
00189         Underlying user folder must support editing users via the usual Zope API.
00190         Passwords for groups seem to be currently irrelevant in GRUF."""
00191         # Edit a group with additional roles, groups and properties (if possible)
00192         self.groups.addGroup("grptest1",)
00193         self.groups.addGroup("grptest2",)
00194         self.groups.editGroup("grptest2", ["Reviewer", ], ["grptest1", ], description = "Sample group")
00195         g = self.groups.getGroupById("grptest2")
00196         g1 = self.groups.getGroupById("grptest1")
00197         self.failUnlessEqual(g.getProperty("description"), "Sample group")
00198         self.failUnless("group_grptest2" in g1.getGroup().getMemberIds(), g1.getGroup().getMemberIds(), )
00199         self.failUnless("grptest2" in g1.getGroupMemberIds(), g1.getGroupMemberIds(), )
00200         self.failUnless(self.gruf.getGroupByName("grptest2").has_role("Reviewer"))
00201 
00202         # Try to edit an invalid group
00203         self.failUnlessRaises(ValueError, self.groups.editGroup, "grptest_toto")
00204 
00205     def test_removeGroups(self, ):
00206         """Remove the group in the provided list (if possible).
00207 
00208         Will by default remove this group's GroupWorkspace if it exists. You may
00209         turn this off by specifying keep_workspaces=true.
00210         Underlying user folder must support removing users via the usual Zope API."""
00211         self.groups.addGroup("grptest1",)
00212         self.groups.addGroup("grptest2",)
00213         self.groups.editGroup("grptest2", ["Reviewer", ], ["grptest1", ], description = "Sample group")
00214         self.groups.removeGroups(["grptest1", "grptest2", ])
00215         self.failIf("grptest2" in self.groups.listGroupIds())
00216         self.failIf("grptest1" in self.groups.listGroupIds())
00217         self.failIf("grptest2" in self.gruf.getGroupNames())
00218         self.failIf("grptest1" in self.gruf.getGroupNames())
00219 
00220     def test_setGroupOwnership(self, ):
00221         """Make the object 'object' owned by group 'group' (a portal_groupdata-ish object)"""
00222 
00223     def test_setGroupWorkspacesFolder(self, ):
00224         """ Set the location of the Group Workspaces folder by id.
00225 
00226         The Group Workspaces Folder contains all the group workspaces, just like the
00227         Members folder contains all the member folders.
00228 
00229         If anyone really cares, we can probably make the id work as a path as well,
00230         but for the moment it's only an id for a folder in the portal root, just like the
00231         corresponding MembershipTool functionality. """
00232 
00233     def test_getGroupWorkspacesFolderId(self, ):
00234         """ Get the Group Workspaces folder object's id.
00235 
00236         The Group Workspaces Folder contains all the group workspaces, just like the
00237         Members folder contains all the member folders. """
00238 
00239     def test_getGroupWorkspacesFolder(self, ):
00240         """ Get the Group Workspaces folder object.
00241 
00242         The Group Workspaces Folder contains all the group workspaces, just like the
00243         Members folder contains all the member folders. """
00244 
00245     def test_toggleGroupWorkspacesCreation(self, ):
00246         """ Toggles the flag for creation of a GroupWorkspaces folder upon first
00247         use of the group. """
00248 
00249     def test_getGroupWorkspacesCreationFlag(self, ):
00250         """Return the (boolean) flag indicating whether the Groups Tool will create a group workspace
00251         upon the next use of the group (if one doesn't exist). """
00252 
00253     def test_getGroupWorkspaceType(self, ):
00254         """Return the Type (as in TypesTool) to make the GroupWorkspace."""
00255 
00256     def test_setGroupWorkspaceType(self, ):
00257         """Set the Type (as in TypesTool) to make the GroupWorkspace. Expects the name of a Type."""
00258 
00259     def test_createGrouparea(self, ):
00260         """Create a space in the portal for the given group, much like member home
00261         folders."""
00262 
00263     def test_getGroupareaFolder(self, ):
00264         """Returns the object of the group's work area."""
00265 
00266     def test_getGroupareaURL(self, ):
00267         """Returns the full URL to the group's work area."""
00268 
00269     
00270 class TestGroupDataTool(GroupTestCase, testInterface.TestInterface):
00271     klasses = (        # tell which classes to check
00272         GroupDataTool.GroupDataTool,
00273         )
00274     ignore_interfaces = (
00275         ActionProviderBase.__implements__,
00276         )
00277 
00278     def test_wrapGroup(self,):
00279         """Test group wrapping"""
00280         g1 = self.groups.getGroupById("g1")
00281         self.failUnlessEqual(g1.__class__.__name__, "GroupData")
00282         self.failUnlessEqual(g1.getGroupName(), "g1")
00283         g1 = self.groups.getGroupById("group_g1")
00284         self.failUnlessEqual(g1.__class__.__name__, "GroupData")
00285         self.failUnlessEqual(g1.getGroupName(), "g1")
00286 
00287 
00288 class TestGroupData(GroupTestCase, testInterface.TestInterface):
00289     klasses = (        # tell which classes to check
00290         GroupDataTool.GroupData,
00291         )
00292     ignore_interfaces = (
00293         ActionProviderBase.__implements__,
00294     )
00295     
00296     def test_setProperties(self, properties = None, **kw):
00297         """We set some properties on groups
00298         """
00299         g = self.groups.getGroupById("g1")
00300 
00301         # Regular property setting
00302         g.setProperties({
00303             "email": "test@toto.com",
00304             "description": "azer",
00305             })
00306         self.failUnlessEqual(g.getProperty("email"), "test@toto.com", )
00307         self.failUnlessEqual(g.getProperty("description"), "azer", )
00308         
00309         # Keyword property setting
00310         g.setProperties(email = "other@toto.com", description = "Bloub.")
00311         self.failUnlessEqual(g.getProperty("email"), "other@toto.com", )
00312         self.failIfEqual(g.getProperty("name"), "Bloub.")
00313 
00314         # The Hacky Touch
00315         g.setProperties(id = "INVALID")
00316         self.failIfEqual(g.getProperty("id"), "g1")
00317 
00318 
00319     def test_getProperty(self,):
00320         g1 = self.groups.getGroupById("g1")
00321         self.failUnlessEqual(g1.getProperty("name"), "g1")
00322 
00323 
00324     def test_getProperties(self,):
00325         g1 = self.groups.getGroupById("g1")
00326         self.failUnlessEqual(
00327             g1.getProperties(),
00328             {"email": "", "description": "", "title": ""},
00329             )
00330         g1.setProperties(email = "test@toto.com", description = "marih", title = "Hello")
00331         self.failUnlessEqual(
00332             g1.getProperties(),
00333             {
00334             "email": "test@toto.com",
00335             "description": "marih",
00336             "title": "Hello",
00337             },
00338             )
00339 
00340     def test_getGroupId(self,):
00341         g1 = self.groups.getGroupById("g1")
00342         self.failUnlessEqual(g1.getGroupId(), "g1")
00343 
00344     def test_getMemberId(self,):
00345         g1 = self.groups.getGroupById("g1")
00346         self.failUnlessEqual(g1.getMemberId(), "g1")
00347 
00348     def test_getGroupName(self,):
00349         g1 = self.groups.getGroupById("g1")
00350         self.failUnlessEqual(g1.getGroupName(), "g1")
00351 
00352     def test_getGroupMembers(self,):
00353         # Flat group members
00354         g1 = self.groups.getGroupById("g1")
00355         members = map(lambda x: x.getMemberId(), g1.getGroupMembers())
00356         members.sort()
00357         self.failUnlessEqual(
00358             members,
00359             ["u2", "u3", "u4", ]
00360             )
00361 
00362         # Multiple level group members (ie. nested groups)
00363         g1.addMember("g2")
00364         members = map(lambda x: x.getMemberId(), g1.getGroupMembers())
00365         members.sort()
00366         self.failUnlessEqual(
00367             members,
00368             ["g2", "u2", "u3", "u4", ]
00369             )
00370 
00371     def test_getAllGroupMembers(self,):
00372         # Flat group members
00373         g1 = self.groups.getGroupById("g1")
00374         members = map(lambda x: x.getMemberId(), g1.getAllGroupMembers())
00375         members.sort()
00376         self.failUnlessEqual(
00377             members,
00378             ["u2", "u3", "u4", ]
00379             )
00380 
00381         # Multiple level group members (ie. nested groups)
00382         g1.addMember("g2")
00383         members = map(lambda x: x.getMemberId(), g1.getAllGroupMembers())
00384         members.sort()
00385         self.failUnlessEqual(
00386             members,
00387             ["g2", "u2", "u3", "u4", "u5", ]
00388             )
00389 
00390     def test_getGroupMemberIds(self,):
00391         g1 = self.groups.getGroupById("g1")
00392         members = g1.getGroupMemberIds()
00393         members.sort()
00394         self.failUnlessEqual(
00395             members,
00396             ["u2", "u3", "u4", ]
00397             )
00398 
00399         # Multiple level group members (ie. nested groups)
00400         g1.addMember("g2")
00401         members = g1.getGroupMemberIds()
00402         members.sort()
00403         self.failUnlessEqual(
00404             members,
00405             ["g2", "u2", "u3", "u4", ]
00406             )
00407 
00408     def test_getAllGroupMemberIds(self,):
00409         g1 = self.groups.getGroupById("g1")
00410         members = g1.getAllGroupMemberIds()
00411         members.sort()
00412         self.failUnlessEqual(
00413             members,
00414             ["u2", "u3", "u4", ]
00415             )
00416 
00417         # Multiple level group members (ie. nested groups)
00418         g1.addMember("g2")
00419         members = g1.getAllGroupMemberIds()
00420         members.sort()
00421         self.failUnlessEqual(
00422             members,
00423             ["g2", "u2", "u3", "u4", "u5", ]
00424             )
00425 
00426     def test_addMember(self,):
00427         g1 = self.groups.getGroupById("g1")
00428 
00429         # Valid user
00430         g1.addMember("u1")
00431         members = g1.getGroupMemberIds()
00432         members.sort()
00433         self.failUnlessEqual(
00434             members,
00435             ["u1", "u2", "u3", "u4", ]
00436             )
00437 
00438         # Invalid user
00439         self.failUnlessRaises(
00440             ValueError,
00441             g1.addMember,
00442             "bloubbloub",
00443             )
00444 
00445     def test_removeMember(self,):
00446         g1 = self.groups.getGroupById("g1")
00447 
00448         # Valid user
00449         g1.removeMember("u2")
00450         members = g1.getGroupMemberIds()
00451         members.sort()
00452         self.failUnlessEqual(
00453             members,
00454             ["u3", "u4", ]
00455             )
00456 
00457         # Invalid user
00458         self.failUnlessRaises(
00459             ValueError,
00460             g1.removeMember,
00461             "bloubbloub",
00462             )
00463 
00464     def test_getGroup(self,):
00465         g1 = self.groups.getGroupById("g1")
00466         self.failUnlessEqual(
00467             g1.getGroup().__class__.__name__,
00468             "GRUFGroup",
00469             )
00470 
00471 
00472 from AccessControl import Unauthorized
00473 from AccessControl import Permissions
00474 from Testing.ZopeTestCase import user_name
00475 
00476 class TestMethodProtection(PloneTestCase.PloneTestCase):
00477     # GroupData has wrong security declarations
00478 
00479     def afterSetUp(self):
00480         self.groups = self.portal.portal_groups
00481         self.groups.groupWorkspacesCreationFlag = 0
00482         self.groups.addGroup('foo')
00483         self.groupdata = self.groups.getGroupById('foo')
00484 
00485     def testAnonAddMember(self):
00486         self.logout()
00487         self.assertRaises(Unauthorized, self.groupdata.addMember, user_name)
00488 
00489     def testAnonRemoveMember(self):
00490         self.logout()
00491         self.assertRaises(Unauthorized, self.groupdata.removeMember, user_name)
00492 
00493     def testMemberAddMember(self):
00494         self.assertRaises(Unauthorized, self.groupdata.addMember, user_name)
00495 
00496     def testMemberRemoveMember(self):
00497         self.assertRaises(Unauthorized, self.groupdata.removeMember, user_name)
00498 
00499     def testManagerAddMember(self):
00500         self.setPermissions([Permissions.manage_users])
00501         self.groupdata.addMember(user_name)
00502 
00503     def testManagerRemoveMember(self):
00504         self.setPermissions([Permissions.manage_users])
00505         self.groupdata.addMember(user_name)
00506         self.groupdata.removeMember(user_name)
00507 
00508 
00509 if __name__ == '__main__':
00510     framework(descriptions=1, verbosity=1)
00511 else:
00512     import unittest
00513     def test_suite():
00514         suite = unittest.TestSuite()
00515         suite.addTest(unittest.makeSuite(TestGroupsTool))
00516         suite.addTest(unittest.makeSuite(TestGroupDataTool))
00517         suite.addTest(unittest.makeSuite(TestGroupData))
00518         suite.addTest(unittest.makeSuite(TestMethodProtection))
00519         return suite
00520