Commit 850d8a27 authored by Tatuya Kamada's avatar Tatuya Kamada Committed by Arnaud Fontaine

ERP5Catalog: Fix UnicodeDecodeError on non ascii catalog search when the inituser name is unicode

Zope4 inituser is decoded by decode('utf-8') on Zope startup and it is unicode on python2,
therefore it raises UnicodeDecodeError when searching non ascii charater on catalog.

  Traceback (most recent call last):
  File "<portal_components/test.erp5.testERP5Catalog>", line 4144, in testSearchNonAsciiWithTheInitUser
    person_module.searchFolder(title=person_title)]
  File "./erp5/product/ERP5Type/Core/Folder.py", line 452, in searchFolder
    return self.portal_catalog.searchResults(**kw)
  File "./erp5/product/ERP5Catalog/CatalogTool.py", line 819, in searchResults
    return ZCatalog.searchResults(self, sql_catalog_id=catalog_id, **kw)
  File "./erp5/product/ZSQLCatalog/ZSQLCatalog.py", line 1070, in searchResults
    return catalog.searchResults(REQUEST, **kw)
  File "./erp5/product/ZSQLCatalog/SQLCatalog.py", line 2362, in searchResults
    **kw
  File "./erp5/product/ZSQLCatalog/SQLCatalog.py", line 2326, in queryResults
    **kw
  File "./erp5/product/ZSQLCatalog/SQLCatalog.py", line 2214, in buildSQLQuery
    only_group_columns,
  File "./erp5/product/ZSQLCatalog/SQLExpression.py", line 397, in asSQLExpressionDict
    'where_expression': self.getWhereExpression(),
  File "./erp5/product/ZSQLCatalog/SQLExpression.py", line 298, in getWhereExpression
    result = self.sql_expression_list[0].getWhereExpression()
  File "./erp5/product/ZSQLCatalog/SQLExpression.py", line 303, in getWhereExpression
    result = '(%s)' % (operator.join(x.getWhereExpression() for x in self.sql_expression_list), )
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 25: ordinal not in range(128)
parent f1e6ce36
......@@ -4118,7 +4118,34 @@ VALUES
# but a proper page
self.assertIn('<title>Catalog Tool - portal_catalog', ret.getBody())
def testSearchNonAsciiWithTheInitUser(self):
"""
Test non ascii search with the inituser given by the inituser-login from slapos
(typically the 'zope' user)
"""
person_module = self.getPersonModule()
person_title = 'abcdé'
person_module.newContent(portal_type='Person', title=person_title)
self.tic()
uf = self.getPortal().acl_users
# The inituser is decoded by `decode('utf-8')` on Zope4 startup, and it is unicode on py2
# see:
# https://github.com/zopefoundation/AccessControl/commit/9a9c57f1c6311251a6e51a326751cf81e2810e2c
# imitate the inituser handling
inituser_login = b'zope_testSearchNonAsciiWithLegacyUserFolderUser'
inituser_str = inituser_login.decode('utf-8')
self.assertTrue(isinstance(inituser_str, six.text_type))
uf._doAddUser(inituser_str, '', ['Member', 'Assignor'], [])
self.loginByUserName(inituser_str)
folder_object_list = [x.getObject().getId() for x in
person_module.searchFolder(title=person_title)]
search_result = person_module.searchFolder(title=person_title)
self.assertTrue(len(search_result) > 0)
self.assertEqual('abcdé', search_result[0].getObject().getTitle())
class CatalogToolUpgradeSchemaTestCase(ERP5TypeTestCase):
"""Tests for "upgrade schema" feature of ERP5 Catalog.
"""
......
......@@ -624,6 +624,8 @@ class CatalogTool (UniqueObject, ZCatalog, CMFCoreCatalogTool, ActiveObject):
"""
user = _getAuthenticatedUser(self)
user_str = user.getIdOrUserName()
if isinstance(user_str, six.text_type):
user_str = user_str.encode('utf-8')
user_is_superuser = (user == system_user) or (user_str == ERP5Security.SUPER_USER)
allowedRolesAndUsers = self._listAllowedRolesAndUsers(user)
role_column_dict = {}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment