[Zope3-checkins] CVS: Zope3/src/zope/app/component/tests - exampleclass.py:1.1 module.py:1.1 test_contentdirective.py:1.1 test_factory.py:1.1 test_requirepermissions.py:1.1 test_directives.py:1.4 test_servicedirective.py:1.5
Steve Alexander
steve@cat-box.net
Mon, 12 May 2003 12:33:11 -0400
Update of /cvs-repository/Zope3/src/zope/app/component/tests
In directory cvs.zope.org:/tmp/cvs-serv2424/src/zope/app/component/tests
Modified Files:
test_directives.py test_servicedirective.py
Added Files:
exampleclass.py module.py test_contentdirective.py
test_factory.py test_requirepermissions.py
Log Message:
Moved zope.app.contentdirective into zope.app.component.
=== Added File Zope3/src/zope/app/component/tests/exampleclass.py ===
##############################################################################
#
# Copyright (c) 2003 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.
#
##############################################################################
from zope.interface import Interface
class ExampleClass:
pass
class IExample(Interface):
pass
class IExample2(Interface):
pass
class IExampleContainer(Interface):
pass
=== Added File Zope3/src/zope/app/component/tests/module.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.
#
##############################################################################
"""Preliminaries to hookup a test suite with the external TestModule.
This is necessary because the test framework interferes with seeing changes in
the running modules via the module namespace. This enables having some
subject classes, instances, permissions, etc, that don't live in the test
modules, themselves."""
from zope.interface import Interface
from zope.schema import Text
class I(Interface):
def m1():
pass
def m2():
pass
class I2(I):
def m4():
pass
class I3(Interface):
def m3():
pass
class I4(Interface):
def m2():
pass
class S(Interface):
foo = Text()
bar = Text()
baro = Text(readonly=True)
class S2(Interface):
foo2 = Text()
bar2 = Text()
template_bracket = """<zopeConfigure
xmlns="http://namespaces.zope.org/zope">
%s
</zopeConfigure>"""
=== Added File Zope3/src/zope/app/component/tests/test_contentdirective.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.
#
##############################################################################
"""
$Id: test_contentdirective.py,v 1.1 2003/05/12 16:32:40 stevea Exp $
"""
import unittest
from StringIO import StringIO
from zope.configuration.xmlconfig import xmlconfig, XMLConfig
from zope.app.tests.placelesssetup import PlacelessSetup
from zope.security.management import newSecurityManager, system_user
from zope.security.proxy import Proxy
import zope.configuration
import zope.app.security
import zope.app.component
from zope.app.security.exceptions import UndefinedPermissionError
from zope.component import getService
from zope.app.services.servicenames import Factories
from zope.app.component.globalinterfaceservice import queryInterface
# explicitly import ExampleClass and IExample using full paths
# so that they are the same objects as resolve will get.
from zope.app.component.tests.exampleclass import ExampleClass
from zope.app.component.tests.exampleclass import IExample, IExample2
def configfile(s):
return StringIO("""<zopeConfigure
xmlns='http://namespaces.zope.org/zope'>
%s
</zopeConfigure>
""" % s)
class TestContentDirective(PlacelessSetup, unittest.TestCase):
def setUp(self):
PlacelessSetup.setUp(self)
newSecurityManager(system_user)
XMLConfig('metameta.zcml', zope.configuration)()
XMLConfig('meta.zcml', zope.app.component)()
XMLConfig('meta.zcml', zope.app.security)()
try:
del ExampleClass.__implements__
except AttributeError:
pass
def testEmptyDirective(self):
f = configfile("""
<content class="zope.app.component.tests.exampleclass.ExampleClass">
</content>
""")
xmlconfig(f)
def testImplements(self):
self.assertEqual(queryInterface(
"zope.app.component.tests.exampleclass.IExample"), None)
f = configfile("""
<content class="zope.app.component.tests.exampleclass.ExampleClass">
<implements interface="zope.app.component.tests.exampleclass.IExample" />
</content>
""")
xmlconfig(f)
self.failUnless(IExample.isImplementedByInstancesOf(ExampleClass))
self.assertEqual(queryInterface(
"zope.app.component.tests.exampleclass.IExample"), IExample)
def testMulImplements(self):
self.assertEqual(queryInterface(
"zope.app.component.tests.exampleclass.IExample"), None)
self.assertEqual(queryInterface(
"zope.app.component.tests.exampleclass.IExample2"), None)
f = configfile("""
<content class="zope.app.component.tests.exampleclass.ExampleClass">
<implements interface="
zope.app.component.tests.exampleclass.IExample
zope.app.component.tests.exampleclass.IExample2
" />
</content>
""")
xmlconfig(f)
self.failUnless(IExample.isImplementedByInstancesOf(ExampleClass))
self.failUnless(IExample2.isImplementedByInstancesOf(ExampleClass))
self.assertEqual(queryInterface(
"zope.app.component.tests.exampleclass.IExample"), IExample)
self.assertEqual(queryInterface(
"zope.app.component.tests.exampleclass.IExample2"),
IExample2)
def testRequire(self):
f = configfile("""
<permission id="zope.View" title="Zope view permission" />
<content class="zope.app.component.tests.exampleclass.ExampleClass">
<require permission="zope.View"
attributes="anAttribute anotherAttribute" />
</content>
""")
xmlconfig(f)
def testAllow(self):
f = configfile("""
<content class="zope.app.component.tests.exampleclass.ExampleClass">
<allow attributes="anAttribute anotherAttribute" />
</content>
""")
xmlconfig(f)
def testMimic(self):
f = configfile("""
<content class="zope.app.component.tests.exampleclass.ExampleClass">
<require like_class="zope.app.component.tests.exampleclass.ExampleClass" />
</content>
""")
xmlconfig(f)
class TestFactorySubdirective(PlacelessSetup, unittest.TestCase):
def setUp(self):
PlacelessSetup.setUp(self)
newSecurityManager(system_user)
XMLConfig('metameta.zcml', zope.configuration)()
XMLConfig('meta.zcml', zope.app.component)()
XMLConfig('meta.zcml', zope.app.security)()
def testFactory(self):
f = configfile("""
<permission id="zope.Foo" title="Zope Foo Permission" />
<content class="zope.app.component.tests.exampleclass.ExampleClass">
<factory
id="Example"
permission="zope.Foo"
title="Example content"
description="Example description"
/>
</content>
""")
xmlconfig(f)
def testFactoryUndefinedPermission(self):
f = configfile("""
<permission id="zope.Foo" title="Zope Foo Permission" />
<content class="zope.app.component.tests.exampleclass.ExampleClass">
<factory
id="Example"
permission="UndefinedPermission"
title="Example content"
description="Example description"
/>
</content>
""")
self.assertRaises(UndefinedPermissionError, xmlconfig, f,
testing=1)
def testFactoryPublicPermission(self):
f = configfile("""
<permission id="zope.Foo" title="Zope Foo Permission" />
<content class="zope.app.component.tests.exampleclass.ExampleClass">
<factory
id="Example"
permission="zope.Public"
title="Example content"
description="Example description"
/>
</content>
""")
xmlconfig(f)
factory = getService(None, Factories).getFactory('Example')
self.failUnless(type(factory) is Proxy)
def test_suite():
suite = unittest.TestSuite()
loader = unittest.TestLoader()
suite.addTest(loader.loadTestsFromTestCase(TestContentDirective))
suite.addTest(loader.loadTestsFromTestCase(TestFactorySubdirective))
return suite
if __name__=='__main__':
unittest.TextTestRunner().run(test_suite())
=== Added File Zope3/src/zope/app/component/tests/test_factory.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 handler for 'factory' subdirective of 'content' directive """
import unittest
from cStringIO import StringIO
from zope.configuration.xmlconfig import xmlconfig
from zope.configuration.xmlconfig import XMLConfig
from zope.app.services.servicenames import Factories
from zope.app.tests.placelesssetup import PlacelessSetup
from zope.security.management import newSecurityManager, system_user
import zope.configuration
import zope.app.security
import zope.app.component
from zope.app.component.tests.exampleclass import ExampleClass
def configfile(s):
return StringIO("""<zopeConfigure
xmlns='http://namespaces.zope.org/zope'>
%s
</zopeConfigure>
""" % s)
class Test(PlacelessSetup, unittest.TestCase):
def setUp(self):
PlacelessSetup.setUp(self)
newSecurityManager(system_user)
XMLConfig('metameta.zcml', zope.configuration)()
XMLConfig('meta.zcml', zope.app.component)()
XMLConfig('meta.zcml', zope.app.security)()
def testFactory(self):
from zope.component import getService
from zope.proxy.introspection import removeAllProxies
f = configfile("""
<permission id="zope.Foo" title="Zope Foo Permission" />
<content class="zope.app.component.tests.exampleclass.ExampleClass">
<factory
id="Example"
permission="zope.Foo"
title="Example content"
description="Example description"
/>
</content>
""")
xmlconfig(f)
obj = getService(None, Factories).createObject('Example')
obj = removeAllProxies(obj)
self.failUnless(isinstance(obj, ExampleClass))
def testFactoryDefaultId(self):
from zope.component import getService
from zope.proxy.introspection import removeAllProxies
f = configfile("""
<permission id="zope.Foo" title="Zope Foo Permission" />
<content class="zope.app.component.tests.exampleclass.ExampleClass">
<factory
permission="zope.Foo"
title="Example content"
description="Example description"
/>
</content>
""")
xmlconfig(f)
obj = getService(None, Factories).createObject(
'zope.app.component.tests.exampleclass.ExampleClass')
obj = removeAllProxies(obj)
self.failUnless(isinstance(obj, ExampleClass))
def test_suite():
loader=unittest.TestLoader()
return loader.loadTestsFromTestCase(Test)
if __name__=='__main__':
unittest.TextTestRunner().run(test_suite())
=== Added File Zope3/src/zope/app/component/tests/test_requirepermissions.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 handler for 'require' subdirective of 'content' directive """
from cStringIO import StringIO
from zope.app.component.globalinterfaceservice import queryInterface
from zope.app.component.tests import module
from zope.app.tests.placelesssetup import PlacelessSetup
from zope.security.checker import selectChecker
import unittest
import zope.app.component
import zope.app.security
import zope.configuration
# So we can use config parser to exercise protectClass stuff.
from zope.configuration.xmlconfig import XMLConfig
from zope.configuration.xmlconfig import xmlconfig, ZopeXMLConfigurationError
PREFIX = module.__name__ + '.'
def defineDirectives():
XMLConfig('metameta.zcml', zope.configuration)()
XMLConfig('meta.zcml', zope.app.component)()
XMLConfig('meta.zcml', zope.app.security)()
xmlconfig(StringIO("""<zopeConfigure
xmlns='http://namespaces.zope.org/zope' >
<permission id="extravagant" title="extravagant" />
<permission id="paltry" title="paltry" />
</zopeConfigure>"""))
NOTSET = []
P1 = "extravagant"
P2 = "paltry"
class Test(PlacelessSetup, unittest.TestCase):
def setUp(self):
PlacelessSetup.setUp(self)
defineDirectives()
class B:
def m1(self):
return "m1"
def m2(self):
return "m2"
class C(B):
__implements__ = module.I
def m3(self):
return "m3"
def m4(self):
return "m4"
module.test_base = B
module.test_class = C
module.test_instance = C()
self.assertState()
def tearDown(self):
PlacelessSetup.tearDown(self)
module.test_class = None
def assertState(self, m1P=NOTSET, m2P=NOTSET, m3P=NOTSET):
"Verify that class, instance, and methods have expected permissions."
from zope.security.checker import selectChecker
checker = selectChecker(module.test_instance)
self.assertEqual(checker.permission_id('m1'), (m1P or None))
self.assertEqual(checker.permission_id('m2'), (m2P or None))
self.assertEqual(checker.permission_id('m3'), (m3P or None))
def assertDeclaration(self, declaration, **state):
apply_declaration(module.template_bracket % declaration)
self.assertState(**state)
# "testSimple*" exercises tags that do NOT have children. This mode
# inherently sets the instances as well as the class attributes.
def testSimpleMethodsPlural(self):
declaration = ("""<content class="%s">
<require
permission="%s"
attributes="m1 m3"/>
</content>"""
% (PREFIX+"test_class", P1))
self.assertDeclaration(declaration, m1P=P1, m3P=P1)
def assertSetattrState(self, m1P=NOTSET, m2P=NOTSET, m3P=NOTSET):
"Verify that class, instance, and methods have expected permissions."
from zope.security.checker import selectChecker
checker = selectChecker(module.test_instance)
self.assertEqual(checker.setattr_permission_id('m1'), (m1P or None))
self.assertEqual(checker.setattr_permission_id('m2'), (m2P or None))
self.assertEqual(checker.setattr_permission_id('m3'), (m3P or None))
def assertSetattrDeclaration(self, declaration, **state):
self.assertSetattrState(**state)
def test_set_attributes(self):
declaration = ("""<content class="%s">
<require
permission="%s"
set_attributes="m1 m3"/>
</content>"""
% (PREFIX+"test_class", P1))
apply_declaration(module.template_bracket % declaration)
checker = selectChecker(module.test_instance)
self.assertEqual(checker.setattr_permission_id('m1'), P1)
self.assertEqual(checker.setattr_permission_id('m2'), None)
self.assertEqual(checker.setattr_permission_id('m3'), P1)
def test_set_schema(self):
self.assertEqual(queryInterface(PREFIX+"S"), None)
declaration = ("""<content class="%s">
<require
permission="%s"
set_schema="%s"/>
</content>"""
% (PREFIX+"test_class", P1, PREFIX+"S"))
apply_declaration(module.template_bracket % declaration)
self.assertEqual(queryInterface(PREFIX+"S"), module.S)
checker = selectChecker(module.test_instance)
self.assertEqual(checker.setattr_permission_id('m1'), None)
self.assertEqual(checker.setattr_permission_id('m2'), None)
self.assertEqual(checker.setattr_permission_id('m3'), None)
self.assertEqual(checker.setattr_permission_id('foo'), P1)
self.assertEqual(checker.setattr_permission_id('bar'), P1)
self.assertEqual(checker.setattr_permission_id('baro'), None)
def test_multiple_set_schema(self):
self.assertEqual(queryInterface(PREFIX+"S"), None)
self.assertEqual(queryInterface(PREFIX+"S2"), None)
declaration = ("""<content class="%s">
<require
permission="%s"
set_schema="%s %s"/>
</content>"""
% (PREFIX+"test_class", P1, PREFIX+"S", PREFIX+"S2"))
apply_declaration(module.template_bracket % declaration)
self.assertEqual(queryInterface(PREFIX+"S"), module.S)
self.assertEqual(queryInterface(PREFIX+"S2"), module.S2)
checker = selectChecker(module.test_instance)
self.assertEqual(checker.setattr_permission_id('m1'), None)
self.assertEqual(checker.setattr_permission_id('m2'), None)
self.assertEqual(checker.setattr_permission_id('m3'), None)
self.assertEqual(checker.setattr_permission_id('foo'), P1)
self.assertEqual(checker.setattr_permission_id('bar'), P1)
self.assertEqual(checker.setattr_permission_id('foo2'), P1)
self.assertEqual(checker.setattr_permission_id('bar2'), P1)
self.assertEqual(checker.setattr_permission_id('baro'), None)
def testSimpleInterface(self):
self.assertEqual(queryInterface(PREFIX+"I"), None)
declaration = ("""<content class="%s">
<require
permission="%s"
interface="%s"/>
</content>"""
% (PREFIX+"test_class", P1, PREFIX+"I"))
# m1 and m2 are in the interface, so should be set, and m3 should not:
self.assertDeclaration(declaration, m1P=P1, m2P=P1)
# Make sure we know about the interfaces
self.assertEqual(queryInterface(PREFIX+"I"), module.I)
def testMultipleInterface(self):
self.assertEqual(queryInterface(PREFIX+"I3"), None)
self.assertEqual(queryInterface(PREFIX+"I4"), None)
declaration = ("""<content class="%s">
<require
permission="%s"
interface=" %s
%s "/>
</content>"""
% (PREFIX+"test_class", P1, PREFIX+"I3", PREFIX+"I4"))
self.assertDeclaration(declaration, m3P=P1, m2P=P1)
# Make sure we know about the interfaces
self.assertEqual(queryInterface(PREFIX+"I3"), module.I3)
self.assertEqual(queryInterface(PREFIX+"I4"), module.I4)
# "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 = ("""<content class="%s">
<require
attributes="m1"/>
</content>"""
% (PREFIX+"test_class"))
self.assertRaises(ZopeXMLConfigurationError,
self.assertDeclaration,
declaration)
def testCompositeMethodsPluralElementPerm(self):
declaration = ("""<content class="%s">
<require
permission="%s"
attributes="m1 m3"/>
</content>"""
% (PREFIX+"test_class", P1))
self.assertDeclaration(declaration,
m1P=P1, m3P=P1)
def testCompositeInterfaceTopPerm(self):
declaration = ("""<content class="%s">
<require
permission="%s"
interface="%s"/>
</content>"""
% (PREFIX+"test_class", P1, PREFIX+"I"))
self.assertDeclaration(declaration,
m1P=P1, m2P=P1)
def testSubInterfaces(self):
declaration = ("""<content class="%s">
<require
permission="%s"
interface="%s"/>
</content>"""
% (PREFIX+"test_class", P1, PREFIX+"I2"))
# m1 and m2 are in the interface, so should be set, and m3 should not:
self.assertDeclaration(declaration, m1P=P1, m2P=P1)
def testMimicOnly(self):
declaration = ("""<content class="%s">
<require
permission="%s"
attributes="m1 m2"/>
</content>
<content class="%s">
<require like_class="%s" />
</content>
""" % (PREFIX+"test_base", P1,
PREFIX+"test_class", PREFIX+"test_base"))
# m1 and m2 are in the interface, so should be set, and m3 should not:
self.assertDeclaration(declaration,
m1P=P1, m2P=P1)
def testMimicAsDefault(self):
declaration = ("""<content class="%s">
<require
permission="%s"
attributes="m1 m2"/>
</content>
<content class="%s">
<require like_class="%s" />
<require
permission="%s"
attributes="m2 m3"/>
</content>
""" % (PREFIX+"test_base", P1,
PREFIX+"test_class", PREFIX+"test_base", P2))
# m1 and m2 are in the interface, so should be set, and m3 should not:
self.assertDeclaration(declaration,
m1P=P1, m2P=P2, m3P=P2)
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())
=== Zope3/src/zope/app/component/tests/test_directives.py 1.3 => 1.4 ===
--- Zope3/src/zope/app/component/tests/test_directives.py:1.3 Thu May 1 15:35:08 2003
+++ Zope3/src/zope/app/component/tests/test_directives.py Mon May 12 12:32:40 2003
@@ -42,7 +42,7 @@
__implements__ = IC
def definePermissions():
- XMLConfig('meta.zcml', zope.app.contentdirective)()
+ XMLConfig('meta.zcml', zope.app.component)()
class Test(PlacelessSetup, unittest.TestCase):
@@ -50,6 +50,7 @@
def setUp(self):
PlacelessSetup.setUp(self)
+ XMLConfig('metameta.zcml', zope.configuration)()
XMLConfig('meta.zcml', zope.app.component)()
XMLConfig('meta.zcml', zope.app.security)()
=== Zope3/src/zope/app/component/tests/test_servicedirective.py 1.4 => 1.5 ===
--- Zope3/src/zope/app/component/tests/test_servicedirective.py:1.4 Thu May 1 15:35:08 2003
+++ Zope3/src/zope/app/component/tests/test_servicedirective.py Mon May 12 12:32:40 2003
@@ -39,6 +39,7 @@
def setUp(self):
PlacelessSetup.setUp(self)
+ XMLConfig('metameta.zcml', zope.configuration)()
XMLConfig('meta.zcml', zope.app.component)()
def testServiceConfigNoType(self):