Skip to content

Commit

Permalink
Typescriptifying Create
Browse files Browse the repository at this point in the history
  • Loading branch information
kobelb committed Sep 7, 2018
1 parent 485d983 commit f707e03
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@
import expect from 'expect.js';
import { DEFAULT_SPACE_ID } from '../../../../../plugins/spaces/common/constants';
import { getIdPrefix, getUrlPrefix } from '../../../common/lib/space_test_utils';
import { DescribeFn } from '../../../common/lib/types';

interface Authentication {
username: string;
password: string;
}
import { DescribeFn, TestDefinitionAuthentication } from '../../../common/lib/types';

interface BulkGetTest {
statusCode: number;
Expand All @@ -24,7 +19,7 @@ interface BulkGetTests {
}

interface BulkGetTestDefinition {
auth?: Authentication;
auth?: TestDefinitionAuthentication;
spaceId?: string;
otherSpaceId?: string;
tests: BulkGetTests;
Expand All @@ -35,15 +30,7 @@ export function bulkGetTestSuiteFactory(esArchiver: any, supertest: SuperTest<an
description: string,
definition: BulkGetTestDefinition
) => {
const {
auth = {
username: undefined,
password: undefined,
},
spaceId,
otherSpaceId,
tests,
} = definition;
const { auth = {}, spaceId, otherSpaceId, tests } = definition;

const BULK_REQUESTS = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,35 @@
* you may not use this file except in compliance with the Elastic License.
*/
import expect from 'expect.js';
import { getUrlPrefix } from '../../lib/space_test_utils';
import { DEFAULT_SPACE_ID } from '../../../../../plugins/spaces/common/constants';
import { DescribeFn, TestDefinitionAuthentication } from '../../../common/lib/types';
import { getUrlPrefix } from '../../lib/space_test_utils';

export function createTestSuiteFactory(es, esArchiver, supertest) {
interface CreateTest {
statusCode: number;
response: (resp: any) => void;
}

interface CreateTests {
spaceAware: CreateTest;
notSpaceAware: CreateTest;
}

interface CreateTestDefinition {
auth?: TestDefinitionAuthentication;
spaceId?: string;
tests: CreateTests;
}

export function createTestSuiteFactory(es: any, esArchiver: any, supertest: SuperTest<any>) {
const spaceAwareType = 'visualization';
const notSpaceAwareType = 'globaltype';

const makeCreateTest = (describeFn) => (description, {
auth = {
username: undefined,
password: undefined
},
spaceId,
tests,
}) => {
const makeCreateTest = (describeFn: DescribeFn) => (
description: string,
definition: CreateTestDefinition
) => {
const { auth = {}, spaceId = DEFAULT_SPACE_ID, tests } = definition;
describeFn(description, () => {
before(() => esArchiver.load('saved_objects/spaces'));
after(() => esArchiver.unload('saved_objects/spaces'));
Expand All @@ -28,8 +42,8 @@ export function createTestSuiteFactory(es, esArchiver, supertest) {
.auth(auth.username, auth.password)
.send({
attributes: {
title: 'My favorite vis'
}
title: 'My favorite vis',
},
})
.expect(tests.spaceAware.statusCode)
.then(tests.spaceAware.response);
Expand All @@ -42,32 +56,52 @@ export function createTestSuiteFactory(es, esArchiver, supertest) {
.send({
attributes: {
name: `Can't be contained to a space`,
}
},
})
.expect(tests.notSpaceAware.statusCode)
.then(tests.notSpaceAware.response);
});

});
};

const createTest = makeCreateTest(describe);
createTest.only = makeCreateTest(describe.only);

const createExpectSpaceAwareResults = (spaceId = DEFAULT_SPACE_ID) => async (resp) => {
expect(resp.body).to.have.property('id').match(/^[0-9a-f-]{36}$/);
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/index] is unauthorized for user [${username}]: [security_exception] action [indices:data/write/index] is unauthorized for user [${username}]`,
});
};

const createExpectRbacForbidden = (type: string) => (resp: any) => {
expect(resp.body).to.eql({
statusCode: 403,
error: 'Forbidden',
message: `Unable to create ${type}, missing action:saved_objects/${type}/create`,
});
};

const createExpectSpaceAwareResults = (spaceId = DEFAULT_SPACE_ID) => async (resp: any) => {
expect(resp.body)
.to.have.property('id')
.match(/^[0-9a-f-]{36}$/);

// loose ISO8601 UTC time with milliseconds validation
expect(resp.body).to.have.property('updated_at').match(/^[\d-]{10}T[\d:\.]{12}Z$/);
expect(resp.body)
.to.have.property('updated_at')
.match(/^[\d-]{10}T[\d:\.]{12}Z$/);

expect(resp.body).to.eql({
id: resp.body.id,
type: spaceAwareType,
updated_at: resp.body.updated_at,
version: 1,
attributes: {
title: 'My favorite vis'
}
title: 'My favorite vis',
},
});

const expectedSpacePrefix = spaceId === DEFAULT_SPACE_ID ? '' : `${spaceId}:`;
Expand All @@ -76,12 +110,10 @@ export function createTestSuiteFactory(es, esArchiver, supertest) {
const { _source } = await es.get({
id: `${expectedSpacePrefix}${spaceAwareType}:${resp.body.id}`,
type: 'doc',
index: '.kibana'
index: '.kibana',
});

const {
namespace: actualNamespace
} = _source;
const { namespace: actualNamespace } = _source;

if (spaceId === DEFAULT_SPACE_ID) {
expect(actualNamespace).to.eql(undefined);
Expand All @@ -90,11 +122,15 @@ export function createTestSuiteFactory(es, esArchiver, supertest) {
}
};

const expectNotSpaceAwareResults = () => async (resp) => {
expect(resp.body).to.have.property('id').match(/^[0-9a-f-]{36}$/);
const expectNotSpaceAwareResults = async (resp: any) => {
expect(resp.body)
.to.have.property('id')
.match(/^[0-9a-f-]{36}$/);

// loose ISO8601 UTC time with milliseconds validation
expect(resp.body).to.have.property('updated_at').match(/^[\d-]{10}T[\d:\.]{12}Z$/);
expect(resp.body)
.to.have.property('updated_at')
.match(/^[\d-]{10}T[\d:\.]{12}Z$/);

expect(resp.body).to.eql({
id: resp.body.id,
Expand All @@ -103,28 +139,27 @@ export function createTestSuiteFactory(es, esArchiver, supertest) {
version: 1,
attributes: {
name: `Can't be contained to a space`,
}
},
});

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

const {
namespace: actualNamespace
} = _source;
const { namespace: actualNamespace } = _source;

expect(actualNamespace).to.eql(undefined);
};

return {
createTest,
createExpectLegacyForbidden,
createExpectSpaceAwareResults,
expectNotSpaceAwareResults,
notSpaceAwareType,
spaceAwareType,
expectNotSpaceAwareRbacForbidden: createExpectRbacForbidden(notSpaceAwareType),
expectSpaceAwareRbacForbidden: createExpectRbacForbidden(spaceAwareType),
};
}
Loading

0 comments on commit f707e03

Please sign in to comment.