[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Traversing/tests - __init__.py:1.2 testAcquire.py:1.2 testAtterItem.py:1.2 testEtc.py:1.2 testNamespaceTrversal.py:1.2 testPresentation.py:1.2 testTraverser.py:1.2
Jim Fulton
jim@zope.com
Mon, 10 Jun 2002 19:28:49 -0400
Update of /cvs-repository/Zope3/lib/python/Zope/App/Traversing/tests
In directory cvs.zope.org:/tmp/cvs-serv17445/lib/python/Zope/App/Traversing/tests
Added Files:
__init__.py testAcquire.py testAtterItem.py testEtc.py
testNamespaceTrversal.py testPresentation.py testTraverser.py
Log Message:
Merged Zope-3x-branch into newly forked Zope3 CVS Tree.
=== Zope3/lib/python/Zope/App/Traversing/tests/__init__.py 1.1 => 1.2 ===
+#
+# 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.
+#
+##############################################################################
+"""App tests"""
=== Zope3/lib/python/Zope/App/Traversing/tests/testAcquire.py 1.1 => 1.2 ===
+#
+# 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
+#
+##############################################################################
+"""
+
+Revision information:
+$Id$
+"""
+
+from unittest import TestCase, TestSuite, main, makeSuite
+from Zope.ComponentArchitecture.tests.PlacelessSetup import PlacelessSetup
+from Zope.App.Traversing.ITraversable import ITraversable
+from Zope.App.Traversing.DefaultTraversable import DefaultTraversable
+from Zope.ComponentArchitecture.GlobalAdapterService import provideAdapter
+from Zope.Proxy.ContextWrapper import ContextWrapper, getWrapperContext
+from Zope.App.Traversing.AcquireNamespace import acquire
+from Zope.Exceptions import NotFoundError
+
+class Test(PlacelessSetup, TestCase):
+
+ def test(self):
+ provideAdapter(None, ITraversable, DefaultTraversable)
+
+ class C:
+ def __init__(self, name):
+ self.name = name
+
+ a = C('a')
+ a.a1 = C('a1')
+ a.a2 = C('a2')
+ a.a2.a21 = C('a21')
+ a.a2.a21.a211 = C('a211')
+
+ a2 = ContextWrapper(a.a2, a)
+ a21 = ContextWrapper(a.a2.a21, a2)
+ a211 = ContextWrapper(a.a2.a21.a211, a21)
+
+ acquired = acquire('a1', (), 'a1;acquire', a211, None)
+
+ self.assertEqual(acquired.name, 'a1')
+
+ self.assertRaises(NotFoundError,
+ acquire, 'a3', (), 'a1;acquire', a211, None)
+
+
+
+def test_suite():
+ return TestSuite((
+ makeSuite(Test),
+ ))
+
+if __name__=='__main__':
+ main(defaultTest='test_suite')
=== Zope3/lib/python/Zope/App/Traversing/tests/testAtterItem.py 1.1 => 1.2 ===
+#
+# 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
+#
+##############################################################################
+"""
+
+Revision information:
+$Id$
+"""
+
+from unittest import TestCase, TestSuite, main, makeSuite
+from Zope.Testing.CleanUp import CleanUp # Base class w registry cleanup
+
+
+class C:
+ a = 1
+ def __getitem__(self, key): return key+'value'
+c=C()
+
+
+class Test(CleanUp, TestCase):
+
+ def testAttr(self):
+ from Zope.App.Traversing.AttrItemNamespaces import attr
+ self.assertEqual(attr('a', (), 'a;attribute', c, None), 1)
+
+ def testItem(self):
+ from Zope.App.Traversing.AttrItemNamespaces import item
+ self.assertEqual(item('a', (), 'a;item', c, None), 'avalue')
+
+
+
+def test_suite():
+ return TestSuite((
+ makeSuite(Test),
+ ))
+
+if __name__=='__main__':
+ main(defaultTest='test_suite')
=== Zope3/lib/python/Zope/App/Traversing/tests/testEtc.py 1.1 => 1.2 ===
+#
+# 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
+#
+##############################################################################
+"""
+
+Revision information:
+$Id$
+"""
+
+from unittest import TestCase, TestSuite, main, makeSuite
+from Zope.Testing.CleanUp import CleanUp # Base class w registry cleanup
+
+class Test(CleanUp, TestCase):
+
+ def testApplicationControl(self):
+ from Zope.App.Traversing.EtcNamespace import etc
+ from Zope.App.OFS.ApplicationControl.ApplicationControl \
+ import ApplicationController
+
+ self.assertEqual(
+ etc('ApplicationController', (), '++etc++Services', None, None),
+ ApplicationController)
+
+ def testServices(self):
+ from Zope.App.Traversing.EtcNamespace import etc
+ class C:
+ def getServiceManager(self): return 42
+
+ self.assertEqual(etc('Services', (), 'etc:Services', C(), None), 42)
+
+
+
+def test_suite():
+ return TestSuite((
+ makeSuite(Test),
+ ))
+
+if __name__=='__main__':
+ main(defaultTest='test_suite')
=== Zope3/lib/python/Zope/App/Traversing/tests/testNamespaceTrversal.py 1.1 => 1.2 ===
+#
+# 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
+#
+##############################################################################
+"""
+
+Revision information:
+$Id$
+"""
+
+from unittest import TestCase, TestSuite, main, makeSuite
+from Zope.Testing.CleanUp import CleanUp # Base class w registry cleanup
+
+class C:
+ a = 1
+ def __getitem__(self, key): return key+'value'
+
+c=C()
+
+
+class Test(CleanUp, TestCase):
+
+ def testAttr(self):
+ from Zope.App.Traversing.Traverser import Traverser
+ traverser = Traverser(c)
+ v = traverser.traverse('++attribute++a')
+ self.assertEqual(v, 1)
+
+ def testItem(self):
+ from Zope.App.Traversing.Traverser import Traverser
+ traverser = Traverser(c)
+ v = traverser.traverse('++item++a')
+ self.assertEqual(v, 'avalue')
+
+
+
+def test_suite():
+ return TestSuite((
+ makeSuite(Test),
+ ))
+
+if __name__=='__main__':
+ main(defaultTest='test_suite')
=== Zope3/lib/python/Zope/App/Traversing/tests/testPresentation.py 1.1 => 1.2 ===
+#
+# 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
+#
+##############################################################################
+"""
+
+Revision information:
+$Id$
+"""
+
+from unittest import TestCase, TestSuite, main, makeSuite
+from Zope.ComponentArchitecture.tests.PlacelessSetup import PlacelessSetup
+from Zope.ComponentArchitecture.GlobalViewService import provideView
+from Zope.ComponentArchitecture.GlobalResourceService import provideResource
+from Zope.App.Traversing.PresentationNamespaces import view, resource
+from Zope.Exceptions import NotFoundError
+from Interface import Interface
+
+class IContent(Interface): pass
+class IPresentationType(Interface): pass
+
+class Content: __implements__ = IContent
+
+class Resource:
+
+ def __init__(self, request): pass
+ __implements__ = IPresentationType
+
+class View:
+ __implements__ = IPresentationType
+
+ def __init__(self, content, request):
+ self.content = content
+
+class Request:
+
+ def getPresentationType(self): return IPresentationType
+ def getPresentationSkin(self): return ''
+
+
+class Test(PlacelessSetup, TestCase):
+
+ def testView(self):
+ provideView(IContent, 'foo', IPresentationType, [View])
+
+ ob = Content()
+ v = view('foo', (), '@@foo', ob, Request())
+ self.assertEqual(v.__class__, View)
+
+ def testResource(self):
+ provideResource('foo', IPresentationType, Resource)
+
+ ob = Content()
+ r = resource('foo', (), '++resource++foo', ob, Request())
+ self.assertEqual(r.__class__, Resource)
+
+
+def test_suite():
+ return TestSuite((
+ makeSuite(Test),
+ ))
+
+if __name__=='__main__':
+ main(defaultTest='test_suite')
=== Zope3/lib/python/Zope/App/Traversing/tests/testTraverser.py 1.1 => 1.2 ===
+#
+# 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$
+"""
+
+import unittest
+from Zope.App.Traversing.ITraverser import ITraverser
+from Zope.App.Traversing.ITraversable import ITraversable
+from Zope.App.Traversing.Traverser import Traverser
+from Zope.App.Traversing.DefaultTraversable import DefaultTraversable
+from Zope.Proxy.ContextWrapper import ContextWrapper
+from Zope.Exceptions import NotFoundError, Unauthorized
+from Zope.ComponentArchitecture import getService
+from Zope.Security.SecurityManagement import setSecurityPolicy, \
+ noSecurityManager
+
+from Interface.Verify import verifyClass
+from Interface.Implements import instancesOfObjectImplements
+from Zope.App.OFS.Services.ServiceManager.tests.PlacefulSetup import PlacefulSetup
+from Zope.Security.Checker \
+ import ProxyFactory, defineChecker, NamesChecker, CheckerPublic, Checker
+from Zope.Security.SecurityManagement import newSecurityManager
+
+class C:
+ def __init__(self, name):
+ self.name = name
+
+class TraverserTests(PlacefulSetup, unittest.TestCase):
+
+ def setUp(self):
+ PlacefulSetup.setUp(self)
+ # Build up a wrapper chain
+ self.root = ContextWrapper(C('root'), None, name='')
+ self.folder = ContextWrapper(C('folder'), self.root, name='folder')
+ self.item = ContextWrapper(C('item'), self.folder, name='item')
+ self.tr = Traverser(self.item)
+
+ def testImplementsITraverser(self):
+ self.failUnless(ITraverser.isImplementedBy(self.tr))
+
+ def testVerifyInterfaces(self):
+ for interface in instancesOfObjectImplements(Traverser):
+ verifyClass(interface, Traverser)
+
+ def testPhysicalRoot(self):
+ self.failUnless(self.tr.getPhysicalRoot() is self.root)
+
+ def testPhysicalPath(self):
+ self.assertEquals(self.tr.getPhysicalPath(), ('', 'folder', 'item'))
+
+ def testUnwrapped(self):
+ # The feature should do the right thing for unwrapped objects too
+ unwrapped = C('unwrapped')
+ tr = Traverser(unwrapped)
+ self.assertEquals(tr.getPhysicalPath(), ())
+ self.failUnless(tr.getPhysicalRoot() is unwrapped)
+
+class UnrestrictedNoTraverseTests(unittest.TestCase):
+ def setUp(self):
+ self.root = root = C('root')
+ self.folder = folder = C('folder')
+ self.item = item = C('item')
+
+ root.folder = folder
+ folder.item = item
+
+ self.tr = Traverser(root)
+
+ def testNoTraversable(self):
+ self.assertRaises(NotFoundError, self.tr.traverse,
+ 'folder')
+
+class UnrestrictedTraverseTests(PlacefulSetup, unittest.TestCase):
+ def setUp(self):
+ PlacefulSetup.setUp(self)
+ # Build up a wrapper chain
+
+ getService(None,"Adapters").provideAdapter(
+ None, ITraversable, DefaultTraversable)
+
+ self.root = root = C('root')
+ self.folder = folder = C('folder')
+ self.item = item = C('item')
+
+ root.folder = folder
+ folder.item = item
+
+ self.tr = Traverser(root)
+
+ def testSimplePathString(self):
+ tr = self.tr
+ item = self.item
+
+ self.assertEquals(tr.traverse('/folder/item'), item)
+ self.assertEquals(tr.traverse('folder/item'), item)
+ self.assertEquals(tr.traverse('/folder/item/'), item)
+
+ def testSimplePathUnicode(self):
+ tr = self.tr
+ item = self.item
+
+ self.assertEquals(tr.traverse(u'/folder/item'), item)
+ self.assertEquals(tr.traverse(u'folder/item'), item)
+ self.assertEquals(tr.traverse(u'/folder/item/'), item)
+
+ def testSimplePathTuple(self):
+ tr = self.tr
+ item = self.item
+
+ self.assertEquals(tr.traverse(('', 'folder', 'item')),
+ item)
+ self.assertEquals(tr.traverse(('folder', 'item')), item)
+
+ def testComplexPathString(self):
+ tr = self.tr
+ item = self.item
+
+ self.assertEquals(tr.traverse('/folder/../folder/./item'),
+ item)
+ self.assertEquals(tr.traverse(
+ '/../folder/../../folder/item'), item)
+ self.assertEquals(tr.traverse('../../folder/item'), item)
+
+ def testNotFoundDefault(self):
+ self.assertEquals(self.tr.traverse('foo', 'notFound'),
+ 'notFound')
+
+ def testNotFoundNoDefault(self):
+ self.assertRaises(NotFoundError, self.tr.traverse, 'foo')
+
+def Denied(*names):
+
+ def check(name):
+ if name in names:
+ return 'Waaaa'
+ return CheckerPublic
+
+ return Checker(check)
+
+class RestrictedTraverseTests(PlacefulSetup, unittest.TestCase):
+ _oldPolicy = None
+ _deniedNames = ()
+
+ def setUp(self):
+ PlacefulSetup.setUp(self)
+
+ getService(None,"Adapters").provideAdapter(
+ None, ITraversable, DefaultTraversable)
+
+ self.root = root = C('root')
+ self.folder = folder = C('folder')
+ self.item = item = C('item')
+
+ root.folder = folder
+ folder.item = item
+
+ self.tr = Traverser(ProxyFactory(root))
+
+ def testAllAllowed(self):
+ defineChecker(C, Checker(lambda name: CheckerPublic))
+ tr = Traverser(ProxyFactory(self.root))
+ item = self.item
+
+ self.assertEquals(tr.traverse(('', 'folder', 'item')), item)
+ self.assertEquals(tr.traverse(('folder', 'item')), item)
+
+ def testItemDenied(self):
+ newSecurityManager('no one')
+ defineChecker(C, Denied('item'))
+ tr = Traverser(ProxyFactory(self.root))
+ folder = self.folder
+
+ self.assertRaises(Unauthorized, tr.traverse,
+ ('', 'folder', 'item'))
+ self.assertRaises(Unauthorized, tr.traverse,
+ ('folder', 'item'))
+ self.assertEquals(tr.traverse(('', 'folder')), folder)
+ self.assertEquals(tr.traverse(('folder', '..', 'folder')),
+ folder)
+ self.assertEquals(tr.traverse(('folder',)), folder)
+
+class DefaultTraversableTests(unittest.TestCase):
+ def testImplementsITraversable(self):
+ self.failUnless(ITraversable.isImplementedBy(DefaultTraversable(None)))
+
+ def testVerifyInterfaces(self):
+ for interface in instancesOfObjectImplements(DefaultTraversable):
+ verifyClass(interface, DefaultTraversable)
+
+ def testAttributeTraverse(self):
+ root = C('root')
+ item = C('item')
+ root.item = item
+ df = DefaultTraversable(root)
+
+ further = []
+ next = df.traverse('item', (), 'item', further)
+ self.failUnless(next is item)
+ self.assertEquals(further, [])
+
+ def testDictionaryTraverse(self):
+ dict = {}
+ foo = C('foo')
+ dict['foo'] = foo
+ df = DefaultTraversable(dict)
+
+ further = []
+ next = df.traverse('foo', (), 'foo', further)
+ self.failUnless(next is foo)
+ self.assertEquals(further, [])
+
+ def testNotFound(self):
+ df = DefaultTraversable(C('dummy'))
+
+ self.assertRaises(NotFoundError, df.traverse, 'bar', (), 'bar', [])
+
+def test_suite():
+ loader = unittest.TestLoader()
+ suite = loader.loadTestsFromTestCase(TraverserTests)
+ suite.addTest(loader.loadTestsFromTestCase(DefaultTraversableTests))
+ suite.addTest(loader.loadTestsFromTestCase(UnrestrictedNoTraverseTests))
+ suite.addTest(loader.loadTestsFromTestCase(UnrestrictedTraverseTests))
+ suite.addTest(loader.loadTestsFromTestCase(RestrictedTraverseTests))
+ return suite
+
+if __name__=='__main__':
+ unittest.TextTestRunner().run(test_suite())