Change action create and update APIs to only return id (#39899)

* Change action create and update APIs to only return id

* Fix broken tests
This commit is contained in:
Mike Côté 2019-07-02 09:20:26 -04:00 committed by GitHub
parent b2a064bbe8
commit 4ebacb33d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 111 additions and 113 deletions

View file

@ -36,7 +36,7 @@ it('creates an action with proper parameters', async () => {
],
},
};
const expectedResult = {
const createResult = {
id: '1',
type: 'action',
attributes: {
@ -57,11 +57,11 @@ it('creates an action with proper parameters', async () => {
],
};
actionsClient.create.mockResolvedValueOnce(expectedResult);
actionsClient.create.mockResolvedValueOnce(createResult);
const { payload, statusCode } = await server.inject(request);
expect(statusCode).toBe(200);
const response = JSON.parse(payload);
expect(response).toEqual(expectedResult);
expect(response).toEqual({ id: '1' });
expect(actionsClient.create).toHaveBeenCalledTimes(1);
expect(actionsClient.create.mock.calls[0]).toMatchInlineSnapshot(`
Array [

View file

@ -59,13 +59,15 @@ export function createRoute(server: Hapi.Server) {
async handler(request: CreateRequest) {
const actionsClient = request.getActionsClient!();
return await actionsClient.create({
const createdAction = await actionsClient.create({
attributes: request.payload.attributes,
options: {
migrationVersion: request.payload.migrationVersion,
references: request.payload.references,
},
});
return { id: createdAction.id };
},
});
}

View file

@ -33,7 +33,7 @@ it('calls the update function with proper parameters', async () => {
],
},
};
const expectedResult = {
const updateResult = {
id: '1',
type: 'action',
attributes: {
@ -50,11 +50,11 @@ it('calls the update function with proper parameters', async () => {
],
};
actionsClient.update.mockResolvedValueOnce(expectedResult);
actionsClient.update.mockResolvedValueOnce(updateResult);
const { payload, statusCode } = await server.inject(request);
expect(statusCode).toBe(200);
const response = JSON.parse(payload);
expect(response).toEqual(expectedResult);
expect(response).toEqual({ id: '1' });
expect(actionsClient.update).toHaveBeenCalledTimes(1);
expect(actionsClient.update.mock.calls[0]).toMatchInlineSnapshot(`
Array [

View file

@ -62,11 +62,12 @@ export function updateRoute(server: Hapi.Server) {
const { attributes, version, references } = request.payload;
const options = { version, references };
const actionsClient = request.getActionsClient!();
return await actionsClient.update({
await actionsClient.update({
id,
attributes,
options,
});
return { id };
},
});
}

View file

@ -17,7 +17,7 @@ export default function emailTest({ getService }: KibanaFunctionalTestDefaultPro
after(() => esArchiver.unload('empty_kibana'));
it('should return 200 when creating an email action successfully', async () => {
await supertest
const { body: createdAction } = await supertest
.post('/api/action')
.set('kbn-xsrf', 'foo')
.send({
@ -32,25 +32,33 @@ export default function emailTest({ getService }: KibanaFunctionalTestDefaultPro
},
},
})
.expect(200)
.then((resp: any) => {
expect(resp.body).to.eql({
type: 'action',
id: resp.body.id,
attributes: {
description: 'An email action',
actionTypeId: '.email',
actionTypeConfig: {
from: 'bob@example.com',
service: 'gmail',
},
},
references: [],
updated_at: resp.body.updated_at,
version: resp.body.version,
});
expect(typeof resp.body.id).to.be('string');
});
.expect(200);
expect(createdAction).to.eql({
id: createdAction.id,
});
expect(typeof createdAction.id).to.be('string');
const { body: fetchedAction } = await supertest
.get(`/api/action/${createdAction.id}`)
.expect(200);
expect(fetchedAction).to.eql({
type: 'action',
id: fetchedAction.id,
attributes: {
description: 'An email action',
actionTypeId: '.email',
actionTypeConfig: {
from: 'bob@example.com',
service: 'gmail',
},
},
references: [],
updated_at: fetchedAction.updated_at,
version: fetchedAction.version,
});
});
it('should respond with a 400 Bad Request when creating an email action with an invalid config', async () => {

View file

@ -17,7 +17,7 @@ export default function serverLogTest({ getService }: KibanaFunctionalTestDefaul
after(() => esArchiver.unload('empty_kibana'));
it('should return 200 when creating a builtin server-log action', async () => {
await supertest
const { body: createdAction } = await supertest
.post('/api/action')
.set('kbn-xsrf', 'foo')
.send({
@ -27,22 +27,30 @@ export default function serverLogTest({ getService }: KibanaFunctionalTestDefaul
actionTypeConfig: {},
},
})
.expect(200)
.then((resp: any) => {
expect(resp.body).to.eql({
type: 'action',
id: resp.body.id,
attributes: {
description: 'A server.log action',
actionTypeId: '.server-log',
actionTypeConfig: {},
},
references: [],
updated_at: resp.body.updated_at,
version: resp.body.version,
});
expect(typeof resp.body.id).to.be('string');
});
.expect(200);
expect(createdAction).to.eql({
id: createdAction.id,
});
expect(typeof createdAction.id).to.be('string');
const { body: fetchedAction } = await supertest
.get(`/api/action/${createdAction.id}`)
.expect(200);
expect(fetchedAction).to.eql({
type: 'action',
id: fetchedAction.id,
attributes: {
description: 'A server.log action',
actionTypeId: '.server-log',
actionTypeConfig: {},
},
references: [],
updated_at: fetchedAction.updated_at,
version: fetchedAction.version,
});
});
});
}

View file

@ -17,7 +17,7 @@ export default function slackTest({ getService }: KibanaFunctionalTestDefaultPro
after(() => esArchiver.unload('empty_kibana'));
it('should return 200 when creating a slack action successfully', async () => {
await supertest
const { body: createdAction } = await supertest
.post('/api/action')
.set('kbn-xsrf', 'foo')
.send({
@ -29,22 +29,30 @@ export default function slackTest({ getService }: KibanaFunctionalTestDefaultPro
},
},
})
.expect(200)
.then((resp: any) => {
expect(resp.body).to.eql({
type: 'action',
id: resp.body.id,
attributes: {
description: 'A slack action',
actionTypeId: '.slack',
actionTypeConfig: {},
},
references: [],
updated_at: resp.body.updated_at,
version: resp.body.version,
});
expect(typeof resp.body.id).to.be('string');
});
.expect(200);
expect(createdAction).to.eql({
id: createdAction.id,
});
expect(typeof createdAction.id).to.be('string');
const { body: fetchedAction } = await supertest
.get(`/api/action/${createdAction.id}`)
.expect(200);
expect(fetchedAction).to.eql({
type: 'action',
id: fetchedAction.id,
attributes: {
description: 'A slack action',
actionTypeId: '.slack',
actionTypeConfig: {},
},
references: [],
updated_at: fetchedAction.updated_at,
version: fetchedAction.version,
});
});
it('should respond with a 400 Bad Request when creating a slack action with no webhookUrl', async () => {

View file

@ -32,18 +32,7 @@ export default function createActionTests({ getService }: KibanaFunctionalTestDe
.expect(200)
.then((resp: any) => {
expect(resp.body).to.eql({
type: 'action',
id: resp.body.id,
attributes: {
description: 'My action',
actionTypeId: 'test.index-record',
actionTypeConfig: {
unencrypted: `This value shouldn't get encrypted`,
},
},
references: [],
updated_at: resp.body.updated_at,
version: resp.body.version,
});
expect(typeof resp.body.id).to.be('string');
});

View file

@ -124,17 +124,6 @@ export default function({ getService }: KibanaFunctionalTestDefaultProviders) {
.expect(200);
expect(updatedAction).to.eql({
id: ES_ARCHIVER_ACTION_ID,
type: 'action',
updated_at: updatedAction.updated_at,
version: updatedAction.version,
references: [],
attributes: {
description: 'My action updated',
actionTypeId: 'test.index-record',
actionTypeConfig: {
unencrypted: `This value shouldn't get encrypted`,
},
},
});
await supertest
.post(`/api/action/${ES_ARCHIVER_ACTION_ID}/_fire`)

View file

@ -34,17 +34,6 @@ export default function updateActionTests({ getService }: KibanaFunctionalTestDe
.then((resp: any) => {
expect(resp.body).to.eql({
id: ES_ARCHIVER_ACTION_ID,
type: 'action',
references: [],
version: resp.body.version,
updated_at: resp.body.updated_at,
attributes: {
actionTypeId: 'test.index-record',
description: 'My action updated',
actionTypeConfig: {
unencrypted: `This value shouldn't get encrypted`,
},
},
});
});
});
@ -75,7 +64,7 @@ export default function updateActionTests({ getService }: KibanaFunctionalTestDe
});
it('should not return encrypted attributes', async () => {
await supertest
const { body: updatedAction } = await supertest
.put(`/api/action/${ES_ARCHIVER_ACTION_ID}`)
.set('kbn-xsrf', 'foo')
.send({
@ -87,23 +76,27 @@ export default function updateActionTests({ getService }: KibanaFunctionalTestDe
},
},
})
.expect(200)
.then((resp: any) => {
expect(resp.body).to.eql({
id: ES_ARCHIVER_ACTION_ID,
type: 'action',
references: [],
version: resp.body.version,
updated_at: resp.body.updated_at,
attributes: {
actionTypeId: 'test.index-record',
description: 'My action updated',
actionTypeConfig: {
unencrypted: `This value shouldn't get encrypted`,
},
},
});
});
.expect(200);
expect(updatedAction).to.eql({
id: ES_ARCHIVER_ACTION_ID,
});
const { body: fetchedAction } = await supertest
.get(`/api/action/${ES_ARCHIVER_ACTION_ID}`)
.expect(200);
expect(fetchedAction).to.eql({
id: ES_ARCHIVER_ACTION_ID,
type: 'action',
references: [],
version: fetchedAction.version,
updated_at: fetchedAction.updated_at,
attributes: {
actionTypeId: 'test.index-record',
description: 'My action updated',
actionTypeConfig: {
unencrypted: `This value shouldn't get encrypted`,
},
},
});
});
it('should return 404 when updating a non existing document', async () => {