Back to index

plone3  3.1.7
testFolderLocalRoleForm.py
Go to the documentation of this file.
00001 #
00002 # Tests folder local roles
00003 #
00004 
00005 from Products.CMFPlone.tests import PloneTestCase
00006 from zExceptions import Forbidden
00007 
00008 def sortTuple(t):
00009     l = list(t)
00010     l.sort()
00011     return tuple(l)
00012 
00013 
00014 class TestFolderLocalRole(PloneTestCase.PloneTestCase):
00015 
00016     def afterSetUp(self):
00017         self.membership = self.portal.portal_membership
00018         self.membership.addMember('user2', 'secret', ['Member'], [])
00019         self.portal.acl_users.addRole('Foo')
00020         self.portal.acl_users.addRole('Bar')
00021         self.portal.acl_users.addRole('Baz')
00022         # Cannot assign a role I do not have myself...
00023         self.setRoles(['Member', 'Foo', 'Bar', 'Baz'])
00024         self.setupAuthenticator()
00025 
00026     def testFolderLocalRoleAddPostOnly(self):
00027         # Should try to use get on assing a local role
00028         self.setRequestMethod('GET')
00029         self.assertRaises(Forbidden, self.folder.folder_localrole_edit,'add',['user2'],'Foo')
00030 
00031     def testFolderLocalRoleDeletePostOnly(self):
00032         # Should try to use get on delete a local role
00033         self.setRequestMethod('POST')
00034         self.folder.folder_localrole_edit('add', ['user2'], 'Foo')
00035         self.setRequestMethod('GET')
00036         self.assertRaises(Forbidden, self.folder.folder_localrole_edit,'delete',['user2'])
00037 
00038     def testFolderLocalRoleDelete2PostOnly(self):
00039         # Should try to use get on delete a local role
00040         self.setRequestMethod('POST')
00041         self.folder.folder_localrole_edit('add', ['user2'], 'Foo')
00042         self.setRequestMethod('GET')
00043         self.assertRaises(Forbidden, self.folder.folder_localrole_delete,['user2'])
00044 
00045     def testFolderLocalRoleSetPostOnly(self):
00046         # Should try to use get on assing a local role
00047         self.setRequestMethod('GET')
00048         self.assertRaises(Forbidden, self.folder.folder_localrole_set)
00049 
00050     def testFolderLocalRoleAdd(self):
00051         # Should assing a local role
00052         self.setRequestMethod('POST')
00053         self.folder.folder_localrole_edit('add', ['user2'], 'Foo')
00054         self.setRequestMethod('GET')
00055         member = self.membership.getMemberById('user2')
00056         self.assertEqual(sortTuple(member.getRolesInContext(self.folder)),
00057                          ('Authenticated', 'Foo', 'Member'))
00058 
00059     def testFolderLocalRoleDelete(self):
00060         # Should delete a local role
00061         self.setRequestMethod('POST')
00062         self.folder.folder_localrole_edit('add', ['user2'], 'Foo')
00063         member = self.membership.getMemberById('user2')
00064         self.assertEqual(sortTuple(member.getRolesInContext(self.folder)),
00065                          ('Authenticated', 'Foo', 'Member'))
00066         self.folder.folder_localrole_edit('delete', ['user2'])
00067         self.setRequestMethod('GET')
00068         self.assertEqual(sortTuple(member.getRolesInContext(self.folder)),
00069                          ('Authenticated', 'Member'))
00070 
00071     def testFolderLocalRoleView(self):
00072         # sharing should render
00073         self.loginAsPortalOwner()
00074         view = self.folder.restrictedTraverse("sharing")
00075         view()
00076 
00077     def testDeleteSingleRole(self):
00078         # Try deleting a single role
00079         member = self.membership.getMemberById('user2')
00080         # add two roles
00081         self.setRequestMethod('POST')
00082         self.folder.folder_localrole_edit('add', ['user2'], 'Foo')
00083         self.folder.folder_localrole_edit('add', ['user2'], 'Bar')
00084 
00085         # remove the Bar role
00086         self.folder.folder_localrole_delete(member_role_ids=['user2((Bar))'])
00087         self.setRequestMethod('GET')
00088         self.assertEqual(sortTuple(member.getRolesInContext(self.folder)),
00089                          ('Authenticated', 'Foo', 'Member'))
00090 
00091     def testDeleteAllUserRoles(self):
00092         # Try deleting a all roles
00093         member = self.membership.getMemberById('user2')
00094         # add two roles
00095         self.setRequestMethod('POST')
00096         self.folder.folder_localrole_edit('add', ['user2'], 'Foo')
00097         self.folder.folder_localrole_edit('add', ['user2'], 'Bar')
00098 
00099         # remove a user
00100         self.folder.folder_localrole_delete(member_ids=['user2'])
00101         self.setRequestMethod('GET')
00102         self.assertEqual(sortTuple(member.getRolesInContext(self.folder)),
00103                          ('Authenticated', 'Member'))
00104 
00105     def testAddRoleForUser(self):
00106         # Try adding a new role for a user
00107         member = self.membership.getMemberById('user2')
00108         # add a role the old way
00109         self.setRequestMethod('POST')
00110         self.folder.folder_localrole_edit('add', ['user2'], 'Foo')
00111 
00112         # add another role the new way
00113         self.folder.folder_localrole_add(member_ids=['user2'], member_roles=['Bar', 'Baz'])
00114         self.setRequestMethod('GET')
00115         self.assertEqual(sortTuple(member.getRolesInContext(self.folder)),
00116                          ('Authenticated', 'Bar', 'Baz', 'Foo', 'Member'))
00117 
00118     def testIsLocalRoleAcquired(self):
00119         # Try setting the stop acquisition flag on a folder
00120         putils = self.portal.plone_utils
00121         putils.acquireLocalRoles(self.folder, status=0)
00122         self.assertEqual(putils.isLocalRoleAcquired(self.folder), 0)
00123 
00124     def testStopAcquireLocalRole(self):
00125         # See if a sub folder really didn't get the roles by acquisition
00126         member = self.membership.getMemberById('user2')
00127         self.setRequestMethod('POST')
00128         self.folder.folder_localrole_add(member_ids=['user2'], member_roles=['Bar'])
00129 
00130         # check if the role was assigned
00131         self.assertEqual(sortTuple(member.getRolesInContext(self.folder)),
00132                          ('Authenticated', 'Bar', 'Member'))
00133 
00134         self.folder.invokeFactory('Folder', id='A')
00135         putils = self.portal.plone_utils
00136         putils.acquireLocalRoles(self.folder.A, status=0)
00137         self.setRequestMethod('GET')
00138 
00139         # check if inheritance is blocked
00140         self.assertEqual(sortTuple(member.getRolesInContext(self.folder.A)),
00141                          ('Authenticated', 'Member'))
00142 
00143 
00144 def test_suite():
00145     from unittest import TestSuite, makeSuite
00146     suite = TestSuite()
00147     suite.addTest(makeSuite(TestFolderLocalRole))
00148     return suite