[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/ContentDirective/tests - ExampleClass.py:1.1.2.1 TestModuleHookup.py:1.1.2.1 __init__.py:1.1.2.1 testDirectives.py:1.1.2.1 testFactory.py:1.1.2.1 testRequirePermissions.py:1.1.2.1

Steve Alexander steve@cat-box.net
Mon, 3 Jun 2002 16:01:14 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/ContentDirective/tests
In directory cvs.zope.org:/tmp/cvs-serv23405/lib/python/Zope/App/ContentDirective/tests

Added Files:
      Tag: Zope3InWonderland-branch
	ExampleClass.py TestModuleHookup.py __init__.py 
	testDirectives.py testFactory.py testRequirePermissions.py 
Log Message:
added content directive.


=== Added File Zope3/lib/python/Zope/App/ContentDirective/tests/ExampleClass.py ===
from Interface import Interface

class ExampleClass:
    pass
    
class IExample(Interface):
    pass

class IExampleContainer(Interface):
    pass


=== Added File Zope3/lib/python/Zope/App/ContentDirective/tests/TestModuleHookup.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 Interface import Interface

PREFIX = "Zope.App.Security.tests.TestModule."
import Zope.App.Security.tests.TestModule as TestModule
TestModule.test_class = None
class I(Interface):
    def m1():
        pass
    def m2():
        pass

class I2(I):
    def m4():
        pass
    

TestModule.I = I
TestModule.I2 = I2

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


=== Added File Zope3/lib/python/Zope/App/ContentDirective/tests/__init__.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.
# 
##############################################################################
""" Zope.App.ClassDirective unit tests """


=== Added File Zope3/lib/python/Zope/App/ContentDirective/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.
# 
##############################################################################
"""

$Id: testDirectives.py,v 1.1.2.1 2002/06/03 20:01:13 stevea Exp $
"""

import unittest, sys, os

from Zope.Configuration.xmlconfig import xmlconfig
from StringIO import StringIO
from Zope.Testing.CleanUp import CleanUp # Base class w registry cleanup
from Zope.Configuration.xmlconfig import ZopeXMLConfigurationError

# explicitly import ExampleClass and IExample using full paths
# so that they are the same objects as resolve will get.
from Zope.App.ContentDirective.tests.ExampleClass import ExampleClass, IExample

import Zope.App.ContentDirective
defs_path = os.path.join(
    os.path.split(Zope.App.ContentDirective.__file__)[0],
    'content-meta.zcml')

import Zope.App.Security
security_defs_path = os.path.join(
    os.path.split(Zope.App.Security.__file__)[0],
    'security-meta.zcml')


def configfile(s):
    return StringIO("""<zopeConfigure
      xmlns='http://namespaces.zope.org/zope'
      xmlns:security='http://namespaces.zope.org/security'
      xmlns:zmi='http://namespaces.zope.org/zmi'>
      %s
      </zopeConfigure>
      """ % s)

class TestContentDirective(CleanUp, unittest.TestCase):
    def setUp(self):
        xmlconfig(open(defs_path))
        xmlconfig(open(security_defs_path))
        
        try:
            del ExampleClass.__implements__
        except AttributeError:
            pass
            
    def testEmptyDirective(self):
        f = configfile("""
<content class="Zope.App.ContentDirective.tests.ExampleClass.">
</content>
                       """)
        xmlconfig(f)

            
    def testImplements(self):
        f = configfile("""
<content class="Zope.App.ContentDirective.tests.ExampleClass.">
  <implements interface="Zope.App.ContentDirective.tests.ExampleClass.IExample" />
</content>
                       """)
        xmlconfig(f)
        self.failUnless(IExample.isImplementedByInstancesOf(ExampleClass))
        
        
    def testRequire(self):
        f = configfile("""
<security:permission id="Zope.View" title="Zope view permission" />
<content class="Zope.App.ContentDirective.tests.ExampleClass.">
    <security:require permission="Zope.View"
                      attributes="anAttribute anotherAttribute" />
</content>
                       """)
        xmlconfig(f)

    def testAllow(self):
        f = configfile("""
<content class="Zope.App.ContentDirective.tests.ExampleClass.">
    <security:allow attributes="anAttribute anotherAttribute" />
</content>
                       """)
        xmlconfig(f)
        
    def testMimic(self):
        f = configfile("""
<content class="Zope.App.ContentDirective.tests.ExampleClass.">
    <security:mimic class="Zope.App.ContentDirective.tests.ExampleClass." />
</content>
                       """)
        xmlconfig(f)
        
        
from Zope.App.OFS.Services.AddableService.tests.AddableSetup \
    import AddableSetup

class TestFactorySubdirective(AddableSetup, CleanUp, unittest.TestCase):
    def setUp(self):
        AddableSetup.setUp(self)
        xmlconfig(open(defs_path))
        xmlconfig(open(security_defs_path))

    def testFactory(self):
        f = configfile("""
<security:permission id="Zope.Foo" title="Zope Foo Permission" />

<content class="Zope.App.ContentDirective.tests.ExampleClass.">
    <zmi:factory 
      id="Example" 
      permission="Zope.Foo"
      title="Example content"
      description="Example description"
      for_container="Zope.App.ContentDirective.tests.ExampleClass."
      creation_markers="Zope.App.ContentDirective.tests.ExampleClass.IExample"
       />
</content>
                       """)
        xmlconfig(f)

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/lib/python/Zope/App/ContentDirective/tests/testFactory.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, sys, os

from cStringIO import StringIO
from Zope.Configuration.xmlconfig import xmlconfig, ZopeXMLConfigurationError
from Zope.Testing.CleanUp import CleanUp # Base class w registry cleanup
from Zope.App.Security.Exceptions import UndefinedPermissionError
from Zope.App.OFS.Services.AddableService.tests.AddableSetup \
    import AddableSetup

from Zope.App.ContentDirective.tests.ExampleClass \
    import ExampleClass, IExample, IExampleContainer

import Zope.App.ContentDirective
defs_path = os.path.join(
    os.path.split(Zope.App.ContentDirective.__file__)[0],
    'content-meta.zcml')

import Zope.App.Security
security_defs_path = os.path.join(
    os.path.split(Zope.App.Security.__file__)[0],
    'security-meta.zcml')


def configfile(s):
    return StringIO("""<zopeConfigure
      xmlns='http://namespaces.zope.org/zope'
      xmlns:security='http://namespaces.zope.org/security'
      xmlns:zmi='http://namespaces.zope.org/zmi'>
      %s
      </zopeConfigure>
      """ % s)

class Test(AddableSetup, CleanUp, unittest.TestCase):
    def setUp(self):
        AddableSetup.setUp(self)
        xmlconfig(open(defs_path))
        xmlconfig(open(security_defs_path))


    def testFactory(self):
        from Zope.ComponentArchitecture import getService
        from Zope.Proxy.ProxyIntrospection import removeAllProxies
        f = configfile("""
<security:permission id="Zope.Foo" title="Zope Foo Permission" />
<content class="Zope.App.ContentDirective.tests.ExampleClass.">
    <zmi:factory 
      id="Example" 
      permission="Zope.Foo"
      title="Example content"
      description="Example description"
      for_container="Zope.App.ContentDirective.tests.ExampleClass.IExampleContainer"
      creation_markers="Zope.App.ContentDirective.tests.ExampleClass.IExample"
       />
</content>
                       """)
        xmlconfig(f)
        obj = getService(None, "Factories").createObject(
            'Zope.App.ContentDirective.tests.ExampleClass.IExample')
        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/lib/python/Zope/App/ContentDirective/tests/testRequirePermissions.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 """

import unittest, sys, os

from Zope.App.Security import protectClass

# So we can use config parser to exercise protectClass stuff.
from cStringIO import StringIO
from Zope.Configuration.xmlconfig import xmlconfig, ZopeXMLConfigurationError
from TestModuleHookup import *
from Zope.Testing.CleanUp import CleanUp # Base class w registry cleanup
from Zope.App.Security.Exceptions import UndefinedPermissionError


import Zope.App.ContentDirective
defs_path = os.path.join(
    os.path.split(Zope.App.ContentDirective.__file__)[0],
    'content-meta.zcml')

import Zope.App.Security
security_defs_path = os.path.join(
    os.path.split(Zope.App.Security.__file__)[0],
    'security-meta.zcml')

def defineDirectives():
    xmlconfig(open(defs_path))
    xmlconfig(open(security_defs_path))
    xmlconfig(StringIO("""<zopeConfigure
        xmlns='http://namespaces.zope.org/zope' 
        xmlns:security='http://namespaces.zope.org/security'>
       <security:permission id="extravagant" title="extravagant" />
       <security:permission id="paltry" title="paltry" />
    </zopeConfigure>"""))

NOTSET = []

P1 = "extravagant"
P2 = "paltry"

class Test(CleanUp, unittest.TestCase):

    def setUp(self):
        defineDirectives()
        class B:
            def m1(self):
                return "m1"
            def m2(self):
                return "m2"
        class C(B):
            __implements__ = I
            def m3(self):
                return "m3"
            def m4(self):
                return "m4"
        TestModule.test_base = B
        TestModule.test_class = C
        TestModule.test_instance = C()
        self.assertState()

    def tearDown(self):
        CleanUp.tearDown(self)
        TestModule.test_class = None

    def assertState(self, instP=NOTSET,
                    m1P=NOTSET, m2P=NOTSET, m3P=NOTSET):
        "Verify that class, instance, and methods have expected permissions."

        from Zope.Security.Checker import selectChecker
        from Zope.Exceptions import Forbidden

        checker = selectChecker(TestModule.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(template_bracket % declaration)
        self.assertState(**state)

    def testClass(self):
        declaration = ("""<content class="%s">
                            <security:require permission="%s" />
                          </content>"""
                       % (PREFIX+"test_class", P1))
        self.assertDeclaration(declaration,
                               instP=P1)

    # "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">
                            <security:require
                                permission="%s" 
                                attributes="m1 m3"/>
                          </content>"""
                       % (PREFIX+"test_class", P1))
        self.assertDeclaration(declaration,
                               instP=P1, m1P=P1, m3P=P1)

    def testSimpleInterface(self):
        declaration = ("""<content class="%s">
                            <security: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,
                               instP=P1, m1P=P1, m2P=P1)

    # "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">
                            <security:require
                                attributes="m1"/>
                          </content>"""
                       % (PREFIX+"test_class"))
        self.assertRaises(ZopeXMLConfigurationError,
                          self.assertDeclaration,
                          declaration)



    def testCompositeMethodsPluralElementPerm(self):
        declaration = ("""<content class="%s">
                            <security: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">
                            <security: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">
                            <security: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,
                               instP=P1, m1P=P1, m2P=P1)


    def testMimicOnly(self):
        declaration = ("""<content class="%s">
                            <security:require
                                permission="%s"
                                attributes="m1 m2"/>
                          </content>
                          <content class="%s">
                            <security:mimic 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">
                            <security:require
                                permission="%s"
                                attributes="m1 m2"/>
                          </content>
                          <content class="%s">
                            <security:mimic class="%s" />
                            <security: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())