Back to index

plone3  3.1.7
test_RecursiveGroupsPlugin.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 OFS.SimpleItem import SimpleItem
00018 from Products.PluggableAuthService.tests.conformance \
00019     import IGroupsPlugin_conformance
00020 
00021 from Products.PluggableAuthService.tests.test_PluggableAuthService \
00022     import FauxContainer
00023 
00024 class FauxPAS( FauxContainer ):
00025 
00026     def __init__( self ):
00027         self._id = 'acl_users'
00028         
00029     def _getGroupsForPrincipal( self, principal, request=None, plugins=None
00030                               , ignore_plugins=None ):
00031         all_groups = []
00032 
00033         if ignore_plugins is None:
00034             ignore_plugins = ()
00035 
00036         for plugin in self.objectValues():
00037             if plugin.getId() in ignore_plugins:
00038                 continue
00039             groups = plugin.getGroupsForPrincipal( principal, request )
00040             groups = [ '%s:%s' % ( plugin.getId(), x ) for x in groups ]
00041             all_groups.extend( groups )
00042 
00043         return all_groups
00044 
00045 class DummyPrincipal:
00046     
00047     def __init__( self, id, groups=() ):
00048         self._id = id
00049         self._groups = groups
00050         
00051     def getId( self ):
00052         return self._id
00053         
00054     def getGroups( self ):
00055         return self._groups
00056 
00057 class DummyGroupsManager( SimpleItem ):
00058 
00059     def __init__( self, id ):
00060         self._id = id
00061         self._groups = {}
00062         self._prin_to_group = {}
00063 
00064     def getId( self ):
00065         return self._id
00066     
00067     def addGroup( self, group_id ):
00068         self._groups[group_id] = 1
00069         
00070     def addPrincipalToGroup( self, prin, group_id ):
00071         prin_id = prin.getId()
00072         already_in = self._prin_to_group.get( prin_id, () )
00073         if group_id not in already_in:
00074             self._prin_to_group[prin_id] = already_in + ( group_id, )
00075 
00076     def getGroupsForPrincipal( self, prin, request=None ):
00077         prin_id = prin.getId()
00078         return self._prin_to_group.get( prin_id, () )
00079 
00080 class RecursiveGroupsPluginTests( unittest.TestCase
00081                                 , IGroupsPlugin_conformance
00082                                 ):
00083 
00084     def _getTargetClass( self ):
00085 
00086         from Products.PluggableAuthService.plugins.RecursiveGroupsPlugin \
00087             import RecursiveGroupsPlugin
00088 
00089         return RecursiveGroupsPlugin
00090 
00091     def _makeOne( self, id='test', *args, **kw ):
00092 
00093         return self._getTargetClass()( id=id, *args, **kw )
00094 
00095     def test_simple_flattening( self ):
00096         
00097         pas = FauxPAS()
00098         dgm = DummyGroupsManager( 'dummy' )
00099         rgp = self._makeOne( 'rgp' )
00100 
00101         pas._setObject( 'dgm', dgm )
00102         pas._setObject( 'rgp', rgp )
00103 
00104         dgm = pas._getOb( 'dgm' )
00105         dgm.addGroup( 'group1' )
00106         dgm.addGroup( 'group2' )
00107         
00108         rgp = pas._getOb( 'rgp' )
00109 
00110         user = DummyPrincipal( 'user1', ( 'dummy:group1', ) )
00111                 
00112         dgm.addPrincipalToGroup( user, 'group1' )
00113         dgm.addPrincipalToGroup( DummyPrincipal( 'dummy:group1' ), 'group2' )
00114         
00115         self.assertEqual( rgp.getGroupsForPrincipal( user )
00116                         , ( 'dummy:group1', 'dummy:group2' ) 
00117                         )
00118 
00119     def test_complex_flattening( self ):
00120         
00121         pas = FauxPAS()
00122         dgm = DummyGroupsManager( 'dummy' )
00123         odgm = DummyGroupsManager( 'other_dummy' )
00124         rgp = self._makeOne( 'rgp' )
00125 
00126         pas._setObject( 'dgm', dgm )
00127         pas._setObject( 'odgm', odgm )
00128         pas._setObject( 'rgp', rgp )
00129 
00130         dgm = pas._getOb( 'dgm' )
00131         dgm.addGroup( 'group1' )
00132         dgm.addGroup( 'group2' )
00133 
00134         odgm = pas._getOb( 'odgm' )
00135         odgm.addGroup( 'group3' )
00136         odgm.addGroup( 'group4' )
00137 
00138         rgp = pas._getOb( 'rgp' )
00139         
00140         user = DummyPrincipal( 'user1', ( 'dummy:group1'
00141                                         , 'other_dummy:group3' ) )
00142                 
00143         dgm.addPrincipalToGroup( user, 'group1' )
00144         dgm.addPrincipalToGroup( DummyPrincipal( 'dummy:group1' ), 'group2' )
00145 
00146         odgm.addPrincipalToGroup( user, 'group3' )
00147         odgm.addPrincipalToGroup( DummyPrincipal( 'dummy:group2' ), 'group4' )
00148         
00149         groups = rgp.getGroupsForPrincipal( user )
00150         self.assertEqual( len( groups ), 4 )
00151         self.failUnless( 'dummy:group1' in groups )
00152         self.failUnless( 'dummy:group2' in groups )
00153         self.failUnless( 'other_dummy:group3' in groups )
00154         self.failUnless( 'other_dummy:group4' in groups )
00155 
00156     def test_cross_nested_flattening( self ):
00157         
00158         pas = FauxPAS()
00159         dgm = DummyGroupsManager( 'dummy' )
00160         odgm = DummyGroupsManager( 'other_dummy' )
00161         rgp = self._makeOne( 'rgp' )
00162 
00163         pas._setObject( 'dgm', dgm )
00164         pas._setObject( 'odgm', odgm )
00165         pas._setObject( 'rgp', rgp )
00166 
00167         dgm = pas._getOb( 'dgm' )
00168         dgm.addGroup( 'group1' )
00169 
00170         odgm = pas._getOb( 'odgm' )
00171         odgm.addGroup( 'group2' )
00172 
00173         rgp = pas._getOb( 'rgp' )
00174         
00175         user = DummyPrincipal( 'user1', ( 'dummy:group1'
00176                                         , 'other_dummy:group2' ) )
00177                 
00178         dgm.addPrincipalToGroup( user, 'group1' )
00179         dgm.addPrincipalToGroup( DummyPrincipal( 'other_dummy:group1' )
00180                                , 'group1' )
00181 
00182         odgm.addPrincipalToGroup( user, 'group2' )
00183         odgm.addPrincipalToGroup( DummyPrincipal( 'dummy:group1' ), 'group2' )
00184         
00185         groups = rgp.getGroupsForPrincipal( user )
00186         self.assertEqual( len( groups ), 2 )
00187         self.failUnless( 'dummy:group1' in groups )
00188         self.failUnless( 'other_dummy:group2' in groups )
00189 
00190 if __name__ == "__main__":
00191     unittest.main()
00192 
00193 
00194 def test_suite():
00195     return unittest.TestSuite((
00196         unittest.makeSuite( RecursiveGroupsPluginTests ),
00197         ))