Skip to content

Commit

Permalink
Generalizing bulk get
Browse files Browse the repository at this point in the history
  • Loading branch information
kobelb committed Sep 7, 2018
1 parent 14d9058 commit 481943f
Show file tree
Hide file tree
Showing 12 changed files with 451 additions and 368 deletions.
6 changes: 4 additions & 2 deletions x-pack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
"@kbn/es": "link:../packages/kbn-es",
"@kbn/plugin-helpers": "link:../packages/kbn-plugin-helpers",
"@kbn/test": "link:../packages/kbn-test",
"@types/jest": "^22.2.3",
"@types/expect.js": "^0.3.29",
"@types/jest": "^23.3.1",
"@types/mocha": "^5.2.5",
"@types/pngjs": "^3.3.1",
"abab": "^1.0.4",
"ansicolors": "0.3.2",
Expand Down Expand Up @@ -171,4 +173,4 @@
"engines": {
"yarn": "^1.6.0"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,50 @@
export const AUTHENTICATION = {
NOT_A_KIBANA_USER: {
USERNAME: 'not_a_kibana_user',
PASSWORD: 'password'
PASSWORD: 'password',
},
SUPERUSER: {
USERNAME: 'elastic',
PASSWORD: 'changeme'
PASSWORD: 'changeme',
},
KIBANA_LEGACY_USER: {
USERNAME: 'a_kibana_legacy_user',
PASSWORD: 'password'
PASSWORD: 'password',
},
KIBANA_LEGACY_DASHBOARD_ONLY_USER: {
USERNAME: 'a_kibana_legacy_dashboard_only_user',
PASSWORD: 'password'
PASSWORD: 'password',
},
KIBANA_DUAL_PRIVILEGES_USER: {
USERNAME: 'a_kibana_dual_privileges_user',
PASSWORD: 'password'
PASSWORD: 'password',
},
KIBANA_DUAL_PRIVILEGES_DASHBOARD_ONLY_USER: {
USERNAME: 'a_kibana_dual_privileges_dashboard_only_user',
PASSWORD: 'password'
PASSWORD: 'password',
},
KIBANA_RBAC_USER: {
USERNAME: 'a_kibana_rbac_user',
PASSWORD: 'password'
PASSWORD: 'password',
},
KIBANA_RBAC_DASHBOARD_ONLY_USER: {
USERNAME: 'a_kibana_rbac_dashboard_only_user',
PASSWORD: 'password'
PASSWORD: 'password',
},
KIBANA_RBAC_DEFAULT_SPACE_ALL_USER: {
USERNAME: 'a_kibana_rbac_default_space_all_user',
PASSWORD: 'password'
PASSWORD: 'password',
},
KIBANA_RBAC_DEFAULT_SPACE_READ_USER: {
USERNAME: 'a_kibana_rbac_default_space_read_user',
PASSWORD: 'password'
PASSWORD: 'password',
},
KIBANA_RBAC_SPACE_1_ALL_USER: {
USERNAME: 'a_kibana_rbac_space_1_all_user',
PASSWORD: 'password'
PASSWORD: 'password',
},
KIBANA_RBAC_SPACE_1_READ_USER: {
USERNAME: 'a_kibana_rbac_space_1_read_user',
PASSWORD: 'password'
PASSWORD: 'password',
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@

import { DEFAULT_SPACE_ID } from '../../../../plugins/spaces/common/constants';

export function getUrlPrefix(spaceId) {
export function getUrlPrefix(spaceId: string) {
return spaceId && spaceId !== DEFAULT_SPACE_ID ? `/s/${spaceId}` : ``;
}

export function getIdPrefix(spaceId) {
export function getIdPrefix(spaceId: string) {
return spaceId === DEFAULT_SPACE_ID ? '' : `${spaceId}-`;
}

export function getExpectedSpaceIdProperty(spaceId) {
export function getExpectedSpaceIdProperty(spaceId: string) {
if (spaceId === DEFAULT_SPACE_ID) {
return {};
}
return {
spaceId
spaceId,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ export const SPACES = {
},
DEFAULT: {
spaceId: 'default',
}
},
};
36 changes: 36 additions & 0 deletions x-pack/test/saved_object_api_integration/common/lib/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export type DescribeFn = (text: string, fn: () => void) => void;

export interface TestResultDescriptor {
statusCode: number;
response: (resp: any) => void;
space?: any;
}

export interface TestsObject {
[key: string]: TestResultDescriptor;
}

export interface TestOptions {
auth?: {
username?: string;
password?: string;
};
currentSpaceId?: string;
spaceId?: string;
tests: TestsObject;
}

export type LoadTestFileFn = (path: string) => string;

export type GetServiceFn = (service: string) => any;

export interface TestInvoker {
getService: GetServiceFn;
loadTestFile: LoadTestFileFn;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import expect from 'expect.js';
import { DEFAULT_SPACE_ID } from '../../../../../plugins/spaces/common/constants';
import { getIdPrefix, getUrlPrefix } from '../../../common/lib/space_test_utils';

interface Authentication {
username: string;
password: string;
}

interface BulkGetTest {
statusCode: number;
response: (resp: any) => void;
}

interface BulkGetTests {
default: BulkGetTest;
}

interface BulkGetTestDefinition {
auth?: Authentication;
spaceId?: string;
otherSpaceId?: string;
tests: BulkGetTests;
}

type DescribeFn = (description: string, fn: () => void) => void;

export function bulkGetTestSuiteFactory(esArchiver: any, supertest: any) {
const makeBulkGetTest = (describeFn: DescribeFn) => (
description: string,
definition: BulkGetTestDefinition
) => {
const {
auth = {
username: undefined,
password: undefined,
},
spaceId,
otherSpaceId,
tests,
} = definition;

const BULK_REQUESTS = [
{
type: 'visualization',
id: 'dd7caf20-9efd-11e7-acb3-3dab96693fab',
},
{
type: 'dashboard',
id: 'does not exist',
},
];

const createBulkRequests = (requestSpaceId: string) =>
BULK_REQUESTS.map(r => ({
...r,
id: `${getIdPrefix(requestSpaceId)}${r.id}`,
}));

describeFn(description, () => {
before(() => esArchiver.load('saved_objects/spaces'));
after(() => esArchiver.unload('saved_objects/spaces'));

it(`should return ${tests.default.statusCode}`, async () => {
await supertest
.post(`${getUrlPrefix(spaceId || DEFAULT_SPACE_ID)}/api/saved_objects/_bulk_get`)
.auth(auth.username, auth.password)
.send(createBulkRequests(otherSpaceId || spaceId || DEFAULT_SPACE_ID))
.expect(tests.default.statusCode)
.then(tests.default.response);
});
});
};

const createExpectLegacyForbidden = (username: string) => (resp: any) => {
expect(resp.body).to.eql({
statusCode: 403,
error: 'Forbidden',
// eslint-disable-next-line max-len
message: `action [indices:data/read/mget] is unauthorized for user [${username}]: [security_exception] action [indices:data/read/mget] is unauthorized for user [${username}]`,
});
};

const createExpectNotFoundResults = (spaceId: string) => (resp: any) => {
expect(resp.body).to.eql({
saved_objects: [
{
id: `${getIdPrefix(spaceId)}dd7caf20-9efd-11e7-acb3-3dab96693fab`,
type: 'visualization',
error: {
statusCode: 404,
message: 'Not found',
},
},
{
id: `${getIdPrefix(spaceId)}does not exist`,
type: 'dashboard',
error: {
statusCode: 404,
message: 'Not found',
},
},
],
});
};

const createExpectResults = (spaceId = DEFAULT_SPACE_ID) => (resp: any) => {
expect(resp.body).to.eql({
saved_objects: [
{
id: `${getIdPrefix(spaceId)}dd7caf20-9efd-11e7-acb3-3dab96693fab`,
type: 'visualization',
updated_at: '2017-09-21T18:51:23.794Z',
version: resp.body.saved_objects[0].version,
attributes: {
title: 'Count of requests',
description: '',
version: 1,
// cheat for some of the more complex attributes
visState: resp.body.saved_objects[0].attributes.visState,
uiStateJSON: resp.body.saved_objects[0].attributes.uiStateJSON,
kibanaSavedObjectMeta: resp.body.saved_objects[0].attributes.kibanaSavedObjectMeta,
},
},
{
id: `${getIdPrefix(spaceId)}does not exist`,
type: 'dashboard',
error: {
statusCode: 404,
message: 'Not found',
},
},
],
});
};

const bulkGetTest = makeBulkGetTest(describe);
bulkGetTest.only = makeBulkGetTest(describe.only);

return {
bulkGetTest,
createExpectLegacyForbidden,
createExpectNotFoundResults,
createExpectResults,
};
}
Loading

0 comments on commit 481943f

Please sign in to comment.