Back to index

plone3  3.1.7
test_PropertiedUser.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 Acquisition import Implicit
00018 from conformance import IBasicUser_conformance, \
00019                         IPropertiedUser_conformance
00020 
00021 class FauxMethod:
00022 
00023     def __init__( self, im_self, local_roles=() ):
00024 
00025         self.im_self = im_self
00026         self.__ac_local_roles__ = local_roles
00027 
00028 class FauxProtected( Implicit ):
00029 
00030     def __init__( self, local_roles=() ):
00031 
00032         self.__ac_local_roles__ = local_roles
00033 
00034 class PropertiedUserTests( unittest.TestCase
00035                            , IBasicUser_conformance
00036                            , IPropertiedUser_conformance
00037                            ):
00038 
00039     def _getTargetClass( self ):
00040 
00041         from Products.PluggableAuthService.PropertiedUser \
00042             import PropertiedUser
00043 
00044         return PropertiedUser
00045 
00046     def _makeOne( self, id='testing', login=None, *args, **kw ):
00047 
00048         return self._getTargetClass()( id, login, *args, **kw )
00049 
00050     def test_empty( self ):
00051 
00052         user = self._makeOne( 'empty' )
00053 
00054         # BaseUser interface
00055         self.assertEqual( user.getId(), 'empty' )
00056         self.assertEqual( user.getUserName(), 'empty' )
00057         self.assertRaises( NotImplementedError, user._getPassword )
00058         self.assertEqual( len( user.getRoles() ), 0 )
00059         self.assertEqual(len(user.getGroups()), 0)
00060         self.assertEqual( len( user.getDomains() ), 0 )
00061 
00062         # plus propertysheets
00063         self.assertEqual( len( user.listPropertysheets() ), 0 )
00064         self.assertRaises( KeyError, user.getPropertysheet, 'nonesuch'  )
00065 
00066     def test_groups(self):
00067         groups = ('Group A', 'Group B')
00068         user = self._makeOne('groups')
00069         user._addGroups(groups)
00070         self.assertEqual(len(user.getGroups()), len(groups))
00071         for g in user.getGroups():
00072             self.assert_(g in groups)
00073 
00074     def test_username(self):
00075         user = self._makeOne('username', 'User with Username')
00076         self.assertEqual(user.getUserName(), 'User with Username')
00077 
00078     def test_roles(self):
00079         roles = ['Manager', 'Members']
00080         user = self._makeOne('user')
00081         user._addRoles(roles)
00082         self.assertEqual(len(user.getRoles()), 2)
00083         for r in user.getRoles():
00084             self.assert_(r in roles)
00085 
00086     def test_addPropertysheet( self ):
00087 
00088         user = self._makeOne()
00089 
00090         user.addPropertysheet( 'one', { 'a' : 0, 'b' : 'jabber' } )
00091 
00092         ids = user.listPropertysheets()
00093 
00094         self.assertEqual( len( ids ), 1 )
00095         self.assertEqual( ids[ 0 ], 'one' )
00096 
00097         sheet = user.getPropertysheet( 'one' )
00098 
00099         self.assertEqual( len( sheet.propertyMap() ), 2 )
00100         self.assertEqual( sheet.getPropertyType( 'a' ), 'int' )
00101         self.assertEqual( sheet.getPropertyType( 'b' ), 'string' )
00102         self.assertEqual( sheet.getId(), 'one' )
00103 
00104         sheet = user[ 'one' ]
00105 
00106         self.assertEqual( len( sheet.propertyMap() ), 2 )
00107         self.assertEqual( sheet.getPropertyType( 'a' ), 'int' )
00108         self.assertEqual( sheet.getPropertyType( 'b' ), 'string' )
00109 
00110         self.assertRaises( KeyError, user.getPropertysheet, 'another'  )
00111 
00112     def test_getRolesInContext_no_local( self ):
00113 
00114         groups = ( 'Group A', 'Group B' )
00115         user = self._makeOne()
00116         user._addGroups( groups )
00117 
00118         self.assertEqual( len( user.getRoles() ), 0 )
00119         self.assertEqual( len( user.getGroups() ), len( groups ) )
00120 
00121         faux = FauxProtected()
00122 
00123         local_roles = user.getRolesInContext( faux )
00124         self.assertEqual( len( local_roles ), 0 )
00125 
00126     def test_getRolesInContext_group_match( self ):
00127 
00128         groups = ( 'Group A', 'Group B' )
00129         user = self._makeOne()
00130         user._addGroups( groups )
00131 
00132         faux = FauxProtected( { 'Group A' : ( 'Manager', ) } )
00133 
00134         local_roles = user.getRolesInContext( faux )
00135         self.assertEqual( len( local_roles ), 1 )
00136         self.failUnless( 'Manager' in local_roles )
00137 
00138     def test_getRolesInContext_group_overlap( self ):
00139 
00140         groups = ( 'Group A', 'Group B' )
00141         user = self._makeOne()
00142         user._addGroups( groups )
00143 
00144         faux = FauxProtected( { 'Group A' : ( 'Manager', )
00145                               , 'Group B' : ( 'Manager', 'Owner' )
00146                               } )
00147 
00148         local_roles = user.getRolesInContext( faux )
00149         self.assertEqual( len( local_roles ), 2 )
00150         self.failUnless( 'Manager' in local_roles )
00151         self.failUnless( 'Owner' in local_roles )
00152 
00153     def test_getRolesInContext_group_nomatch( self ):
00154 
00155         groups = ( 'Group A', 'Group B' )
00156         user = self._makeOne()
00157         user._addGroups( groups )
00158 
00159         faux = FauxProtected( { 'Group C' : ( 'Manager', ) } )
00160 
00161         local_roles = user.getRolesInContext( faux )
00162         self.assertEqual( len( local_roles ), 0 )
00163 
00164     def test_getRolesInContext_acquisition( self ):
00165 
00166         groups = ( 'Group A', 'Group B' )
00167         user = self._makeOne()
00168         user._addGroups( groups )
00169 
00170         faux_container = FauxProtected( { 'Group A' : ( 'Manager', ) } )
00171         faux_contained = FauxProtected( { 'Group C' : ( 'Manager', 'Owner' ) }
00172                                       ).__of__( faux_container )
00173 
00174         local_roles = user.getRolesInContext( faux_contained )
00175         self.assertEqual( len( local_roles ), 1 )
00176         self.failUnless( 'Manager' in local_roles )
00177 
00178     def test_getRolesInContext_weslayan( self ):
00179 
00180         # Test "methodish" checks.
00181 
00182         groups = ( 'Group A', 'Group B' )
00183         user = self._makeOne()
00184         user._addGroups( groups )
00185 
00186         faux_self = FauxProtected( { 'Group A' : ( 'Manager', ) } )
00187         faux_method = FauxMethod( faux_self
00188                                 , { 'Group C' : ( 'Manager', 'Owner' ) } )
00189 
00190         local_roles = user.getRolesInContext( faux_method )
00191         self.assertEqual( len( local_roles ), 1 )
00192         self.failUnless( 'Manager' in local_roles )
00193 
00194     def test_allowed_not_even_god_should( self ):
00195 
00196         from AccessControl.PermissionRole import _what_not_even_god_should_do
00197         user = self._makeOne()
00198 
00199         self.failIf( user.allowed( None, _what_not_even_god_should_do ) )
00200 
00201     def test_allowed_anonymous( self ):
00202 
00203         user = self._makeOne()
00204 
00205         self.failUnless( user.allowed( None, ('Anonymous',) ) )
00206 
00207     def test_allowed_authenticated( self ):
00208 
00209         user = self._makeOne()
00210 
00211         self.failUnless( user.allowed( None, ('Authenticated',) ) )
00212 
00213     def test_allowed_authenticated_required_but_anonymous( self ):
00214 
00215         user = self._makeOne('Anonymous User')
00216 
00217         self.failIf( user.allowed( None, ('Authenticated',) ) )
00218 
00219     def test_allowed_global_roles_ok( self ):
00220 
00221         user = self._makeOne()
00222         user._addRoles( ( 'Role 1', 'Role 2' ) )
00223 
00224         self.failUnless( user.allowed( None, ( 'Role 1', ) ) )
00225 
00226     def test_allowed_global_roles_not_ok( self ):
00227 
00228         user = self._makeOne()
00229         user._addRoles( ( 'Role 1', 'Role 2' ) )
00230 
00231         self.failIf( user.allowed( None, ( 'Role 3', ) ) )
00232 
00233     def test_allowed_local_roles_on_user_ok( self ):
00234 
00235         user = self._makeOne( 'user' )
00236         object = FauxProtected( { 'user' : ( 'Role 1', ) } )
00237 
00238         self.failUnless( user.allowed( object, ( 'Role 1', ) ) )
00239 
00240     def test_allowed_local_roles_on_user_not_ok( self ):
00241 
00242         user = self._makeOne( 'user' )
00243         object = FauxProtected( { 'user' : ( 'Role 1', ) } )
00244 
00245         self.failIf( user.allowed( object, ( 'Role 2', ) ) )
00246 
00247     def test_allowed_local_roles_on_group_ok( self ):
00248 
00249         user = self._makeOne( 'user' )
00250         user._addGroups( ( 'Group 1', 'Group 2' ) )
00251         object = FauxProtected( { 'Group 1' : ( 'Role 1', ) } )
00252 
00253         self.failUnless( user.allowed( object, ( 'Role 1', ) ) )
00254 
00255     def test_allowed_acquisition( self ):
00256 
00257         groups = ( 'Group A', 'Group B' )
00258         user = self._makeOne()
00259         user._addGroups( groups )
00260 
00261         faux_container = FauxProtected( { 'Group A' : ( 'Manager', ) } )
00262         faux_contained = FauxProtected( { 'Group C' : ( 'Manager', 'Owner' ) }
00263                                       ).__of__( faux_container )
00264 
00265         self.failUnless( user.allowed( faux_contained, ( 'Manager', ) ) )
00266 
00267     def test_allowed_weslayan( self ):
00268 
00269         # Test "methodish" checks.
00270 
00271         groups = ( 'Group A', 'Group B' )
00272         user = self._makeOne()
00273         user._addGroups( groups )
00274 
00275         faux_self = FauxProtected( { 'Group A' : ( 'Manager', ) } )
00276         faux_method = FauxMethod( faux_self
00277                                 , { 'Group C' : ( 'Manager', 'Owner' ) } )
00278 
00279         self.failUnless( user.allowed( faux_method, ( 'Manager', ) ) )
00280 
00281 
00282 if __name__ == "__main__":
00283     unittest.main()
00284 
00285 def test_suite():
00286     return unittest.TestSuite((
00287         unittest.makeSuite( PropertiedUserTests ),
00288         ))
00289