Skip to content

Commit

Permalink
Typescriptifying Get
Browse files Browse the repository at this point in the history
  • Loading branch information
kobelb committed Sep 7, 2018
1 parent f53f2ab commit 369a429
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,46 @@
* you may not use this file except in compliance with the Elastic License.
*/
import expect from 'expect.js';
import { getIdPrefix, getUrlPrefix } from "../../lib/space_test_utils";
import { DEFAULT_SPACE_ID } from '../../../../../plugins/spaces/common/constants';
import { DescribeFn, TestDefinitionAuthentication } from '../../../common/lib/types';
import { getIdPrefix, getUrlPrefix } from '../../lib/space_test_utils';

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

interface GetTests {
exists: GetTest;
doesntExist: GetTest;
}

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

export function getTestSuiteFactory(esArchiver: any, supertest: SuperTest<any>) {
const existsId = 'dd7caf20-9efd-11e7-acb3-3dab96693fab';
const doesntExistId = 'foobar';
const makeGetTest = (describeFn) => (description, {
auth = {
username: undefined,
password: undefined,
},
spaceId = DEFAULT_SPACE_ID,
otherSpaceId = spaceId,
tests
}) => {
const makeGetTest = (describeFn: DescribeFn) => (
description: string,
definition: GetTestDefinition
) => {
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.exists.statusCode}`, async () => {
await supertest
.get(`${getUrlPrefix(spaceId)}/api/saved_objects/visualization/${getIdPrefix(otherSpaceId)}${existsId}`)
.get(
`${getUrlPrefix(spaceId)}/api/saved_objects/visualization/${getIdPrefix(
spaceId
)}${existsId}`
)
.auth(auth.username, auth.password)
.expect(tests.exists.statusCode)
.then(tests.exists.response);
Expand All @@ -34,7 +52,11 @@ export function getTestSuiteFactory(esArchiver, supertest) {
describe('document does not exist', () => {
it(`should return ${tests.doesntExist.statusCode}`, async () => {
await supertest
.get(`${getUrlPrefix(spaceId)}/api/saved_objects/visualization/${getIdPrefix(otherSpaceId)}${doesntExistId}`)
.get(
`${getUrlPrefix(spaceId)}/api/saved_objects/visualization/${getIdPrefix(
spaceId
)}${doesntExistId}`
)
.auth(auth.username, auth.password)
.expect(tests.doesntExist.statusCode)
.then(tests.doesntExist.response);
Expand All @@ -46,16 +68,16 @@ export function getTestSuiteFactory(esArchiver, supertest) {
const getTest = makeGetTest(describe);
getTest.only = makeGetTest(describe.only);

const createExpectLegacyForbidden = username => resp => {
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/read/get] is unauthorized for user [${username}]: [security_exception] action [indices:data/read/get] is unauthorized for user [${username}]`
message: `action [indices:data/read/get] is unauthorized for user [${username}]: [security_exception] action [indices:data/read/get] is unauthorized for user [${username}]`,
});
};

const createExpectNotFound = (id, spaceId = DEFAULT_SPACE_ID) => (resp) => {
const createExpectNotFound = (id: string, spaceId = DEFAULT_SPACE_ID) => (resp: any) => {
expect(resp.body).to.eql({
error: 'Not Found',
message: `Saved object [visualization/${getIdPrefix(spaceId)}${id}] not found`,
Expand All @@ -71,15 +93,15 @@ export function getTestSuiteFactory(esArchiver, supertest) {
return createExpectNotFound(existsId, spaceId);
};

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

const createExpectResults = (spaceId = DEFAULT_SPACE_ID) => (resp) => {
const createExpectResults = (spaceId = DEFAULT_SPACE_ID) => (resp: any) => {
expect(resp.body).to.eql({
id: `${getIdPrefix(spaceId)}dd7caf20-9efd-11e7-acb3-3dab96693fab`,
type: 'visualization',
Expand All @@ -92,8 +114,8 @@ export function getTestSuiteFactory(esArchiver, supertest) {
// cheat for some of the more complex attributes
visState: resp.body.attributes.visState,
uiStateJSON: resp.body.attributes.uiStateJSON,
kibanaSavedObjectMeta: resp.body.attributes.kibanaSavedObjectMeta
}
kibanaSavedObjectMeta: resp.body.attributes.kibanaSavedObjectMeta,
},
});
};

Expand All @@ -103,6 +125,6 @@ export function getTestSuiteFactory(esArchiver, supertest) {
createExpectLegacyForbidden,
createExpectRbacForbidden,
createExpectResults,
getTest
getTest,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
*/

import { AUTHENTICATION } from '../../../common/lib/authentication';
import { getTestSuiteFactory } from '../../../common/suites/saved_objects/get';
import { SPACES } from '../../../common/lib/spaces';
import { TestInvoker } from '../../../common/lib/types';
import { getTestSuiteFactory } from '../../../common/suites/saved_objects/get';

export default function ({ getService }) {
// tslint:disable:no-default-export
export default function({ getService }: TestInvoker) {
const supertest = getService('supertestWithoutAuth');
const esArchiver = getService('esArchiver');

Expand All @@ -17,7 +19,7 @@ export default function ({ getService }) {
createExpectLegacyForbidden,
createExpectRbacForbidden,
createExpectResults,
getTest
getTest,
} = getTestSuiteFactory(esArchiver, supertest);

describe('get', () => {
Expand All @@ -33,13 +35,8 @@ export default function ({ getService }) {
userWithAllAtSpace: AUTHENTICATION.KIBANA_RBAC_SPACE_1_ALL_USER,
userWithReadAtSpace: AUTHENTICATION.KIBANA_RBAC_SPACE_1_READ_USER,
userWithAllAtOtherSpace: AUTHENTICATION.KIBANA_RBAC_DEFAULT_SPACE_ALL_USER,
}
].forEach(({
spaceId,
userWithAllAtSpace,
userWithReadAtSpace,
userWithAllAtOtherSpace,
}) => {
},
].forEach(({ spaceId, userWithAllAtSpace, userWithReadAtSpace, userWithAllAtOtherSpace }) => {
getTest(AUTHENTICATION.NOT_A_KIBANA_USER.USERNAME, {
auth: {
username: AUTHENTICATION.NOT_A_KIBANA_USER.USERNAME,
Expand All @@ -55,10 +52,9 @@ export default function ({ getService }) {
statusCode: 403,
response: createExpectLegacyForbidden(AUTHENTICATION.NOT_A_KIBANA_USER.USERNAME),
},
}
},
});


getTest(AUTHENTICATION.SUPERUSER.USERNAME, {
auth: {
username: AUTHENTICATION.SUPERUSER.USERNAME,
Expand All @@ -74,7 +70,7 @@ export default function ({ getService }) {
statusCode: 404,
response: createExpectDoesntExistNotFound(spaceId),
},
}
},
});

getTest(AUTHENTICATION.KIBANA_LEGACY_USER.USERNAME, {
Expand All @@ -92,7 +88,7 @@ export default function ({ getService }) {
statusCode: 404,
response: createExpectDoesntExistNotFound(spaceId),
},
}
},
});

getTest(AUTHENTICATION.KIBANA_LEGACY_DASHBOARD_ONLY_USER.USERNAME, {
Expand All @@ -110,7 +106,7 @@ export default function ({ getService }) {
statusCode: 404,
response: createExpectDoesntExistNotFound(spaceId),
},
}
},
});

getTest(AUTHENTICATION.KIBANA_DUAL_PRIVILEGES_USER.USERNAME, {
Expand All @@ -128,7 +124,7 @@ export default function ({ getService }) {
statusCode: 404,
response: createExpectDoesntExistNotFound(spaceId),
},
}
},
});

getTest(AUTHENTICATION.KIBANA_DUAL_PRIVILEGES_DASHBOARD_ONLY_USER.USERNAME, {
Expand All @@ -146,7 +142,7 @@ export default function ({ getService }) {
statusCode: 404,
response: createExpectDoesntExistNotFound(spaceId),
},
}
},
});

getTest(AUTHENTICATION.KIBANA_RBAC_USER.USERNAME, {
Expand All @@ -164,7 +160,7 @@ export default function ({ getService }) {
statusCode: 404,
response: createExpectDoesntExistNotFound(spaceId),
},
}
},
});

getTest(AUTHENTICATION.KIBANA_RBAC_DASHBOARD_ONLY_USER.USERNAME, {
Expand All @@ -182,7 +178,7 @@ export default function ({ getService }) {
statusCode: 404,
response: createExpectDoesntExistNotFound(spaceId),
},
}
},
});

getTest(`${userWithAllAtSpace.USERNAME} user`, {
Expand All @@ -200,7 +196,7 @@ export default function ({ getService }) {
statusCode: 404,
response: createExpectDoesntExistNotFound(spaceId),
},
}
},
});

getTest(`${userWithReadAtSpace.USERNAME} user`, {
Expand All @@ -218,7 +214,7 @@ export default function ({ getService }) {
statusCode: 404,
response: createExpectDoesntExistNotFound(spaceId),
},
}
},
});

getTest(`${userWithAllAtOtherSpace.USERNAME} user`, {
Expand All @@ -236,7 +232,7 @@ export default function ({ getService }) {
statusCode: 403,
response: createExpectRbacForbidden(),
},
}
},
});
});
});
Expand Down
Loading

0 comments on commit 369a429

Please sign in to comment.