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

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


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

Modified Files:
      Tag: Zope3InWonderland-branch
	testProtectClass.py 
Log Message:
added content directive.


=== Zope3/lib/python/Zope/App/Security/tests/testProtectClass.py 1.1.2.14.2.2 => 1.1.2.14.2.3 ===
 """ Test handler for 'protectClass' directive """
 
-import unittest, sys, os
+import unittest
 
-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.App.Security.PermissionRegistry import permissionRegistry
 from Zope.Testing.CleanUp import CleanUp # Base class w registry cleanup
-from Zope.App.Security.Exceptions import UndefinedPermissionError
-
-
-import Zope.App.Security
-defs_path = os.path.join(
-    os.path.split(Zope.App.Security.__file__)[0],
-    'security-meta.zcml')
+from Zope.App.Security.protectClass import protectName, protectLikeUnto
 
 NOTSET = []
 
@@ -38,13 +28,9 @@
 class Test(CleanUp, unittest.TestCase):
 
     def setUp(self):
-        xmlconfig(open(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>"""))
+        permissionRegistry.definePermission(P1, P1)
+        permissionRegistry.definePermission(P2, P2)
+        
         class B:
             def m1(self):
                 return "m1"
@@ -77,155 +63,31 @@
         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 = ("""<security:protectClass 
-                              class="%s" permission="%s" />"""
-                       % (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 = ("""<security:protectClass 
-                              class="%s" permission="%s"
-                              names="m1 m3" />"""
-                       % (PREFIX+"test_class", P1))
-        self.assertDeclaration(declaration,
-                               instP=P1, m1P=P1, m3P=P1)
-
-    def testSimpleInterface(self):
-        declaration = ("""<security:protectClass 
-                              class="%s" permission="%s" interface="%s" />"""
-                       % (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 = ("""<security:protectClass class="%s">
-                               <security:protect names="m1"/>
-                             </security:protectClass>"""
-                       % (PREFIX+"test_class"))
-        self.assertRaises(protectClass.ProtectionDeclarationException,
-                          self.assertDeclaration,
-                          declaration)
-        # Permission not in top tag and in one subtag but not in the other:
-        declaration = ("""<security:protectClass class="%s">
-                               <security:protect permission="%s"
-                                names="m1"/>
-                               <security:protect
-                                names="m2"/>
-                             </security:protectClass>"""
-                          % (PREFIX+"test_class", P1))
-        self.assertRaises(protectClass.ProtectionDeclarationException,
-                          self.assertDeclaration, declaration, m1P=P1)
-
-    def testCompositeMethodTopPerm(self):
-        declaration = ("""<security:protectClass class="%s" permission="%s">
-                            <security:protect names="m1"/>
-                          </security:protectClass>"""
-                       % (PREFIX+"test_class", P1))
-        self.assertDeclaration(declaration,
-                               m1P=P1)
-
-    def testCompositeMethodElementPerm(self):
-        declaration = ("""<security:protectClass class="%s">
-                            <security:protect permission="%s" names="m1"/>
-                          </security:protectClass>"""
-                       % (PREFIX+"test_class", P1))
-        self.assertDeclaration(declaration,
-                               m1P=P1)
-
-    def testCompositeMethodsPluralTopPerm(self):
-        declaration = ("""<security:protectClass class="%s" permission="%s">
-                            <security:protect names="m1 m2"/>
-                          </security:protectClass>"""
-                       % (PREFIX+"test_class", P1))
-        self.assertDeclaration(declaration,
-                               m1P=P1, m2P=P1)
-
-    def testCompositeMethodsPluralElementPerm(self):
-        declaration = ("""<security:protectClass class="%s">
-                            <security:protect permission="%s"
-                                              names="m1 m3"/>
-                          </security:protectClass>"""
-                       % (PREFIX+"test_class", P1))
-        self.assertDeclaration(declaration,
-                               m1P=P1, m3P=P1)
-
-    def testCompositeInterfaceTopPerm(self):
-        declaration = ("""<security:protectClass class="%s" permission="%s">
-                            <security:protect interface="%s"/>
-                          </security:protectClass>"""
-                       % (PREFIX+"test_class", P1, PREFIX+"I"))
-        self.assertDeclaration(declaration,
-                               m1P=P1, m2P=P1)
-
-    def testCompositeInterfaceElementPerm(self):
-        declaration = ("""<security:protectClass class="%s">
-                            <security:protect permission="%s"
-                            interface="%s"/>
-                          </security:protectClass>"""
-                       % (PREFIX+"test_class", P1, PREFIX+"I"))
-        self.assertDeclaration(declaration,
-                               m1P=P1, m2P=P1)
-
-    def testCompositeInstancesTopPerm(self):
-        declaration = ("""<security:protectClass class="%s" permission="%s">
-                          </security:protectClass>"""
-                       % (PREFIX+"test_class", P1))
-        self.assertDeclaration(declaration,
-                               instP=P1)
-
-    def testSubInterfaces(self):
-        declaration = ("""<security:protectClass 
-                              class="%s" permission="%s"
-                              interface="%s" />"""
-                       % (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)
-
+        protectName(TestModule.test_class, 'm1', P1)
+        protectName(TestModule.test_class, 'm3', P1)
+        self.assertState(instP=P1, m1P=P1, m3P=P1)
 
     def testLikeUntoOnly(self):
-        declaration = ("""
-        <security:protectClass class="%s" names="m1 m2" permission="%s" />
-        <security:protectClass class="%s" like_unto="%s" />
-        """  % (PREFIX+"test_base", P1,
-                PREFIX+"test_class", PREFIX+"test_base"))
+        protectName(TestModule.test_base, 'm1', P1)
+        protectName(TestModule.test_base, 'm2', P1)
+        protectLikeUnto(TestModule.test_class, TestModule.test_base)
         # m1 and m2 are in the interface, so should be set, and m3 should not:
-        self.assertDeclaration(declaration,
-                               m1P=P1, m2P=P1)
+        self.assertState(m1P=P1, m2P=P1)
         
 
     def testLikeUntoAsDefault(self):
-        declaration = ("""
-        <security:protectClass class="%s" names="m1 m2" permission="%s" />
-        <security:protectClass class="%s" like_unto="%s"
-            names="m2 m3" permission="%s"/>
-        """  % (PREFIX+"test_base", P1,
-                PREFIX+"test_class", PREFIX+"test_base", P2))
+        protectName(TestModule.test_base, 'm1', P1)
+        protectName(TestModule.test_base, 'm2', P1)
+        protectLikeUnto(TestModule.test_class, TestModule.test_base)
+        protectName(TestModule.test_class, 'm2', P2)
+        protectName(TestModule.test_class, 'm3', 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)
+        self.assertState(m1P=P1, m2P=P2, m3P=P2)
         
-
-
-def apply_declaration(declaration):
-    """Apply the xmlconfig machinery."""
-    return xmlconfig(StringIO(declaration), testing=1)
-
 def test_suite():
     loader=unittest.TestLoader()
     return loader.loadTestsFromTestCase(Test)