[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Security/tests - testProtectSubClass.py:1.1.2.1
Jim Fulton
jim@zope.com
Mon, 4 Mar 2002 11:57:55 -0500
Update of /cvs-repository/Zope3/lib/python/Zope/App/Security/tests
In directory cvs.zope.org:/tmp/cvs-serv15600/tests
Added Files:
Tag: Zope-3x-branch
testProtectSubClass.py
Log Message:
Added logic to protectClass to make sure that inherited methods were
protected correctly.
=== Added File Zope3/lib/python/Zope/App/Security/tests/testProtectSubClass.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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
#
##############################################################################
"""Test proper protection of inherited methods
Revision information:
$Id: testProtectSubClass.py,v 1.1.2.1 2002/03/04 16:57:55 jim Exp $
"""
from unittest import TestCase, TestSuite, main, makeSuite
from Zope.Testing.CleanUp import CleanUp # Base class w registry cleanup
from Zope.App.Security.protectClass import protectMethod
from Zope.App.Security.PermissionRegistry import permissionRegistry
class Test(CleanUp, TestCase):
def testInherited(self):
class B1(object):
def g(self): return 'B1.g'
class B2(object):
def h(self): return 'B2.h'
class S(B1, B2):
pass
permissionRegistry.definePermission('B1', '')
permissionRegistry.definePermission('S', '')
protectMethod(B1, 'g', 'B1')
protectMethod(S, 'g', 'S')
protectMethod(S, 'h', 'S')
self.assertEqual(B1.__dict__['g'].__permission__, 'B1')
self.assertEqual(getattr(B2.__dict__['h'], '__permission__', ''), '')
self.assertEqual(S().g.__permission__, 'S')
self.assertEqual(S().h.__permission__, 'S')
self.assertEqual(S().g(), 'B1.g')
self.assertEqual(S().h(), 'B2.h')
def testOldStyleClass(self):
class B1:
def g(self): return 'B1.g'
class S(B1):
pass
permissionRegistry.definePermission('S', '')
self.assertRaises(TypeError, protectMethod, S, 'g', 'S')
def test_suite():
return TestSuite((
makeSuite(Test),
))
if __name__=='__main__':
main(defaultTest='test_suite')