[Zope-Checkins] CVS: Zope/lib/python/OFS/tests - testObjectManager.py:1.3.2.1

Zachery Bir zbir at urbanape.com
Wed Mar 17 17:46:27 EST 2004


Update of /cvs-repository/Zope/lib/python/OFS/tests
In directory cvs.zope.org:/tmp/cvs-serv11338/tests

Added Files:
      Tag: Zope-2_7-branch
	testObjectManager.py 
Log Message:
Test module for ObjectManager, testing only the changes to the
_setObject method.

Specifically the key for 'Owner' local role should be the user's Id,
not UserName

Tests this behavior with six combinations:

  'None' security manager attempting to set ownership
  'Emergency User' security manager attempting to set ownership
  'System User' security manager attempting to set ownership
  'Anonymous User' security manager attempting to set ownership
  'User' security manager attempting to set ownership
  'Faux User' security manager attempting to set ownership

  'None' security manager not attempting to set ownership
  'Emergency User' security manager not attempting to set ownership
  'System User' security manager not attempting to set ownership
  'Anonymous User' security manager not attempting to set ownership
  'User' security manager not attempting to set ownership
  'Faux User' security manager not attempting to set ownership



=== Added File Zope/lib/python/OFS/tests/testObjectManager.py ===
import unittest

from Acquisition import Implicit, aq_base, aq_parent
from AccessControl.SecurityManagement import newSecurityManager
from AccessControl.SecurityManagement import noSecurityManager
from AccessControl.User import User
from AccessControl.SpecialUsers import emergency_user, nobody, system
from AccessControl.Owned import EmergencyUserCannotOwn, Owned
from OFS.ObjectManager import ObjectManager
from OFS.SimpleItem import SimpleItem

class FauxRoot( Implicit ):

    id = '/'

    def getPhysicalRoot( self ):
        return self

    def getPhysicalPath( self ):
        return ()

class FauxUser( Implicit ):

    def __init__( self, id, login ):

        self._id = id
        self._login = login
        self._roles = {}
        self._groups = {}

    def getId( self ):

        return self._id

    def getUserName( self ):

        return self._login

    def getRoles( self ):

        return self._roles

    def getGroups( self ):

        return self._groups

    def allowed( self, value, roles ):

        return 1

    def _addRoles( self, roles ):

        for role in roles:
            self._roles[role] = 1

    def _addGroups(self, groups):
        
        for group in groups:
            self._groups[group] = 1

    def __repr__( self ):

        return '<FauxUser: %s>' % self._id


class ObjectManagerTests( unittest.TestCase ):

    def tearDown( self ):

        noSecurityManager()

    def _getTargetClass( self ):

        from OFS.ObjectManager import ObjectManager

        return ObjectManager

    def _makeOne( self, *args, **kw ):

        return self._getTargetClass()( *args, **kw ).__of__( FauxRoot() )

    def test_setObject_set_owner_with_no_user( self ):

        om = self._makeOne()

        newSecurityManager( None, None )

        si = SimpleItem( 'no_user' )

        om._setObject( 'no_user', si )

        self.assertEqual( si.__ac_local_roles__, None )

    def test_setObject_set_owner_with_emergency_user( self ):

        om = self._makeOne()

        newSecurityManager( None, emergency_user )

        si = SimpleItem( 'should_fail' )

        self.assertEqual( si.__ac_local_roles__, None )

        self.assertRaises( EmergencyUserCannotOwn
                         , om._setObject, 'should_fail', si )

    def test_setObject_set_owner_with_system_user( self ):

        om = self._makeOne()

        newSecurityManager( None, system )

        si = SimpleItem( 'system' )

        self.assertEqual( si.__ac_local_roles__, None )

        om._setObject( 'system', si )

        self.assertEqual( si.__ac_local_roles__, None )

    def test_setObject_set_owner_with_anonymous_user( self ):

        om = self._makeOne()

        newSecurityManager( None, nobody )

        si = SimpleItem( 'anon' )

        self.assertEqual( si.__ac_local_roles__, None )

        om._setObject( 'anon', si )

        self.assertEqual( si.__ac_local_roles__, None )

    def test_setObject_set_owner_with_simple_user( self ):

        om = self._makeOne()

        user = User( 'user', '123', (), () ).__of__( FauxRoot() )

        newSecurityManager( None, user )

        si = SimpleItem( 'faux_creation' )

        self.assertEqual( si.__ac_local_roles__, None )

        om._setObject( 'faux_creation', si )

        self.assertEqual( si.__ac_local_roles__, { 'user': ['Owner'] } )

    def test_setObject_set_owner_with_faux_user( self ):

        om = self._makeOne()

        user = FauxUser( 'user_id', 'user_login' ).__of__( FauxRoot() )

        newSecurityManager( None, user )

        si = SimpleItem( 'faux_creation' )

        self.assertEqual( si.__ac_local_roles__, None )

        om._setObject( 'faux_creation', si )

        self.assertEqual( si.__ac_local_roles__, { 'user_id': ['Owner'] } )

    def test_setObject_no_set_owner_with_no_user( self ):

        om = self._makeOne()

        newSecurityManager( None, None )

        si = SimpleItem( 'should_be_okay' )

        self.assertEqual( si.__ac_local_roles__, None )

        om._setObject( 'should_be_okay', si, set_owner=0 )

        self.assertEqual( si.__ac_local_roles__, None )

    def test_setObject_no_set_owner_with_emergency_user( self ):

        om = self._makeOne()

        newSecurityManager( None, emergency_user )

        si = SimpleItem( 'should_be_okay' )

        self.assertEqual( si.__ac_local_roles__, None )

        om._setObject( 'should_be_okay', si, set_owner=0 )

        self.assertEqual( si.__ac_local_roles__, None )

    def test_setObject_no_set_owner_with_system_user( self ):

        om = self._makeOne()

        newSecurityManager( None, system )

        si = SimpleItem( 'should_be_okay' )

        self.assertEqual( si.__ac_local_roles__, None )

        om._setObject( 'should_be_okay', si, set_owner=0 )

        self.assertEqual( si.__ac_local_roles__, None )

    def test_setObject_no_set_owner_with_anonymous_user( self ):

        om = self._makeOne()

        newSecurityManager( None, nobody )

        si = SimpleItem( 'should_be_okay' )

        self.assertEqual( si.__ac_local_roles__, None )

        om._setObject( 'should_be_okay', si, set_owner=0 )

        self.assertEqual( si.__ac_local_roles__, None )

    def test_setObject_no_set_owner_with_user( self ):

        om = self._makeOne()

        user = User( 'user', '123', (), () ).__of__( FauxRoot() )

        newSecurityManager( None, user )

        si = SimpleItem( 'should_be_okay' )

        self.assertEqual( si.__ac_local_roles__, None )

        om._setObject( 'should_be_okay', si, set_owner=0 )

        self.assertEqual( si.__ac_local_roles__, None )

    def test_setObject_no_set_owner_with_faux_user( self ):

        om = self._makeOne()

        user = FauxUser( 'user_id', 'user_login' ).__of__( FauxRoot() )

        newSecurityManager( None, user )

        si = SimpleItem( 'should_be_okay' )

        self.assertEqual( si.__ac_local_roles__, None )

        om._setObject( 'should_be_okay', si, set_owner=0 )

        self.assertEqual( si.__ac_local_roles__, None )

if __name__ == "__main__":
    unittest.main()




More information about the Zope-Checkins mailing list