Merge "Return __searchlight-user-role with highlighting"

This commit is contained in:
Jenkins 2016-09-28 07:57:56 +00:00 committed by Gerrit Code Review
commit 8c0cefb135
4 changed files with 37 additions and 3 deletions

View File

@ -502,10 +502,13 @@ class IndexBase(plugin.Plugin):
return None
def filter_result(self, hit, request_context):
"""Filter each outgoing search result; document in hit['_source']. By
default, this does nothing since information shouldn't be indexed.
"""Filter each outgoing search result; document in hit['_source'].
"""
pass
if 'highlight' in hit:
# We need to clean up any fields 'leaked' by highlighting.
# As a reminder since this is a rare case, the 'highlight'
# dict is a peer of '_source' in the overall hit structure.
hit['highlight'].pop(ROLE_USER_FIELD, None)
@abc.abstractmethod
def get_mapping(self):

View File

@ -126,6 +126,8 @@ class MetadefIndex(base.IndexBase):
return serialize_glance_metadef_ns(metadef_obj)
def filter_result(self, hit, request_context):
super(MetadefIndex, self).filter_result(hit, request_context)
# Revert the change we make to fit the 'tags' mapping used in other
# plugins (see serialize_glance_metadef_ns in __init__.py)
source = hit['_source']

View File

@ -207,6 +207,8 @@ class ServerIndex(base.IndexBase):
return serialize_nova_server(server)
def filter_result(self, hit, request_context):
super(ServerIndex, self).filter_result(hit, request_context)
# Reverse the change we make to security groups in serialize() to
# maintain compatibility with the nova API response
source = hit['_source']

View File

@ -14,6 +14,7 @@
# under the License.
import collections
import copy
import mock
import operator
import six
@ -713,3 +714,29 @@ class TestPlugin(test_utils.BaseTestCase):
ignore_unavailable=True,
index='searchlight-search',
size=0)
def test_filter_result(self):
"""Verify that any highlighted query results will filter out
the ROLE_USER_FIELD field.
"""
request = unit_test_utils.get_fake_request(is_admin=True)
mock_engine = mock.Mock()
simple_plugin = fake_plugins.FakeSimplePlugin(es_engine=mock_engine)
# Verify with ROLE_USER_FIELD
hit = {"_source": {"owner": "<em>admin</em>"},
"highlight": {
"owner": "<em>admin</em>",
"__searchlight-user-role": "<em>admin</em>"}}
simple_plugin.filter_result(hit, request.context)
self.assertNotIn('__searchlight-user-role', hit['highlight'])
# Verify without ROLE_USER_FIELD
hit = {"_source": {"owner": "<em>admin</em>"},
"highlight": {
"owner": "<em>admin</em>"}}
original_hit = copy.deepcopy(hit)
simple_plugin.filter_result(hit, request.context)
self.assertEqual(original_hit, hit)