Skip to content

Commit

Permalink
use PIT search for savedObjectTagging plugin (#120476)
Browse files Browse the repository at this point in the history
* use PIT search for savedObjectTagging plugin

* fixing FTR tests

* foo is the new grep

* fix tests

* fix tests again
  • Loading branch information
pgayvallet committed Dec 8, 2021
1 parent 0438566 commit 6fb7325
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
* 2.0.
*/

import { SavedObjectsClientContract, SavedObjectsFindOptionsReference } from 'src/core/server';
import {
SavedObjectsClientContract,
SavedObjectsFindOptionsReference,
SavedObject,
} from 'src/core/server';
import { tagSavedObjectTypeName } from '../../../common/constants';
import { Tag, TagWithRelations } from '../../../common/types';
import { Tag, TagAttributes, TagWithRelations } from '../../../common/types';

export const addConnectionCount = async (
tags: Tag[],
Expand All @@ -22,14 +26,19 @@ export const addConnectionCount = async (
id,
}));

const allResults = await client.find({
const pitFinder = client.createPointInTimeFinder<TagAttributes>({
type: targetTypes,
page: 1,
perPage: 10000,
perPage: 1000,
hasReference: references,
hasReferenceOperator: 'OR',
});
allResults.saved_objects.forEach((obj) => {

const results: SavedObject[] = [];
for await (const response of pitFinder.find()) {
results.push(...response.saved_objects);
}

results.forEach((obj) => {
obj.references.forEach((ref) => {
if (ref.type === tagSavedObjectTypeName && ids.has(ref.id)) {
counts.set(ref.id, counts.get(ref.id)! + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,19 @@ describe('TagsClient', () => {
it('calls `soClient.find` with the correct parameters', async () => {
await tagsClient.getAll();

expect(soClient.find).toHaveBeenCalledTimes(1);
expect(soClient.find).toHaveBeenCalledWith({
expect(soClient.createPointInTimeFinder).toHaveBeenCalledTimes(1);
expect(soClient.createPointInTimeFinder).toHaveBeenCalledWith({
type: 'tag',
perPage: 10000,
perPage: 1000,
});

expect(soClient.find).toHaveBeenCalledTimes(1);
expect(soClient.find).toHaveBeenCalledWith(
expect.objectContaining({
type: 'tag',
perPage: 1000,
})
);
});

it('converts the objects returned from the soClient to tags', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,18 @@ export class TagsClient implements ITagsClient {
}

public async getAll() {
const result = await this.soClient.find<TagAttributes>({
const pitFinder = this.soClient.createPointInTimeFinder<TagAttributes>({
type: this.type,
perPage: 10000,
perPage: 1000,
});

return result.saved_objects.map(savedObjectToTag);
const results: TagSavedObject[] = [];
for await (const response of pitFinder.find()) {
results.push(...response.saved_objects);
}
await pitFinder.close();

return results.map(savedObjectToTag);
}

public async delete(id: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default function ({ getService }: FtrProviderContext) {
});
},
},
unauthorized: {
noResults: {
httpCode: 200,
expectResponse: ({ body }) => {
expect(body).to.eql({
Expand All @@ -54,8 +54,17 @@ export default function ({ getService }: FtrProviderContext) {
});
},
},
unauthorized: {
httpCode: 403,
expectResponse: ({ body }) => {
expect(body).to.eql({
error: 'Forbidden',
message: 'unauthorized',
statusCode: 403,
});
},
},
};

const expectedResults: Record<string, User[]> = {
authorized: [
USERS.SUPERUSER,
Expand All @@ -67,7 +76,8 @@ export default function ({ getService }: FtrProviderContext) {
USERS.DEFAULT_SPACE_VISUALIZE_READ_USER,
USERS.DEFAULT_SPACE_MAPS_READ_USER,
],
unauthorized: [USERS.NOT_A_KIBANA_USER, USERS.DEFAULT_SPACE_ADVANCED_SETTINGS_READ_USER],
noResults: [USERS.DEFAULT_SPACE_ADVANCED_SETTINGS_READ_USER],
unauthorized: [USERS.NOT_A_KIBANA_USER],
};

const createUserTest = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ export default function ({ getService }: FtrProviderContext) {
},
},
unauthorized: {
httpCode: 200,
httpCode: 403,
expectResponse: ({ body }) => {
expect(body).to.eql({
tags: [],
error: 'Forbidden',
message: 'unauthorized',
statusCode: 403,
});
},
},
Expand Down

0 comments on commit 6fb7325

Please sign in to comment.