Back to index

plone3  3.1.7
test_ZODBGroupManager.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights
00004 # Reserved.
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 import unittest
00016 
00017 from Products.PluggableAuthService.tests.conformance \
00018     import IGroupEnumerationPlugin_conformance
00019 from Products.PluggableAuthService.tests.conformance \
00020     import IGroupsPlugin_conformance
00021 
00022 from Products.PluggableAuthService.plugins.tests.helpers \
00023      import FauxPAS, FauxSmartPAS, DummyUser, makeRequestAndResponse
00024 
00025 class DummyGroup:
00026 
00027     def __init__( self, id ):
00028         self._id = id
00029 
00030     def getId( self ):
00031         return self._id
00032 
00033 class ZODBGroupManagerTests( unittest.TestCase
00034                            , IGroupEnumerationPlugin_conformance
00035                            , IGroupsPlugin_conformance
00036                            ):
00037 
00038     def _getTargetClass( self ):
00039 
00040         from Products.PluggableAuthService.plugins.ZODBGroupManager \
00041             import ZODBGroupManager
00042 
00043         return ZODBGroupManager
00044 
00045     def _makeOne( self, id='test', *args, **kw ):
00046 
00047         return self._getTargetClass()( id=id, *args, **kw )
00048 
00049     def test_empty( self ):
00050 
00051         zgm = self._makeOne()
00052 
00053         self.assertEqual( len( zgm.listGroupIds() ), 0 )
00054         self.assertEqual( len( zgm.enumerateGroups() ), 0 )
00055 
00056         user = DummyUser( 'userid' )
00057         groups = zgm.getGroupsForPrincipal( user )
00058         self.assertEqual( len( groups ), 0 )
00059 
00060     def test_addGroup( self ):
00061 
00062         from Products.PluggableAuthService.tests.test_PluggableAuthService \
00063             import FauxRoot
00064 
00065         root = FauxRoot()
00066         zgm = self._makeOne().__of__( root )
00067 
00068         zgm.addGroup( 'group' )
00069 
00070         group_ids = zgm.listGroupIds()
00071         self.assertEqual( len( group_ids ), 1 )
00072         self.assertEqual( group_ids[0], 'group' )
00073 
00074         info_list = zgm.enumerateGroups()
00075         self.assertEqual( len( info_list ), 1 )
00076         info = info_list[ 0 ]
00077         self.assertEqual( info[ 'id' ], 'group' )
00078 
00079     def test_addGroup_exists( self ):
00080 
00081         zgm = self._makeOne()
00082 
00083         zgm.addGroup( 'group' )
00084         self.assertRaises( KeyError, zgm.addGroup, 'group' )
00085 
00086     def test_updateGroup_normal( self ):
00087 
00088         zgm = self._makeOne()
00089 
00090         zgm.addGroup( 'group', 'group_title', 'group_desc' )
00091 
00092         group_info = zgm.getGroupInfo( 'group' )
00093         self.assertEqual( group_info['title'], 'group_title' )
00094         zgm.updateGroup( 'group', 'group_title_changed', 'group_desc' )
00095         group_info = zgm.getGroupInfo( 'group' )
00096         self.assertEqual( group_info['title'], 'group_title_changed' )
00097 
00098     def test_addPrincipalToGroup( self ):
00099 
00100         zgm = self._makeOne()
00101 
00102         zgm.addGroup( 'group' )
00103 
00104         user = DummyUser( 'userid' )
00105 
00106         zgm.addPrincipalToGroup( user.getId(), 'group' )
00107         groups = zgm.getGroupsForPrincipal( user )
00108         self.assertEqual( groups, ( 'group', ) )
00109 
00110     def test_addPrincipalToGroupThenRemovePrincipal( self ):
00111 
00112         root = FauxSmartPAS()
00113         root.user_ids['foo'] = 'foo'
00114 
00115         zgm = self._makeOne( id='groups' ).__of__( root )
00116 
00117         zgm.addGroup( 'group', 'group_title', 'group_desc' )
00118         self.assertEqual( len( zgm.listAssignedPrincipals( 'group' ) ), 0 )
00119 
00120         new = zgm.addPrincipalToGroup( 'foo', 'group' )
00121 
00122         self.failUnless( new )
00123 
00124         assigned = [x[1] for x in zgm.listAssignedPrincipals( 'group' )]
00125 
00126         self.assertEqual( len( assigned ), 1 )
00127         self.assertEqual( assigned[0], 'foo' )
00128 
00129         del root.user_ids['foo']
00130 
00131         assigned = [x[1] for x in zgm.listAssignedPrincipals( 'group' )]
00132 
00133         self.assertEqual( len( assigned ), 1 )
00134         self.assertEqual( assigned[0], '<foo: not found>' )
00135 
00136     def test_removePrincipalFromGroup( self ):
00137 
00138         zgm = self._makeOne()
00139 
00140         zgm.addGroup( 'group' )
00141 
00142         user = DummyUser( 'userid' )
00143         
00144         zgm.addPrincipalToGroup( user.getId(), 'group' )
00145         zgm.removePrincipalFromGroup( user.getId(), 'group' )
00146         groups = zgm.getGroupsForPrincipal( user )
00147         self.assertEqual( groups, () )
00148 
00149     def test_removeGroupOutFromUnderPrincipal( self ):
00150 
00151         zgm = self._makeOne()
00152 
00153         zgm.addGroup( 'group' )
00154 
00155         user = DummyUser( 'userid' )
00156         
00157         zgm.addPrincipalToGroup( user.getId(), 'group' )
00158         zgm.removeGroup( 'group' )
00159         group_ids = zgm.listGroupIds()
00160         self.assertEqual( len( group_ids ), 0 )
00161         groups = zgm.getGroupsForPrincipal( user )
00162         self.assertEqual( groups, () )
00163 
00164     def test_multiplePrincipalsPerGroup( self ):
00165 
00166         pas = FauxPAS()
00167         zgm = self._makeOne().__of__( pas )
00168         
00169         zgm.addGroup( 'group1' )
00170         zgm.addGroup( 'group2' )
00171 
00172         user1 = DummyUser( 'userid1' )
00173         user2 = DummyUser( 'userid2' )
00174         
00175         zgm.addPrincipalToGroup( user1.getId(), 'group1' )
00176         zgm.addPrincipalToGroup( user1.getId(), 'group2' )
00177         zgm.addPrincipalToGroup( user2.getId(), 'group2' )
00178 
00179         group_ids = zgm.listGroupIds()
00180         self.assertEqual( len( group_ids ), 2 )
00181         principals = zgm.listAssignedPrincipals( 'group2' )
00182         self.assertEqual( principals, [ ( 'userid1', 'userid1' ),
00183                                         ( 'userid2', 'userid2' ) ] )
00184 
00185     def test_enumerateGroups_exact_nonesuch( self ):
00186 
00187         from Products.PluggableAuthService.tests.test_PluggableAuthService \
00188             import FauxRoot
00189 
00190         root = FauxRoot()
00191         zgm = self._makeOne( id='exact_nonesuch' ).__of__( root )
00192 
00193         ID_LIST = ( 'foo', 'bar', 'baz', 'bam' )
00194 
00195         for id in ID_LIST:
00196 
00197             zgm.addGroup( id, 'Group %s' % id, 'This is group, %s' % id )
00198 
00199         self.assertEqual( zgm.enumerateGroups( id='qux', exact_match=True )
00200                         , () )
00201 
00202     def test_enumerateGroups_multiple( self ):
00203 
00204         from Products.PluggableAuthService.tests.test_PluggableAuthService \
00205             import FauxRoot
00206 
00207         root = FauxRoot()
00208         zrm = self._makeOne( id='partial' ).__of__( root )
00209 
00210         ID_LIST = ( 'foo', 'bar', 'baz', 'bam' )
00211 
00212         for id in ID_LIST:
00213 
00214             zrm.addGroup( id, 'Group %s' % id, 'This is group, %s' % id )
00215 
00216         info_list = zrm.enumerateGroups( id=ID_LIST, exact_match=False )
00217 
00218         self.assertEqual( len( info_list ), len( ID_LIST ) )
00219 
00220         for info in info_list:
00221             self.failUnless( info[ 'id' ] in ID_LIST )
00222 
00223         SUBSET = ID_LIST[:3]
00224 
00225         info_list = zrm.enumerateGroups( id=SUBSET, exact_match=False )
00226 
00227         self.assertEqual( len( info_list ), len( SUBSET ) )
00228 
00229         for info in info_list:
00230             self.failUnless( info[ 'id' ] in SUBSET )
00231 
00232     def test_enumerateGroups_prefixed( self ):
00233 
00234         from Products.PluggableAuthService.tests.test_PluggableAuthService \
00235             import FauxRoot
00236 
00237         root = FauxRoot()
00238         zrm = self._makeOne( id='prefixed' ).__of__( root )
00239         zrm.prefix = 'prefixed_'
00240 
00241         ID_LIST = ( 'foo', 'bar', 'baz', 'bam' )
00242         PRE_LIST = tuple(['prefixed_%s' % x for x in ID_LIST])
00243 
00244         for id in ID_LIST:
00245 
00246             zrm.addGroup( id, 'Group %s' % id, 'This is group, %s' % id )
00247 
00248         info_list = zrm.enumerateGroups()
00249 
00250         self.assertEqual( len( info_list ), len( ID_LIST ) )
00251 
00252         for info in info_list:
00253             self.failUnless( info[ 'id' ] in PRE_LIST )
00254 
00255     def test_addPrincipalToGroup( self ):
00256 
00257         zgm = self._makeOne()
00258         zgm.prefix = 'prefixed_'
00259 
00260         zgm.addGroup( 'group' )
00261 
00262         user = DummyUser( 'userid' )
00263 
00264         zgm.addPrincipalToGroup( user.getId(), 'group' )
00265         groups = zgm.getGroupsForPrincipal( user )
00266         self.assertEqual( groups, ( 'prefixed_group', ) )
00267 
00268     def testPOSTProtections(self):
00269         from zExceptions import Forbidden
00270 
00271         USER_ID = 'testuser'
00272         GROUP_ID = 'testgroup'
00273 
00274         zgm = self._makeOne()
00275         zgm.prefix = 'prefixed_'
00276 
00277         zgm.addGroup( GROUP_ID )
00278         user = DummyUser( USER_ID )
00279 
00280         req, res = makeRequestAndResponse()
00281 
00282         # test addPrincipalToGroup
00283         # Fails with a GET
00284         req.set('REQUEST_METHOD', 'GET')
00285         self.assertRaises(Forbidden, zgm.addPrincipalToGroup,
00286                           USER_ID, GROUP_ID, REQUEST=req)
00287         # Works with a POST
00288         req.set('REQUEST_METHOD', 'POST')
00289         zgm.addPrincipalToGroup(USER_ID, GROUP_ID, REQUEST=req)
00290 
00291         # test removePrincipalFromGroup
00292         req.set('REQUEST_METHOD', 'GET')
00293         self.assertRaises(Forbidden, zgm.removePrincipalFromGroup,
00294                           USER_ID, GROUP_ID, REQUEST=req)
00295         # Works with a POST
00296         req.set('REQUEST_METHOD', 'POST')
00297         zgm.removePrincipalFromGroup(USER_ID, GROUP_ID, REQUEST=req)
00298 
00299         # test manage_addPrincipalsToGroup
00300         req.set('REQUEST_METHOD', 'GET')
00301         self.assertRaises(Forbidden, zgm.manage_addPrincipalsToGroup,
00302                           GROUP_ID, [USER_ID], REQUEST=req)
00303         req.set('REQUEST_METHOD', 'POST')
00304         zgm.manage_addPrincipalsToGroup(GROUP_ID, [USER_ID], REQUEST=req)
00305 
00306         # test manage_removePrincipalsFromGroup
00307         req.set('REQUEST_METHOD', 'GET')
00308         self.assertRaises(Forbidden, zgm.manage_removePrincipalsFromGroup,
00309                           GROUP_ID, [USER_ID], REQUEST=req)
00310         req.set('REQUEST_METHOD', 'POST')
00311         zgm.manage_removePrincipalsFromGroup(GROUP_ID, [USER_ID], REQUEST=req)
00312 
00313         # test manage_removeGroup
00314         req.set('REQUEST_METHOD', 'GET')
00315         self.assertRaises(Forbidden, zgm.manage_removeGroups,
00316                           [GROUP_ID], REQUEST=req)
00317         # Works with a POST
00318         req.set('REQUEST_METHOD', 'POST')
00319         zgm.manage_removeGroups([GROUP_ID], REQUEST=req)
00320 
00321 if __name__ == "__main__":
00322     unittest.main()
00323 
00324 def test_suite():
00325     return unittest.TestSuite((
00326         unittest.makeSuite( ZODBGroupManagerTests ),
00327         ))