[Checkins] SVN: grokcore.formlib/trunk/src/grokcore/formlib/ Fix ftests. The problem was that the forms were obviously trying to modify

Philipp von Weitershausen philikon at philikon.de
Fri Aug 1 08:21:03 EDT 2008


Log message for revision 89128:
  Fix ftests. The problem was that the forms were obviously trying to modify
  object's attributes.  Under Zope3's standard publication, this yields to 
  ForbiddenAttribute errors due to the lack of security declarations.  So just
  for the ftests I invented two directives and a grokker that allow us to
  make security declarations in the model class.
  
  Maybe this or something like this makes it into grokcore.security (and thereby
  Grok itself) soon.
  

Changed:
  U   grokcore.formlib/trunk/src/grokcore/formlib/ftesting.zcml
  U   grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/actions.py
  U   grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/addform.py
  U   grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/addform_applydata.py
  U   grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/editform_applydata.py
  U   grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/editform_applydata_schema.py
  U   grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/editform_applydata_schemafields.py
  U   grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/form.py
  U   grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/templateform.py
  U   grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/update.py
  U   grokcore.formlib/trunk/src/grokcore/formlib/testing.py

-=-
Modified: grokcore.formlib/trunk/src/grokcore/formlib/ftesting.zcml
===================================================================
--- grokcore.formlib/trunk/src/grokcore/formlib/ftesting.zcml	2008-08-01 12:17:44 UTC (rev 89127)
+++ grokcore.formlib/trunk/src/grokcore/formlib/ftesting.zcml	2008-08-01 12:21:01 UTC (rev 89128)
@@ -9,6 +9,7 @@
   <include package="zope.app.zcmlfiles" file="meta.zcml" />
   <include package="zope.securitypolicy" file="meta.zcml" />
   <include package="grokcore.formlib" file="meta.zcml" />
+  <grok:grok package="grokcore.formlib.testing" />
 
   <include package="zope.app.zcmlfiles" />
   <include package="zope.securitypolicy" />

Modified: grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/actions.py
===================================================================
--- grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/actions.py	2008-08-01 12:17:44 UTC (rev 89127)
+++ grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/actions.py	2008-08-01 12:21:01 UTC (rev 89128)
@@ -52,9 +52,11 @@
     name = schema.TextLine(title=u"Name")
     size = schema.TextLine(title=u"Size", default=u"Quite normal")
 
-class Mammoth(grok.Context):
+class Mammoth(grok.testing.Model):
     implements(IMammoth)
-    
+    grok.testing.protect_get(grok.Public, 'name', 'size')
+    grok.testing.protect_set(grok.Public, 'name', 'size')
+
     name = FieldProperty(IMammoth['name'])    
     size = FieldProperty(IMammoth['size'])    
 

Modified: grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/addform.py
===================================================================
--- grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/addform.py	2008-08-01 12:17:44 UTC (rev 89127)
+++ grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/addform.py	2008-08-01 12:21:01 UTC (rev 89128)
@@ -29,16 +29,20 @@
 from zope import schema
 from zope.interface import Interface, implements
 from zope.app.container.btree import BTreeContainer
+from zope.app.container.contained import Contained
+from zope.app.container.interfaces import IContainer
 
-class Zoo(BTreeContainer):
-    pass
+class Zoo(grok.testing.Model, BTreeContainer):
+    grok.testing.protect_get(grok.Public, *IContainer)
 
 class IMammoth(Interface):
     name = schema.TextLine(title=u"Name")
     size = schema.TextLine(title=u"Size", default=u"Quite normal")
 
-class Mammoth(grok.Context):
+class Mammoth(Contained, grok.testing.Model):
     implements(IMammoth)
+    grok.testing.protect_get(grok.Public, 'name', 'size')
+    grok.testing.protect_set(grok.Public, 'name', 'size')
 
     def __init__(self, name='', size=''):
         self.name = name

Modified: grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/addform_applydata.py
===================================================================
--- grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/addform_applydata.py	2008-08-01 12:17:44 UTC (rev 89127)
+++ grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/addform_applydata.py	2008-08-01 12:21:01 UTC (rev 89128)
@@ -31,16 +31,19 @@
 from zope import schema, interface
 from zope.lifecycleevent.interfaces import IObjectModifiedEvent
 from zope.app.container.btree import BTreeContainer
+from zope.app.container.interfaces import IContainer
 
-class Zoo(BTreeContainer):
-    pass
+class Zoo(grok.testing.Model, BTreeContainer):
+    grok.testing.protect_get(grok.Public, *IContainer)
 
 class IMammoth(interface.Interface):
     name = schema.TextLine(title=u"Name")
     size = schema.TextLine(title=u"Size")
 
-class Mammoth(grok.Context):
+class Mammoth(grok.testing.Model):
     grok.implements(IMammoth)
+    grok.testing.protect_get(grok.Public, 'name', 'size')
+    grok.testing.protect_set(grok.Public, 'name', 'size')
 
 class Index(grok.View):
     grok.context(Mammoth)

Modified: grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/editform_applydata.py
===================================================================
--- grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/editform_applydata.py	2008-08-01 12:17:44 UTC (rev 89127)
+++ grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/editform_applydata.py	2008-08-01 12:21:01 UTC (rev 89128)
@@ -55,7 +55,9 @@
 from zope import schema
 from zope.lifecycleevent.interfaces import IObjectModifiedEvent
 
-class Mammoth(grok.Context):
+class Mammoth(grok.testing.Model):
+    grok.testing.protect_get(grok.Public, 'name', 'size')
+    grok.testing.protect_set(grok.Public, 'name', 'size')
 
     def __init__(self, name='', size=''):
         self._name = name

Modified: grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/editform_applydata_schema.py
===================================================================
--- grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/editform_applydata_schema.py	2008-08-01 12:17:44 UTC (rev 89127)
+++ grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/editform_applydata_schema.py	2008-08-01 12:21:01 UTC (rev 89128)
@@ -62,8 +62,10 @@
     name = schema.TextLine(title=u"Name")
     size = schema.TextLine(title=u"Size")
 
-class Mammoth(grok.Context):
+class Mammoth(grok.testing.Model):
     grok.implements(IMammoth)
+    grok.testing.protect_get(grok.Public, 'name', 'size')
+    grok.testing.protect_set(grok.Public, 'name', 'size')
 
     def __init__(self, name='', size=''):
         self._name = name

Modified: grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/editform_applydata_schemafields.py
===================================================================
--- grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/editform_applydata_schemafields.py	2008-08-01 12:17:44 UTC (rev 89127)
+++ grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/editform_applydata_schemafields.py	2008-08-01 12:21:01 UTC (rev 89128)
@@ -62,8 +62,10 @@
     name = schema.TextLine(title=u"Name")
     size = schema.TextLine(title=u"Size", default=u"Quite normal")
 
-class Mammoth(grok.Context):
+class Mammoth(grok.testing.Model):
     implements(IMammoth)
+    grok.testing.protect_get(grok.Public, 'name', 'size')
+    grok.testing.protect_set(grok.Public, 'name', 'size')
     
 class Edit(grok.EditForm):
     pass

Modified: grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/form.py
===================================================================
--- grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/form.py	2008-08-01 12:17:44 UTC (rev 89127)
+++ grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/form.py	2008-08-01 12:21:01 UTC (rev 89128)
@@ -35,8 +35,10 @@
     name = schema.TextLine(title=u"Name")
     size = schema.TextLine(title=u"Size", default=u"Quite normal")
 
-class Mammoth(grok.Context):
+class Mammoth(grok.testing.Model):
     implements(IMammoth)
+    grok.testing.protect_get(grok.Public, 'name', 'size')
+    grok.testing.protect_set(grok.Public, 'name', 'size')
     
     name = FieldProperty(IMammoth['name'])    
     size = FieldProperty(IMammoth['size'])    

Modified: grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/templateform.py
===================================================================
--- grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/templateform.py	2008-08-01 12:17:44 UTC (rev 89127)
+++ grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/templateform.py	2008-08-01 12:21:01 UTC (rev 89128)
@@ -46,7 +46,10 @@
 import grokcore.formlib as grok
 from zope import schema
 
-class Mammoth(grok.Context):
+class Mammoth(grok.testing.Model):
+    grok.testing.protect_get(grok.Public, 'name', 'size')
+    grok.testing.protect_set(grok.Public, 'name', 'size')
+
     class fields:
         name = schema.TextLine(title=u"Name")
         size = schema.TextLine(title=u"Size", default=u"Quite normal")

Modified: grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/update.py
===================================================================
--- grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/update.py	2008-08-01 12:17:44 UTC (rev 89127)
+++ grokcore.formlib/trunk/src/grokcore/formlib/ftests/form/update.py	2008-08-01 12:21:01 UTC (rev 89128)
@@ -50,8 +50,10 @@
 class IMammoth(Interface):
     name = schema.TextLine(title=u"Name")
 
-class Mammoth(grok.Context):
+class Mammoth(grok.testing.Model):
     implements(IMammoth)
+    grok.testing.protect_get(grok.Public, 'name', 'report')
+    grok.testing.protect_set(grok.Public, 'name', 'report')
     
     name = u'Manfred'
 

Modified: grokcore.formlib/trunk/src/grokcore/formlib/testing.py
===================================================================
--- grokcore.formlib/trunk/src/grokcore/formlib/testing.py	2008-08-01 12:17:44 UTC (rev 89127)
+++ grokcore.formlib/trunk/src/grokcore/formlib/testing.py	2008-08-01 12:21:01 UTC (rev 89128)
@@ -13,9 +13,88 @@
 ##############################################################################
 """Grok test helpers
 """
+import martian
+from martian.error import GrokError
 from zope.configuration.config import ConfigurationMachine
+
+import grokcore.security
+from grokcore.security.util import protect_getattr
+from grokcore.security.util import protect_setattr
 from grokcore.component import zcml
+from grokcore.component import Context
 
+# Below is a simple grokker + directives that allow you to protect
+# attributes of a class with Zope 3 security checkers.  This is needed
+# to run the ftests of this package with the standard Zope publication.
+# We may hope that something like this eventually makes it into
+# grokcore.security.
+
+class Model(Context):
+    pass
+
+
+class protect_get(grokcore.security.require):
+    store = martian.MULTIPLE
+    default = []
+
+    def validate(self, permission, *attrs):
+        super(protect_get, self).validate(permission)
+        for name in attrs:
+            # TODO We should probably check whether 'name' is a valid
+            # Python identifier
+            martian.validateText(self, name)
+
+    def factory(self, permission, *attrs):
+        permission = super(protect_get, self).factory(permission)
+        return (permission, attrs)
+
+    # Override baseclass's __call__.  This directive can't be used as
+    # a decorator at the same time like grok.require() can.
+    def __call__(self, *args, **kw):
+        raise NotImplementedError
+
+class protect_set(protect_get):
+    pass
+
+class ModelSecurityGrokker(martian.ClassGrokker):
+    martian.component(Model)
+    martian.directive(protect_get)
+    martian.directive(protect_set)
+
+    def execute(self, factory, config, protect_get, protect_set, **kw):
+        get_names = {}
+        for permission, attrs in protect_get:
+            for name in attrs:
+                if name in get_names:
+                    raise GrokError("Duplicate read protection for %r "
+                                    "attribute on %r." % (name, factory),
+                                    factory)
+                get_names[name] = permission
+
+        set_names = {}
+        for permission, attrs in protect_set:
+            for name in attrs:
+                if name in set_names:
+                    raise GrokError("Duplicate write protection for %r "
+                                    "attribute on %r." % (name, factory),
+                                    factory)
+                set_names[name] = permission
+
+        for name, permission in get_names.items():
+            config.action(
+                discriminator=('protectName', factory, name),
+                callable=protect_getattr,
+                args=(factory, name, permission),
+                )
+        for name, permission in set_names.items():
+            config.action(
+                discriminator=('protectSetAttribute', factory, name),
+                callable=protect_setattr,
+                args=(factory, name, permission),
+                )
+        return True
+
+
 def grok(module_name):
     config = ConfigurationMachine()
     zcml.do_grok('grokcore.component.meta', config)



More information about the Checkins mailing list