Skip to content

Commit

Permalink
Adding bulk create tests and testing non space aware type with bulkGet
Browse files Browse the repository at this point in the history
  • Loading branch information
kobelb committed Sep 8, 2018
1 parent 5388b5a commit f9383fd
Show file tree
Hide file tree
Showing 11 changed files with 671 additions and 29 deletions.
170 changes: 170 additions & 0 deletions x-pack/test/saved_object_api_integration/common/suites/bulk_create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* 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 { SuperTest } from 'supertest';
import { DEFAULT_SPACE_ID } from '../../../../plugins/spaces/common/constants';
import { getIdPrefix, getUrlPrefix } from '../lib/space_test_utils';
import { DescribeFn, TestDefinitionAuthentication } from '../lib/types';

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

interface BulkCreateTests {
default: BulkCreateTest;
}

interface BulkCreateTestDefinition {
auth?: TestDefinitionAuthentication;
spaceId?: string;
tests: BulkCreateTests;
}

export function bulkCreateTestSuiteFactory(es: any, esArchiver: any, supertest: SuperTest<any>) {
const isGlobalType = (type: string) => type === 'globaltype';

const createBulkRequests = (spaceId: string) => [
{
type: 'visualization',
id: `${getIdPrefix(spaceId)}dd7caf20-9efd-11e7-acb3-3dab96693fab`,
attributes: {
title: 'An existing visualization',
},
},
{
type: 'dashboard',
id: `${getIdPrefix(spaceId)}a01b2f57-fcfd-4864-b735-09e28f0d815e`,
attributes: {
title: 'A great new dashboard',
},
},
{
type: 'globaltype',
id: '05976c65-1145-4858-bbf0-d225cc78a06e',
attributes: {
name: 'A new globaltype object',
},
},
{
type: 'globaltype',
id: '8121a00-8efd-21e7-1cb3-34ab966434445',
attributes: {
name: 'An existing globaltype',
},
},
];

const makeBulkCreateTest = (describeFn: DescribeFn) => (
description: string,
definition: BulkCreateTestDefinition
) => {
const { auth = {}, spaceId = DEFAULT_SPACE_ID, tests } = definition;

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)}/api/saved_objects/_bulk_create`)
.auth(auth.username, auth.password)
.send(createBulkRequests(spaceId))
.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/write/bulk] is unauthorized for user [${username}]: [security_exception] action [indices:data/write/bulk] is unauthorized for user [${username}]`,
});
};

const expectRbacForbidden = (resp: any) => {
expect(resp.body).to.eql({
statusCode: 403,
error: 'Forbidden',
message: `Unable to bulk_create dashboard,globaltype,visualization, missing action:saved_objects/dashboard/bulk_create,action:saved_objects/globaltype/bulk_create,action:saved_objects/visualization/bulk_create`,
});
};

const createExpectResults = (spaceId = DEFAULT_SPACE_ID) => async (resp: any) => {
expect(resp.body).to.eql({
saved_objects: [
{
type: 'visualization',
id: `${getIdPrefix(spaceId)}dd7caf20-9efd-11e7-acb3-3dab96693fab`,
error: {
message: 'version conflict, document already exists',
statusCode: 409,
},
},
{
type: 'dashboard',
id: `${getIdPrefix(spaceId)}a01b2f57-fcfd-4864-b735-09e28f0d815e`,
updated_at: resp.body.saved_objects[1].updated_at,
version: 1,
attributes: {
title: 'A great new dashboard',
},
},
{
type: 'globaltype',
id: `05976c65-1145-4858-bbf0-d225cc78a06e`,
updated_at: resp.body.saved_objects[2].updated_at,
version: 1,
attributes: {
name: 'A new globaltype object',
},
},
{
type: 'globaltype',
id: '8121a00-8efd-21e7-1cb3-34ab966434445',
error: {
message: 'version conflict, document already exists',
statusCode: 409,
},
},
],
});

for (const savedObject of createBulkRequests(spaceId)) {
const expectedSpacePrefix =
spaceId === DEFAULT_SPACE_ID || isGlobalType(savedObject.type) ? '' : `${spaceId}:`;

// query ES directory to ensure namespace was or wasn't specified
const { _source } = await es.get({
id: `${expectedSpacePrefix}${savedObject.type}:${savedObject.id}`,
type: 'doc',
index: '.kibana',
});

const { namespace: actualNamespace } = _source;

if (spaceId === DEFAULT_SPACE_ID || isGlobalType(savedObject.type)) {
expect(actualNamespace).to.eql(undefined);
} else {
expect(actualNamespace).to.eql(spaceId);
}
}
};

const bulkCreateTest = makeBulkCreateTest(describe);
bulkCreateTest.only = makeBulkCreateTest(describe.only);

return {
bulkCreateTest,
createExpectLegacyForbidden,
createExpectResults,
expectRbacForbidden,
};
}
47 changes: 27 additions & 20 deletions x-pack/test/saved_object_api_integration/common/suites/bulk_get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,36 @@ interface BulkGetTestDefinition {
}

export function bulkGetTestSuiteFactory(esArchiver: any, supertest: SuperTest<any>) {
const createBulkRequests = (spaceId: string) => [
{
type: 'visualization',
id: `${getIdPrefix(spaceId)}dd7caf20-9efd-11e7-acb3-3dab96693fab`,
},
{
type: 'dashboard',
id: `${getIdPrefix(spaceId)}does not exist`,
},
{
type: 'globaltype',
id: '8121a00-8efd-21e7-1cb3-34ab966434445',
},
];

const makeBulkGetTest = (describeFn: DescribeFn) => (
description: string,
definition: BulkGetTestDefinition
) => {
const { auth = {}, 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}`,
}));
const { auth = {}, spaceId = DEFAULT_SPACE_ID, otherSpaceId, tests } = definition;

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`)
.post(`${getUrlPrefix(spaceId)}/api/saved_objects/_bulk_get`)
.auth(auth.username, auth.password)
.send(createBulkRequests(otherSpaceId || spaceId || DEFAULT_SPACE_ID))
.send(createBulkRequests(otherSpaceId || spaceId))
.expect(tests.default.statusCode)
.then(tests.default.response);
});
Expand Down Expand Up @@ -131,6 +129,15 @@ export function bulkGetTestSuiteFactory(esArchiver: any, supertest: SuperTest<an
message: 'Not found',
},
},
{
id: `8121a00-8efd-21e7-1cb3-34ab966434445`,
type: 'globaltype',
updated_at: '2017-09-21T18:59:16.270Z',
version: resp.body.saved_objects[2].version,
attributes: {
name: 'My favorite global object',
},
},
],
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface FindTestDefinition {
tests: FindTests;
}

// TODO: add space unaware type
export function findTestSuiteFactory(esArchiver: any, supertest: SuperTest<any>) {
const makeFindTest = (describeFn: DescribeFn) => (
description: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface GetTestDefinition {
tests: GetTests;
}

// TODO: add space unaware type
export function getTestSuiteFactory(esArchiver: any, supertest: SuperTest<any>) {
const existsId = 'dd7caf20-9efd-11e7-acb3-3dab96693fab';
const doesntExistId = 'foobar';
Expand Down
Loading

0 comments on commit f9383fd

Please sign in to comment.