Back to index

plone3  3.1.7
test_groups_tool.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # PlonePAS - Adapt PluggableAuthService for use in Plone
00004 # Copyright (C) 2005 Enfold Systems, Kapil Thangavelu, et al
00005 #
00006 # This software is subject to the provisions of the Zope Public License,
00007 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this
00008 # distribution.
00009 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00010 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00011 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00012 # FOR A PARTICULAR PURPOSE.
00013 #
00014 ##############################################################################
00015 """
00016 """
00017 
00018 import unittest
00019 
00020 from PlonePASTestCase import PlonePASTestCase
00021 
00022 from cStringIO import StringIO
00023 from zExceptions import BadRequest
00024 from Acquisition import aq_base, aq_inner, aq_parent
00025 from AccessControl import Permissions
00026 from AccessControl import Unauthorized
00027 from Products.CMFCore.utils import getToolByName
00028 from Products.PlonePAS.tools.groupdata import GroupData
00029 from Products.PlonePAS.plugins.group import PloneGroup
00030 from Testing.ZopeTestCase import user_name
00031 
00032 class GroupsToolTest(PlonePASTestCase):
00033 
00034     def afterSetUp(self):
00035         self.gt = gt = getToolByName(self.portal, 'portal_groups')
00036         self.gd = gd = getToolByName(self.portal, 'portal_groupdata')
00037 
00038         self.group_id = 'group1'
00039         # Create a new Group
00040         self.loginAsPortalOwner()
00041         gt.addGroup(self.group_id, ['Reviewer'], [],
00042                     {'email': 'group1@host.com',
00043                      'title': 'Group #1'})
00044 
00045     def test_get_group(self):
00046         # Use PAS (monkeypatched) API method to get a group by id.
00047         group = self.portal.acl_users.getGroup(self.group_id)
00048         self.failIf(group is None)
00049 
00050         # Should be wrapped into the GroupManagement, which is wrapped
00051         # into the PAS.
00052         got = aq_base(aq_parent(aq_parent(group)))
00053         expected = aq_base(self.portal.acl_users)
00054         self.assertEquals(got, expected)
00055 
00056         self.failUnless(isinstance(group, PloneGroup))
00057 
00058     def test_get_group_by_id(self):
00059         # Use tool way of getting group by id. This returns a
00060         # GroupData object wrapped by the group
00061         group = self.gt.getGroupById(self.group_id)
00062         self.failIf(group is None)
00063         self.failUnless(isinstance(group, GroupData))
00064         self.failUnless(isinstance(aq_parent(group), PloneGroup))
00065 
00066     def test_edit_group(self):
00067         # Use the tool way to edit a group.
00068         properties = {
00069             'email': 'group1@host2.com',
00070             'title': 'Group #1 new title'
00071         }
00072         self.gt.editGroup(self.group_id, roles=['Manager'],
00073             **properties)
00074 
00075         # test edition of roles and properties
00076         group = self.gt.getGroupById(self.group_id)
00077         self.failUnless(group.has_role('Manager'))
00078         self.assertEqual(group.getProperty('email'), properties['email'])
00079         self.assertEqual(group.getProperty('title'), properties['title'])
00080 
00081         # test for empty list of roles
00082         self.gt.editGroup(self.group_id, roles=[])
00083         self.failUnless(group.has_role('Authenticated'))
00084 
00085 class GroupWorkspacesTest(PlonePASTestCase):
00086 
00087     def afterSetUp(self):
00088         self.gt = gt = getToolByName(self.portal, 'portal_groups')
00089         self.gd = gd = getToolByName(self.portal, 'portal_groupdata')
00090         # Enable group-area creation
00091         self.gt.groupWorkspacesCreationFlag = 1
00092         # Those are all valid chars in Zope.
00093         self.gid = "Group #1 - Houston, TX. ($100)"
00094         self.loginAsPortalOwner()
00095 
00096     def test_funky_group_ids_1(self):
00097         gid = self.gid
00098         ginfo = (gid, ['Reviewer'], [],
00099                  {'email': 'group1@host.com',
00100                   'title': 'Group #1'})
00101         # Create a new Group
00102         self.gt.addGroup(*ginfo)
00103 
00104     def test_funky_group_ids_2(self):
00105         # Forward-slash is not allowed
00106         gid = self.gid + '/'
00107         ginfo = (gid, ['Reviewer'], [],
00108                  {'email': 'group1@host.com',
00109                   'title': 'Group #1'})
00110         # Create a new Group
00111         self.failUnlessRaises(BadRequest, self.gt.addGroup, *ginfo)
00112 
00113 class TestMethodProtection(PlonePASTestCase):
00114     # GroupData has wrong security declarations
00115 
00116     def afterSetUp(self):
00117         self.groups = self.portal.portal_groups
00118         self.groups.groupWorkspacesCreationFlag = 0
00119         self.groups.addGroup('foo')
00120         self.groupdata = self.groups.getGroupById('foo')
00121 
00122     def testAnonAddMember(self):
00123         self.logout()
00124         self.assertRaises(Unauthorized, self.groupdata.addMember, user_name)
00125 
00126     def testAnonRemoveMember(self):
00127         self.logout()
00128         self.assertRaises(Unauthorized, self.groupdata.removeMember, user_name)
00129 
00130     def testMemberAddMember(self):
00131         self.assertRaises(Unauthorized, self.groupdata.addMember, user_name)
00132 
00133     def testMemberRemoveMember(self):
00134         self.assertRaises(Unauthorized, self.groupdata.removeMember, user_name)
00135 
00136     def testManagerAddMember(self):
00137         self.setPermissions([Permissions.manage_users])
00138         self.groupdata.addMember(user_name)
00139 
00140     def testManagerRemoveMember(self):
00141         self.setPermissions([Permissions.manage_users])
00142         self.groupdata.addMember(user_name)
00143         self.groupdata.removeMember(user_name)
00144 
00145 
00146 
00147 def test_suite():
00148     suite = unittest.TestSuite()
00149     suite.addTest(unittest.makeSuite(GroupsToolTest))
00150     suite.addTest(unittest.makeSuite(GroupWorkspacesTest))
00151     suite.addTest(unittest.makeSuite(TestMethodProtection))
00152     return suite
00153