Skip to content

Commit

Permalink
Making the space suites define their own test expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
kobelb committed Sep 7, 2018
1 parent 5407866 commit de2f994
Show file tree
Hide file tree
Showing 13 changed files with 207 additions and 199 deletions.
22 changes: 3 additions & 19 deletions x-pack/test/spaces_api_integration/common/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,10 @@

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

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

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;
Expand Down
46 changes: 31 additions & 15 deletions x-pack/test/spaces_api_integration/common/suites/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,35 @@
import expect from 'expect.js';
import { SuperTest } from 'supertest';
import { getUrlPrefix } from '../lib/space_test_utils';
import { DescribeFn, TestOptions } from '../lib/types';
import { DescribeFn, TestDefinitionAuthentication } from '../lib/types';

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

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

interface CreateTests {
newSpace: CreateTestWithSpace;
alreadyExists: CreateTestWithoutSpace;
reservedSpecified: CreateTestWithSpace;
}

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

export function createTestSuiteFactory(esArchiver: any, supertest: SuperTest<any>) {
const makeCreateTest = (describeFn: DescribeFn) => (
description: string,
{
auth = {
username: undefined,
password: undefined,
},
spaceId,
tests,
}: TestOptions
{ auth = {}, spaceId, tests }: CreateTestDefinition
) => {
describeFn(description, () => {
before(() => esArchiver.load('saved_objects/spaces'));
Expand Down Expand Up @@ -69,7 +85,7 @@ export function createTestSuiteFactory(esArchiver: any, supertest: SuperTest<any
expect(resp.body).to.eql(expectedResult);
};

const createExpectConflictResponse = () => (resp: any) => {
const expectConflictResponse = (resp: any) => {
const spaceId = 'space_1';
expect(resp.body).to.only.have.keys(['error', 'message', 'statusCode']);
expect(resp.body.error).to.equal('Conflict');
Expand All @@ -79,27 +95,27 @@ export function createTestSuiteFactory(esArchiver: any, supertest: SuperTest<any
);
};

const createExpectForbiddenResponse = () => (resp: any) => {
const expectRbacForbiddenResponse = (resp: any) => {
expect(resp.body).to.eql({
statusCode: 403,
error: 'Forbidden',
message: 'Unauthorized to create spaces',
});
};

const createExpectLegacyForbiddenResponse = () => (resp: any) => {
const createExpectLegacyForbiddenResponse = (username: string) => (resp: any) => {
expect(resp.body).to.eql({
statusCode: 403,
error: 'Forbidden',
message: `action [indices:data/write/index] is unauthorized for user [a_kibana_legacy_dashboard_only_user]: [security_exception] action [indices:data/write/index] is unauthorized for user [a_kibana_legacy_dashboard_only_user]`,
message: `action [indices:data/write/index] is unauthorized for user [${username}]: [security_exception] action [indices:data/write/index] is unauthorized for user [${username}]`,
});
};

return {
createTest,
createExpectResult,
createExpectConflictResponse,
createExpectForbiddenResponse,
expectConflictResponse,
expectRbacForbiddenResponse,
createExpectLegacyForbiddenResponse,
};
}
50 changes: 30 additions & 20 deletions x-pack/test/spaces_api_integration/common/suites/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,29 @@
import expect from 'expect.js';
import { SuperTest } from 'supertest';
import { getUrlPrefix } from '../lib/space_test_utils';
import { DescribeFn, TestOptions } from '../lib/types';
import { DescribeFn, TestDefinitionAuthentication } from '../lib/types';

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

interface DeleteTests {
exists: DeleteTest;
reservedSpace: DeleteTest;
doesntExist: DeleteTest;
}

interface DeleteTestDefinition {
auth?: TestDefinitionAuthentication;
spaceId: string;
tests: DeleteTests;
}

export function deleteTestSuiteFactory(esArchiver: any, supertest: SuperTest<any>) {
const makeDeleteTest = (describeFn: DescribeFn) => (
description: string,
{
auth = {
username: undefined,
password: undefined,
},
spaceId,
tests,
}: TestOptions
{ auth = {}, spaceId, tests }: DeleteTestDefinition
) => {
describeFn(description, () => {
before(() => esArchiver.load('saved_objects/spaces'));
Expand Down Expand Up @@ -60,49 +70,49 @@ export function deleteTestSuiteFactory(esArchiver: any, supertest: SuperTest<any
expect(resp.body).to.eql(expectedResult);
};

const createExpectEmptyResult = () => (resp: any) => {
const expectEmptyResult = (resp: any) => {
expect(resp.body).to.eql('');
};

const createExpectNotFoundResult = () => (resp: any) => {
const expectNotFoundResult = (resp: any) => {
expect(resp.body).to.eql({
error: 'Not Found',
statusCode: 404,
message: `Saved object [space/space_3] not found`,
});
};

const createExpectReservedSpaceResult = () => (resp: any) => {
const expectReservedSpaceResult = (resp: any) => {
expect(resp.body).to.eql({
error: 'Bad Request',
statusCode: 400,
message: `This Space cannot be deleted because it is reserved.`,
});
};

const createExpectForbiddenResult = () => (resp: any) => {
const expectRbacForbiddenResult = (resp: any) => {
expect(resp.body).to.eql({
statusCode: 403,
error: 'Forbidden',
message: 'Unauthorized to delete spaces',
});
};

const createExpectLegacyForbiddenResult = () => (resp: any) => {
const createExpectLegacyForbiddenResult = (username: string) => (resp: any) => {
expect(resp.body).to.eql({
statusCode: 403,
error: 'Forbidden',
message: `action [indices:data/write/delete] is unauthorized for user [a_kibana_legacy_dashboard_only_user]: [security_exception] action [indices:data/write/delete] is unauthorized for user [a_kibana_legacy_dashboard_only_user]`,
message: `action [indices:data/write/delete] is unauthorized for user [${username}]: [security_exception] action [indices:data/write/delete] is unauthorized for user [${username}]`,
});
};

return {
deleteTest,
createExpectResult,
createExpectForbiddenResult,
createExpectEmptyResult,
createExpectNotFoundResult,
createExpectReservedSpaceResult,
createExpectLegacyForbiddenResult,
createExpectResult,
expectRbacForbiddenResult,
expectEmptyResult,
expectNotFoundResult,
expectReservedSpaceResult,
};
}
37 changes: 18 additions & 19 deletions x-pack/test/spaces_api_integration/common/suites/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,30 @@
import expect from 'expect.js';
import { SuperAgent } from 'superagent';
import { getUrlPrefix } from '../lib/space_test_utils';
import { DescribeFn, TestOptions } from '../lib/types';
import { DescribeFn, TestDefinitionAuthentication } from '../lib/types';

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

interface GetTests {
default: GetTest;
}

interface GetTestDefinition {
auth?: TestDefinitionAuthentication;
currentSpaceId: string;
spaceId: string;
tests: GetTests;
}

export function getTestSuiteFactory(esArchiver: any, supertest: SuperAgent<any>) {
const nonExistantSpaceId = 'not-a-space';

const makeGetTest = (describeFn: DescribeFn) => (
description: string,
{
auth = {
username: undefined,
password: undefined,
},
currentSpaceId,
spaceId,
tests,
}: TestOptions
{ auth = {}, currentSpaceId, spaceId, tests }: GetTestDefinition
) => {
describeFn(description, () => {
before(() => esArchiver.load('saved_objects/spaces'));
Expand Down Expand Up @@ -73,14 +81,6 @@ export function getTestSuiteFactory(esArchiver: any, supertest: SuperAgent<any>)
});
};

const createExpectReservedSpaceResult = () => (resp: any) => {
expect(resp.body).to.eql({
error: 'Bad Request',
statusCode: 400,
message: `This Space cannot be deleted because it is reserved.`,
});
};

const createExpectForbiddenResult = (spaceId: string) => (resp: any) => {
expect(resp.body).to.eql({
statusCode: 403,
Expand All @@ -96,6 +96,5 @@ export function getTestSuiteFactory(esArchiver: any, supertest: SuperAgent<any>)
createExpectForbiddenResult,
createExpectEmptyResult,
createExpectNotFoundResult,
createExpectReservedSpaceResult,
};
}
57 changes: 19 additions & 38 deletions x-pack/test/spaces_api_integration/common/suites/get_all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@
import expect from 'expect.js';
import { SuperTest } from 'supertest';
import { getUrlPrefix } from '../lib/space_test_utils';
import { DescribeFn, TestOptions } from '../lib/types';
import { DescribeFn, TestDefinitionAuthentication } from '../lib/types';

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

interface GetAllTests {
exists: GetAllTest;
}

interface GetAllTestDefinition {
auth?: TestDefinitionAuthentication;
spaceId: string;
tests: GetAllTests;
}

export function getAllTestSuiteFactory(esArchiver: any, supertest: SuperTest<any>) {
const makeGetAllTest = (describeFn: DescribeFn) => (
description: string,
{
auth = {
username: undefined,
password: undefined,
},
spaceId,
tests,
}: TestOptions
{ auth = {}, spaceId, tests }: GetAllTestDefinition
) => {
describeFn(description, () => {
before(() => esArchiver.load('saved_objects/spaces'));
Expand Down Expand Up @@ -58,40 +66,13 @@ export function getAllTestSuiteFactory(esArchiver: any, supertest: SuperTest<any
expect(resp.body).to.eql(expectedBody);
};

const createExpectEmptyResult = () => (resp: any) => {
const expectEmptyResult = (resp: any) => {
expect(resp.body).to.eql('');
};

const createExpectNotFoundResult = () => (resp: any) => {
expect(resp.body).to.eql({
error: 'Not Found',
statusCode: 404,
message: `Saved object [space/space_3] not found`,
});
};

const createExpectReservedSpaceResult = () => (resp: any) => {
expect(resp.body).to.eql({
error: 'Bad Request',
statusCode: 400,
message: `This Space cannot be deleted because it is reserved.`,
});
};

const createExpectForbiddenResult = () => (resp: any) => {
expect(resp.body).to.eql({
statusCode: 403,
error: 'Forbidden',
message: 'Unauthorized to delete spaces',
});
};

return {
getAllTest,
createExpectResults,
createExpectForbiddenResult,
createExpectEmptyResult,
createExpectNotFoundResult,
createExpectReservedSpaceResult,
expectEmptyResult,
};
}
Loading

0 comments on commit de2f994

Please sign in to comment.