Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate imported field values #39

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions news/38.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
validate imported field values
[petschki]
3 changes: 3 additions & 0 deletions plone/app/registry/exportimport/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ def importRecord(self, node):
if value is _marker:
value = field.default
value_purge = True
else:
# validate incoming value
field.validate(value)

if existing_record is not None:
if change_field:
Expand Down
26 changes: 25 additions & 1 deletion plone/app/registry/tests/test_exportimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
from plone.registry.interfaces import IRegistry
from plone.supermodel.utils import prettyXML
from plone.testing import zca
from Products.GenericSetup.tests.common import DummyImportContext as BaseDummyImportContext
from Products.GenericSetup.tests.common import DummyImportContext as BaseDummyImportContext # noqa
from Products.GenericSetup.tests.common import DummyExportContext
from zope.component import provideUtility
from zope.configuration import xmlconfig
from zope.interface import alsoProvides

import unittest
import zope.schema


configuration = """\
Expand Down Expand Up @@ -393,6 +394,29 @@ def test_import_value_only(self):
self.registry['test.export.simple']
)

def test_import_value_validation(self):
xml = """\
<registry>
<record name="test.export.simple">
<value></value>
</record>
</registry>
"""
context = DummyImportContext(self.site, purge=False)
context._files = {'registry.xml': xml}

self.registry.records['test.export.simple'] = \
Record(field.TextLine(title=u"Simple record", default=u"N/A"),
value=u"Sample value")

try:
importRegistry(context)
except zope.schema._bootstrapinterfaces.RequiredMissing:
# this should raise RequiredMissing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be done with:

with self.assertRaises(zope.schema._bootstrapinterfaces.RequiredMissing):
    importRegistry(context)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank's for the hint ... I'll change that

pass
else:
self.fail()

def test_import_value_only_condition_installed(self):
xml = """\
<registry>
Expand Down