diff --git a/x-pack/test/spaces_api_integration/common/lib/types.ts b/x-pack/test/spaces_api_integration/common/lib/types.ts index a58ec390966302..f149ad02cc1f73 100644 --- a/x-pack/test/spaces_api_integration/common/lib/types.ts +++ b/x-pack/test/spaces_api_integration/common/lib/types.ts @@ -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; diff --git a/x-pack/test/spaces_api_integration/common/suites/create.ts b/x-pack/test/spaces_api_integration/common/suites/create.ts index aa606ffc17100b..d4de96db8ed718 100644 --- a/x-pack/test/spaces_api_integration/common/suites/create.ts +++ b/x-pack/test/spaces_api_integration/common/suites/create.ts @@ -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) { 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')); @@ -69,7 +85,7 @@ export function createTestSuiteFactory(esArchiver: any, supertest: SuperTest (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'); @@ -79,7 +95,7 @@ export function createTestSuiteFactory(esArchiver: any, supertest: SuperTest (resp: any) => { + const expectRbacForbiddenResponse = (resp: any) => { expect(resp.body).to.eql({ statusCode: 403, error: 'Forbidden', @@ -87,19 +103,19 @@ export function createTestSuiteFactory(esArchiver: any, supertest: SuperTest (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, }; } diff --git a/x-pack/test/spaces_api_integration/common/suites/delete.ts b/x-pack/test/spaces_api_integration/common/suites/delete.ts index 78facf746eef9b..5e2c66794826bf 100644 --- a/x-pack/test/spaces_api_integration/common/suites/delete.ts +++ b/x-pack/test/spaces_api_integration/common/suites/delete.ts @@ -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) { 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')); @@ -60,11 +70,11 @@ export function deleteTestSuiteFactory(esArchiver: any, supertest: SuperTest (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, @@ -72,7 +82,7 @@ export function deleteTestSuiteFactory(esArchiver: any, supertest: SuperTest (resp: any) => { + const expectReservedSpaceResult = (resp: any) => { expect(resp.body).to.eql({ error: 'Bad Request', statusCode: 400, @@ -80,7 +90,7 @@ export function deleteTestSuiteFactory(esArchiver: any, supertest: SuperTest (resp: any) => { + const expectRbacForbiddenResult = (resp: any) => { expect(resp.body).to.eql({ statusCode: 403, error: 'Forbidden', @@ -88,21 +98,21 @@ export function deleteTestSuiteFactory(esArchiver: any, supertest: SuperTest (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, }; } diff --git a/x-pack/test/spaces_api_integration/common/suites/get.ts b/x-pack/test/spaces_api_integration/common/suites/get.ts index 84f33e92885366..e2b5f1558c5458 100644 --- a/x-pack/test/spaces_api_integration/common/suites/get.ts +++ b/x-pack/test/spaces_api_integration/common/suites/get.ts @@ -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) { 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')); @@ -73,14 +81,6 @@ export function getTestSuiteFactory(esArchiver: any, supertest: SuperAgent) }); }; - 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, @@ -96,6 +96,5 @@ export function getTestSuiteFactory(esArchiver: any, supertest: SuperAgent) createExpectForbiddenResult, createExpectEmptyResult, createExpectNotFoundResult, - createExpectReservedSpaceResult, }; } diff --git a/x-pack/test/spaces_api_integration/common/suites/get_all.ts b/x-pack/test/spaces_api_integration/common/suites/get_all.ts index b56532b8f0a5bb..e378197e150a19 100644 --- a/x-pack/test/spaces_api_integration/common/suites/get_all.ts +++ b/x-pack/test/spaces_api_integration/common/suites/get_all.ts @@ -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) { 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')); @@ -58,40 +66,13 @@ export function getAllTestSuiteFactory(esArchiver: any, supertest: SuperTest (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, }; } diff --git a/x-pack/test/spaces_api_integration/common/suites/select.ts b/x-pack/test/spaces_api_integration/common/suites/select.ts index 64e2af3cdae05e..62232717074c98 100644 --- a/x-pack/test/spaces_api_integration/common/suites/select.ts +++ b/x-pack/test/spaces_api_integration/common/suites/select.ts @@ -8,22 +8,30 @@ import expect from 'expect.js'; import { SuperTest } from 'supertest'; import { DEFAULT_SPACE_ID } from '../../../../plugins/spaces/common/constants'; import { getUrlPrefix } from '../lib/space_test_utils'; -import { DescribeFn, TestOptions } from '../lib/types'; +import { DescribeFn, TestDefinitionAuthentication } from '../lib/types'; + +interface SelectTest { + statusCode: number; + response: (resp: any) => void; +} + +interface SelectTests { + default: SelectTest; +} + +interface SelectTestDefinition { + auth?: TestDefinitionAuthentication; + currentSpaceId: string; + spaceId: string; + tests: SelectTests; +} export function selectTestSuiteFactory(esArchiver: any, supertest: SuperTest) { const nonExistantSpaceId = 'not-a-space'; const makeSelectTest = (describeFn: DescribeFn) => ( description: string, - { - auth = { - username: undefined, - password: undefined, - }, - currentSpaceId = '', - spaceId = '', - tests, - }: TestOptions + { auth = {}, currentSpaceId, spaceId, tests }: SelectTestDefinition ) => { describeFn(description, () => { before(() => esArchiver.load('saved_objects/spaces')); @@ -84,7 +92,7 @@ export function selectTestSuiteFactory(esArchiver: any, supertest: SuperTest (resp: any) => { + const expectDefaultSpaceResponse = (resp: any) => { expect(resp.body).to.eql({ location: `/app/kibana`, }); @@ -92,7 +100,7 @@ export function selectTestSuiteFactory(esArchiver: any, supertest: SuperTest (resp: any) => { if (spaceId === DEFAULT_SPACE_ID) { - createExpectDefaultSpaceResponse()(resp); + expectDefaultSpaceResponse(resp); } else { expect(resp.body).to.eql({ location: `/s/${spaceId}/app/kibana`, @@ -103,7 +111,7 @@ export function selectTestSuiteFactory(esArchiver: any, supertest: SuperTest void; +} + +interface UpdateTests { + alreadyExists: UpdateTest; + newSpace: UpdateTest; +} + +interface UpdateTestDefinition { + auth?: TestDefinitionAuthentication; + spaceId: string; + tests: UpdateTests; +} export function updateTestSuiteFactory(esArchiver: any, supertest: SuperTest) { const makeUpdateTest = (describeFn: DescribeFn) => ( description: string, - { - auth = { - username: undefined, - password: undefined, - }, - spaceId, - tests, - }: TestOptions + { auth = {}, spaceId, tests }: UpdateTestDefinition ) => { describeFn(description, () => { before(() => esArchiver.load('saved_objects/spaces')); @@ -60,7 +70,7 @@ export function updateTestSuiteFactory(esArchiver: any, supertest: SuperTest (resp: any) => { + const expectRbacForbiddenResult = (resp: any) => { expect(resp.body).to.eql({ statusCode: 403, error: 'Forbidden', @@ -68,11 +78,11 @@ export function updateTestSuiteFactory(esArchiver: any, supertest: SuperTest (resp: any) => { + const createExpectLegacyForbiddenResult = (username: string) => (resp: any) => { expect(resp.body).to.eql({ statusCode: 403, error: 'Forbidden', - message: `action [indices:data/write/update] is unauthorized for user [a_kibana_legacy_dashboard_only_user]: [security_exception] action [indices:data/write/update] is unauthorized for user [a_kibana_legacy_dashboard_only_user]`, + message: `action [indices:data/write/update] is unauthorized for user [${username}]: [security_exception] action [indices:data/write/update] is unauthorized for user [${username}]`, }); }; @@ -80,7 +90,7 @@ export function updateTestSuiteFactory(esArchiver: any, supertest: SuperTest { + describe('spaces api with security', () => { before(async () => { await createUsersAndRoles(es, supertest); }); diff --git a/x-pack/test/spaces_api_integration/security_and_spaces/apis/update.ts b/x-pack/test/spaces_api_integration/security_and_spaces/apis/update.ts index 125514c08ba3cb..a92772c079749a 100644 --- a/x-pack/test/spaces_api_integration/security_and_spaces/apis/update.ts +++ b/x-pack/test/spaces_api_integration/security_and_spaces/apis/update.ts @@ -18,7 +18,7 @@ export default function updateSpaceTestSuite({ getService }: TestInvoker) { updateTest, createExpectResult, createExpectNotFoundResult, - createExpectForbiddenResult, + expectRbacForbiddenResult, createExpectLegacyForbiddenResult, } = updateTestSuiteFactory(esArchiver, supertestWithoutAuth); @@ -186,7 +186,7 @@ export default function updateSpaceTestSuite({ getService }: TestInvoker) { _reserved: true, }, statusCode: 403, - response: createExpectForbiddenResult(), + response: expectRbacForbiddenResult, }, newSpace: { space: { @@ -196,7 +196,7 @@ export default function updateSpaceTestSuite({ getService }: TestInvoker) { color: '#5c5959', }, statusCode: 403, - response: createExpectForbiddenResult(), + response: expectRbacForbiddenResult, }, }, } @@ -221,7 +221,7 @@ export default function updateSpaceTestSuite({ getService }: TestInvoker) { _reserved: true, }, statusCode: 403, - response: createExpectForbiddenResult(), + response: expectRbacForbiddenResult, }, newSpace: { space: { @@ -231,7 +231,7 @@ export default function updateSpaceTestSuite({ getService }: TestInvoker) { color: '#5c5959', }, statusCode: 403, - response: createExpectForbiddenResult(), + response: expectRbacForbiddenResult, }, }, } @@ -256,7 +256,7 @@ export default function updateSpaceTestSuite({ getService }: TestInvoker) { _reserved: true, }, statusCode: 403, - response: createExpectLegacyForbiddenResult(), + response: createExpectLegacyForbiddenResult(scenario.userWithLegacyRead.USERNAME), }, newSpace: { space: { @@ -266,7 +266,7 @@ export default function updateSpaceTestSuite({ getService }: TestInvoker) { color: '#5c5959', }, statusCode: 403, - response: createExpectLegacyForbiddenResult(), + response: createExpectLegacyForbiddenResult(scenario.userWithLegacyRead.USERNAME), }, }, } @@ -288,7 +288,7 @@ export default function updateSpaceTestSuite({ getService }: TestInvoker) { _reserved: true, }, statusCode: 403, - response: createExpectForbiddenResult(), + response: expectRbacForbiddenResult, }, newSpace: { space: { @@ -298,7 +298,7 @@ export default function updateSpaceTestSuite({ getService }: TestInvoker) { color: '#5c5959', }, statusCode: 403, - response: createExpectForbiddenResult(), + response: expectRbacForbiddenResult, }, }, }); @@ -319,7 +319,7 @@ export default function updateSpaceTestSuite({ getService }: TestInvoker) { _reserved: true, }, statusCode: 403, - response: createExpectForbiddenResult(), + response: expectRbacForbiddenResult, }, newSpace: { space: { @@ -329,7 +329,7 @@ export default function updateSpaceTestSuite({ getService }: TestInvoker) { color: '#5c5959', }, statusCode: 403, - response: createExpectForbiddenResult(), + response: expectRbacForbiddenResult, }, }, }); diff --git a/x-pack/test/spaces_api_integration/spaces_only/apis/create.ts b/x-pack/test/spaces_api_integration/spaces_only/apis/create.ts index 7b0f4f7038113e..8a770ca5028ab1 100644 --- a/x-pack/test/spaces_api_integration/spaces_only/apis/create.ts +++ b/x-pack/test/spaces_api_integration/spaces_only/apis/create.ts @@ -13,7 +13,7 @@ export default function createSpacesOnlySuite({ getService }: TestInvoker) { const supertestWithoutAuth = getService('supertestWithoutAuth'); const esArchiver = getService('esArchiver'); - const { createTest, createExpectResult, createExpectConflictResponse } = createTestSuiteFactory( + const { createTest, createExpectResult, expectConflictResponse } = createTestSuiteFactory( esArchiver, supertestWithoutAuth ); @@ -47,7 +47,7 @@ export default function createSpacesOnlySuite({ getService }: TestInvoker) { }, alreadyExists: { statusCode: 409, - response: createExpectConflictResponse(), + response: expectConflictResponse, }, reservedSpecified: { space: { diff --git a/x-pack/test/spaces_api_integration/spaces_only/apis/delete.ts b/x-pack/test/spaces_api_integration/spaces_only/apis/delete.ts index 15c4bb8ab621d2..3b073e377a4e5b 100644 --- a/x-pack/test/spaces_api_integration/spaces_only/apis/delete.ts +++ b/x-pack/test/spaces_api_integration/spaces_only/apis/delete.ts @@ -15,9 +15,9 @@ export default function deleteSpaceTestSuite({ getService }: TestInvoker) { const { deleteTest, - createExpectEmptyResult, - createExpectReservedSpaceResult, - createExpectNotFoundResult, + expectEmptyResult, + expectReservedSpaceResult, + expectNotFoundResult, } = deleteTestSuiteFactory(esArchiver, supertestWithoutAuth); describe('delete', () => { @@ -34,15 +34,15 @@ export default function deleteSpaceTestSuite({ getService }: TestInvoker) { tests: { exists: { statusCode: 204, - response: createExpectEmptyResult(), + response: expectEmptyResult, }, reservedSpace: { statusCode: 400, - response: createExpectReservedSpaceResult(), + response: expectReservedSpaceResult, }, doesntExist: { statusCode: 404, - response: createExpectNotFoundResult(), + response: expectNotFoundResult, }, }, });