def test_save_parent_only_separated(self, mock_role_separated): """Test where the parent document is role separated but this child is not; it's expected the parent's _USER documents be used for _parent """ mock_role_separated.return_value = False mock_engine = mock.Mock() plugin = fake_plugins.FakeSeparatedChildPlugin(es_engine=mock_engine) parent_plugin = fake_plugins.RoleSeparatedPlugin(es_engine=mock_engine) plugin.register_parent(parent_plugin) indexing_helper = helper.IndexingHelper(plugin) bulk_name = 'searchlight.elasticsearch.plugins.helper.helpers.bulk' child_docs = copy.deepcopy(fake_plugins.CHILD_DATA) with mock.patch(bulk_name) as mock_bulk: indexing_helper.save_documents(child_docs) self.assertEqual(1, len(mock_bulk.call_args_list)) actions = list(mock_bulk.call_args_list[0][1]['actions']) expected_doc = copy.deepcopy(child_docs[0]) expected_doc[ROLE_USER_FIELD] = ['user', 'admin'] expected_actions = [ {'_op_type': 'index', '_id': 'child1', '_source': expected_doc, '_parent': 'simple1_USER'}, ] self.assertEqual(expected_actions, list(actions))
def test_save_child_parent_both_separated(self): mock_engine = mock.Mock() plugin = fake_plugins.FakeSeparatedChildPlugin(es_engine=mock_engine) parent_plugin = fake_plugins.RoleSeparatedPlugin(es_engine=mock_engine) plugin.register_parent(parent_plugin) indexing_helper = helper.IndexingHelper(plugin) bulk_name = 'searchlight.elasticsearch.plugins.helper.helpers.bulk' child_docs = copy.deepcopy(fake_plugins.CHILD_DATA) # First run where both child and parent are role-separated (and thus # we'd expect two copies of both parent and child with appropriate # ids linking them) with mock.patch(bulk_name) as mock_bulk: indexing_helper.save_documents(child_docs) self.assertEqual(1, len(mock_bulk.call_args_list)) actions = list(mock_bulk.call_args_list[0][1]['actions']) expected_admin_doc = copy.deepcopy(child_docs[0]) expected_admin_doc[ROLE_USER_FIELD] = 'admin' expected_user_doc = copy.deepcopy(child_docs[0]) expected_user_doc[ROLE_USER_FIELD] = 'user' expected_actions = [ {'_op_type': 'index', '_id': 'child1_ADMIN', '_source': expected_admin_doc, '_parent': 'simple1_ADMIN'}, {'_op_type': 'index', '_id': 'child1_USER', '_source': expected_user_doc, '_parent': 'simple1_USER'} ] self.assertEqual(expected_actions, list(actions))
def test_delete_children_role_separated(self): mock_engine = mock.Mock() plugin = fake_plugins.FakeSeparatedChildPlugin(es_engine=mock_engine) parent_plugin = fake_plugins.RoleSeparatedPlugin(es_engine=mock_engine) plugin.register_parent(parent_plugin) indexing_helper = helper.IndexingHelper(plugin) scan_name = 'searchlight.elasticsearch.plugins.helper.helpers.scan' bulk_name = 'searchlight.elasticsearch.plugins.helper.helpers.bulk' mock_scan_data = [{ '_id': '1_ADMIN', 'fields': { '_parent': 'p1_ADMIN' } }, { '_id': '1_USER', 'fields': { '_parent': 'p1_USER' } }] with mock.patch(scan_name, return_value=mock_scan_data) as mock_scan: with mock.patch(bulk_name) as mock_bulk: indexing_helper.delete_documents_with_parent('p1') parent_type = plugin.parent_plugin_type() base_full_parent_type = '%s#%s' % (parent_type, 'p1') expected_scan_query = { 'fields': ['_parent', '_routing'], 'query': { 'terms': { '_parent': [ base_full_parent_type + '_ADMIN', base_full_parent_type + '_USER' ] } } } mock_scan.assert_called_with(client=plugin.engine, index=plugin.alias_name_listener, doc_type=plugin.document_type, query=expected_scan_query) expected_delete_actions = [{ '_op_type': 'delete', '_id': '1_ADMIN', '_parent': 'p1_ADMIN' }, { '_op_type': 'delete', '_id': '1_USER', '_parent': 'p1_USER' }] mock_bulk.assert_called_with(client=plugin.engine, index=plugin.alias_name_listener, doc_type=plugin.document_type, actions=expected_delete_actions)
def setUp(self): super(TestReindexing, self).setUp() role_plugin = fake_plugins.RoleSeparatedPlugin( es_engine=self.elastic_connection) self.role_plugin = role_plugin non_role_plugin = fake_plugins.NonRoleSeparatedPlugin( es_engine=self.elastic_connection) self.non_role_plugin = non_role_plugin self.initialized_plugins['re-non-role-separated'] = non_role_plugin self.initialized_plugins['re-role-separated'] = role_plugin
def test_role_separated_save_docs(self): """Test admin only fields are correctly removed from serialization and that alternate _id values are used """ mock_engine = mock.Mock() plugin = fake_plugins.RoleSeparatedPlugin(es_engine=mock_engine) indexing_helper = helper.IndexingHelper(plugin) bulk_name = 'searchlight.elasticsearch.plugins.helper.helpers.bulk' with mock.patch(bulk_name) as mock_bulk: count = len(plugin.get_objects()) fake_versions = range(1, count + 1) indexing_helper.save_documents(plugin.get_objects(), fake_versions) self.assertEqual(1, len(mock_bulk.call_args_list)) actions = list(mock_bulk.call_args_list[0][1]['actions']) self.assertEqual(4, len(actions)) self.assertEqual( set([ 'role-fake1_ADMIN', 'role-fake2_ADMIN', 'role-fake1_USER', 'role-fake2_USER' ]), set(action['_id'] for action in actions)) self.assertEqual(set(fake_versions), set(action['_version'] for action in actions)) self.assertEqual(['external'] * 4, [action['_version_type'] for action in actions]) # This plugin filters on admin_wildcard_* and admin_specific fake1_admin = list( filter(lambda a: a['_id'] == 'role-fake1_ADMIN', actions))[0]['_source'] self.assertEqual('role-fake1', fake1_admin['id']) self.assertIn('public_field', fake1_admin) self.assertIn('admin_wildcard_this', fake1_admin) self.assertEqual('admin', fake1_admin[ROLE_USER_FIELD]) fake1_user = list( filter(lambda a: a['_id'] == 'role-fake1_USER', actions))[0]['_source'] self.assertEqual('role-fake1', fake1_user['id']) self.assertIn('public_field', fake1_user) self.assertNotIn('admin_wildcard_this', fake1_user) self.assertNotIn('admin_specific', fake1_user) self.assertEqual('user', fake1_user[ROLE_USER_FIELD])
def test_role_separated_delete(self): """Test that deletion for a role-separated plugin deletes both docs""" mock_engine = mock.Mock() plugin = fake_plugins.RoleSeparatedPlugin(es_engine=mock_engine) indexing_helper = helper.IndexingHelper(plugin) bulk_name = 'searchlight.elasticsearch.plugins.helper.helpers.bulk' with mock.patch(bulk_name) as mock_bulk: indexing_helper.delete_document({'_id': 'role-fake1'}) expected_delete_actions = [{ '_op_type': 'delete', '_id': 'role-fake1_ADMIN' }, { '_op_type': 'delete', '_id': 'role-fake1_USER' }] mock_bulk.assert_called_once_with(client=plugin.engine, index=plugin.alias_name_listener, doc_type=plugin.document_type, actions=expected_delete_actions)