Skip to content

Commit

Permalink
Adding update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kobelb committed Sep 7, 2018
1 parent f60e953 commit 71c2122
Show file tree
Hide file tree
Showing 10 changed files with 836 additions and 390 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@
"value": {
"index": ".kibana",
"type": "doc",
"id": "chapo:8121a00-8efd-21e7-1cb3-34ab96643444",
"id": "chapo:8121a00-8efd-21e7-1cb3-34ab966434445",
"source": {
"type": "chapo",
"updated_at": "2017-09-21T18:59:16.270Z",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function deleteTestSuiteFactory(esArchiver: any, supertest: SuperTest<any
} when deleting a non-space-aware doc`, async () =>
await supertest
.delete(
`${getUrlPrefix(spaceId)}/api/saved_objects/chapo/8121a00-8efd-21e7-1cb3-34ab96643444`
`${getUrlPrefix(spaceId)}/api/saved_objects/chapo/8121a00-8efd-21e7-1cb3-34ab966434445`
)
.auth(auth.username, auth.password)
.expect(tests.notSpaceAware.statusCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export function findTestSuiteFactory(esArchiver, supertest) {
attributes: resp.body.saved_objects[3].attributes
},
{
id: `8121a00-8efd-21e7-1cb3-34ab96643444`,
id: `8121a00-8efd-21e7-1cb3-34ab966434445`,
type: 'chapo',
updated_at: '2017-09-21T18:59:16.270Z',
version: 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import expect from 'expect.js';
import { DEFAULT_SPACE_ID } from '../../../../../plugins/spaces/common/constants';
import { getIdPrefix, getUrlPrefix } from '../../lib/space_test_utils';
import { DescribeFn, TestDefinitionAuthentication } from '../../lib/types';

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

interface UpdateTests {
spaceAware: UpdateTest;
notSpaceAware: UpdateTest;
doesntExist: UpdateTest;
}

interface UpdateTestDefinition {
auth?: TestDefinitionAuthentication;
spaceId?: string;
tests: UpdateTests;
}

export function updateTestSuiteFactory(esArchiver: any, supertest: SuperTest<any>) {
const makeUpdateTest = (describeFn: DescribeFn) => (
description: string,
definition: UpdateTestDefinition
) => {
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.spaceAware.statusCode} for a space-aware doc`, async () => {
await supertest
.put(
`${getUrlPrefix(spaceId)}/api/saved_objects/visualization/${getIdPrefix(
spaceId
)}dd7caf20-9efd-11e7-acb3-3dab96693fab`
)
.auth(auth.username, auth.password)
.send({
attributes: {
title: 'My second favorite vis',
},
})
.expect(tests.spaceAware.statusCode)
.then(tests.spaceAware.response);
});

it(`should return ${tests.notSpaceAware.statusCode} for a non space-aware doc`, async () => {
await supertest
.put(
`${getUrlPrefix(spaceId)}/api/saved_objects/chapo/8121a00-8efd-21e7-1cb3-34ab966434445`
)
.auth(auth.username, auth.password)
.send({
attributes: {
name: 'My second favorite',
},
})
.expect(tests.notSpaceAware.statusCode)
.then(tests.notSpaceAware.response);
});

describe('unknown id', () => {
it(`should return ${tests.doesntExist.statusCode}`, async () => {
await supertest
.put(`${getUrlPrefix(spaceId)}/api/saved_objects/visualization/not an id`)
.auth(auth.username, auth.password)
.send({
attributes: {
title: 'My second favorite vis',
},
})
.expect(tests.doesntExist.statusCode)
.then(tests.doesntExist.response);
});
});
});
};

const updateTest = makeUpdateTest(describe);
updateTest.only = makeUpdateTest(describe.only);

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/update] is unauthorized for user [${username}]: [security_exception] action [indices:data/write/update] is unauthorized for user [${username}]`,
});
};

const expectSpaceAwareResults = (resp: any) => {
// loose uuid validation ignoring prefix
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.eql({
id: resp.body.id,
type: 'visualization',
updated_at: resp.body.updated_at,
version: 2,
attributes: {
title: 'My second favorite vis',
},
});
};

const expectNotSpaceAwareResults = (resp: any) => {
// loose uuid validation
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.eql({
id: resp.body.id,
type: 'chapo',
updated_at: resp.body.updated_at,
version: 2,
attributes: {
name: 'My second favorite',
},
});
};

const expectNotFound = (resp: any) => {
expect(resp.body).eql({
statusCode: 404,
error: 'Not Found',
message: 'Saved object [visualization/not an id] not found',
});
};

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

return {
createExpectLegacyForbidden,
expectDoesntExistRbacForbidden: createExpectRbacForbidden('visualization'),
expectNotSpaceAwareResults,
expectNotSpaceAwareRbacForbidden: createExpectRbacForbidden('chapo'),
expectNotFound,
expectSpaceAwareResults,
expectSpaceAwareRbacForbidden: createExpectRbacForbidden('visualization'),
updateTest,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export default function ({ loadTestFile }) {
loadTestFile(require.resolve('./delete'));
loadTestFile(require.resolve('./find'));
loadTestFile(require.resolve('./get'));
loadTestFile(require.resolve('./update'));
});
}
Loading

0 comments on commit 71c2122

Please sign in to comment.