Back to index

plone3  3.1.7
test_LocalRolePlugin.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, aq_base, aq_parent
00018 from OFS.ObjectManager import ObjectManager
00019 
00020 from Products.PluggableAuthService.tests.conformance \
00021     import IRolesPlugin_conformance
00022 
00023 class FauxObject( Implicit ):
00024 
00025     def __init__( self, id=None ):
00026 
00027         self._id = id
00028 
00029     def getId( self ):
00030 
00031         return self._id
00032 
00033     def __repr__( self ):
00034 
00035         return '<FauxObject: %s>' % self._id
00036 
00037     def publishable( self, *args, **kw ):
00038 
00039         return 'Args: %s\nKeywords: %s' % ( args, kw )
00040 
00041 class FauxContainer( FauxObject, ObjectManager ):
00042 
00043     pass
00044 
00045 class FauxRoot( FauxContainer ):
00046 
00047     isTopLevelPrincipiaApplicationObject = 1
00048     
00049     def getPhysicalRoot( self ):
00050         return self
00051 
00052     def getPhysicalPath( self ):
00053         return ()
00054 
00055 class FauxUser( Implicit ):
00056 
00057     def __init__( self, id, name=None, roles={} ):
00058 
00059         self._id = id
00060         self._name = name
00061         self._roles = roles
00062 
00063     def getId( self ):
00064 
00065         return self._id
00066 
00067     def getUserName( self ):
00068 
00069         return self._name
00070 
00071     def getRoles( self ):
00072 
00073         return self._roles.keys()
00074 
00075     def _addRoles(self, roles):
00076         for role in roles:
00077             self._roles[role] = 1
00078 
00079     def __repr__( self ):
00080 
00081         return '<FauxUser: %s>' % self._id
00082 
00083 class LocalRolePluginTestCase( unittest.TestCase
00084                              , IRolesPlugin_conformance
00085                              ):
00086 
00087     def _getTargetClass( self ):
00088 
00089         from Products.PluggableAuthService.plugins.LocalRolePlugin \
00090              import LocalRolePlugin
00091 
00092         return LocalRolePlugin
00093 
00094     def _makeOne( self, id='test', *args, **kw ):
00095 
00096         return self._getTargetClass()( id, *args, **kw )
00097 
00098     def _makeTree( self ):
00099 
00100         rc = FauxObject( 'rc' )
00101         root = FauxRoot( 'root' ).__of__( rc )
00102         folder = FauxContainer( 'folder' ).__of__( root )
00103         object = FauxObject( 'object' ).__of__( folder )
00104 
00105         return rc, root, folder, object
00106 
00107     def test_no_local_roles( self ):
00108 
00109         rc, root, folder, object = self._makeTree()
00110         
00111         lrp = self._makeOne( 'no_roles' ).__of__(root)
00112 
00113         user = FauxUser( 'loser' ).__of__(root)
00114 
00115         self.assertEquals( lrp.getRolesForPrincipal( user ), None )
00116 
00117     def test_some_local_roles( self ):
00118 
00119         rc, root, folder, object = self._makeTree()
00120 
00121         root.__ac_local_roles__ = { 'some_manager' : [ 'Manager' ] }
00122         
00123         lrp = self._makeOne( 'roles' ).__of__(root)
00124 
00125         user = FauxUser( 'some_manager' ).__of__(root)
00126 
00127         self.assertEquals( lrp.getRolesForPrincipal( user ), [ 'Manager' ] )
00128 
00129 if __name__ == "__main__":
00130     unittest.main()
00131 
00132     
00133 def test_suite():
00134     return unittest.TestSuite((
00135         unittest.makeSuite( LocalRolePluginTestCase ),
00136         ))