Back to index

plone3  3.1.7
testGroupUserFolder.py
Go to the documentation of this file.
00001 #
00002 # GRUF3 tests
00003 #
00004 
00005 from Products.CMFPlone.tests import PloneTestCase
00006 from Products.CMFCore.tests.base.testcase import WarningInterceptor
00007 
00008 default_user = PloneTestCase.default_user
00009 default_group = 'test_group_1_'
00010 try:
00011     import Products.PlonePAS
00012 except ImportError:
00013     PREFIX = 'group_'
00014 else:
00015     PREFIX = ''
00016 
00017 
00018 class TestGroupUserFolder(PloneTestCase.PloneTestCase, WarningInterceptor):
00019 
00020     def afterSetUp(self):
00021         self.uf = self.portal.acl_users
00022         self._trap_warning_output()
00023         
00024         if 'auto_group' in self.uf.objectIds():
00025             self.uf.manage_delObjects(['auto_group'])
00026 
00027         # Nuke Administators and Reviewers groups added in 2.1a2 migrations
00028         # (and any other migrated-in groups) to avoid test confusion
00029         self.portal.portal_groups.removeGroups(self.portal.portal_groups.listGroupIds())
00030         
00031         self.uf.userFolderAddGroup(default_group, [])
00032         self.uf.userSetGroups(default_user, groupnames=[default_group])
00033 
00034     def testGetUser(self):
00035         self.failIfEqual(self.uf.getUser(default_user), None)
00036 
00037     def testGetBadUser(self):
00038         self.assertEqual(self.uf.getUser('user2'), None)
00039 
00040     def testGetUserById(self):
00041         self.failIfEqual(self.uf.getUserById(default_user), None)
00042 
00043     def testGetBadUserById(self):
00044         self.assertEqual(self.uf.getUserById('user2'), None)
00045 
00046     def testGetGroup(self):
00047         self.failIfEqual(self.uf.getGroup(default_group), None)
00048 
00049     def testGetBadGroup(self):
00050         self.assertEqual(self.uf.getGroup('group2'), None)
00051 
00052     def testGetGroupById(self):
00053         self.failIfEqual(self.uf.getGroupById(PREFIX+default_group), None)
00054 
00055     def testGetBadGroupById(self):
00056         self.assertEqual(self.uf.getGroupById('group2'), None)
00057 
00058     def testGetGroupByName(self):
00059         self.failIfEqual(self.uf.getGroupByName(default_group), None)
00060 
00061     def testGetBadGroupByName(self):
00062         self.assertEqual(self.uf.getGroupByName('group2'), None)
00063 
00064     def testGetUsers(self):
00065         # Returns users and groups
00066         users = self.uf.getUsers()
00067         self.failUnless(users)
00068         self.assertEqual(len(users), 1)
00069         userids = [x.getId() for x in users]
00070         self.failUnless(default_user in userids)
00071         self.failIf(PREFIX+default_group in userids)
00072 
00073     def testGetUserIds(self):
00074         # Returns user and group ids
00075         userids = self.uf.getUserIds()
00076         self.failUnless(userids)
00077         self.assertEqual(len(userids), 1)
00078         self.failUnless(default_user in userids)
00079         self.failIf(PREFIX+default_group in userids)
00080 
00081     def testGetUserNames(self):
00082         # Returns user and group names
00083         usernames = self.uf.getUserNames()
00084         self.failUnless(usernames)
00085         self.assertEqual(len(usernames), 1)
00086         self.failUnless(default_user in usernames)
00087         self.failIf(default_group in usernames)
00088 
00089     def testGetGroups(self):
00090         # Returns groups
00091         groups = self.uf.getGroups()
00092         self.failUnless(groups)
00093         self.assertEqual(len(groups), 1)
00094         groupids = [x.getId() for x in groups]
00095         self.failUnless(PREFIX+default_group in groupids)
00096 
00097     def testGetGroupIds(self):
00098         # Returns group ids
00099         groupids = self.uf.getGroupIds()
00100         self.failUnless(groupids)
00101         self.assertEqual(len(groupids), 1)
00102         self.failUnless(PREFIX+default_group in groupids)
00103 
00104     def testGetGroupNames(self):
00105         # Returns group names
00106         groupnames = self.uf.getGroupNames()
00107         self.failUnless(groupnames)
00108         self.assertEqual(len(groupnames), 1)
00109         self.failUnless(default_group in groupnames)
00110 
00111     def beforeTearDown(self):
00112         self._free_warning_output()
00113 
00114 
00115 class TestUserManagement(PloneTestCase.PloneTestCase, WarningInterceptor):
00116 
00117     def afterSetUp(self):
00118         self.uf = self.portal.acl_users
00119         self._trap_warning_output()
00120 
00121         if 'auto_group' in self.uf.objectIds():
00122             self.uf.manage_delObjects(['auto_group'])
00123         self.uf.userFolderAddGroup(default_group, [])
00124         self.uf.userSetGroups(default_user, groupnames=[default_group])
00125 
00126     # Classic UF interface
00127 
00128     def test_doAddUser(self):
00129         self.uf._doAddUser('user2', 'secret', ['Member'], [])
00130         user = self.uf.getUser('user2')
00131         self.assertEqual(tuple(user.getRoles()), ('Member', 'Authenticated'))
00132         self.assertEqual(user.getGroups(), [])  # XXX: Should be tuple
00133         self.assertEqual(user.getGroupIds(), [])
00134         self.assertEqual(user.getGroupNames(), [])
00135 
00136     def test_doAddUser_WithGroups(self):
00137         self.uf._doAddUser('user2', 'secret', ['Member'], [], [default_group])
00138         user = self.uf.getUser('user2')
00139         self.assertEqual(tuple(user.getRoles()), ('Member', 'Authenticated'))
00140         self.assertEqual(user.getGroups(), [PREFIX+default_group])  # XXX: WTF?
00141         self.assertEqual(user.getGroupIds(), [PREFIX+default_group])
00142         self.assertEqual(user.getGroupNames(), [default_group])
00143 
00144     def test_doChangeUser(self):
00145         self.uf._doChangeUser(default_user, None, ['Reviewer'], [])
00146         user = self.uf.getUser(default_user)
00147         self.assertEqual(tuple(user.getRoles()), ('Reviewer', 'Authenticated'))
00148         self.assertEqual(user.getGroups(), [PREFIX+default_group])
00149 
00150     def test_doChangeUser_WithGroups(self):
00151         self.uf.userFolderAddGroup('group2', [])
00152         self.uf._doChangeUser(default_user, None, ['Reviewer'], [], ['group2'])
00153 
00154         user = self.uf.getUser(default_user)
00155         self.assertEqual(tuple(user.getRoles()), ('Reviewer', 'Authenticated'))
00156         self.assertEqual(user.getGroups(), [PREFIX+'group2'])
00157 
00158     def test_doDelUsers(self):
00159         self.uf._doDelUsers([default_user])
00160         self.failUnless(self.uf.getUser(default_user) is None)
00161 
00162     # The following tests must behave *exactly* like the ones above
00163     # but using the Zope-2.5 interface
00164 
00165     def testUserFolderAddUser(self):
00166         self.uf.userFolderAddUser('user2', 'secret', ['Member'], [])
00167         user = self.uf.getUser('user2')
00168         self.assertEqual(tuple(user.getRoles()), ('Member', 'Authenticated'))
00169         self.assertEqual(user.getGroups(), [])
00170         self.assertEqual(user.getGroupIds(), [])
00171         self.assertEqual(user.getGroupNames(), [])
00172 
00173     def testUserFolderAddUser_WithGroups(self):
00174         self.uf.userFolderAddUser('user2', 'secret', ['Member'], [], [default_group])
00175         user = self.uf.getUser('user2')
00176         self.assertEqual(tuple(user.getRoles()), ('Member', 'Authenticated'))
00177         self.assertEqual(user.getGroups(), [PREFIX+default_group])
00178         self.assertEqual(user.getGroupIds(), [PREFIX+default_group])
00179         self.assertEqual(user.getGroupNames(), [default_group])
00180 
00181     def testUserFolderEditUser(self):
00182         self.uf.userFolderEditUser(default_user, None, ['Reviewer'], [])
00183         user = self.uf.getUser(default_user)
00184         self.assertEqual(tuple(user.getRoles()), ('Reviewer', 'Authenticated'))
00185         self.assertEqual(user.getGroups(), [PREFIX+default_group])
00186 
00187     def testUserFolderEditUser_WithGroups(self):
00188         self.uf.userFolderAddGroup('group2', [])
00189         self.uf.userFolderEditUser(default_user, None, ['Reviewer'], [], ['group2'])
00190         user = self.uf.getUser(default_user)
00191         self.assertEqual(tuple(user.getRoles()), ('Reviewer', 'Authenticated'))
00192         self.assertEqual(user.getGroups(), [PREFIX+'group2'])
00193 
00194     def testUserFolderDelUsers(self):
00195         self.uf.userFolderDelUsers([default_user])
00196         self.failUnless(self.uf.getUser(default_user) is None)
00197 
00198     # GRUF update interface
00199 
00200     def test_updateUser_Groups(self):
00201         self.uf.userFolderAddGroup('group2', [])
00202         self.uf.userSetGroups(default_user, groupnames=['group2'])
00203         user = self.uf.getUser(default_user)
00204         self.assertEqual(tuple(user.getRoles()), ('Member', 'Authenticated'))
00205         self.assertEqual(user.getGroups(), [PREFIX+'group2'])
00206 
00207     def beforeTearDown(self):
00208         self._free_warning_output()
00209 
00210 
00211 class TestGroupManagement(PloneTestCase.PloneTestCase, WarningInterceptor):
00212 
00213     def afterSetUp(self):
00214         self.uf = self.portal.acl_users
00215         self._trap_warning_output()
00216 
00217         if 'auto_group' in self.uf.objectIds():
00218             self.uf.manage_delObjects(['auto_group'])
00219         self.uf.userFolderAddGroup(default_group, [])
00220         self.uf.userFolderAddGroup('group2', [])
00221 
00222     # Classic-style interface
00223 
00224     def test_doAddGroup(self):
00225         self.uf._doAddGroup('group3', ['Reviewer'])
00226         group = self.uf.getGroup('group3')
00227         self.assertEqual(tuple(group.getRoles()), ('Reviewer', 'Authenticated'))
00228         self.assertEqual(group.getGroups(), [])  # XXX: Should be tuple
00229 
00230     def test_doChangeGroup(self):
00231         self.uf._doChangeGroup(default_group, ['Reviewer'])
00232         group = self.uf.getGroup(default_group)
00233         self.assertEqual(tuple(group.getRoles()), ('Reviewer', 'Authenticated'))
00234 
00235     def test_doDelGroups(self):
00236         self.uf._doDelGroups([default_group])
00237         self.failUnless(self.uf.getGroup(default_group) is None)
00238 
00239     # Zope-2.5-style interface
00240 
00241     def testUserFolderAddGroup(self):
00242         self.uf.userFolderAddGroup('group3', ['Reviewer'])
00243         group = self.uf.getGroup('group3')
00244         self.assertEqual(tuple(group.getRoles()), ('Reviewer', 'Authenticated'))
00245         self.assertEqual(group.getGroups(), [])
00246 
00247     def testUserFolderEditGroup(self):
00248         self.uf.userFolderEditGroup(default_group, ['Reviewer'], [])
00249         group = self.uf.getGroup(default_group)
00250         self.assertEqual(tuple(group.getRoles()), ('Reviewer', 'Authenticated'))
00251 
00252     def testUserFolderDelGroups(self):
00253         self.uf.userFolderDelGroups([default_group])
00254         self.failUnless(self.uf.getGroup(default_group) is None)
00255 
00256     # Update interface
00257 
00258     def test_updateGroup_Roles(self):
00259         self.uf._updateGroup(default_group, roles=['Reviewer'])
00260         group = self.uf.getGroup(default_group)
00261         self.assertEqual(tuple(group.getRoles()), ('Reviewer', 'Authenticated'))
00262 
00263     def beforeTearDown(self):
00264         self._free_warning_output()
00265 
00266 
00267 class TestUsersAndGroups(PloneTestCase.PloneTestCase, WarningInterceptor):
00268 
00269     def afterSetUp(self):
00270         self.uf = self.portal.acl_users
00271         self._trap_warning_output()
00272 
00273         self.uf.userFolderAddGroup(default_group, [])
00274         if 'auto_group' in self.uf.objectIds():
00275             self.uf.manage_delObjects(['auto_group'])
00276         self.uf.userSetGroups(default_user, groupnames=[default_group])
00277 
00278         self.user = self.uf.getUser(default_user)
00279         self.group = self.uf.getGroup(default_group)
00280 
00281     # User interface
00282 
00283     def testUserGetId(self):
00284         self.assertEqual(self.user.getId(), default_user)
00285 
00286     def testUserGetName(self):
00287         self.assertEqual(self.user.getUserName(), default_user)
00288 
00289     def testUserGetRoles(self):
00290         self.assertEqual(tuple(self.user.getRoles()), ('Member', 'Authenticated'))
00291 
00292     def testUserGetGroups(self):
00293         # XXX: This should return a tuple!
00294         # Also note that it returns group ids, i.e. including the prefix
00295         self.assertEqual(self.user.getGroups(), [PREFIX+default_group])
00296 
00297     def testUserGetGroupNames(self):
00298         self.assertEqual(self.user.getGroupNames(), [default_group]) # XXX: Should be tuple
00299 
00300     # Group interface
00301 
00302     def testGroupGetId(self):
00303         self.assertEqual(self.group.getId(), PREFIX+default_group)
00304 
00305     def testGroupGetName(self):
00306         # XXX: Bah! Why no getGroupName? Only available on GroupData-wrapped members!
00307         #self.assertEqual(self.group.getGroupName(), default_group)
00308         self.assertEqual(self.group.getName(), default_group)
00309 
00310     def testGroupGetRoles(self):
00311         self.assertEqual(tuple(self.group.getRoles()), ('Authenticated',))
00312 
00313     def testGroupGetGroups(self):
00314         self.assertEqual(self.group.getGroups(), []) # XXX: Should be tuple
00315 
00316     def testGroupGetGroupNames(self):
00317         self.assertEqual(self.group.getGroupNames(), []) # XXX: Should be tuple
00318 
00319     def testGroupGetMembers(self):
00320         # XXX: No getMembers! GroupData-wrapper provides getGroupMembers.
00321         pass
00322 
00323     def testGroupGetMemberIds(self):
00324         self.assertEqual(list(self.group.getMemberIds()), [default_user])
00325 
00326     def beforeTearDown(self):
00327         self._free_warning_output()
00328 
00329 
00330 def test_suite():
00331     from unittest import TestSuite, makeSuite
00332     suite = TestSuite()
00333     suite.addTest(makeSuite(TestGroupUserFolder))
00334     suite.addTest(makeSuite(TestUserManagement))
00335     suite.addTest(makeSuite(TestGroupManagement))
00336     suite.addTest(makeSuite(TestUsersAndGroups))
00337     return suite