[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Security/tests - testProtectClass.py:1.1.2.14.2.1 testPublicClass.py:1.1.2.14.2.1 testSecurityDirectives.py:1.1.2.15.2.1

Jim Fulton jim@zope.com
Mon, 3 Jun 2002 13:15:57 -0400


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

Modified Files:
      Tag: Zope3InWonderland-branch
	testProtectClass.py testPublicClass.py 
	testSecurityDirectives.py 
Log Message:
- Attribute renaming.

  In directives that define things, renamed thing_id to id. For
  example:

    <permission permission_id='xxx' ...

  became:

    <permission id='xxx' ...

  In directives that used things defined in this way, removed the id
  suffix. For example:

     <view permission_id='xxx' ...

  became:

     <view permission='xxx' ...

- Changed the way that exceptions from configuration files are
  reported. Went back to normal Python tracebacks followed by
  "configuration tracebacks". The configuration tracebacks look
  somewhat similar to Python tracebacks, with file location
  information. The most specific configuration file location is at the
  end of the traceback, followed by the original error type and
  value. 

- Added a testxmlconfig function to the xmlconfig module to support
  unit testing. This function suppresses usual configuration error
  generation so that the original error is raised. This is needed so
  that unit tests can detect that proper low-level errors are raised. 

Note that everyone will need to edit their principals.zcml files to
reflect these changes!



=== Zope3/lib/python/Zope/App/Security/tests/testProtectClass.py 1.1.2.14 => 1.1.2.14.2.1 ===
 """ Test handler for 'protectClass' directive """
 
-import unittest, sys
+import unittest, sys, os
 
 from Zope.App.Security import protectClass
 
@@ -24,26 +24,11 @@
 from Zope.Testing.CleanUp import CleanUp # Base class w registry cleanup
 from Zope.App.Security.Exceptions import UndefinedPermissionError
 
-def defineDirectives():
-    xmlconfig(StringIO("""
-    <zopeConfigure xmlns='http://namespaces.zope.org/zope'
-                   xmlns:security='http://namespaces.zope.org/security'>
-       <directive namespace="http://namespaces.zope.org/security"
-               name="permission"
-               attributes="permission_id, title, description"
-               handler="Zope.App.Security.metaConfigure.definePermission" />
-       <directive namespace="http://namespaces.zope.org/security"
-          name="protectClass"
-          attributes="class, permission, interface, methods, like_unto"
-          handler="Zope.App.Security.protectClass.">
-          <subdirective namespace="http://namespaces.zope.org/security"
-                        name="protect"
-                        attributes="permission, interface, methods, like_unto"
-                        />
-       </directive>
-       <security:permission permission_id="extravagant" title="extravagant" />
-       <security:permission permission_id="paltry" title="paltry" />
-    </zopeConfigure>"""))
+
+import Zope.App.Security
+defs_path = os.path.join(
+    os.path.split(Zope.App.Security.__file__)[0],
+    'security-meta.zcml')
 
 NOTSET = []
 
@@ -53,7 +38,13 @@
 class Test(CleanUp, unittest.TestCase):
 
     def setUp(self):
-        defineDirectives()
+        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>"""))
         class B:
             def m1(self):
                 return "m1"
@@ -92,7 +83,7 @@
 
     def testClass(self):
         declaration = ("""<security:protectClass 
-                              class="%s" permission_id="%s" />"""
+                              class="%s" permission="%s" />"""
                        % (PREFIX+"test_class", P1))
         self.assertDeclaration(declaration,
                                instP=P1)
@@ -102,7 +93,7 @@
 
     def testSimpleMethodsPlural(self):
         declaration = ("""<security:protectClass 
-                              class="%s" permission_id="%s"
+                              class="%s" permission="%s"
                               names="m1, m3" />"""
                        % (PREFIX+"test_class", P1))
         self.assertDeclaration(declaration,
@@ -110,7 +101,7 @@
 
     def testSimpleInterface(self):
         declaration = ("""<security:protectClass 
-                              class="%s" permission_id="%s" interface="%s" />"""
+                              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,
@@ -131,7 +122,7 @@
                           declaration)
         # Permission not in top tag and in one subtag but not in the other:
         declaration = ("""<security:protectClass class="%s">
-                               <security:protect permission_id="%s"
+                               <security:protect permission="%s"
                                 names="m1"/>
                                <security:protect
                                 names="m2"/>
@@ -141,7 +132,7 @@
                           self.assertDeclaration, declaration, m1P=P1)
 
     def testCompositeMethodTopPerm(self):
-        declaration = ("""<security:protectClass class="%s" permission_id="%s">
+        declaration = ("""<security:protectClass class="%s" permission="%s">
                             <security:protect names="m1"/>
                           </security:protectClass>"""
                        % (PREFIX+"test_class", P1))
@@ -150,14 +141,14 @@
 
     def testCompositeMethodElementPerm(self):
         declaration = ("""<security:protectClass class="%s">
-                            <security:protect permission_id="%s" names="m1"/>
+                            <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_id="%s">
+        declaration = ("""<security:protectClass class="%s" permission="%s">
                             <security:protect names="m1, m2"/>
                           </security:protectClass>"""
                        % (PREFIX+"test_class", P1))
@@ -166,7 +157,7 @@
 
     def testCompositeMethodsPluralElementPerm(self):
         declaration = ("""<security:protectClass class="%s">
-                            <security:protect permission_id="%s"
+                            <security:protect permission="%s"
                                               names="m1, m3"/>
                           </security:protectClass>"""
                        % (PREFIX+"test_class", P1))
@@ -174,7 +165,7 @@
                                m1P=P1, m3P=P1)
 
     def testCompositeInterfaceTopPerm(self):
-        declaration = ("""<security:protectClass class="%s" permission_id="%s">
+        declaration = ("""<security:protectClass class="%s" permission="%s">
                             <security:protect interface="%s"/>
                           </security:protectClass>"""
                        % (PREFIX+"test_class", P1, PREFIX+"I"))
@@ -183,7 +174,7 @@
 
     def testCompositeInterfaceElementPerm(self):
         declaration = ("""<security:protectClass class="%s">
-                            <security:protect permission_id="%s"
+                            <security:protect permission="%s"
                             interface="%s"/>
                           </security:protectClass>"""
                        % (PREFIX+"test_class", P1, PREFIX+"I"))
@@ -191,7 +182,7 @@
                                m1P=P1, m2P=P1)
 
     def testCompositeInstancesTopPerm(self):
-        declaration = ("""<security:protectClass class="%s" permission_id="%s">
+        declaration = ("""<security:protectClass class="%s" permission="%s">
                           </security:protectClass>"""
                        % (PREFIX+"test_class", P1))
         self.assertDeclaration(declaration,
@@ -199,7 +190,7 @@
 
     def testSubInterfaces(self):
         declaration = ("""<security:protectClass 
-                              class="%s" permission_id="%s"
+                              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:
@@ -209,7 +200,7 @@
 
     def testLikeUntoOnly(self):
         declaration = ("""
-        <security:protectClass class="%s" names="m1,m2" permission_id="%s" />
+        <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"))
@@ -220,9 +211,9 @@
 
     def testLikeUntoAsDefault(self):
         declaration = ("""
-        <security:protectClass class="%s" names="m1,m2" permission_id="%s" />
+        <security:protectClass class="%s" names="m1,m2" permission="%s" />
         <security:protectClass class="%s" like_unto="%s"
-            names="m2,m3" permission_id="%s"/>
+            names="m2,m3" permission="%s"/>
         """  % (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:
@@ -233,7 +224,7 @@
 
 def apply_declaration(declaration):
     """Apply the xmlconfig machinery."""
-    return xmlconfig(StringIO(declaration))
+    return xmlconfig(StringIO(declaration), testing=1)
 
 def test_suite():
     loader=unittest.TestLoader()


=== Zope3/lib/python/Zope/App/Security/tests/testPublicClass.py 1.1.2.14 => 1.1.2.14.2.1 ===
         declaration = (template_bracket
                        % """<security:publicClass class="%s"
-                               permission_id="X"/>""")
+                               permission="X"/>""")
         self.assertRaises(InvalidDirective,
                           self.assertDeclaration,
                           declaration)
@@ -119,7 +119,7 @@
 
 def apply_declaration(declaration):
     """Apply the xmlconfig machinery."""
-    return xmlconfig(StringIO(declaration))
+    return xmlconfig(StringIO(declaration), testing=1)
 
 def test_suite():
     loader=unittest.TestLoader()


=== Zope3/lib/python/Zope/App/Security/tests/testSecurityDirectives.py 1.1.2.15 => 1.1.2.15.2.1 ===
 """
 
-import unittest, sys
+import unittest, sys, os
 
 from Zope.Configuration.xmlconfig import xmlconfig
 from StringIO import StringIO
@@ -34,6 +34,12 @@
     import principalRoleManager as principal_role_mgr
 from Zope.App.Security.Settings import Allow, Deny, Unset, Remove, Assign
 
+
+import Zope.App.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'
@@ -42,76 +48,16 @@
       </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="class, permission, interface, methods"
-             handler="Zope.App.Security.protectClass.">
-    <subdirective  namespace="http://namespaces.zope.org/security"
-                   name="protect"
-                   attributes="permission, interface, methods" />
-    <subdirective  namespace="http://namespaces.zope.org/security"
-                   name="instances"
-                   attributes="permission" />
-    </directive>
-  <directive namespace="http://namespaces.zope.org/security"
-             name="publicClass"
-             attributes="class, interface, 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(CleanUp, unittest.TestCase):
     def setUp(self):
-        metaConfigure()
+        xmlconfig(open(defs_path))
 
     def testRegister(self):
-        f = configfile("""<security:principal principal_id="1"
+        f = configfile("""<security:principal id="1"
                              title="Sir Tim Peters"
                              description="Tim Peters"
                              login="tim" password="123" />
-                          <security:principal principal_id="2"
+                          <security:principal id="2"
                              title="Sir Jim Fulton"
                              description="Jim Fulton"
                              login="jim" password="123" />""")
@@ -133,12 +79,12 @@
 
 class TestPermissionDirective(CleanUp, unittest.TestCase):
     def setUp(self):
-        metaConfigure()
+        xmlconfig(open(defs_path))
 
     def testRegister(self):
         f = configfile("""
- <security:definePermission
-     permission_id="Can Do It"
+ <security:permission
+     id="Can Do It"
      title="A Permissive Permission"
      description="This permission lets you do anything" />""")
 
@@ -152,13 +98,13 @@
 
     def testDuplicationRegistration(self):
         f = configfile("""
- <security:definePermission
-     permission_id="Can Do It"
+ <security:permission
+     id="Can Do It"
      title="A Permissive Permission"
      description="This permission lets you do anything" />
 
- <security:definePermission
-     permission_id="Can Do It"
+ <security:permission
+     id="Can Do It"
      title="A Permissive Permission"
      description="This permission lets you do anything" />
      """)
@@ -168,12 +114,12 @@
         
 class TestRoleDirective(CleanUp, unittest.TestCase):
     def setUp(self):
-        metaConfigure()
+        xmlconfig(open(defs_path))
 
     def testRegister(self):
         f = configfile("""
- <security:defineRole
-     role_id="Everyperson"
+ <security:role
+     id="Everyperson"
      title="Tout le monde"
      description="The common man, woman, person, or thing" />
      """)
@@ -188,13 +134,13 @@
         
     def testDuplicationRegistration(self):
         f = configfile("""
- <security:defineRole
-     role_id="Everyperson"
+ <security:role
+     id="Everyperson"
      title="Tout le monde"
      description="The common man, woman, person, or thing" />
 
- <security:defineRole
-     role_id="Everyperson"
+ <security:role
+     id="Everyperson"
      title="Tout le monde"
      description="The common man, woman, person, or thing" />
      """)
@@ -205,13 +151,13 @@
 class TestRolePermission(CleanUp, unittest.TestCase):
 
     def setUp( self ):
-        metaConfigure()
+        xmlconfig(open(defs_path))
 
     def testMap( self ):
         f = configfile("""
  <security:grantPermissionToRole
-     permission_id="Foo"
-     role_id="Bar" />
+     permission="Foo"
+     role="Bar" />
      """)
 
         xmlconfig(f)
@@ -228,13 +174,13 @@
 class TestPrincipalPermission(CleanUp, unittest.TestCase):
 
     def setUp( self ):
-        metaConfigure()
+        xmlconfig(open(defs_path))
 
     def testMap( self ):
         f = configfile("""
  <security:grantPermissionToPrincipal
-     permission_id="Foo"
-     principal_id="Bar" />
+     permission="Foo"
+     principal="Bar" />
      """)
 
         xmlconfig(f)
@@ -251,13 +197,13 @@
 class TestPrincipalRole(CleanUp, unittest.TestCase):
 
     def setUp( self ):
-        metaConfigure()
+        xmlconfig(open(defs_path))
 
     def testMap( self ):
         f = configfile("""
  <security:assignRoleToPrincipal
-     role_id="Foo"
-     principal_id="Bar" />
+     role="Foo"
+     principal="Bar" />
      """)
 
         xmlconfig(f)