[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Security/tests - testProtectClass.py:1.1.2.1
Ken Manheimer
klm@zope.com
Fri, 30 Nov 2001 14:40:23 -0500
Update of /cvs-repository/Zope3/lib/python/Zope/App/Security/tests
In directory cvs.zope.org:/tmp/cvs-serv22772/tests
Added Files:
Tag: Zope-3x-branch
testProtectClass.py
Log Message:
Test basline.
=== Added File Zope3/lib/python/Zope/App/Security/tests/testProtectClass.py ===
# This software is subject to the provisions of the Zope Public License,
# Version 1.1 (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 handler for 'definePermission' directive """
# TODO:
# - Test variants on composite cases - with permission explicit in elements,
# and overriding in elements, etc.
import unittest, sys
from Interface import Interface
from Zope.App.Security import protectClass
from Zope.App.Security.metaConfigure import metaConfigure
metaConfigure()
# So we can use config parser to exercise protectClass stuff.
from cStringIO import StringIO
from Zope.Configuration.xmlconfig import xmlconfig, ZopeXMLConfigurationError
NOTSET = []
PREFIX = "Zope.App.Security.tests.testmodule."
import Zope.App.Security.tests.testmodule as testmodule
testmodule.test_class = None
class I(Interface):
def m1():
pass
def m2():
pass
testmodule.I = I
P1 = "extravagant"
P2 = "paltry"
testmodule.P1 = P1
testmodule.P2 = P2
template_bracket = """<zopeConfigure
xmlns="http://namespaces.zope.org/zope"
xmlns:security='http://namespaces.zope.org/security'>
%s
</zopeConfigure>"""
class Test(unittest.TestCase):
def setUp(self):
class C:
__implements__ = I
def m1(self):
return "m1"
def m2(self):
return "m2"
def m3(self):
return "m3"
testmodule.test_class = C
testmodule.test_instance = C()
self.assertState()
def tearDown(self):
testmodule.test_class = None
def assertState(self, instP=NOTSET,
m1P=NOTSET, m2P=NOTSET, m3P=NOTSET):
"Verify that class, instance, and methods have expected permissions."
tclass, instance = testmodule.test_class, testmodule.test_instance
self.assertEqual(getattr(instance, "__permission__", NOTSET), instP)
self.assertEqual(getattr(tclass.m1, "__permission__", NOTSET), m1P)
self.assertEqual(getattr(tclass.m2, "__permission__", NOTSET), m2P)
self.assertEqual(getattr(tclass.m3, "__permission__", NOTSET), m3P)
self.assertEqual(getattr(instance.m1, "__permission__", NOTSET), m1P)
self.assertEqual(getattr(instance.m2, "__permission__", NOTSET), m2P)
self.assertEqual(getattr(instance.m3, "__permission__", NOTSET), m3P)
def assertDeclaration(self, declaration, **state):
apply_declaration(template_bracket % declaration)
self.assertState(**state)
def testSimpleNoPerm(self):
"""Establish rejection of declarations lacking a permission spec."""
declaration = (template_bracket
% """<security:protectClass name="%s" />""")
self.assertRaises(ZopeXMLConfigurationError,
self.assertDeclaration,
declaration)
# "testSimple*" exercises tags that do NOT have children. This mode
# inherently sets the instances as well as the class attributes.
def testSimpleMethod(self):
declaration = ("""<security:protectClass
name="%s" permission="%s" method="m1" />"""
% (PREFIX+"test_class", P1))
self.assertDeclaration(declaration,
instP=P1, m1P=P1)
def testSimpleMethodsPlural(self):
declaration = ("""<security:protectClass
name="%s" permission="%s" methods="m1, m3" />"""
% (PREFIX+"test_class", P1))
self.assertDeclaration(declaration,
instP=P1, m1P=P1, m3P=P1)
def testSimpleInterface(self):
declaration = ("""<security:protectClass
name="%s" permission="%s" interface="%s" />"""
% (PREFIX+"test_class", P1, PREFIX+"I"))
# m1 and m2 are in the interface, so should be set, and m3 should not:
self.assertDeclaration(declaration,
instP=P1, m1P=P1, m2P=P1)
# "testComposite*" exercises tags that DO have children.
# "testComposite*TopPerm" exercises tags with permission in containing tag.
# "testComposite*ElementPerm" exercises tags w/permission in children.
def testCompositeNoPerm(self):
"""Establish rejection of declarations lacking a permission spec."""
declaration = (template_bracket
% ("""<security:protectClass name="%s">
<security:protect method="m1"/>
</security:protectClass>"""
% (PREFIX+"test_class")))
self.assertRaises(ZopeXMLConfigurationError,
self.assertDeclaration,
declaration)
# Permission not in top tag and in one subtag but not in the other:
declaration = (template_bracket
% ("""<security:protectClass name="%s">
<security:protect permission="%s" method="m1"/>
<security:instances/>
</security:protectClass>"""
% (PREFIX+"test_class", P1)))
self.assertRaises(ZopeXMLConfigurationError,
self.assertDeclaration,
declaration)
def testCompositeMethodTopPerm(self):
declaration = ("""<security:protectClass name="%s" permission="%s">
<security:protect method="m1"/>
</security:protectClass>"""
% (PREFIX+"test_class", P1))
self.assertDeclaration(declaration,
m1P=P1)
def testCompositeMethodElementPerm(self):
declaration = ("""<security:protectClass name="%s">
<security:protect permission="%s" method="m1"/>
</security:protectClass>"""
% (PREFIX+"test_class", P1))
self.assertDeclaration(declaration,
m1P=P1)
def testCompositeMethodsPluralTopPerm(self):
declaration = ("""<security:protectClass name="%s" permission="%s">
<security:protect methods="m1, m2"/>
</security:protectClass>"""
% (PREFIX+"test_class", P1))
self.assertDeclaration(declaration,
m1P=P1, m2P=P1)
def testCompositeMethodsPluralElementPerm(self):
declaration = ("""<security:protectClass name="%s">
<security:protect permission="%s"
methods="m1, m3"/>
</security:protectClass>"""
% (PREFIX+"test_class", P1))
self.assertDeclaration(declaration,
m1P=P1, m3P=P1)
def testCompositeInterfaceTopPerm(self):
declaration = ("""<security:protectClass name="%s" permission="%s">
<security:protect interface="%s"/>
</security:protectClass>"""
% (PREFIX+"test_class", P1, PREFIX+"I"))
self.assertDeclaration(declaration,
m1P=P1, m2P=P1)
def testCompositeInterfaceElementPerm(self):
declaration = ("""<security:protectClass name="%s">
<security:protect permission="%s" interface="%s"/>
</security:protectClass>"""
% (PREFIX+"test_class", P1, PREFIX+"I"))
self.assertDeclaration(declaration,
m1P=P1, m2P=P1)
def testCompositeInstancesTopPerm(self):
declaration = ("""<security:protectClass name="%s" permission="%s">
<security:instances/>
</security:protectClass>"""
% (PREFIX+"test_class", P1))
self.assertDeclaration(declaration,
instP=P1)
def testCompositeInstancesElementPerm(self):
declaration = ("""<security:protectClass name="%s">
<security:instances permission="%s"/>
</security:protectClass>"""
% (PREFIX+"test_class", P1))
self.assertDeclaration(declaration,
instP=P1)
def apply_declaration(declaration):
"""Apply the xmlconfig machinery."""
return xmlconfig(StringIO(declaration))
def test_suite():
loader=unittest.TestLoader()
return loader.loadTestsFromTestCase(Test)
if __name__=='__main__':
unittest.TextTestRunner().run(test_suite())