[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Security/tests - testPublicClass.py:1.1.2.1

Ken Manheimer klm@zope.com
Fri, 30 Nov 2001 17:14:23 -0500


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

Added Files:
      Tag: Zope-3x-branch
	testPublicClass.py 
Log Message:
Very similar to testProtectClass, but only simple (empty) tags, and
explicit permissions should not be allowed.


=== Added File Zope3/lib/python/Zope/App/Security/tests/testPublicClass.py ===
# testPublicClass.py
#
# Copyright (c) 2001 Zope Coporation and Contributors.  All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 1.1 (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 'publicClass' directive """

import unittest, sys
from Interface import Interface

from Zope.App.Security import publicClass
from Zope.App.Security.metaConfigure import metaConfigure
metaConfigure()

# So we can use config parser to exercise publicClass stuff.
from cStringIO import StringIO
from Zope.Configuration.xmlconfig import xmlconfig, ZopeXMLConfigurationError
from testmodulehookup import *

NOTSET = []

PublicPermission = publicClass.PublicPermission

class Test(unittest.TestCase):

    def setUp(self):
        class C:
            __implements__ = I
            def m1(self):
                return "m1"
            def m2(self):
                return "m2"
            def m3(self):
                return "m3"
        testmodule.test_class = C
        testmodule.test_instance = C()
        self.assertState()

    def 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."

        tclass, instance = testmodule.test_class, testmodule.test_instance
        self.assertEqual(getattr(instance, "__permission__", NOTSET), instP)
        self.assertEqual(getattr(tclass.m1, "__permission__", NOTSET), m1P)
        self.assertEqual(getattr(tclass.m2, "__permission__", NOTSET), m2P)
        self.assertEqual(getattr(tclass.m3, "__permission__", NOTSET), m3P)
        self.assertEqual(getattr(instance.m1, "__permission__", NOTSET), m1P)
        self.assertEqual(getattr(instance.m2, "__permission__", NOTSET), m2P)
        self.assertEqual(getattr(instance.m3, "__permission__", NOTSET), m3P)

    def assertDeclaration(self, declaration, **state):
        apply_declaration(template_bracket % declaration)
        self.assertState(**state)

    def testClass(self):
        declaration = ("""<security:publicClass name="%s" />"""
                       % (PREFIX+"test_class"))
        self.assertDeclaration(declaration,
                               instP=PublicPermission)

    def testInvalidPerm(self):
        """Establish rejection of declarations having a permission spec."""
        declaration = (template_bracket
                       % """<security:publicClass name="%s"
                               permission="X"/>""")
        self.assertRaises(ZopeXMLConfigurationError,
                          self.assertDeclaration,
                          declaration)

    def testMethod(self):
        declaration = ("""<security:publicClass name="%s" method="m1" />"""
                       % (PREFIX+"test_class"))
        self.assertDeclaration(declaration,
                               instP=PublicPermission, m1P=PublicPermission)

    def testMethodsPlural(self):
        declaration = ("""<security:publicClass 
                              name="%s" methods="m1, m3" />"""
                       % (PREFIX+"test_class"))
        self.assertDeclaration(declaration,
                               instP=PublicPermission, m1P=PublicPermission,
                               m3P=PublicPermission)

    def testInterface(self):
        declaration = ("""<security:publicClass name="%s" interface="%s" />"""
                       % (PREFIX+"test_class", PREFIX+"I"))
        # m1 and m2 are in the interface, so should be set, and m3 should not:
        self.assertDeclaration(declaration,
                               instP=PublicPermission, m1P=PublicPermission,
                               m2P=PublicPermission)

    # XXX test instances

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())