Merge pull request #151 from plone/unicodeerror

Prevent UnicodeDecodeErrors from being swallowed
This commit is contained in:
Nejc Zupan 2013-11-26 02:29:33 -08:00
commit 2a8027faf0
3 changed files with 39 additions and 1 deletions

View File

@ -6,6 +6,8 @@ Changelog
- Add ``api.env.plone_version()`` and ``api.env.zope_version()`` refs. #126.
[hvelarde]
- Stop UnicodeDecodeErrors being swallowed in ``api.content.create``
[mattss]
1.1.0 (2013-10-12)

View File

@ -65,7 +65,11 @@ def create(
try:
container.invokeFactory(type, content_id, **kwargs)
except ValueError, e:
except UnicodeDecodeError:
# UnicodeDecodeError is a subclass of ValueError,
# so will be swallowed below unless we re-raise it here
raise
except ValueError as e:
if ISiteRoot.providedBy(container):
allowed_types = container.allowedContentTypes()
types = [allowed_type.id for allowed_type in allowed_types]

View File

@ -3,12 +3,16 @@
from Acquisition import aq_base
from OFS.CopySupport import CopyError
from Products.CMFCore.interfaces import IContentish
from Products.ZCatalog.interfaces import IZCatalog
from plone import api
from plone.api.tests.base import INTEGRATION_TESTING
from plone.indexer import indexer
from plone.uuid.interfaces import IMutableUUID
from plone.uuid.interfaces import IUUIDGenerator
from zExceptions import BadRequest
from zope.component import getUtility
from zope.component import getGlobalSiteManager
import mock
import pkg_resources
@ -233,6 +237,34 @@ class TestPloneApiContent(unittest.TestCase):
self.assertEqual(second_page.id, 'test-document-1')
self.assertEqual(second_page.portal_type, 'Document')
def test_create_raises_unicodedecodeerror(self):
"""Test that the create method raises UnicodeDecodeErrors correctly."""
site = getGlobalSiteManager()
unicode_exception_message = "This is a fake unicode error"
# register a title indexer that will force a UnicodeDecodeError
# during content reindexing
@indexer(IContentish, IZCatalog)
def force_unicode_error(object):
raise UnicodeDecodeError('ascii', 'x', 1, 5,
unicode_exception_message)
site.registerAdapter(factory=force_unicode_error, name='Title')
def unregister_indexer():
site.unregisterAdapter(factory=force_unicode_error, name='Title')
self.addCleanup(unregister_indexer)
with self.assertRaises(UnicodeDecodeError) as ude:
api.content.create(
type='Folder', id='test-unicode-folder',
container=self.portal,
)
# check that the exception is the one we raised
self.assertEqual(ude.exception.reason, unicode_exception_message)
def test_get_constraints(self):
"""Test the constraints when content is fetched with get."""