[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Publisher/Browser/tests - __init__.py:1.1.4.1 test.pt:1.1.4.1 testDirectives.py:1.1.4.1

Jim Fulton jim@zope.com
Fri, 7 Jun 2002 10:41:18 -0400


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

Added Files:
      Tag: Zope-3x-branch
	__init__.py test.pt testDirectives.py 
Log Message:
Merging in Zope3InWonderland-branch, which implemented the following
proposals (see
http://dev.zope.org/Wikis/DevSite/Projects/ComponentArchitecture/OldProposals): 
- RenameAllowToRequire

- GroupClassRelatedDirectivesInClassDirective

- ViewInterfaceAndSimplification

- ConsistentUseOfSpacesAsDelimitersInZCMLAttributes

- TwoArgumentViewConstructors

- ImplementsInZCML

- SimpleViewCreationInZCML

- RemoveGetView

- ReplaceProtectWithAllow

- ViewMethodsAsViews

- MergeProtectionAndComponentDefinitions

There were also various security fixes resulting of better integration
of security with components.


=== Added File Zope3/lib/python/Zope/App/Publisher/Browser/tests/__init__.py ===
##############################################################################
#
# Copyright (c) 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.
# 
##############################################################################


=== Added File Zope3/lib/python/Zope/App/Publisher/Browser/tests/test.pt ===
<html><body><p>test</p></body></html>


=== Added File Zope3/lib/python/Zope/App/Publisher/Browser/tests/testDirectives.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 os
import unittest
import sys

from Zope.Configuration.xmlconfig import xmlconfig
from Zope.Configuration.Exceptions import ConfigurationError
from Zope.ComponentArchitecture.tests.TestViews import IC, V1, VZMI, R1, RZMI
from Zope.ComponentArchitecture import getView, queryView, queryResource
from Zope.ComponentArchitecture import getDefaultViewName, getResource
from Zope.ComponentArchitecture.tests.PlacelessSetup import PlacelessSetup
from cStringIO import StringIO

from Zope.ComponentArchitecture.tests.Request import Request

from Zope.Publisher.Browser.IBrowserPresentation import IBrowserPresentation

import Zope.App.Publisher.Browser

defs_path = os.path.join(
    os.path.split(Zope.App.Publisher.Browser.__file__)[0],
    'browser-meta.zcml')

template = """<zopeConfigure
   xmlns='http://namespaces.zope.org/zope'
   xmlns:security='http://namespaces.zope.org/security'
   xmlns:browser='http://namespaces.zope.org/browser'>
   %s
   </zopeConfigure>"""

request = Request(IBrowserPresentation)

class Ob:
    __implements__ = IC

ob = Ob()

class Test(PlacelessSetup, unittest.TestCase):

    def setUp(self):
        PlacelessSetup.setUp(self)
        xmlconfig(open(defs_path))

    def testView(self):
        self.assertEqual(queryView(ob, 'test', request),
                         None)

        xmlconfig(StringIO(template % (
            """
            <browser:view name="test"
                  factory="Zope.ComponentArchitecture.tests.TestViews.V1"
                  for="Zope.ComponentArchitecture.tests.TestViews.IC" /> 
            """
            ))) 
        
        self.assertEqual(
            queryView(ob, 'test', request).__class__,
            V1)
         
    def testDefaultView(self):
        self.assertEqual(queryView(ob, 'test', request,
                                   None), None)

        xmlconfig(StringIO(template % (
            """
            <browser:defaultView name="test"
                  factory="Zope.ComponentArchitecture.tests.TestViews.V1"
                  for="Zope.ComponentArchitecture.tests.TestViews.IC" /> 
            """
            ))) 

        self.assertEqual(queryView(ob, 'test',
                                   request, None
                                 ).__class__, V1)
        self.assertEqual(getDefaultViewName(ob, request
                                 ), 'test')
                                 
      
    def testSKinView(self):
        self.assertEqual(queryView(ob, 'test', request,
                                   None), None)

        xmlconfig(StringIO(template % (
            """
            <browser:skin name="zmi" layers="zmi default" />
            <browser:view name="test"
                  factory="Zope.ComponentArchitecture.tests.TestViews.VZMI"
                  layer="zmi" 
                  for="Zope.ComponentArchitecture.tests.TestViews.IC" /> 
            <browser:view name="test"
                  factory="Zope.ComponentArchitecture.tests.TestViews.V1"
                  for="Zope.ComponentArchitecture.tests.TestViews.IC" />
            """
            ))) 
        
        self.assertEqual(
            queryView(ob, 'test', request).__class__,
            V1)
        self.assertEqual(
            queryView(ob, 'test',
                      Request(IBrowserPresentation, 'zmi')).__class__,
            VZMI)

    def testResource(self):
        self.assertEqual(queryResource(ob, 'test', request,
                                       None),
                         None)

        xmlconfig(StringIO(template % (
            """
            <browser:resource name="test"
                  factory="Zope.ComponentArchitecture.tests.TestViews.R1" /> 
            """
            ))) 

        self.assertEqual(
            queryResource(ob, 'test', request).__class__,
            R1)
         
    def testSkinResource(self):
        self.assertEqual(
            queryResource(ob, 'test', request, None),
            None)

        xmlconfig(StringIO(template % (
            """
            <browser:skin name="zmi" layers="zmi default" />
            <browser:resource name="test"
                  factory="Zope.ComponentArchitecture.tests.TestViews.RZMI"
                  layer="zmi" /> 
            <browser:resource name="test"
                  factory="Zope.ComponentArchitecture.tests.TestViews.R1" />
            """
            ))) 
        
        self.assertEqual(
            queryResource(ob, 'test', request).__class__,
            R1)
        self.assertEqual(
            queryResource(ob, 'test',
                          Request(IBrowserPresentation, 'zmi')).__class__,
            RZMI)


    def testInterfaceProtectedView(self):
        xmlconfig(StringIO(template %
            """
            <browser:view name="test"
                  factory="Zope.ComponentArchitecture.tests.TestViews.V1"
                  for="Zope.ComponentArchitecture.tests.TestViews.IC" 
                  permission="Zope.Public"
              allowed_interface="Zope.ComponentArchitecture.tests.TestViews.IV"
                  /> 
            """
            ))

        v = getView(ob, 'test', request)
        self.assertEqual(v.index(), 'V1 here')
        self.assertRaises(Exception, getattr, v, 'action')

    def testAttributeProtectedView(self):
        xmlconfig(StringIO(template %
            """
            <browser:view name="test"
                  factory="Zope.ComponentArchitecture.tests.TestViews.V1"
                  for="Zope.ComponentArchitecture.tests.TestViews.IC" 
                  permission="Zope.Public"
                  allowed_attributes="action"
                  /> 
            """
            ))

        v = getView(ob, 'test', request)
        self.assertEqual(v.action(), 'done')
        self.assertRaises(Exception, getattr, v, 'index')

    def testInterfaceAndAttributeProtectedView(self):
        xmlconfig(StringIO(template %
            """
            <browser:view name="test"
                  factory="Zope.ComponentArchitecture.tests.TestViews.V1"
                  for="Zope.ComponentArchitecture.tests.TestViews.IC" 
                  permission="Zope.Public"
                  allowed_attributes="action"
              allowed_interface="Zope.ComponentArchitecture.tests.TestViews.IV"
                  /> 
            """
            ))

        v = getView(ob, 'test', request)
        self.assertEqual(v.index(), 'V1 here')
        self.assertEqual(v.action(), 'done')

    def testDuplicatedInterfaceAndAttributeProtectedView(self):
        xmlconfig(StringIO(template %
            """
            <browser:view name="test"
                  factory="Zope.ComponentArchitecture.tests.TestViews.V1"
                  for="Zope.ComponentArchitecture.tests.TestViews.IC" 
                  permission="Zope.Public"
                  allowed_attributes="action index"
              allowed_interface="Zope.ComponentArchitecture.tests.TestViews.IV"
                  /> 
            """
            ))

        v = getView(ob, 'test', request)
        self.assertEqual(v.index(), 'V1 here')
        self.assertEqual(v.action(), 'done')

    def testIncompleteProtectedViewNoPermission(self):
        self.assertRaises(
            ConfigurationError,
            xmlconfig,
            StringIO(template %
            """
            <browser:view name="test"
                  factory="Zope.ComponentArchitecture.tests.TestViews.V1"
                  for="Zope.ComponentArchitecture.tests.TestViews.IC" 
                  allowed_attributes="action index"
                  /> 
            """
            ))


    def testPageViews(self):
        self.assertEqual(queryView(ob, 'test', request),
                         None)

        xmlconfig(StringIO(template %
            """
            <browser:view
                  factory="Zope.ComponentArchitecture.tests.TestViews.V1"
                  for="Zope.ComponentArchitecture.tests.TestViews.IC">

                <browser:page name="index.html" attribute="index" /> 
                <browser:page name="action.html" attribute="action" /> 
            </browser:view>
            """
            ))

        v = getView(ob, 'index.html', request)
        self.assertEqual(v(), 'V1 here')
        v = getView(ob, 'action.html', request)
        self.assertEqual(v(), 'done')

    def testPageViewsWithName(self):
        self.assertEqual(queryView(ob, 'test', request),
                         None)

        xmlconfig(StringIO(template %
            """
            <browser:view name="test"
                  factory="Zope.ComponentArchitecture.tests.TestViews.V1"
                  for="Zope.ComponentArchitecture.tests.TestViews.IC">

                <browser:page name="index.html" attribute="index" /> 
                <browser:page name="action.html" attribute="action" /> 
            </browser:view>
            """
            ))

        v = getView(ob, 'index.html', request)
        self.assertEqual(v(), 'V1 here')
        v = getView(ob, 'action.html', request)
        self.assertEqual(v(), 'done')
        v = getView(ob, 'test', request)
        self.assertEqual(v.index(), 'V1 here')
        self.assertEqual(v.action(), 'done')
    

    def testProtectedPageViews(self):
        self.assertEqual(queryView(ob, 'test', request),
                         None)

        xmlconfig(StringIO(template %
            """
            <directives namespace="http://namespaces.zope.org/security">
              <directive name="permission"
                 attributes="id title description"
                 handler="Zope.App.Security.metaConfigure.definePermission" />
            </directives>

            <security:permission id="XXX" title="xxx" />

            <browser:view
                  factory="Zope.ComponentArchitecture.tests.TestViews.V1"
                  for="Zope.ComponentArchitecture.tests.TestViews.IC" 
                  permission="XXX">

                <browser:page name="index.html" attribute="index" /> 
                <browser:page name="action.html" attribute="action"
                              permission="Zope.Public" /> 
            </browser:view>
            """
            ))

        # Need to "log someone in" to turn on checks
        from Zope.Security.SecurityManagement import newSecurityManager
        newSecurityManager('someuser')

        v = getView(ob, 'index.html', request)
        self.assertRaises(Exception, v)
        v = getView(ob, 'action.html', request)
        self.assertEqual(v(), 'done')

    def testSkinnedPageView(self):
        self.assertEqual(queryView(ob, 'test', request), None)

        xmlconfig(StringIO(template %
            """
            <browser:skin name="skinny" layers="layer default" />
            <browser:view
                  factory="Zope.ComponentArchitecture.tests.TestViews.V1">

                <browser:page name="index.html" attribute="index" /> 
                <browser:page name="index.html" attribute="action"
                              layer="layer"/> 
            </browser:view>
            """
            ))

        v = getView(ob, 'index.html', request)
        self.assertEqual(v(), 'V1 here')
        v = getView(ob, 'index.html',
                    Request(IBrowserPresentation, "skinny"))
        self.assertEqual(v(), 'done')

    def testPageResource(self):
        self.assertEqual(queryResource(ob, 'test', request), None)

        xmlconfig(StringIO(template %
            """
            <browser:resource
                  factory="Zope.ComponentArchitecture.tests.TestViews.R1">

                <browser:page name="index.html" attribute="index" /> 
                <browser:page name="action.html" attribute="action" /> 
            </browser:resource>
            """
            ))

        v = getResource(ob, 'index.html', request)
        self.assertEqual(v(), 'R1 here')
        v = getResource(ob, 'action.html', request)
        self.assertEqual(v(), 'R done')

    def testtemplate(self):
        path = os.path.join(os.path.split(defs_path)[0], 'tests', 'test.pt')
        
        self.assertEqual(queryView(ob, 'test', request),
                         None)

        xmlconfig(StringIO(template %
            """
            <browser:view
                  name="index.html"
                  template="%s"
                  for="Zope.ComponentArchitecture.tests.TestViews.IC" />
            """ % path
            ))

        v = getView(ob, 'index.html', request)
        self.assertEqual(v().strip(), '<html><body><p>test</p></body></html>')

    def testProtectedtemplate(self):
        path = os.path.join(os.path.split(defs_path)[0], 'tests', 'test.pt')
        
        self.assertEqual(queryView(ob, 'test', request),
                         None)

        xmlconfig(StringIO(template %
            """
            <directives namespace="http://namespaces.zope.org/security">
              <directive name="permission"
                 attributes="id title description"
                 handler="Zope.App.Security.metaConfigure.definePermission" />
            </directives>

            <security:permission id="XXX" title="xxx" />

            <browser:view
                  name="xxx.html"
                  template="%s"
                  permission="XXX"
                  for="Zope.ComponentArchitecture.tests.TestViews.IC" />
            """ % path
            ))

        xmlconfig(StringIO(template %
            """
            <browser:view
                  name="index.html"
                  template="%s"
                  permission="Zope.Public"
                  for="Zope.ComponentArchitecture.tests.TestViews.IC" />
            """ % path
            ))

        # Need to "log someone in" to turn on checks
        from Zope.Security.SecurityManagement import newSecurityManager
        newSecurityManager('someuser')
        
        v = getView(ob, 'xxx.html', request)
        self.assertRaises(Exception, v)

        v = getView(ob, 'index.html', request)
        self.assertEqual(v().strip(), '<html><body><p>test</p></body></html>')
        

    def testtemplateNoName(self):
        path = os.path.join(os.path.split(defs_path)[0], 'tests', 'test.pt')
        self.assertRaises(
            ConfigurationError,
            xmlconfig,
            StringIO(template %
            """
            <browser:view
                  template="%s"
                  for="Zope.ComponentArchitecture.tests.TestViews.IC" 
                  /> 
            """ % path
            ))

    def testtemplateAndFactory(self):
        path = os.path.join(os.path.split(defs_path)[0], 'tests', 'test.pt')
        self.assertRaises(
            ConfigurationError,
            xmlconfig,
            StringIO(template %
            """
            <browser:view
                  name="index.html"
                  template="%s"
                  for="Zope.ComponentArchitecture.tests.TestViews.IC" 
                  factory="Zope.ComponentArchitecture.tests.TestViews.V1">
                  /> 
            """ % path
            ))

    def testtemplateAndPage(self):
        path = os.path.join(os.path.split(defs_path)[0], 'tests', 'test.pt')
        self.assertRaises(
            ConfigurationError,
            xmlconfig,
            StringIO(template %
            """
            <browser:view
                  name="index.html"
                  template="%s"
                  for="Zope.ComponentArchitecture.tests.TestViews.IC" 
                  >
               <browser:page name="foo.html" attribute="index" />
            </browser:view>
            """ % path
            ))


    
def test_suite():
    loader=unittest.TestLoader()
    return loader.loadTestsFromTestCase(Test)

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