Back to index

plone3  3.1.7
testGroupsTool.py
Go to the documentation of this file.
00001 #
00002 # Tests for GRUF's GroupsTool
00003 #
00004 
00005 from Products.CMFPlone.tests import PloneTestCase
00006 from Products.CMFCore.tests.base.testcase import WarningInterceptor
00007 
00008 from Acquisition import aq_base
00009 
00010 default_user = PloneTestCase.default_user
00011 
00012 def sortTuple(t):
00013     l = list(t)
00014     l.sort()
00015     return tuple(l)
00016 
00017 
00018 class TestGroupsTool(PloneTestCase.PloneTestCase, WarningInterceptor):
00019 
00020     def afterSetUp(self):
00021         self.membership = self.portal.portal_membership
00022         self.acl_users = self.portal.acl_users
00023         self.groups = self.portal.portal_groups
00024         self.groups.groupWorkspacesCreationFlag = 0
00025         self._trap_warning_output()
00026 
00027         if 'auto_group' in self.acl_users.objectIds():
00028             self.acl_users.manage_delObjects(['auto_group'])
00029 
00030         # Nuke Administators and Reviewers groups added in 2.1a2 migrations
00031         # (and any other migrated-in groups) to avoid test confusion
00032         self.groups.removeGroups(self.groups.listGroupIds())
00033 
00034     def testAddGroup(self):
00035         self.groups.addGroup('foo', [], [])
00036         self.assertEqual(self.groups.listGroupIds(), ['foo'])
00037         # No group workspace should have been created
00038         self.failIf(hasattr(aq_base(self.portal), self.groups.getGroupWorkspacesFolderId()))
00039 
00040     def testGetGroupById(self):
00041         self.groups.addGroup('foo', [], [])
00042         g = self.groups.getGroupById('foo')
00043         self.failIfEqual(g, None)
00044 
00045     def testGetBadGroupById(self):
00046         g = self.groups.getGroupById('foo')
00047         self.assertEqual(g, None)
00048 
00049     def testGroupByIdIsWrapped(self):
00050         self.groups.addGroup('foo', [], [])
00051         g = self.groups.getGroupById('foo')
00052         self.assertEqual(g.__class__.__name__, 'GroupData')
00053         self.assertEqual(g.aq_parent.__class__.__name__, 'PloneGroup')
00054         self.assertEqual(g.aq_parent.aq_parent.__class__.__name__, 'GroupManager')
00055 
00056     def testEditGroup(self):
00057         self.groups.addGroup('foo', )
00058         self.groups.editGroup('foo', roles = ['Reviewer']) #, ['foo.com']) => no domains on groups
00059         g = self.groups.getGroupById('foo')
00060         self.assertEqual(sortTuple(g.getRoles()), ('Authenticated', 'Reviewer'))
00061         ##self.assertEqual(g.getDomains(), ('foo.com',))                  # No domains on groups
00062         ##self.assertEqual(g.getGroup()._getPassword(), 'secret')         # No password for groups
00063 
00064     def testEditBadGroup(self):
00065         # Error type depends on the user folder...
00066         try:
00067             self.groups.editGroup('foo', [], [])
00068         except KeyError:
00069             pass        # Ok, this is the wanted behaviour
00070         except ValueError:
00071             pass        # Ok, this is the wanted behaviour
00072         else:
00073             self.fail("Should have raised KeyError or ValueError")
00074 
00075     def testRemoveGroups(self):
00076         self.groups.addGroup('foo', [], [])
00077         self.groups.removeGroups(['foo'])
00078         self.assertEqual(len(self.groups.listGroupIds()), 0)
00079 
00080     def testListGroupIds(self):
00081         self.groups.addGroup('foo', [], [])
00082         self.groups.addGroup('bar', [], [])
00083         grps = self.groups.listGroupIds()
00084         grps.sort()
00085         self.assertEqual(grps, ['bar', 'foo'])
00086 
00087     def testGetGroupsByUserId(self):
00088         self.groups.addGroup('foo', [], [])
00089         self.acl_users.userSetGroups(default_user, groupnames=['foo'])
00090         gs = self.groups.getGroupsByUserId(default_user)
00091         self.assertEqual(gs[0].getId(), 'foo')
00092 
00093     def testGroupsByUserIdAreWrapped(self):
00094         self.groups.addGroup('foo', [], [])
00095         self.acl_users.userSetGroups(default_user, groupnames=['foo'])
00096         gs = self.groups.getGroupsByUserId(default_user)
00097         self.assertEqual(gs[0].__class__.__name__, 'GroupData')
00098         self.assertEqual(gs[0].aq_parent.__class__.__name__, 'PloneGroup')
00099         self.assertEqual(gs[0].aq_parent.aq_parent.__class__.__name__, 'GroupManager')
00100 
00101     def testListGroups(self):
00102         self.groups.addGroup('foo', [], [])
00103         self.groups.addGroup('bar', [], [])
00104         gs = self.groups.listGroups()
00105         self.assertEqual(gs[0].getId(), 'bar')
00106         self.assertEqual(gs[1].getId(), 'foo')
00107 
00108     def testListedGroupsAreWrapped(self):
00109         self.groups.addGroup('foo', [], [])
00110         gs = self.groups.listGroups()
00111         self.assertEqual(gs[0].__class__.__name__, 'GroupData')
00112         self.assertEqual(gs[0].aq_parent.__class__.__name__, 'PloneGroup')
00113         self.assertEqual(gs[0].aq_parent.aq_parent.__class__.__name__, 'GroupManager')
00114 
00115     def testSetGroupOwnership(self):
00116         self.groups.addGroup('foo', [], [])
00117         self.folder.invokeFactory('Document', 'doc')
00118         doc = self.folder.doc
00119         g = self.groups.getGroupById('foo')
00120         self.groups.setGroupOwnership(g, doc)
00121         self.assertEqual(doc.getOwnerTuple()[1], 'foo')
00122         self.assertEqual(doc.get_local_roles_for_userid('foo'), ('Owner',))
00123         # TODO: Initial creator still has Owner role. Is this a bug?
00124         self.assertEqual(doc.get_local_roles_for_userid(default_user), ('Owner',))
00125 
00126     def testWrapGroup(self):
00127         self.groups.addGroup('foo', [], [])
00128         g = self.acl_users.getGroup('foo')
00129         self.assertEqual(g.__class__.__name__, 'PloneGroup')
00130         g = self.groups.wrapGroup(g)
00131         self.assertEqual(g.__class__.__name__, 'GroupData')
00132         self.assertEqual(g.aq_parent.__class__.__name__, 'PloneGroup')
00133         self.assertEqual(g.aq_parent.aq_parent.__class__.__name__, 'GroupManager')
00134 
00135     def testGetGroupInfo(self):
00136         self.groups.addGroup('foo', title='Foo', description='Bar', email='foo@foo.com')
00137         info = self.groups.getGroupInfo('foo')
00138         self.assertEqual(info.get('title'), 'Foo')
00139         self.assertEqual(info.get('description'), 'Bar')
00140         self.assertEqual(info.get('email'), None) # No email!
00141 
00142     def testGetGroupInfoAsAnonymous(self):
00143         self.groups.addGroup('foo', title='Foo', description='Bar')
00144         self.logout()
00145         info = self.groups.restrictedTraverse('getGroupInfo')('foo')
00146         self.assertEqual(info.get('title'), 'Foo')
00147         self.assertEqual(info.get('description'), 'Bar')
00148 
00149     def testGetBadGroupInfo(self):
00150         info = self.groups.getGroupInfo('foo')
00151         self.assertEqual(info, None)
00152 
00153     def beforeTearDown(self):
00154         self._free_warning_output()
00155 
00156 
00157 class TestGroupWorkspacesFolder(PloneTestCase.PloneTestCase, WarningInterceptor):
00158 
00159     def afterSetUp(self):
00160         self.membership = self.portal.portal_membership
00161         self.acl_users = self.portal.acl_users
00162         self.groups = self.portal.portal_groups
00163         self.groups.groupWorkspacesCreationFlag = 0
00164         self._trap_warning_output()
00165 
00166         if 'auto_group' in self.acl_users.objectIds():
00167             self.acl_users.manage_delObjects(['auto_group'])
00168         # Note that this is not a proper portal type (anymore) but we don't care
00169         self.portal.manage_addPortalFolder(self.groups.getGroupWorkspacesFolderId())
00170 
00171         # Nuke Administators and Reviewers groups added in 2.1a2 migrations
00172         # (and any other migrated-in groups) to avoid test confusion
00173         self.groups.removeGroups(self.groups.listGroupIds())
00174 
00175     def testGetGroupWorkspacesFolder(self):
00176         self.failIfEqual(self.groups.getGroupWorkspacesFolder(), None)
00177 
00178     def testCreateGrouparea(self):
00179         self.groups.addGroup('foo', [], [])
00180         self.groups.toggleGroupWorkspacesCreation()
00181         # TODO: Requires typestool
00182         self.groups.createGrouparea('foo')
00183         self.failUnless(hasattr(aq_base(self.groups.getGroupWorkspacesFolder()), 'foo'))
00184 
00185     def testNotCreateGrouparea(self):
00186         self.groups.addGroup('foo', [], [])
00187         # Creation flag is False
00188         self.groups.createGrouparea('foo')
00189         self.failIf(hasattr(aq_base(self.groups.getGroupWorkspacesFolder()), 'foo'))
00190 
00191     def testCreateGroupareaCreatesGroupWorkspacesFolder(self):
00192         self.groups.addGroup('foo', [], [])
00193         self.groups.toggleGroupWorkspacesCreation()
00194         self.portal._delObject(self.groups.getGroupWorkspacesFolderId())
00195         # TODO: Members cannot create folders in the portal root
00196         self.setRoles(['Manager'])
00197         self.groups.createGrouparea('foo')
00198         self.failUnless(hasattr(aq_base(self.groups.getGroupWorkspacesFolder()), 'foo'))
00199 
00200     def testCreateGroupareaIndexesGroupWorkspacesFolder(self):
00201         self.groups.addGroup('foo', [], [])
00202         self.groups.toggleGroupWorkspacesCreation()
00203         self.portal._delObject(self.groups.getGroupWorkspacesFolderId())
00204         # XXX: Members cannot create folders in the portal root
00205         self.setRoles(['Manager'])
00206         self.groups.createGrouparea('foo')
00207         cat_results = self.portal.portal_catalog(getId =
00208                                      self.groups.getGroupWorkspacesFolderId())
00209         self.assertEqual(len(cat_results), 1)
00210         self.assertEqual(cat_results[0].getObject(),
00211                                        self.groups.getGroupWorkspacesFolder())
00212 
00213     def testAddGroupCreatesGrouparea(self):
00214         self.groups.toggleGroupWorkspacesCreation()
00215         self.groups.addGroup('foo', [], [])
00216         self.failUnless(hasattr(aq_base(self.groups.getGroupWorkspacesFolder()), 'foo'))
00217 
00218     def testGetGroupareaFolder(self):
00219         self.groups.toggleGroupWorkspacesCreation()
00220         self.groups.addGroup('foo', [], [])
00221         self.failIfEqual(self.groups.getGroupareaFolder('foo'), None)
00222 
00223     def testGetGroupareaURL(self):
00224         self.groups.toggleGroupWorkspacesCreation()
00225         self.groups.addGroup('foo', [], [])
00226         self.failIfEqual(self.groups.getGroupareaURL('foo'), None)
00227 
00228     def testGetGroupareaFolderPermission(self):
00229         self.groups.toggleGroupWorkspacesCreation()
00230         self.groups.addGroup('foo', [], [])
00231         self.acl_users.userSetGroups(default_user, groupnames=['foo'])
00232         user = self.acl_users.getUser(default_user)
00233         self.failUnless(user.has_permission('View Groups', self.groups.getGroupWorkspacesFolder()))
00234 
00235     #def testGetGroupareaFolderForAuthenticated(self):
00236     #    # XXX: ERROR!
00237     #    self.groups.toggleGroupWorkspacesCreation()
00238     #    self.groups.addGroup('foo', [], [])
00239     #    self.acl_users.userSetGroups(default_user, groupnames=['foo'])
00240     #    self.login(default_user)
00241     #    self.failIfEqual(self.groups.getGroupareaFolder(), None)
00242 
00243     def testAddGroup(self):
00244         self.groups.addGroup('foo', [], [])
00245         self.assertEqual(self.groups.listGroupIds(), ['foo'])
00246         # No group workspace should have been created
00247         self.failIf(hasattr(aq_base(self.groups.getGroupWorkspacesFolder()), 'foo'))
00248 
00249     def testAddGroupWithWorkspace(self):
00250         self.groups.toggleGroupWorkspacesCreation()
00251         self.groups.addGroup('foo', [], [])
00252         self.assertEqual(self.groups.listGroupIds(), ['foo'])
00253         # A group workspace should have been created
00254         self.failUnless(hasattr(aq_base(self.groups.getGroupWorkspacesFolder()), 'foo'))
00255 
00256     def testRemoveGroups(self):
00257         self.groups.addGroup('foo', [], [])
00258         self.groups.removeGroups(['foo'])
00259         self.assertEqual(len(self.groups.listGroupIds()), 0)
00260 
00261     def testRemoveGroupsWithWorkspace(self):
00262         self.groups.toggleGroupWorkspacesCreation()
00263         self.groups.addGroup('foo', [], [])
00264         self.groups.removeGroups(['foo'])
00265         self.assertEqual(len(self.groups.listGroupIds()), 0)
00266         # Group workspace should have been removed
00267         self.failIf(hasattr(aq_base(self.groups.getGroupWorkspacesFolder()), 'foo'))
00268 
00269     def testRemoveGroupsKeepingWorkspaces(self):
00270         self.groups.toggleGroupWorkspacesCreation()
00271         self.groups.addGroup('foo', [], [])
00272         self.groups.removeGroups(['foo'], keep_workspaces=1)
00273         self.assertEqual(len(self.groups.listGroupIds()), 0)
00274         # Group workspace should still be present
00275         self.failUnless(hasattr(aq_base(self.groups.getGroupWorkspacesFolder()), 'foo'))
00276 
00277     def beforeTearDown(self):
00278         self._free_warning_output()
00279 
00280 
00281 def test_suite():
00282     from unittest import TestSuite, makeSuite
00283     suite = TestSuite()
00284     suite.addTest(makeSuite(TestGroupsTool))
00285     suite.addTest(makeSuite(TestGroupWorkspacesFolder))
00286     return suite