Back to index

plone3  3.1.7
testGroupDataTool.py
Go to the documentation of this file.
00001 #
00002 # Tests for GRUF's GroupDataTool
00003 #
00004 
00005 from Products.CMFPlone.tests import PloneTestCase
00006 from Products.CMFCore.tests.base.testcase import WarningInterceptor
00007 
00008 from AccessControl import Unauthorized
00009 from AccessControl import Permissions
00010 
00011 default_user = PloneTestCase.default_user
00012 
00013 def sortTuple(t):
00014     l = list(t)
00015     l.sort()
00016     return tuple(l)
00017 
00018 
00019 class TestGroupDataTool(PloneTestCase.PloneTestCase):
00020 
00021     def afterSetUp(self):
00022         self.acl_users = self.portal.acl_users
00023         self.groups = self.portal.portal_groups
00024         self.groupdata = self.portal.portal_groupdata
00025         self.groups.groupWorkspacesCreationFlag = 0
00026         self.groups.addGroup('foo')
00027         # MUST reset _v_ attributes!
00028         self.groupdata._v_temps = None
00029         if 'auto_group' in self.acl_users.objectIds():
00030             self.acl_users.manage_delObjects(['auto_group'])
00031 
00032     def testWrapGroup(self):
00033         g = self.acl_users.getGroup('foo')
00034         self.assertEqual(g.__class__.__name__, 'PloneGroup')
00035         g = self.groupdata.wrapGroup(g)
00036         self.assertEqual(g.__class__.__name__, 'GroupData')
00037         self.assertEqual(g.aq_parent.__class__.__name__, 'PloneGroup')
00038         self.assertEqual(g.aq_parent.aq_parent.__class__.__name__, 'GroupManager')
00039 
00040 
00041 class TestGroupData(PloneTestCase.PloneTestCase, WarningInterceptor):
00042 
00043     def afterSetUp(self):
00044         self.membership = self.portal.portal_membership
00045         self.memberdata = self.portal.portal_memberdata
00046         self.acl_users = self.portal.acl_users
00047         self.groups = self.portal.portal_groups
00048         self.groupdata = self.portal.portal_groupdata
00049         self.groups.groupWorkspacesCreationFlag = 0
00050         self.groups.addGroup('foo')
00051         if 'auto_group' in self.acl_users.objectIds():
00052             self.acl_users.manage_delObjects(['auto_group'])
00053         # MUST reset _v_ attributes!
00054         self.memberdata._v_temps = None
00055         self.groupdata._v_temps = None
00056         self._trap_warning_output()
00057 
00058     def testGetGroup(self):
00059         g = self.groups.getGroupById('foo')
00060         self.assertEqual(g.__class__.__name__, 'GroupData')
00061         g = g.getGroup()
00062         self.assertEqual(g.__class__.__name__, 'PloneGroup')
00063 
00064     def testGetTool(self):
00065         g = self.groups.getGroupById('foo')
00066         self.assertEqual(g.getTool().getId(), 'portal_groupdata')
00067 
00068     def testGetGroupMembers(self):
00069         g = self.groups.getGroupById('foo')
00070         self.acl_users.userSetGroups(default_user, groupnames=['foo'])
00071         self.assertEqual(g.getGroupMembers()[0].getId(), default_user)
00072 
00073     def testGroupMembersAreWrapped(self):
00074         g = self.groups.getGroupById('foo')
00075         self.acl_users.userSetGroups(default_user, groupnames=['foo'])
00076         ms = g.getGroupMembers()
00077         self.assertEqual(ms[0].__class__.__name__, 'MemberData')
00078         self.assertEqual(ms[0].aq_parent.__class__.__name__, 'PloneUser')
00079         self.assertEqual(ms[0].aq_parent.aq_parent.__class__.__name__, 'PluggableAuthService')
00080 
00081     def testAddMember(self):
00082         self.setPermissions([Permissions.manage_users])
00083         g = self.groups.getGroupById('foo')
00084         g.addMember(default_user)
00085         self.assertEqual(g.getGroupMembers()[0].getId(), default_user)
00086 
00087     def testRemoveMember(self):
00088         self.setPermissions([Permissions.manage_users])
00089         g = self.groups.getGroupById('foo')
00090         g.addMember(default_user)
00091         g.removeMember(default_user)
00092         self.assertEqual(len(g.getGroupMembers()), 0)
00093 
00094     #def testSetProperties(self):
00095     #    # XXX: ERROR!
00096     #    g = self.groups.getGroupById('foo')
00097     #    g.setProperties(email='foo@bar.com')
00098     #    gd = self.groupdata._members['foo']
00099     #    self.assertEqual(gd.email, 'foo@bar.com')
00100 
00101     def testSetGroupProperties(self):
00102         g = self.groups.getGroupById('foo')
00103         g.setGroupProperties({'email': 'foo@bar.com'})
00104         gd = self.groups.getGroupById('foo')
00105         self.assertEqual(gd.getProperty('email'), 'foo@bar.com')
00106 
00107     def testSetMemberProperties(self):
00108         # For reference
00109         m = self.membership.getMemberById(default_user)
00110         m.setMemberProperties({'email': 'foo@bar.com'})
00111         md = self.membership.getMemberById(default_user)
00112         self.assertEqual(md.getProperty('email'), 'foo@bar.com')
00113 
00114     def testGetProperty(self):
00115         g = self.groups.getGroupById('foo')
00116         g.setGroupProperties({'email': 'foo@bar.com'})
00117         self.assertEqual(g.getProperty('email'), 'foo@bar.com')
00118         self.assertEqual(g.getProperty('id'), 'foo')
00119 
00120     def testGetGroupName(self):
00121         g = self.groups.getGroupById('foo')
00122         self.assertEqual(g.getGroupName(), 'foo')
00123 
00124     def testGetGroupId(self):
00125         g = self.groups.getGroupById('foo')
00126         # This changed in GRUF3
00127         #self.assertEqual(g.getGroupId(), 'foo')
00128         self.assertEqual(g.getGroupId(), 'foo')
00129 
00130     def testGetRoles(self):
00131         g = self.groups.getGroupById('foo')
00132         self.assertEqual(tuple(g.getRoles()), ('Authenticated',))
00133         self.groups.editGroup(g.getId(), roles=['Member'])
00134         g = self.groups.getGroupById('foo')
00135         self.assertEqual(sortTuple(tuple(g.getRoles())), ('Authenticated', 'Member'))
00136 
00137     def testGetRolesInContext(self):
00138         g = self.groups.getGroupById('foo')
00139         self.acl_users.userSetGroups(default_user, groupnames=['foo'])
00140         user = self.acl_users.getUser(default_user)
00141         self.assertEqual(user.getRolesInContext(self.folder).sort(),
00142                         ['Member', 'Authenticated', 'Owner'].sort())
00143         self.folder.manage_setLocalRoles(g.getId(), ['NewRole'])
00144         self.assertEqual(user.getRolesInContext(self.folder).sort(), 
00145                         ['Member', 'Authenticated', 'Owner', 'NewRole'].sort())
00146 
00147     def testGetDomains(self):
00148         g = self.groups.getGroupById('foo')
00149         self.assertEqual(g.getDomains(), ())
00150 
00151     def testHasRole(self):
00152         g = self.groups.getGroupById('foo')
00153         self.groups.editGroup(g.getId(), roles=['Member'])
00154         g = self.groups.getGroupById('foo')
00155         self.failUnless(g.has_role('Member'))
00156 
00157     def beforeTearDown(self):
00158         self._free_warning_output()
00159 
00160 
00161 class TestMethodProtection(PloneTestCase.PloneTestCase):
00162     # GroupData has wrong security declarations
00163 
00164     def afterSetUp(self):
00165         self.groups = self.portal.portal_groups
00166         self.groups.groupWorkspacesCreationFlag = 0
00167         self.groups.addGroup('foo')
00168         self.groupdata = self.groups.getGroupById('foo')
00169 
00170     def testAnonAddMember(self):
00171         self.logout()
00172         self.assertRaises(Unauthorized, self.groupdata.addMember, default_user)
00173 
00174     def testAnonRemoveMember(self):
00175         self.logout()
00176         self.assertRaises(Unauthorized, self.groupdata.removeMember, default_user)
00177 
00178     def testMemberAddMember(self):
00179         self.assertRaises(Unauthorized, self.groupdata.addMember, default_user)
00180 
00181     def testMemberRemoveMember(self):
00182         self.assertRaises(Unauthorized, self.groupdata.removeMember, default_user)
00183 
00184     def testManagerAddMember(self):
00185         self.setPermissions([Permissions.manage_users])
00186         self.groupdata.addMember(default_user)
00187 
00188     def testManagerRemoveMember(self):
00189         self.setPermissions([Permissions.manage_users])
00190         self.groupdata.addMember(default_user)
00191         self.groupdata.removeMember(default_user)
00192 
00193 
00194 def test_suite():
00195     from unittest import TestSuite, makeSuite
00196     suite = TestSuite()
00197     suite.addTest(makeSuite(TestGroupDataTool))
00198     suite.addTest(makeSuite(TestGroupData))
00199     suite.addTest(makeSuite(TestMethodProtection))
00200     return suite