[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/ZopePublication/Browser/tests - __init__.py:1.2 testZopePublication.py:1.1

Jim Fulton jim@zope.com
Tue, 16 Jul 2002 19:33:45 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/ZopePublication/Browser/tests
In directory cvs.zope.org:/tmp/cvs-serv19818/tests

Added Files:
	__init__.py testZopePublication.py 
Log Message:
refactored ZopePublicationBrowser tests into separate browser package

=== Zope3/lib/python/Zope/App/ZopePublication/Browser/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.
+# 
+##############################################################################
+""" ZopePublications test module. """


=== Added File Zope3/lib/python/Zope/App/ZopePublication/Browser/tests/testZopePublication.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.
# 
##############################################################################
import unittest

from Interface import Interface

from Zope.ComponentArchitecture import getService, getServiceManager

from Zope.Publisher.Publish import publish
from Zope.Publisher.Browser.BrowserRequest import TestRequest
from Zope.Publisher.Browser.BrowserView import BrowserView
from Zope.Publisher.Browser.IBrowserPublisher import IBrowserPublisher
from Zope.Publisher.Browser.IBrowserPresentation import IBrowserPresentation

from Zope.Proxy.ContextWrapper import getWrapperContext
from Zope.Proxy.ProxyIntrospection import removeAllProxies

from Zope.Security.Checker import defineChecker, NamesChecker

from Zope.ContextWrapper import wrapperTypes

from Zope.App.Security.Registries.PrincipalRegistry import principalRegistry
from Zope.App.Security.Grants.Global.PrincipalRoleManager \
     import principalRoleManager

from Zope.App.ZopePublication.ZopePublication import ZopePublication
from Zope.App.ZopePublication.Browser.Publication import BrowserPublication
from Zope.App.ZopePublication.Traversers import TestTraverser
from Zope.App.ZopePublication.tests.testZopePublication \
     import BasePublicationTests as BasePublicationTests_

def foo():
    " "
    return '<html><body>hello base fans</body></html>'

class DummyPublished:

    __implements__ = IBrowserPublisher

    def publishTraverse(self, request, name):
        if name == 'bruce':
            return foo
        raise KeyError, name

    def browserDefault(self, request):
        return self, ['bruce']

class DummyView(DummyPublished, BrowserView):

    __implements__ = DummyPublished.__implements__, BrowserView.__implements__


class BasePublicationTests(BasePublicationTests_):

    def _createRequest(self, path, publication, **kw):
        request = TestRequest(PATH_INFO=path, **kw)
        request.setPublication(publication)
        return request
    

class BrowserDefaultTests(BasePublicationTests):
    """
    test browser default

    many views lead to a default view
    <base href="/somepath/@@view/view_method">

    """
    klass = BrowserPublication
    
    def testBaseTagNoBase(self):
        self._testBaseTags('/somepath/@@view/', '')

    def testBaseTag1(self):
        self._testBaseTags('/somepath/@@view',
                           'http://127.0.0.1/somepath/@@view/bruce')

    def testBaseTag2(self):
        self._testBaseTags('/somepath/',
                           'http://127.0.0.1/somepath/@@view/bruce')

    def testBaseTag3(self):
        self._testBaseTags('/somepath',
                           'http://127.0.0.1/somepath/@@view/bruce')
        


    def _testBaseTags(self, url, expected):

        class I1(Interface): pass
        
        from Persistence import Persistent
        
        class O1(Persistent):
            __implements__ = I1


        pub = BrowserPublication(self.db)
        
        getService(None,'Views').provideView(I1, 'view',
                           IBrowserPresentation, [DummyView])
        getService(None,'Views').setDefaultViewName(I1,
                             IBrowserPresentation, 'view')
        getService(None, 'Views').provideView(None,
                    '_traverse', IBrowserPresentation, [TestTraverser])
        
        ob = O1()

        ## the following is for running the tests standalone
        principalRegistry.defineDefaultPrincipal(
            'tim', 'timbot', 'ai at its best')
        
        principalRoleManager.assignRoleToPrincipal('Manager', 'tim')


        # now place our object inside the application
        from Transaction import get_transaction
        
        connection = self.db.open()
        app = connection.root()['Application']
        app.somepath = ob
        get_transaction().commit()
        connection.close()        

        defineChecker(app.__class__, NamesChecker(somepath='xxx'))

        req = self._createRequest(url, pub)
        response = req.response

        publish(req, handle_errors=0)
            
        self.assertEqual(response.getBase(), expected)


    def _createRequest(self, path, publication, **kw):
        request = TestRequest(PATH_INFO=path, **kw)
        request.setPublication(publication)
        return request



class SimpleObject:
    def __init__(self, v):
        self.v = v

class I1(Interface):
    pass

class mydict(dict):
    __implements__ = I1


class BrowserPublicationTests(BasePublicationTests):

    klass = BrowserPublication

    def testNativeTraverseNameWrapping(self):
        pub = self.klass(self.db)
        ob = DummyPublished()
        ob2 = pub.traverseName(self._createRequest('/bruce',pub), ob, 'bruce')
        self.failUnless(ob2 is not ob)
        self.failUnless(type(ob2) in wrapperTypes)

    def testAdaptedTraverseNameWrapping(self):

        class Adapter:
            " "
            __implements__ = IBrowserPublisher
            def __init__(self, context, request):
                self.context = context
                self.counter = 0

            def publishTraverse(self, request, name):
                self.counter+=1
                return self.context[name]

        provideView=getService(None, "Views").provideView
        provideView(I1, '_traverse', IBrowserPresentation, [Adapter])
        ob = mydict()
        ob['bruce'] =  SimpleObject('bruce')
        ob['bruce2'] =  SimpleObject('bruce2')
        pub = self.klass(self.db)
        ob2 = pub.traverseName(self._createRequest('/bruce',pub), ob, 'bruce')
        self.failUnless(type(ob2) in wrapperTypes)
        unw = removeAllProxies(ob2)
        self.assertEqual(unw.v, 'bruce')

    def testAdaptedTraverseDefaultWrapping(self):
        """Test default content and make sure that it's wrapped.
        """

        class Adapter:
            __implements__ = IBrowserPublisher
            def __init__(self, context, request):
                self.context = context

            def browserDefault(self, request):
                return (self.context['bruce'], 'dummy')

        provideView=getService(None, "Views").provideView
        provideView(I1, '_traverse', IBrowserPresentation, [Adapter])
        ob = mydict()
        ob['bruce'] =  SimpleObject('bruce')
        ob['bruce2'] =  SimpleObject('bruce2')
        pub = self.klass(self.db)
        ob2, x = pub.getDefaultTraversal(self._createRequest('/bruce',pub), ob)
        self.assertEqual(x, 'dummy')
        self.failUnless(type(ob2) in wrapperTypes)
        unw = removeAllProxies(ob2)
        self.assertEqual(unw.v, 'bruce')


    # XXX we no longer support path parameters! (At least for now)
    def XXXtestTraverseSkinExtraction(self):
        class I1(Interface): pass
        class C: __implements__ = I1
        class BobView(DummyView): pass

        pub = self.klass(self.db)
        ob = C()
        provideView=getService(None, "Views").provideView
        provideView(I1, 'edit', IBrowserPresentation, [BobView])

        r = self._createRequest('/@@edit;skin=zmi',pub)
        ob2 = pub.traverseName(r , ob, '@@edit;skin=zmi')
        self.assertEqual(r.getPresentationSkin(), 'zmi')
        self.assertEqual(ob2.__class__ , BobView)

        r = self._createRequest('/@@edit;skin=zmi',pub)
        ob2 = pub.traverseName(r , ob, '@@edit;skin=zmi')
        self.assertEqual(r.getPresentationSkin(), 'zmi')
        self.assertEqual(ob2.__class__ , BobView)

    def testTraverseName(self):
        pub = self.klass(self.db)
        class C:
            x = SimpleObject(1)
        ob = C()
        r = self._createRequest('/x',pub)
        provideView=getService(None, "Views").provideView
        provideView(None, '_traverse', IBrowserPresentation, [TestTraverser])
        ob2 = pub.traverseName(r, ob, 'x')
        self.assertEqual(removeAllProxies(ob2).v, 1)
        self.assertEqual(getWrapperContext(ob2), ob)

    def testTraverseNameView(self):
        pub = self.klass(self.db)
        class I(Interface): pass
        class C:
            __implements__ = I
        ob = C()
        class V:
            def __init__(self, context, request): pass
            __implements__ = IBrowserPresentation
        r = self._createRequest('/@@spam',pub)
        provideView=getService(None, "Views").provideView
        provideView(I, 'spam', IBrowserPresentation, [V])
        ob2 = pub.traverseName(r, ob, '@@spam')
        self.assertEqual(removeAllProxies(ob2).__class__, V)
        self.assertEqual(getWrapperContext(ob2), ob)

    def testTraverseNameServices(self):
        pub = self.klass(self.db)
        class C:
            def getServiceManager(self):
                return SimpleObject(1)
        ob = C()
        r = self._createRequest('/++etc++Services',pub)
        ob2 = pub.traverseName(r, ob, '++etc++Services')
        self.assertEqual(removeAllProxies(ob2).v, 1)
        self.assertEqual(getWrapperContext(ob2), ob)

    def testTraverseNameApplicationControl(self):
        from Zope.App.OFS.ApplicationControl.ApplicationControl \
             import ApplicationController
        pub = self.klass(self.db)
        r = self._createRequest('/++etc++ApplicationController',pub)
        ac = pub.traverseName(r, None, '++etc++ApplicationController')
        self.assertEqual(ac, ApplicationController)
        r = self._createRequest('/++etc++ApplicationController',pub)
        app = r.publication.getApplication(r)
        self.assertEqual(app, ApplicationController)
        self.failIf(r.getTraversalStack())


def test_suite():
    t2 = unittest.makeSuite(BrowserPublicationTests, 'test')
    t3 = unittest.makeSuite(BrowserDefaultTests, 'test')
    return unittest.TestSuite((t2,t3))

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