Skip to content

Commit

Permalink
Add test to ensure UnicodeDecodeErrors not swallowed
Browse files Browse the repository at this point in the history
This test registers a title indexer that forces a UnicodeException during the creation of an content item, and checks that it is not swallowed by api.content.create
  • Loading branch information
mattss committed Nov 26, 2013
1 parent 37fcb36 commit 7495843
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions src/plone/api/tests/test_content.py
Expand Up @@ -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
Expand Down Expand Up @@ -233,6 +237,34 @@ def test_create_with_safe_id(self):
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."""

Expand Down

0 comments on commit 7495843

Please sign in to comment.