[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Security/tests - testProtectClass.py:1.1.2.3 testPublicClass.py:1.1.2.4 testSecurityDirectives.py:1.1.2.6

Jim Fulton jim@zope.com
Thu, 3 Jan 2002 14:29:54 -0500


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

Modified Files:
      Tag: Zope-3x-branch
	testProtectClass.py testPublicClass.py 
	testSecurityDirectives.py 
Log Message:
Refactored configuration framework:

- Configuration directives must be written to a 
  a different framework. See
  ConfigurationDirectiveInterfaces.

- Configuration directives now don't take actions immediately.
  Instead, they return a sequence of discriminators and callables
  objects with arguments.  This allows configuration to be defered to
  allow overriding and conflct detection.

- Can now detect conflicting directives

- Can override directives. Directives in including configuration files
  override directives in included files. Conflicting directives are
  decided based on discriminators.

- Added new directives for defining directives. All directives, except
  for a few bootstrap irectives, are now configurable in the
  configuration file. This makes directives a little more discoverable
  and facilitates extension of directives.


=== Zope3/lib/python/Zope/App/Security/tests/testProtectClass.py 1.1.2.2 => 1.1.2.3 ===
 
 from Zope.App.Security import protectClass
-from Zope.App.Security.metaConfigure import metaConfigure
-metaConfigure()
 
 # So we can use config parser to exercise protectClass stuff.
 from cStringIO import StringIO
 from Zope.Configuration.xmlconfig import xmlconfig, ZopeXMLConfigurationError
 from testmodulehookup import *
 
+xmlconfig(StringIO("""
+<zopeConfigure xmlns='http://namespaces.zope.org/zope'>
+  <directive namespace="http://namespaces.zope.org/security"
+             name="protectClass"
+             attributes="name, permission, interface, method, methods"
+             handler="Zope.App.Security.protectClass">
+    <subdirective  namespace="http://namespaces.zope.org/security"
+                   name="protect"
+                   attributes="permission, interface, method, methods" />
+    <subdirective  namespace="http://namespaces.zope.org/security"
+                   name="instances"
+                   attributes="permission" />
+    </directive>
+</zopeConfigure>
+"""))
+
 NOTSET = []
 
 P1 = "extravagant"
@@ -46,13 +60,17 @@
         "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)
+        self.assertEqual(
+            (
+            getattr(instance, "__permission__", NOTSET),
+            getattr(tclass.m1, "__permission__", NOTSET),
+            getattr(tclass.m2, "__permission__", NOTSET),
+            getattr(tclass.m3, "__permission__", NOTSET),
+            getattr(instance.m1, "__permission__", NOTSET),
+            getattr(instance.m2, "__permission__", NOTSET),
+            getattr(instance.m3, "__permission__", NOTSET),
+            ),
+            (instP, m1P, m2P, m3P, m1P, m2P, m3P))
 
     def assertDeclaration(self, declaration, **state):
         apply_declaration(template_bracket % declaration)
@@ -70,8 +88,8 @@
 
     def testSimpleNoPerm(self):
         """Establish rejection of declarations lacking a permission spec."""
-        declaration = (template_bracket
-                       % """<security:protectClass name="%s" />""")
+        declaration = ("""<security:protectClass name="%s" />"""
+                       % (PREFIX+"test_class"))
         self.assertRaises(ZopeXMLConfigurationError,
                           self.assertDeclaration,
                           declaration)
@@ -104,21 +122,19 @@
 
     def testCompositeNoPerm(self):
         """Establish rejection of declarations lacking a permission spec."""
-        declaration = (template_bracket
-                       % ("""<security:protectClass name="%s">
+        declaration = ("""<security:protectClass name="%s">
                                <security:protect method="m1"/>
                              </security:protectClass>"""
-                          % (PREFIX+"test_class")))
+                       % (PREFIX+"test_class"))
         self.assertRaises(ZopeXMLConfigurationError,
                           self.assertDeclaration,
                           declaration)
         # Permission not in top tag and in one subtag but not in the other:
-        declaration = (template_bracket
-                       % ("""<security:protectClass name="%s">
+        declaration = ("""<security:protectClass name="%s">
                                <security:protect permission="%s" method="m1"/>
                                <security:instances/>
                              </security:protectClass>"""
-                          % (PREFIX+"test_class", P1)))
+                          % (PREFIX+"test_class", P1))
         self.assertRaises(ZopeXMLConfigurationError,
                           self.assertDeclaration,
                           declaration)


=== Zope3/lib/python/Zope/App/Security/tests/testPublicClass.py 1.1.2.3 => 1.1.2.4 ===
 
 from Zope.App.Security import publicClass
-from Zope.App.Security.metaConfigure import metaConfigure
 from Zope.Configuration.meta import _clear as metaclear
 
 # So we can use config parser to exercise publicClass stuff.
@@ -30,7 +29,14 @@
 class Test(unittest.TestCase):
 
     def setUp(self):
-        metaConfigure()
+        xmlconfig(StringIO("""
+        <zopeConfigure xmlns='http://namespaces.zope.org/zope'>
+        <directive name="publicClass"
+        attributes="name, interface, method, methods"
+        namespace="http://namespaces.zope.org/security"
+        handler="Zope.App.Security.publicClass.publicClass" />
+        </zopeConfigure>
+        """))
         class C:
             __implements__ = I
             def m1(self):


=== Zope3/lib/python/Zope/App/Security/tests/testSecurityDirectives.py 1.1.2.5 => 1.1.2.6 ===
 from Zope.App.Security.PermissionRegistry import permissionRegistry as pregistry
 from Zope.App.Security.RoleRegistry import roleRegistry as rregistry
-from Zope.App.Security.metaConfigure import metaConfigure
 from Zope.App.Security.RolePermissionManager import rolePermissionManager as role_perm_mgr
 from Zope.App.Security.PrincipalPermissionManager \
     import principalPermissionManager as principal_perm_mgr
@@ -32,6 +31,65 @@
       %s
       </zopeConfigure>
       """ % s)
+
+def metaConfigure():
+    xmlconfig(StringIO("""
+    <zopeConfigure xmlns='http://namespaces.zope.org/zope'>
+
+  <!-- Zope.App.Security -->
+  <directive namespace="http://namespaces.zope.org/security"
+             name="definePermission"
+             attributes="permission, title, description"
+             handler="Zope.App.Security.metaConfigure.definePermission" />
+  <directive namespace="http://namespaces.zope.org/security"
+             name="defineRole"
+             attributes="role, title, description"
+             handler="Zope.App.Security.metaConfigure.defineRole" />
+  <directive namespace="http://namespaces.zope.org/security"
+             name="protectClass"
+             attributes="name, permission, interface, method, methods"
+             handler="Zope.App.Security.protectClass">
+    <subdirective  namespace="http://namespaces.zope.org/security"
+                   name="protect"
+                   attributes="permission, interface, method, methods" />
+    <subdirective  namespace="http://namespaces.zope.org/security"
+                   name="instances"
+                   attributes="permission" />
+    </directive>
+  <directive namespace="http://namespaces.zope.org/security"
+             name="publicClass"
+             attributes="name, interface, method, methods"
+             handler="Zope.App.Security.publicClass" />
+  <directive namespace="http://namespaces.zope.org/security"
+             name="defaultPolicy"
+             attributes="name"
+             handler="Zope.App.Security.metaConfigure.defaultPolicy" />
+  <directive namespace="http://namespaces.zope.org/security"
+             name="principal"
+             attributes="principal, title, description"
+             handler="Zope.App.Security.metaConfigure.principal" />
+  <directive namespace="http://namespaces.zope.org/security"
+             name="defaultPrincipal"
+             attributes="principal, title, description"
+             handler="Zope.App.Security.metaConfigure.defaultPrincipal" />
+  <directive 
+     namespace="http://namespaces.zope.org/security"
+     name="grantPermissionToRole"
+     attributes="permission, role"
+     handler="Zope.App.Security.metaConfigure.grantPermissionToRole" />
+  <directive
+     namespace="http://namespaces.zope.org/security"
+     name="grantPermissionToPrincipal"
+     attributes="permission, principal"
+     handler="Zope.App.Security.metaConfigure.grantPermissionToPrincipal"
+     />
+  <directive
+     namespace="http://namespaces.zope.org/security"
+     name="assignRoleToPrincipal"
+     attributes="role, principal"
+     handler="Zope.App.Security.metaConfigure.assignRoleToPrincipal" />
+    </zopeConfigure>
+    """))
 
 
 class TestPrincipalDirective(unittest.TestCase):