[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Security/tests - testPrincipalRoleView.py:1.1.2.1

Tres Seaver tseaver@zope.com
Fri, 8 Feb 2002 15:32:23 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/Security/tests
In directory cvs.zope.org:/tmp/cvs-serv3203/tests

Added Files:
      Tag: Zope-3x-branch
	testPrincipalRoleView.py 
Log Message:


 - Add first pass at Principal-Role managment component (aka "local
   roles").


=== Added File Zope3/lib/python/Zope/App/Security/tests/testPrincipalRoleView.py ===
##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
# 
##############################################################################

import unittest

from Zope.ComponentArchitecture import defineService, provideService, _clear

from Zope.App.Security.IRoleService import IRoleService
from Zope.App.Security.IAuthenticationService import IAuthenticationService

from Zope.App.Security.IPrincipalRoleManager import IPrincipalRoleManager

class DummyManager:

    __implements__ = IPrincipalRoleManager

class DummyRoleService:

    __implements__ = IRoleService

    def __init__( self, roles ):
        self._roles = roles

    def getRoles( self ):
        return self._roles

class DummyObject:
    def __init__( self, id, title ):
        self._id = id
        self._title = title

    def getId( self ):
        return self._id

    def getTitle( self ):
        return self._title

class DummyAuthenticationService:

    __implements__ = IAuthenticationService

    def __init__( self, principals ):
        self._principals = principals

    def getPrincipals( self ):
        return self._principals

class Test( unittest.TestCase ):

    def setUp(self):

        _clear()

        self._roles = []
        self._roles.append( DummyObject( 'qux', 'Qux' ) )
        self._roles.append( DummyObject( 'baz', 'Baz' ) )

        defineService( 'RoleService', IRoleService )
        provideService( 'RoleService', DummyRoleService( roles = self._roles ) )

        defineService( 'AuthenticationService', IAuthenticationService )

        self._principals = []
        self._principals.append( DummyObject( 'foo', 'Foo' ) )
        self._principals.append( DummyObject( 'bar', 'Bar' ) )

        provideService( 'AuthenticationService',
            DummyAuthenticationService( principals = self._principals ) )

    def tearDown(self):
        _clear()

    def _makeOne( self ):
        from Zope.App.Security.PrincipalRoleView import PrincipalRoleView
        return PrincipalRoleView( DummyManager() )

    def testRoles(self):
        view = self._makeOne()
        roles = list(view.getAllRoles())
        self.assertEqual( len( roles ), 2 )

        ids = map( lambda x: x.getId(), self._roles )
        titles = map( lambda x: x.getTitle(), self._roles )

        for role in roles:
            self.failUnless( role.getId() in ids )
            self.failUnless( role.getTitle() in titles )

    def testPrincipals(self):
        view = self._makeOne()
        principals = list(view.getAllPrincipals())
        self.assertEqual( len( principals ), 2 )

        ids = map( lambda x: x.getId(), self._principals )
        titles = map( lambda x: x.getTitle(), self._principals )

        for principal in principals:
            self.failUnless( principal.getId() in ids )
            self.failUnless( principal.getTitle() in titles )



def test_suite():
    loader=unittest.TestLoader()
    return loader.loadTestsFromTestCase(Test)

if __name__=='__main__':
    unittest.TextTestRunner().run(test_suite())