[Zope3-checkins] CVS: Zope3/src/zope/app/publisher/xmlrpc/tests - __init__.py:1.1.2.1 test.pt:1.1.2.1 test_directives.py:1.1.2.1

Jim Fulton jim@zope.com
Mon, 23 Dec 2002 14:32:09 -0500


Update of /cvs-repository/Zope3/src/zope/app/publisher/xmlrpc/tests
In directory cvs.zope.org:/tmp/cvs-serv19908/zope/app/publisher/xmlrpc/tests

Added Files:
      Tag: NameGeddon-branch
	__init__.py test.pt test_directives.py 
Log Message:
Initial renaming before debugging

=== Added File Zope3/src/zope/app/publisher/xmlrpc/tests/__init__.py ===
#
# This file is necessary to make this directory a package.


=== Added File Zope3/src/zope/app/publisher/xmlrpc/tests/test.pt ===
<html><body><p>test</p></body></html>


=== Added File Zope3/src/zope/app/publisher/xmlrpc/tests/test_directives.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_directives.py,v 1.1.2.1 2002/12/23 19:32:08 jim Exp $
"""

import unittest

from zope.configuration.xmlconfig import xmlconfig, XMLConfig
from zope.configuration.exceptions import ConfigurationError
from zope.component.tests.test_views import IC, V1, VZMI, R1, RZMI
from zope.component import getView, queryView
from zope.component import getDefaultViewName
from zope.app.tests.placelesssetup import PlacelessSetup
from zope.security.proxy import ProxyFactory
from cStringIO import StringIO

from zope.component.tests.request import Request

from zope.publisher.interfaces.xmlrpc import IXMLRPCPresentation

import Zope.App.Publisher.XMLRPC

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

request = Request(IXMLRPCPresentation)

class Ob:
    __implements__ = IC

ob = Ob()

class Test(PlacelessSetup, unittest.TestCase):

    def setUp(self):
        PlacelessSetup.setUp(self)
        XMLConfig('meta.zcml', Zope.App.Publisher.XMLRPC)()

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

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

    def testInterfaceProtectedView(self):
        xmlconfig(StringIO(template %
            """
            <xmlrpc: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)
        v = ProxyFactory(v)
        self.assertEqual(v.index(), 'V1 here')
        self.assertRaises(Exception, getattr, v, 'action')


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

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


    def testInterfaceAndAttributeProtectedView(self):
        xmlconfig(StringIO(template %
            """
            <xmlrpc:view name="test"
                  factory="Zope.ComponentArchitecture.tests.TestViews.V1"
                  for="Zope.ComponentArchitecture.tests.TestViews.IC" 
                  permission="Zope.Public"
                  allowed_methods="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 %
            """
            <xmlrpc:view name="test"
                  factory="Zope.ComponentArchitecture.tests.TestViews.V1"
                  for="Zope.ComponentArchitecture.tests.TestViews.IC" 
                  permission="Zope.Public"
                  allowed_methods="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 %
            """
            <xmlrpc:view name="test"
                  factory="Zope.ComponentArchitecture.tests.TestViews.V1"
                  for="Zope.ComponentArchitecture.tests.TestViews.IC" 
                  allowed_methods="action index"
                  /> 
            """
            ))


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

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

                <xmlrpc:method name="index.html" attribute="index" /> 
                <xmlrpc:method name="action.html" attribute="action" /> 
            </xmlrpc:view>
            """
            ))

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


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

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

                <xmlrpc:method name="index.html" attribute="index" /> 
                <xmlrpc:method name="action.html" attribute="action" /> 
            </xmlrpc: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 testProtectedMethodViews(self):
        self.assertEqual(queryView(ob, 'test', request),
                         None)

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

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

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

                <xmlrpc:method name="index.html" attribute="index" /> 
                <xmlrpc:method name="action.html" attribute="action"
                              permission="Zope.Public" /> 
            </xmlrpc: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 test_suite():
    loader=unittest.TestLoader()
    return loader.loadTestsFromTestCase(Test)

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