Skip to content

Commit

Permalink
Fix API integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jportner committed Oct 2, 2020
1 parent cb006b1 commit 08eaf51
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
9 changes: 4 additions & 5 deletions x-pack/test/spaces_api_integration/common/suites/share_add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export interface ShareAddTestCase {
id: string;
namespaces: string[];
failure?: 400 | 403 | 404;
fail403Param?: string;
}

const TYPE = 'sharedtype';
Expand All @@ -38,13 +37,14 @@ const getTestTitle = ({ id, namespaces }: ShareAddTestCase) =>
`{id: ${id}, namespaces: [${namespaces.join(',')}]}`;

export function shareAddTestSuiteFactory(esArchiver: any, supertest: SuperTest<any>) {
const expectForbidden = expectResponses.forbiddenTypes('share_to_space');
const expectResponseBody = (testCase: ShareAddTestCase): ExpectResponseBody => async (
response: Record<string, any>
) => {
const { id, failure, fail403Param } = testCase;
const { id, failure } = testCase;
const object = response.body;
if (failure === 403) {
await expectResponses.forbiddenTypes(fail403Param!)(TYPE)(response);
await expectForbidden(TYPE)(response);
} else if (failure === 404) {
const error = SavedObjectsErrorHelpers.createGenericNotFoundError(TYPE, id);
expect(object.error).to.eql(error.output.payload.error);
Expand All @@ -59,13 +59,12 @@ export function shareAddTestSuiteFactory(esArchiver: any, supertest: SuperTest<a
forbidden: boolean,
options?: {
responseBodyOverride?: ExpectResponseBody;
fail403Param?: string;
}
): ShareAddTestDefinition[] => {
let cases = Array.isArray(testCases) ? testCases : [testCases];
if (forbidden) {
// override the expected result in each test case
cases = cases.map((x) => ({ ...x, failure: 403, fail403Param: options?.fail403Param }));
cases = cases.map((x) => ({ ...x, failure: 403 }));
}
return cases.map((x) => ({
title: getTestTitle(x),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const createRequest = ({ id, namespaces }: ShareRemoveTestCase) => ({
});

export function shareRemoveTestSuiteFactory(esArchiver: any, supertest: SuperTest<any>) {
const expectForbidden = expectResponses.forbiddenTypes('delete');
const expectForbidden = expectResponses.forbiddenTypes('share_to_space');
const expectResponseBody = (testCase: ShareRemoveTestCase): ExpectResponseBody => async (
response: Record<string, any>
) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,20 @@ export default function ({ getService }: TestInvoker) {
const otherSpaceId = spaceId === DEFAULT_SPACE_ID ? SPACE_1_ID : DEFAULT_SPACE_ID;
const otherSpace = calculateSingleSpaceAuthZ(testCases, otherSpaceId);
return {
unauthorized: createTestDefinitions(testCases, true, { fail403Param: 'create' }),
unauthorized: createTestDefinitions(testCases, true),
authorizedInSpace: [
createTestDefinitions(thisSpace.targetsAllSpaces, true, { fail403Param: 'create' }),
createTestDefinitions(thisSpace.targetsOtherSpace, true, { fail403Param: 'create' }),
createTestDefinitions(thisSpace.targetsAllSpaces, true),
createTestDefinitions(thisSpace.targetsOtherSpace, true),
createTestDefinitions(thisSpace.doesntExistInThisSpace, false),
createTestDefinitions(thisSpace.existsInThisSpace, false),
].flat(),
authorizedInOtherSpace: [
createTestDefinitions(thisSpace.targetsAllSpaces, true, { fail403Param: 'create' }),
createTestDefinitions(otherSpace.targetsOtherSpace, true, { fail403Param: 'create' }),
// If the preflight GET request fails, it will return a 404 error; users who are authorized to create saved objects in the target
// space(s) but are not authorized to update saved objects in this space will see a 403 error instead of 404. This is a safeguard to
createTestDefinitions(thisSpace.targetsAllSpaces, true),
createTestDefinitions(otherSpace.targetsOtherSpace, true),
// If the preflight GET request fails, it will return a 404 error; users who are authorized to share saved objects in the target
// space(s) but are not authorized to share saved objects in this space will see a 403 error instead of 404. This is a safeguard to
// prevent potential information disclosure of the spaces that a given saved object may exist in.
createTestDefinitions(otherSpace.doesntExistInThisSpace, true, { fail403Param: 'update' }),
createTestDefinitions(otherSpace.doesntExistInThisSpace, true),
createTestDefinitions(otherSpace.existsInThisSpace, false),
].flat(),
authorized: createTestDefinitions(testCases, false),
Expand Down

0 comments on commit 08eaf51

Please sign in to comment.