Skip to content

Commit

Permalink
Merge pull request #508 from plone/issue-186-search-in-vhm
Browse files Browse the repository at this point in the history
Add VHM support to @search
  • Loading branch information
tisto committed Apr 17, 2018
2 parents 11ee7dd + c02e3dc commit bfd78ce
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
5 changes: 4 additions & 1 deletion CHANGES.rst
Expand Up @@ -4,7 +4,10 @@ Changelog
1.6.1 (unreleased)
------------------

- Nothing changed yet.
Bugfixes:

- Add VHM support to @search
[csenger]


1.6.0 (2018-04-17)
Expand Down
14 changes: 14 additions & 0 deletions src/plone/restapi/search/handler.py
Expand Up @@ -33,6 +33,20 @@ def _constrain_query_by_path(self, query):
if 'path' not in query:
query['path'] = {}

if isinstance(query['path'], str):
query['path'] = {'query': query['path']}

# If this is accessed through a VHM the client does not know
# the complete physical path of an object. But the path index
# indexes the complete physical path. Complete the path.
vhm_physical_path = self.request.get('VirtualRootPhysicalPath')
if vhm_physical_path:
path = query['path'].get('query')
if path:
path = path.lstrip('/')
full_path = '/'.join(vhm_physical_path + (path,))
query['path']['query'] = full_path

if isinstance(query['path'], dict) and 'query' not in query['path']:
# We either had no 'path' parameter at all, or an incomplete
# 'path' query dict (with just ExtendedPathIndex options (like
Expand Down
26 changes: 26 additions & 0 deletions src/plone/restapi/tests/test_search.py
Expand Up @@ -107,6 +107,32 @@ def test_search_on_context_constrains_query_by_path(self):
u'/plone/folder/other-document'},
set(result_paths(response.json())))

def test_search_in_vhm(self):
# Install a Virtual Host Monster
if 'virtual_hosting' not in self.app.objectIds():
# If ZopeLite was imported, we have no default virtual
# host monster
from Products.SiteAccess.VirtualHostMonster \
import manage_addVirtualHostMonster
manage_addVirtualHostMonster(self.app, 'virtual_hosting')
transaction.commit()

# we don't get a result if we do not provide the full physical path
response = self.api_session.get('/@search?path=/folder',)
self.assertSetEqual(set(), set(result_paths(response.json())))

# If we go through the VHM will will get results if we only use
# the part of the path inside the VHM
vhm_url = (
'%s/VirtualHostBase/http/plone.org/plone/VirtualHostRoot/%s' %
(self.app.absolute_url(), '@search?path=/folder'))
response = self.api_session.get(vhm_url)
self.assertSetEqual(
{u'/folder',
u'/folder/doc',
u'/folder/other-document'},
set(result_paths(response.json())))

def test_path_gets_prefilled_if_missing_from_path_query_dict(self):
response = self.api_session.get('/@search?path.depth=1')
self.assertSetEqual(
Expand Down

0 comments on commit bfd78ce

Please sign in to comment.