From dcd574eb76621e3727e350576bfb677e8c2d2120 Mon Sep 17 00:00:00 2001 From: Gidi Morris Date: Tue, 24 Dec 2019 13:14:01 +0000 Subject: [PATCH 1/9] added createdAt and updatedAt fields in alerting --- x-pack/legacy/plugins/alerting/mappings.json | 6 +++ .../alerting/server/alerts_client.test.ts | 38 +++++++++++++++++++ .../plugins/alerting/server/alerts_client.ts | 25 +++++++++++- .../alerting/server/routes/create.test.ts | 2 + .../alerting/server/routes/get.test.ts | 2 + .../alerting/server/routes/update.test.ts | 2 + .../legacy/plugins/alerting/server/types.ts | 4 ++ .../routes/__mocks__/request_responses.ts | 2 + .../tests/alerting/create.ts | 2 + .../tests/alerting/find.ts | 8 ++++ .../security_and_spaces/tests/alerting/get.ts | 4 ++ .../tests/alerting/update.ts | 4 ++ .../spaces_only/tests/alerting/create.ts | 4 ++ .../spaces_only/tests/alerting/find.ts | 4 ++ .../spaces_only/tests/alerting/get.ts | 4 ++ .../spaces_only/tests/alerting/update.ts | 37 ++++++++++-------- 16 files changed, 132 insertions(+), 16 deletions(-) diff --git a/x-pack/legacy/plugins/alerting/mappings.json b/x-pack/legacy/plugins/alerting/mappings.json index bc4a7118666ed..91435f55434dd 100644 --- a/x-pack/legacy/plugins/alerting/mappings.json +++ b/x-pack/legacy/plugins/alerting/mappings.json @@ -54,6 +54,12 @@ "updatedBy": { "type": "keyword" }, + "createdAt": { + "type": "date" + }, + "updatedAt": { + "type": "date" + }, "apiKey": { "type": "binary" }, diff --git a/x-pack/legacy/plugins/alerting/server/alerts_client.test.ts b/x-pack/legacy/plugins/alerting/server/alerts_client.test.ts index d11541e9378bb..4f0fd3482563d 100644 --- a/x-pack/legacy/plugins/alerting/server/alerts_client.test.ts +++ b/x-pack/legacy/plugins/alerting/server/alerts_client.test.ts @@ -100,6 +100,8 @@ describe('create()', () => { params: { bar: true, }, + createdAt: '2019-02-12T21:01:22.479Z', + updatedAt: null, actions: [ { group: 'default', @@ -160,6 +162,7 @@ describe('create()', () => { }, ], "alertTypeId": "123", + "createdAt": 2019-02-12T21:01:22.479Z, "id": "1", "params": Object { "bar": true, @@ -168,6 +171,7 @@ describe('create()', () => { "interval": "10s", }, "scheduledTaskId": "task-123", + "updatedAt": null, } `); expect(savedObjectsClient.create).toHaveBeenCalledTimes(1); @@ -189,6 +193,7 @@ describe('create()', () => { "apiKey": null, "apiKeyOwner": null, "consumer": "bar", + "createdAt": "2019-02-12T21:01:22.479Z", "createdBy": "elastic", "enabled": true, "muteAll": false, @@ -204,6 +209,7 @@ describe('create()', () => { "foo", ], "throttle": null, + "updatedAt": null, "updatedBy": "elastic", } `); @@ -407,6 +413,7 @@ describe('create()', () => { }, ], "alertTypeId": "123", + "createdAt": 2019-02-12T21:01:22.479Z, "id": "1", "params": Object { "bar": true, @@ -415,6 +422,7 @@ describe('create()', () => { "interval": "10s", }, "scheduledTaskId": "task-123", + "updatedAt": null, } `); expect(savedObjectsClient.bulkGet).toHaveBeenCalledWith([ @@ -493,6 +501,7 @@ describe('create()', () => { }, ], "alertTypeId": "123", + "createdAt": 2019-02-12T21:01:22.479Z, "enabled": false, "id": "1", "params": Object { @@ -501,6 +510,7 @@ describe('create()', () => { "schedule": Object { "interval": 10000, }, + "updatedAt": null, } `); expect(savedObjectsClient.create).toHaveBeenCalledTimes(1); @@ -806,7 +816,9 @@ describe('create()', () => { apiKey: Buffer.from('123:abc').toString('base64'), apiKeyOwner: 'elastic', createdBy: 'elastic', + createdAt: '2019-02-12T21:01:22.479Z', updatedBy: 'elastic', + updatedAt: null, enabled: true, schedule: { interval: '10s' }, throttle: null, @@ -865,6 +877,7 @@ describe('enable()', () => { enabled: true, scheduledTaskId: 'task-123', updatedBy: 'elastic', + updatedAt: new Date().toISOString(), apiKey: null, apiKeyOwner: null, }, @@ -948,6 +961,7 @@ describe('enable()', () => { apiKey: Buffer.from('123:abc').toString('base64'), apiKeyOwner: 'elastic', updatedBy: 'elastic', + updatedAt: new Date().toISOString(), }, { version: '123', @@ -997,6 +1011,7 @@ describe('disable()', () => { enabled: false, scheduledTaskId: null, updatedBy: 'elastic', + updatedAt: new Date().toISOString(), }, { version: '123', @@ -1042,6 +1057,7 @@ describe('muteAll()', () => { muteAll: true, mutedInstanceIds: [], updatedBy: 'elastic', + updatedAt: new Date().toISOString(), }); }); }); @@ -1063,6 +1079,7 @@ describe('unmuteAll()', () => { muteAll: false, mutedInstanceIds: [], updatedBy: 'elastic', + updatedAt: new Date().toISOString(), }); }); }); @@ -1091,6 +1108,7 @@ describe('muteInstance()', () => { { mutedInstanceIds: ['2'], updatedBy: 'elastic', + updatedAt: new Date().toISOString(), }, { version: '123' } ); @@ -1160,6 +1178,7 @@ describe('unmuteInstance()', () => { { mutedInstanceIds: [], updatedBy: 'elastic', + updatedAt: new Date().toISOString(), }, { version: '123' } ); @@ -1248,6 +1267,7 @@ describe('get()', () => { }, ], "alertTypeId": "123", + "createdAt": 2019-02-12T21:01:22.479Z, "id": "1", "params": Object { "bar": true, @@ -1255,6 +1275,7 @@ describe('get()', () => { "schedule": Object { "interval": "10s", }, + "updatedAt": null, } `); expect(savedObjectsClient.get).toHaveBeenCalledTimes(1); @@ -1347,6 +1368,7 @@ describe('find()', () => { }, ], "alertTypeId": "123", + "createdAt": 2019-02-12T21:01:22.479Z, "id": "1", "params": Object { "bar": true, @@ -1354,6 +1376,7 @@ describe('find()', () => { "schedule": Object { "interval": "10s", }, + "updatedAt": null, }, ], "page": 1, @@ -1476,6 +1499,8 @@ describe('update()', () => { }, ], scheduledTaskId: 'task-123', + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), }, references: [ { @@ -1517,6 +1542,7 @@ describe('update()', () => { }, }, ], + "createdAt": 2019-02-12T21:01:22.479Z, "enabled": true, "id": "1", "params": Object { @@ -1526,6 +1552,7 @@ describe('update()', () => { "interval": "10s", }, "scheduledTaskId": "task-123", + "updatedAt": 2019-02-12T21:01:22.479Z, } `); expect(savedObjectsClient.update).toHaveBeenCalledTimes(1); @@ -1559,6 +1586,7 @@ describe('update()', () => { "tags": Array [ "foo", ], + "updatedAt": "2019-02-12T21:01:22.479Z", "updatedBy": "elastic", } `); @@ -1624,6 +1652,8 @@ describe('update()', () => { params: { bar: true, }, + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), actions: [ { group: 'default', @@ -1732,6 +1762,7 @@ describe('update()', () => { }, }, ], + "createdAt": 2019-02-12T21:01:22.479Z, "enabled": true, "id": "1", "params": Object { @@ -1741,6 +1772,7 @@ describe('update()', () => { "interval": "10s", }, "scheduledTaskId": "task-123", + "updatedAt": 2019-02-12T21:01:22.479Z, } `); expect(savedObjectsClient.bulkGet).toHaveBeenCalledWith([ @@ -1799,6 +1831,8 @@ describe('update()', () => { params: { bar: true, }, + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), actions: [ { group: 'default', @@ -1853,6 +1887,7 @@ describe('update()', () => { }, ], "apiKey": "MTIzOmFiYw==", + "createdAt": 2019-02-12T21:01:22.479Z, "enabled": true, "id": "1", "params": Object { @@ -1862,6 +1897,7 @@ describe('update()', () => { "interval": "10s", }, "scheduledTaskId": "task-123", + "updatedAt": 2019-02-12T21:01:22.479Z, } `); expect(savedObjectsClient.update).toHaveBeenCalledTimes(1); @@ -1895,6 +1931,7 @@ describe('update()', () => { "tags": Array [ "foo", ], + "updatedAt": "2019-02-12T21:01:22.479Z", "updatedBy": "elastic", } `); @@ -2212,6 +2249,7 @@ describe('updateApiKey()', () => { apiKey: Buffer.from('123:abc').toString('base64'), apiKeyOwner: 'elastic', updatedBy: 'elastic', + updatedAt: new Date().toISOString(), }, { version: '123' } ); diff --git a/x-pack/legacy/plugins/alerting/server/alerts_client.ts b/x-pack/legacy/plugins/alerting/server/alerts_client.ts index 70d2ff8ca3033..86f0ccbe9d93f 100644 --- a/x-pack/legacy/plugins/alerting/server/alerts_client.ts +++ b/x-pack/legacy/plugins/alerting/server/alerts_client.ts @@ -5,7 +5,7 @@ */ import Boom from 'boom'; -import { omit, isEqual } from 'lodash'; +import { omit, isEqual, pick } from 'lodash'; import { i18n } from '@kbn/i18n'; import { Logger, @@ -77,6 +77,8 @@ interface CreateOptions { keyof Alert, | 'createdBy' | 'updatedBy' + | 'createdAt' + | 'updatedAt' | 'apiKey' | 'apiKeyOwner' | 'muteAll' @@ -142,6 +144,8 @@ export class AlertsClient { actions, createdBy: username, updatedBy: username, + createdAt: new Date().toISOString(), + updatedAt: null, params: validatedAlertTypeParams, muteAll: false, mutedInstanceIds: [], @@ -248,6 +252,7 @@ export class AlertsClient { params: validatedAlertTypeParams, actions, updatedBy: username, + updatedAt: new Date().toISOString(), }, { version, @@ -283,6 +288,7 @@ export class AlertsClient { ...attributes, ...this.apiKeyAsAlertAttributes(await this.createAPIKey(), username), updatedBy: username, + updatedAt: new Date().toISOString(), }, { version } ); @@ -301,6 +307,7 @@ export class AlertsClient { enabled: true, ...this.apiKeyAsAlertAttributes(await this.createAPIKey(), username), updatedBy: username, + updatedAt: new Date().toISOString(), scheduledTaskId: scheduledTask.id, }, { version } @@ -321,6 +328,7 @@ export class AlertsClient { apiKey: null, apiKeyOwner: null, updatedBy: await this.getUserName(), + updatedAt: new Date().toISOString(), }, { version } ); @@ -333,6 +341,7 @@ export class AlertsClient { muteAll: true, mutedInstanceIds: [], updatedBy: await this.getUserName(), + updatedAt: new Date().toISOString(), }); } @@ -341,6 +350,7 @@ export class AlertsClient { muteAll: false, mutedInstanceIds: [], updatedBy: await this.getUserName(), + updatedAt: new Date().toISOString(), }); } @@ -361,6 +371,7 @@ export class AlertsClient { { mutedInstanceIds, updatedBy: await this.getUserName(), + updatedAt: new Date().toISOString(), }, { version } ); @@ -382,6 +393,7 @@ export class AlertsClient { alertId, { updatedBy: await this.getUserName(), + updatedAt: new Date().toISOString(), mutedInstanceIds: mutedInstanceIds.filter((id: string) => id !== alertInstanceId), }, { version } @@ -436,6 +448,7 @@ export class AlertsClient { return { id, ...rawAlert, + ...parseDates(pick(rawAlert, 'createdAt', 'updatedAt')), actions, }; } @@ -495,3 +508,13 @@ export class AlertsClient { }; } } + +function parseDates({ + createdAt, + updatedAt, +}: Pick): Pick { + return { + createdAt: new Date(createdAt), + updatedAt: updatedAt ? new Date(updatedAt) : null, + }; +} diff --git a/x-pack/legacy/plugins/alerting/server/routes/create.test.ts b/x-pack/legacy/plugins/alerting/server/routes/create.test.ts index c41e0d068aff2..02f5644250095 100644 --- a/x-pack/legacy/plugins/alerting/server/routes/create.test.ts +++ b/x-pack/legacy/plugins/alerting/server/routes/create.test.ts @@ -19,6 +19,8 @@ const mockedAlert = { params: { bar: true, }, + createdAt: new Date(), + updatedAt: null, actions: [ { group: 'default', diff --git a/x-pack/legacy/plugins/alerting/server/routes/get.test.ts b/x-pack/legacy/plugins/alerting/server/routes/get.test.ts index b97762d10c960..f3e98ba0e5188 100644 --- a/x-pack/legacy/plugins/alerting/server/routes/get.test.ts +++ b/x-pack/legacy/plugins/alerting/server/routes/get.test.ts @@ -17,6 +17,8 @@ const mockedAlert = { params: { bar: true, }, + createdAt: new Date(), + updatedAt: null, actions: [ { group: 'default', diff --git a/x-pack/legacy/plugins/alerting/server/routes/update.test.ts b/x-pack/legacy/plugins/alerting/server/routes/update.test.ts index 8ce9d94140e6d..a69e4a968b0fd 100644 --- a/x-pack/legacy/plugins/alerting/server/routes/update.test.ts +++ b/x-pack/legacy/plugins/alerting/server/routes/update.test.ts @@ -20,6 +20,8 @@ const mockedResponse = { params: { otherField: false, }, + createdAt: new Date(), + updatedAt: new Date(), actions: [ { group: 'default', diff --git a/x-pack/legacy/plugins/alerting/server/types.ts b/x-pack/legacy/plugins/alerting/server/types.ts index 7c2f3dcc918dc..98d43513f8d8e 100644 --- a/x-pack/legacy/plugins/alerting/server/types.ts +++ b/x-pack/legacy/plugins/alerting/server/types.ts @@ -76,6 +76,8 @@ export interface Alert { scheduledTaskId?: string; createdBy: string | null; updatedBy: string | null; + createdAt: Date; + updatedAt: Date | null; apiKey: string | null; apiKeyOwner: string | null; throttle: string | null; @@ -95,6 +97,8 @@ export interface RawAlert extends SavedObjectAttributes { scheduledTaskId?: string; createdBy: string | null; updatedBy: string | null; + createdAt: string; + updatedAt: string | null; apiKey: string | null; apiKeyOwner: string | null; throttle: string | null; diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/__mocks__/request_responses.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/__mocks__/request_responses.ts index a78879924acd0..66e1d29b80852 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/__mocks__/request_responses.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/__mocks__/request_responses.ts @@ -308,6 +308,8 @@ export const getResult = (): RuleAlertType => ({ references: ['http://www.example.com', 'https://ww.example.com'], version: 1, }, + createdAt: new Date('2019-12-13T16:40:33.400Z'), + updatedAt: new Date('2019-12-13T16:40:33.400Z'), schedule: { interval: '5m' }, enabled: true, actions: [], diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/create.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/create.ts index a098a1fe02c1a..bb0a1c94c5970 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/create.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/create.ts @@ -92,6 +92,8 @@ export default function createAlertTests({ getService }: FtrProviderContext) { createdBy: user.username, schedule: { interval: '1m' }, scheduledTaskId: response.body.scheduledTaskId, + createdAt: response.body.createdAt, + updatedAt: response.body.updatedAt, throttle: '1m', updatedBy: user.username, apiKeyOwner: user.username, diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/find.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/find.ts index 92d8447e8f7d5..a86edaeddcbb0 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/find.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/find.ts @@ -66,12 +66,16 @@ export default function createFindTests({ getService }: FtrProviderContext) { params: {}, createdBy: 'elastic', scheduledTaskId: match.scheduledTaskId, + createdAt: match.createdAt, + updatedAt: match.updatedAt, throttle: '1m', updatedBy: 'elastic', apiKeyOwner: 'elastic', muteAll: false, mutedInstanceIds: [], }); + expect(Date.parse(match.createdAt)).to.be.greaterThan(0); + expect(match.updatedAt).to.eql(null); break; default: throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); @@ -157,7 +161,11 @@ export default function createFindTests({ getService }: FtrProviderContext) { apiKeyOwner: 'elastic', muteAll: false, mutedInstanceIds: [], + createdAt: match.createdAt, + updatedAt: match.updatedAt, }); + expect(Date.parse(match.createdAt)).to.be.greaterThan(0); + expect(match.updatedAt).to.eql(null); break; default: throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/get.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/get.ts index eaa361155b61f..613f96739cffa 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/get.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/get.ts @@ -60,12 +60,16 @@ export default function createGetTests({ getService }: FtrProviderContext) { params: {}, createdBy: 'elastic', scheduledTaskId: response.body.scheduledTaskId, + updatedAt: response.body.updatedAt, + createdAt: response.body.createdAt, throttle: '1m', updatedBy: 'elastic', apiKeyOwner: 'elastic', muteAll: false, mutedInstanceIds: [], }); + expect(Date.parse(response.body.createdAt)).to.be.greaterThan(0); + expect(response.body.updatedAt).to.eql(null); break; default: throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update.ts index 8cb01b5467388..98b64358cc647 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update.ts @@ -81,7 +81,11 @@ export default function createUpdateTests({ getService }: FtrProviderContext) { muteAll: false, mutedInstanceIds: [], scheduledTaskId: createdAlert.scheduledTaskId, + createdAt: response.body.createdAt, + updatedAt: response.body.updatedAt, }); + expect(Date.parse(response.body.createdAt)).to.be.greaterThan(0); + expect(Date.parse(response.body.updatedAt)).to.be.greaterThan(0); break; default: throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); diff --git a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/create.ts b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/create.ts index c61c94bd603fb..23cf6e9afe41a 100644 --- a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/create.ts +++ b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/create.ts @@ -79,7 +79,11 @@ export default function createAlertTests({ getService }: FtrProviderContext) { throttle: '1m', muteAll: false, mutedInstanceIds: [], + createdAt: response.body.createdAt, + updatedAt: response.body.updatedAt, }); + expect(Date.parse(response.body.createdAt)).to.be.greaterThan(0); + expect(response.body.updatedAt).to.eql(null); expect(typeof response.body.scheduledTaskId).to.be('string'); const { _source: taskRecord } = await getScheduledTask(response.body.scheduledTaskId); expect(taskRecord.type).to.eql('task'); diff --git a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/find.ts b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/find.ts index 1ee814aace797..3b6eb4e865bff 100644 --- a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/find.ts +++ b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/find.ts @@ -54,7 +54,11 @@ export default function createFindTests({ getService }: FtrProviderContext) { throttle: '1m', muteAll: false, mutedInstanceIds: [], + createdAt: match.createdAt, + updatedAt: match.updatedAt, }); + expect(Date.parse(match.createdAt)).to.be.greaterThan(0); + expect(match.updatedAt).to.eql(null); }); it(`shouldn't find alert from another space`, async () => { diff --git a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get.ts b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get.ts index 328b0a01d5cbd..53b08850b81f3 100644 --- a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get.ts +++ b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get.ts @@ -48,7 +48,11 @@ export default function createGetTests({ getService }: FtrProviderContext) { throttle: '1m', muteAll: false, mutedInstanceIds: [], + createdAt: response.body.createdAt, + updatedAt: response.body.updatedAt, }); + expect(Date.parse(response.body.createdAt)).to.be.greaterThan(0); + expect(response.body.updatedAt).to.eql(null); }); it(`shouldn't find alert from another space`, async () => { diff --git a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/update.ts b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/update.ts index fd6d81e296ef0..1d68072b96068 100644 --- a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/update.ts +++ b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/update.ts @@ -4,6 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ +import expect from '@kbn/expect/expect.js'; import { Spaces } from '../../scenarios'; import { getUrlPrefix, getTestAlertData, ObjectRemover } from '../../../common/lib'; import { FtrProviderContext } from '../../../common/ftr_provider_context'; @@ -35,24 +36,30 @@ export default function createUpdateTests({ getService }: FtrProviderContext) { actions: [], throttle: '1m', }; - await supertest + const response = await supertest .put(`${getUrlPrefix(Spaces.space1.id)}/api/alert/${createdAlert.id}`) .set('kbn-xsrf', 'foo') .send(updatedData) - .expect(200, { - ...updatedData, - id: createdAlert.id, - tags: ['bar'], - alertTypeId: 'test.noop', - consumer: 'bar', - createdBy: null, - enabled: true, - updatedBy: null, - apiKeyOwner: null, - muteAll: false, - mutedInstanceIds: [], - scheduledTaskId: createdAlert.scheduledTaskId, - }); + .expect(200); + + expect(response.body).to.eql({ + ...updatedData, + id: createdAlert.id, + tags: ['bar'], + alertTypeId: 'test.noop', + consumer: 'bar', + createdBy: null, + enabled: true, + updatedBy: null, + apiKeyOwner: null, + muteAll: false, + mutedInstanceIds: [], + scheduledTaskId: createdAlert.scheduledTaskId, + createdAt: response.body.createdAt, + updatedAt: response.body.updatedAt, + }); + expect(Date.parse(response.body.createdAt)).to.be.greaterThan(0); + expect(Date.parse(response.body.updatedAt)).to.be.greaterThan(0); }); it(`shouldn't update alert from another space`, async () => { From 1721ec2d812339a56979fb883091b97564af1d54 Mon Sep 17 00:00:00 2001 From: Gidi Morris Date: Tue, 24 Dec 2019 17:15:18 +0000 Subject: [PATCH 2/9] use the SavedObjets updated_at instead of a new field in alerting --- x-pack/legacy/plugins/alerting/mappings.json | 3 -- .../alerting/server/alerts_client.test.ts | 21 +++------ .../plugins/alerting/server/alerts_client.ts | 45 +++++++++---------- .../alerting/server/routes/create.test.ts | 11 +++-- .../alerting/server/routes/get.test.ts | 4 +- .../alerting/server/routes/update.test.ts | 6 ++- .../legacy/plugins/alerting/server/types.ts | 1 - .../tests/alerting/find.ts | 4 +- .../security_and_spaces/tests/alerting/get.ts | 2 +- .../spaces_only/tests/alerting/create.ts | 2 +- .../spaces_only/tests/alerting/find.ts | 2 +- .../spaces_only/tests/alerting/get.ts | 2 +- 12 files changed, 45 insertions(+), 58 deletions(-) diff --git a/x-pack/legacy/plugins/alerting/mappings.json b/x-pack/legacy/plugins/alerting/mappings.json index 91435f55434dd..31733f44e7ce6 100644 --- a/x-pack/legacy/plugins/alerting/mappings.json +++ b/x-pack/legacy/plugins/alerting/mappings.json @@ -57,9 +57,6 @@ "createdAt": { "type": "date" }, - "updatedAt": { - "type": "date" - }, "apiKey": { "type": "binary" }, diff --git a/x-pack/legacy/plugins/alerting/server/alerts_client.test.ts b/x-pack/legacy/plugins/alerting/server/alerts_client.test.ts index 4f0fd3482563d..95fbe4ceb4c57 100644 --- a/x-pack/legacy/plugins/alerting/server/alerts_client.test.ts +++ b/x-pack/legacy/plugins/alerting/server/alerts_client.test.ts @@ -101,7 +101,6 @@ describe('create()', () => { bar: true, }, createdAt: '2019-02-12T21:01:22.479Z', - updatedAt: null, actions: [ { group: 'default', @@ -209,7 +208,6 @@ describe('create()', () => { "foo", ], "throttle": null, - "updatedAt": null, "updatedBy": "elastic", } `); @@ -317,6 +315,7 @@ describe('create()', () => { params: { bar: true, }, + createdAt: new Date().toISOString(), actions: [ { group: 'default', @@ -468,6 +467,7 @@ describe('create()', () => { params: { bar: true, }, + createdAt: new Date().toISOString(), actions: [ { group: 'default', @@ -818,7 +818,6 @@ describe('create()', () => { createdBy: 'elastic', createdAt: '2019-02-12T21:01:22.479Z', updatedBy: 'elastic', - updatedAt: null, enabled: true, schedule: { interval: '10s' }, throttle: null, @@ -877,7 +876,6 @@ describe('enable()', () => { enabled: true, scheduledTaskId: 'task-123', updatedBy: 'elastic', - updatedAt: new Date().toISOString(), apiKey: null, apiKeyOwner: null, }, @@ -961,7 +959,6 @@ describe('enable()', () => { apiKey: Buffer.from('123:abc').toString('base64'), apiKeyOwner: 'elastic', updatedBy: 'elastic', - updatedAt: new Date().toISOString(), }, { version: '123', @@ -1011,7 +1008,6 @@ describe('disable()', () => { enabled: false, scheduledTaskId: null, updatedBy: 'elastic', - updatedAt: new Date().toISOString(), }, { version: '123', @@ -1057,7 +1053,6 @@ describe('muteAll()', () => { muteAll: true, mutedInstanceIds: [], updatedBy: 'elastic', - updatedAt: new Date().toISOString(), }); }); }); @@ -1079,7 +1074,6 @@ describe('unmuteAll()', () => { muteAll: false, mutedInstanceIds: [], updatedBy: 'elastic', - updatedAt: new Date().toISOString(), }); }); }); @@ -1108,7 +1102,6 @@ describe('muteInstance()', () => { { mutedInstanceIds: ['2'], updatedBy: 'elastic', - updatedAt: new Date().toISOString(), }, { version: '123' } ); @@ -1178,7 +1171,6 @@ describe('unmuteInstance()', () => { { mutedInstanceIds: [], updatedBy: 'elastic', - updatedAt: new Date().toISOString(), }, { version: '123' } ); @@ -1500,8 +1492,8 @@ describe('update()', () => { ], scheduledTaskId: 'task-123', createdAt: new Date().toISOString(), - updatedAt: new Date().toISOString(), }, + updated_at: new Date().toISOString(), references: [ { name: 'action_0', @@ -1586,7 +1578,6 @@ describe('update()', () => { "tags": Array [ "foo", ], - "updatedAt": "2019-02-12T21:01:22.479Z", "updatedBy": "elastic", } `); @@ -1653,7 +1644,6 @@ describe('update()', () => { bar: true, }, createdAt: new Date().toISOString(), - updatedAt: new Date().toISOString(), actions: [ { group: 'default', @@ -1682,6 +1672,7 @@ describe('update()', () => { ], scheduledTaskId: 'task-123', }, + updated_at: new Date().toISOString(), references: [ { name: 'action_0', @@ -1832,7 +1823,6 @@ describe('update()', () => { bar: true, }, createdAt: new Date().toISOString(), - updatedAt: new Date().toISOString(), actions: [ { group: 'default', @@ -1846,6 +1836,7 @@ describe('update()', () => { apiKey: Buffer.from('123:abc').toString('base64'), scheduledTaskId: 'task-123', }, + updated_at: new Date().toISOString(), references: [ { name: 'action_0', @@ -1931,7 +1922,6 @@ describe('update()', () => { "tags": Array [ "foo", ], - "updatedAt": "2019-02-12T21:01:22.479Z", "updatedBy": "elastic", } `); @@ -2249,7 +2239,6 @@ describe('updateApiKey()', () => { apiKey: Buffer.from('123:abc').toString('base64'), apiKeyOwner: 'elastic', updatedBy: 'elastic', - updatedAt: new Date().toISOString(), }, { version: '123' } ); diff --git a/x-pack/legacy/plugins/alerting/server/alerts_client.ts b/x-pack/legacy/plugins/alerting/server/alerts_client.ts index 86f0ccbe9d93f..3a27d91006301 100644 --- a/x-pack/legacy/plugins/alerting/server/alerts_client.ts +++ b/x-pack/legacy/plugins/alerting/server/alerts_client.ts @@ -5,7 +5,7 @@ */ import Boom from 'boom'; -import { omit, isEqual, pick } from 'lodash'; +import { omit, isEqual } from 'lodash'; import { i18n } from '@kbn/i18n'; import { Logger, @@ -145,7 +145,6 @@ export class AlertsClient { createdBy: username, updatedBy: username, createdAt: new Date().toISOString(), - updatedAt: null, params: validatedAlertTypeParams, muteAll: false, mutedInstanceIds: [], @@ -175,12 +174,17 @@ export class AlertsClient { }); createdAlert.attributes.scheduledTaskId = scheduledTask.id; } - return this.getAlertFromRaw(createdAlert.id, createdAlert.attributes, references); + return this.getAlertFromRaw( + createdAlert.id, + createdAlert.attributes, + createdAlert.updated_at, + references + ); } public async get({ id }: { id: string }) { const result = await this.savedObjectsClient.get('alert', id); - return this.getAlertFromRaw(result.id, result.attributes, result.references); + return this.getAlertFromRaw(result.id, result.attributes, result.updated_at, result.references); } public async find({ options = {} }: FindOptions = {}): Promise { @@ -190,7 +194,7 @@ export class AlertsClient { }); const data = results.saved_objects.map(result => - this.getAlertFromRaw(result.id, result.attributes, result.references) + this.getAlertFromRaw(result.id, result.attributes, result.updated_at, result.references) ); return { @@ -252,14 +256,18 @@ export class AlertsClient { params: validatedAlertTypeParams, actions, updatedBy: username, - updatedAt: new Date().toISOString(), }, { version, references, } ); - return this.getAlertFromRaw(id, updatedObject.attributes, updatedObject.references); + return this.getAlertFromRaw( + id, + updatedObject.attributes, + updatedObject.updated_at, + updatedObject.references + ); } private apiKeyAsAlertAttributes( @@ -288,7 +296,6 @@ export class AlertsClient { ...attributes, ...this.apiKeyAsAlertAttributes(await this.createAPIKey(), username), updatedBy: username, - updatedAt: new Date().toISOString(), }, { version } ); @@ -307,7 +314,7 @@ export class AlertsClient { enabled: true, ...this.apiKeyAsAlertAttributes(await this.createAPIKey(), username), updatedBy: username, - updatedAt: new Date().toISOString(), + scheduledTaskId: scheduledTask.id, }, { version } @@ -328,7 +335,6 @@ export class AlertsClient { apiKey: null, apiKeyOwner: null, updatedBy: await this.getUserName(), - updatedAt: new Date().toISOString(), }, { version } ); @@ -341,7 +347,6 @@ export class AlertsClient { muteAll: true, mutedInstanceIds: [], updatedBy: await this.getUserName(), - updatedAt: new Date().toISOString(), }); } @@ -350,7 +355,6 @@ export class AlertsClient { muteAll: false, mutedInstanceIds: [], updatedBy: await this.getUserName(), - updatedAt: new Date().toISOString(), }); } @@ -371,7 +375,6 @@ export class AlertsClient { { mutedInstanceIds, updatedBy: await this.getUserName(), - updatedAt: new Date().toISOString(), }, { version } ); @@ -393,7 +396,7 @@ export class AlertsClient { alertId, { updatedBy: await this.getUserName(), - updatedAt: new Date().toISOString(), + mutedInstanceIds: mutedInstanceIds.filter((id: string) => id !== alertInstanceId), }, { version } @@ -436,6 +439,7 @@ export class AlertsClient { private getAlertFromRaw( id: string, rawAlert: Partial, + updatedAt: SavedObject['updated_at'], references: SavedObjectReference[] | undefined ) { if (!rawAlert.actions) { @@ -448,7 +452,8 @@ export class AlertsClient { return { id, ...rawAlert, - ...parseDates(pick(rawAlert, 'createdAt', 'updatedAt')), + updatedAt: updatedAt ? new Date(updatedAt) : null, + createdAt: new Date(rawAlert.createdAt!), actions, }; } @@ -508,13 +513,3 @@ export class AlertsClient { }; } } - -function parseDates({ - createdAt, - updatedAt, -}: Pick): Pick { - return { - createdAt: new Date(createdAt), - updatedAt: updatedAt ? new Date(updatedAt) : null, - }; -} diff --git a/x-pack/legacy/plugins/alerting/server/routes/create.test.ts b/x-pack/legacy/plugins/alerting/server/routes/create.test.ts index 02f5644250095..6c18deeee7580 100644 --- a/x-pack/legacy/plugins/alerting/server/routes/create.test.ts +++ b/x-pack/legacy/plugins/alerting/server/routes/create.test.ts @@ -4,6 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ +import { omit } from 'lodash'; import { createMockServer } from './_mock_server'; import { createAlertRoute } from './create'; @@ -19,8 +20,6 @@ const mockedAlert = { params: { bar: true, }, - createdAt: new Date(), - updatedAt: null, actions: [ { group: 'default', @@ -41,8 +40,12 @@ test('creates an alert with proper parameters', async () => { payload: mockedAlert, }; + const createdAt = new Date(); + const updatedAt = null; alertsClient.create.mockResolvedValueOnce({ ...mockedAlert, + createdAt, + updatedAt, id: '123', actions: [ { @@ -54,7 +57,8 @@ test('creates an alert with proper parameters', async () => { const { payload, statusCode } = await server.inject(request); expect(statusCode).toBe(200); const response = JSON.parse(payload); - expect(response).toMatchInlineSnapshot(` + expect(new Date(response.createdAt)).toEqual(createdAt); + expect(omit(response, 'createdAt')).toMatchInlineSnapshot(` Object { "actions": Array [ Object { @@ -79,6 +83,7 @@ test('creates an alert with proper parameters', async () => { "tags": Array [ "foo", ], + "updatedAt": null, } `); expect(alertsClient.create).toHaveBeenCalledTimes(1); diff --git a/x-pack/legacy/plugins/alerting/server/routes/get.test.ts b/x-pack/legacy/plugins/alerting/server/routes/get.test.ts index f3e98ba0e5188..7af3a11ee6d77 100644 --- a/x-pack/legacy/plugins/alerting/server/routes/get.test.ts +++ b/x-pack/legacy/plugins/alerting/server/routes/get.test.ts @@ -42,8 +42,8 @@ test('calls get with proper parameters', async () => { alertsClient.get.mockResolvedValueOnce(mockedAlert); const { payload, statusCode } = await server.inject(request); expect(statusCode).toBe(200); - const response = JSON.parse(payload); - expect(response).toEqual(mockedAlert); + const { createdAt, ...response } = JSON.parse(payload); + expect({ createdAt: new Date(createdAt), ...response }).toEqual(mockedAlert); expect(alertsClient.get).toHaveBeenCalledTimes(1); expect(alertsClient.get.mock.calls[0]).toMatchInlineSnapshot(` Array [ diff --git a/x-pack/legacy/plugins/alerting/server/routes/update.test.ts b/x-pack/legacy/plugins/alerting/server/routes/update.test.ts index a69e4a968b0fd..83b70b505234f 100644 --- a/x-pack/legacy/plugins/alerting/server/routes/update.test.ts +++ b/x-pack/legacy/plugins/alerting/server/routes/update.test.ts @@ -61,8 +61,10 @@ test('calls the update function with proper parameters', async () => { alertsClient.update.mockResolvedValueOnce(mockedResponse); const { payload, statusCode } = await server.inject(request); expect(statusCode).toBe(200); - const response = JSON.parse(payload); - expect(response).toEqual(mockedResponse); + const { createdAt, updatedAt, ...response } = JSON.parse(payload); + expect({ createdAt: new Date(createdAt), updatedAt: new Date(updatedAt), ...response }).toEqual( + mockedResponse + ); expect(alertsClient.update).toHaveBeenCalledTimes(1); expect(alertsClient.update.mock.calls[0]).toMatchInlineSnapshot(` Array [ diff --git a/x-pack/legacy/plugins/alerting/server/types.ts b/x-pack/legacy/plugins/alerting/server/types.ts index 98d43513f8d8e..40490f7b5cf69 100644 --- a/x-pack/legacy/plugins/alerting/server/types.ts +++ b/x-pack/legacy/plugins/alerting/server/types.ts @@ -98,7 +98,6 @@ export interface RawAlert extends SavedObjectAttributes { createdBy: string | null; updatedBy: string | null; createdAt: string; - updatedAt: string | null; apiKey: string | null; apiKeyOwner: string | null; throttle: string | null; diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/find.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/find.ts index a86edaeddcbb0..d99ab794cd28f 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/find.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/find.ts @@ -75,7 +75,7 @@ export default function createFindTests({ getService }: FtrProviderContext) { mutedInstanceIds: [], }); expect(Date.parse(match.createdAt)).to.be.greaterThan(0); - expect(match.updatedAt).to.eql(null); + expect(Date.parse(match.updatedAt)).to.be.greaterThan(0); break; default: throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); @@ -165,7 +165,7 @@ export default function createFindTests({ getService }: FtrProviderContext) { updatedAt: match.updatedAt, }); expect(Date.parse(match.createdAt)).to.be.greaterThan(0); - expect(match.updatedAt).to.eql(null); + expect(Date.parse(match.updatedAt)).to.be.greaterThan(0); break; default: throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/get.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/get.ts index 613f96739cffa..20eed4013d7dd 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/get.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/get.ts @@ -69,7 +69,7 @@ export default function createGetTests({ getService }: FtrProviderContext) { mutedInstanceIds: [], }); expect(Date.parse(response.body.createdAt)).to.be.greaterThan(0); - expect(response.body.updatedAt).to.eql(null); + expect(Date.parse(response.body.updatedAt)).to.be.greaterThan(0); break; default: throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); diff --git a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/create.ts b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/create.ts index 23cf6e9afe41a..be62d541a8412 100644 --- a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/create.ts +++ b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/create.ts @@ -83,7 +83,7 @@ export default function createAlertTests({ getService }: FtrProviderContext) { updatedAt: response.body.updatedAt, }); expect(Date.parse(response.body.createdAt)).to.be.greaterThan(0); - expect(response.body.updatedAt).to.eql(null); + expect(Date.parse(response.body.updatedAt)).to.be.greaterThan(0); expect(typeof response.body.scheduledTaskId).to.be('string'); const { _source: taskRecord } = await getScheduledTask(response.body.scheduledTaskId); expect(taskRecord.type).to.eql('task'); diff --git a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/find.ts b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/find.ts index 3b6eb4e865bff..70935a462d03e 100644 --- a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/find.ts +++ b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/find.ts @@ -58,7 +58,7 @@ export default function createFindTests({ getService }: FtrProviderContext) { updatedAt: match.updatedAt, }); expect(Date.parse(match.createdAt)).to.be.greaterThan(0); - expect(match.updatedAt).to.eql(null); + expect(Date.parse(match.updatedAt)).to.be.greaterThan(0); }); it(`shouldn't find alert from another space`, async () => { diff --git a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get.ts b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get.ts index 53b08850b81f3..30b5e43aee585 100644 --- a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get.ts +++ b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get.ts @@ -52,7 +52,7 @@ export default function createGetTests({ getService }: FtrProviderContext) { updatedAt: response.body.updatedAt, }); expect(Date.parse(response.body.createdAt)).to.be.greaterThan(0); - expect(response.body.updatedAt).to.eql(null); + expect(Date.parse(response.body.updatedAt)).to.be.greaterThan(0); }); it(`shouldn't find alert from another space`, async () => { From e8dbc150fb04e1429146976d3e0189e72b26060e Mon Sep 17 00:00:00 2001 From: Gidi Meir Morris Date: Thu, 2 Jan 2020 08:57:23 +0000 Subject: [PATCH 3/9] added missing assetion --- .../security_and_spaces/tests/alerting/create.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/create.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/create.ts index bb0a1c94c5970..fc48798935ee1 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/create.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/create.ts @@ -101,6 +101,8 @@ export default function createAlertTests({ getService }: FtrProviderContext) { mutedInstanceIds: [], }); expect(typeof response.body.scheduledTaskId).to.be('string'); + expect(Date.parse(response.body.createdAt)).to.be.greaterThan(0); + expect(Date.parse(response.body.updatedAt)).to.be.greaterThan(0); const { _source: taskRecord } = await getScheduledTask(response.body.scheduledTaskId); expect(taskRecord.type).to.eql('task'); expect(taskRecord.task.taskType).to.eql('alerting:test.noop'); From 1fb57356215590cc8604f57e0ec3d404a5f62a8e Mon Sep 17 00:00:00 2001 From: Gidi Meir Morris Date: Fri, 3 Jan 2020 09:56:35 +0000 Subject: [PATCH 4/9] used createdAt in place of empty updatedAt --- .../plugins/alerting/server/alerts_client.test.ts | 10 +++++----- x-pack/legacy/plugins/alerting/server/alerts_client.ts | 4 ++-- x-pack/legacy/plugins/alerting/server/types.ts | 2 +- .../security_and_spaces/tests/alerting/create.ts | 3 +++ .../security_and_spaces/tests/alerting/find.ts | 2 ++ .../security_and_spaces/tests/alerting/get.ts | 3 +++ .../security_and_spaces/tests/alerting/update.ts | 3 +++ .../spaces_only/tests/alerting/create.ts | 4 ++++ .../spaces_only/tests/alerting/find.ts | 1 + .../spaces_only/tests/alerting/get.ts | 3 +++ .../spaces_only/tests/alerting/update.ts | 3 +++ 11 files changed, 30 insertions(+), 8 deletions(-) diff --git a/x-pack/legacy/plugins/alerting/server/alerts_client.test.ts b/x-pack/legacy/plugins/alerting/server/alerts_client.test.ts index 95fbe4ceb4c57..fa5d866584fbe 100644 --- a/x-pack/legacy/plugins/alerting/server/alerts_client.test.ts +++ b/x-pack/legacy/plugins/alerting/server/alerts_client.test.ts @@ -170,7 +170,7 @@ describe('create()', () => { "interval": "10s", }, "scheduledTaskId": "task-123", - "updatedAt": null, + "updatedAt": 2019-02-12T21:01:22.479Z, } `); expect(savedObjectsClient.create).toHaveBeenCalledTimes(1); @@ -421,7 +421,7 @@ describe('create()', () => { "interval": "10s", }, "scheduledTaskId": "task-123", - "updatedAt": null, + "updatedAt": 2019-02-12T21:01:22.479Z, } `); expect(savedObjectsClient.bulkGet).toHaveBeenCalledWith([ @@ -510,7 +510,7 @@ describe('create()', () => { "schedule": Object { "interval": 10000, }, - "updatedAt": null, + "updatedAt": 2019-02-12T21:01:22.479Z, } `); expect(savedObjectsClient.create).toHaveBeenCalledTimes(1); @@ -1267,7 +1267,7 @@ describe('get()', () => { "schedule": Object { "interval": "10s", }, - "updatedAt": null, + "updatedAt": 2019-02-12T21:01:22.479Z, } `); expect(savedObjectsClient.get).toHaveBeenCalledTimes(1); @@ -1368,7 +1368,7 @@ describe('find()', () => { "schedule": Object { "interval": "10s", }, - "updatedAt": null, + "updatedAt": 2019-02-12T21:01:22.479Z, }, ], "page": 1, diff --git a/x-pack/legacy/plugins/alerting/server/alerts_client.ts b/x-pack/legacy/plugins/alerting/server/alerts_client.ts index 3a27d91006301..26e532d928c61 100644 --- a/x-pack/legacy/plugins/alerting/server/alerts_client.ts +++ b/x-pack/legacy/plugins/alerting/server/alerts_client.ts @@ -222,7 +222,7 @@ export class AlertsClient { updateResult.scheduledTaskId && !isEqual(alert.attributes.schedule, updateResult.schedule) ) { - this.taskManager.runNow(updateResult.scheduledTaskId).catch(err => { + this.taskManager.runNow(updateResult.scheduledTaskId).catch((err: Error) => { this.logger.error( `Alert update failed to run its underlying task. TaskManager runNow failed with Error: ${err.message}` ); @@ -452,7 +452,7 @@ export class AlertsClient { return { id, ...rawAlert, - updatedAt: updatedAt ? new Date(updatedAt) : null, + updatedAt: updatedAt ? new Date(updatedAt) : new Date(rawAlert.createdAt!), createdAt: new Date(rawAlert.createdAt!), actions, }; diff --git a/x-pack/legacy/plugins/alerting/server/types.ts b/x-pack/legacy/plugins/alerting/server/types.ts index 40490f7b5cf69..a2390bf93d005 100644 --- a/x-pack/legacy/plugins/alerting/server/types.ts +++ b/x-pack/legacy/plugins/alerting/server/types.ts @@ -77,7 +77,7 @@ export interface Alert { createdBy: string | null; updatedBy: string | null; createdAt: Date; - updatedAt: Date | null; + updatedAt: Date; apiKey: string | null; apiKeyOwner: string | null; throttle: string | null; diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/create.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/create.ts index ae21fecaef223..efc6506e009e7 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/create.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/create.ts @@ -103,6 +103,9 @@ export default function createAlertTests({ getService }: FtrProviderContext) { expect(typeof response.body.scheduledTaskId).to.be('string'); expect(Date.parse(response.body.createdAt)).to.be.greaterThan(0); expect(Date.parse(response.body.updatedAt)).to.be.greaterThan(0); + expect(Date.parse(response.body.updatedAt)).to.be.greaterThan( + Date.parse(response.body.createdAt) + ); const { _source: taskRecord } = await getScheduledTask(response.body.scheduledTaskId); expect(taskRecord.type).to.eql('task'); expect(taskRecord.task.taskType).to.eql('alerting:test.noop'); diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/find.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/find.ts index d99ab794cd28f..4d47e0c936d00 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/find.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/find.ts @@ -76,6 +76,7 @@ export default function createFindTests({ getService }: FtrProviderContext) { }); expect(Date.parse(match.createdAt)).to.be.greaterThan(0); expect(Date.parse(match.updatedAt)).to.be.greaterThan(0); + expect(Date.parse(match.updatedAt)).to.be.greaterThan(Date.parse(match.createdAt)); break; default: throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); @@ -166,6 +167,7 @@ export default function createFindTests({ getService }: FtrProviderContext) { }); expect(Date.parse(match.createdAt)).to.be.greaterThan(0); expect(Date.parse(match.updatedAt)).to.be.greaterThan(0); + expect(Date.parse(match.updatedAt)).to.be.greaterThan(Date.parse(match.createdAt)); break; default: throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/get.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/get.ts index 20eed4013d7dd..ad1890ad6beec 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/get.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/get.ts @@ -70,6 +70,9 @@ export default function createGetTests({ getService }: FtrProviderContext) { }); expect(Date.parse(response.body.createdAt)).to.be.greaterThan(0); expect(Date.parse(response.body.updatedAt)).to.be.greaterThan(0); + expect(Date.parse(response.body.updatedAt)).to.be.greaterThan( + Date.parse(response.body.createdAt) + ); break; default: throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update.ts index eed0d57d98d05..e89b54b1caa55 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update.ts @@ -86,6 +86,9 @@ export default function createUpdateTests({ getService }: FtrProviderContext) { }); expect(Date.parse(response.body.createdAt)).to.be.greaterThan(0); expect(Date.parse(response.body.updatedAt)).to.be.greaterThan(0); + expect(Date.parse(response.body.updatedAt)).to.be.greaterThan( + Date.parse(response.body.createdAt) + ); // Ensure AAD isn't broken await checkAAD({ supertest, diff --git a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/create.ts b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/create.ts index 755007f06d369..a34cf0b8beb72 100644 --- a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/create.ts +++ b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/create.ts @@ -84,6 +84,10 @@ export default function createAlertTests({ getService }: FtrProviderContext) { }); expect(Date.parse(response.body.createdAt)).to.be.greaterThan(0); expect(Date.parse(response.body.updatedAt)).to.be.greaterThan(0); + expect(Date.parse(response.body.updatedAt)).to.be.greaterThan( + Date.parse(response.body.createdAt) + ); + expect(typeof response.body.scheduledTaskId).to.be('string'); const { _source: taskRecord } = await getScheduledTask(response.body.scheduledTaskId); expect(taskRecord.type).to.eql('task'); diff --git a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/find.ts b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/find.ts index 70935a462d03e..1e285acf09637 100644 --- a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/find.ts +++ b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/find.ts @@ -59,6 +59,7 @@ export default function createFindTests({ getService }: FtrProviderContext) { }); expect(Date.parse(match.createdAt)).to.be.greaterThan(0); expect(Date.parse(match.updatedAt)).to.be.greaterThan(0); + expect(Date.parse(match.updatedAt)).to.be.greaterThan(Date.parse(match.createdAt)); }); it(`shouldn't find alert from another space`, async () => { diff --git a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get.ts b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get.ts index 30b5e43aee585..a34e82ee888c1 100644 --- a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get.ts +++ b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get.ts @@ -53,6 +53,9 @@ export default function createGetTests({ getService }: FtrProviderContext) { }); expect(Date.parse(response.body.createdAt)).to.be.greaterThan(0); expect(Date.parse(response.body.updatedAt)).to.be.greaterThan(0); + expect(Date.parse(response.body.updatedAt)).to.be.greaterThan( + Date.parse(response.body.createdAt) + ); }); it(`shouldn't find alert from another space`, async () => { diff --git a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/update.ts b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/update.ts index 3dd831cafa59c..5a35d4bf83865 100644 --- a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/update.ts +++ b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/update.ts @@ -60,6 +60,9 @@ export default function createUpdateTests({ getService }: FtrProviderContext) { }); expect(Date.parse(response.body.createdAt)).to.be.greaterThan(0); expect(Date.parse(response.body.updatedAt)).to.be.greaterThan(0); + expect(Date.parse(response.body.updatedAt)).to.be.greaterThan( + Date.parse(response.body.createdAt) + ); // Ensure AAD isn't broken await checkAAD({ From 2f505419f45e4cdf5fe0ad7b78f36571761d77be Mon Sep 17 00:00:00 2001 From: Gidi Meir Morris Date: Fri, 3 Jan 2020 10:02:14 +0000 Subject: [PATCH 5/9] fixed misuse of null on updatedAt --- x-pack/legacy/plugins/alerting/server/routes/create.test.ts | 2 +- x-pack/legacy/plugins/alerting/server/routes/get.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/legacy/plugins/alerting/server/routes/create.test.ts b/x-pack/legacy/plugins/alerting/server/routes/create.test.ts index 6c18deeee7580..51b9668b6131a 100644 --- a/x-pack/legacy/plugins/alerting/server/routes/create.test.ts +++ b/x-pack/legacy/plugins/alerting/server/routes/create.test.ts @@ -41,7 +41,7 @@ test('creates an alert with proper parameters', async () => { }; const createdAt = new Date(); - const updatedAt = null; + const updatedAt = new Date(); alertsClient.create.mockResolvedValueOnce({ ...mockedAlert, createdAt, diff --git a/x-pack/legacy/plugins/alerting/server/routes/get.test.ts b/x-pack/legacy/plugins/alerting/server/routes/get.test.ts index 7af3a11ee6d77..2ac7d257139f3 100644 --- a/x-pack/legacy/plugins/alerting/server/routes/get.test.ts +++ b/x-pack/legacy/plugins/alerting/server/routes/get.test.ts @@ -18,7 +18,7 @@ const mockedAlert = { bar: true, }, createdAt: new Date(), - updatedAt: null, + updatedAt: new Date(), actions: [ { group: 'default', From 5a4057d39cc6b952c696a88363ecb267a285d933 Mon Sep 17 00:00:00 2001 From: Gidi Meir Morris Date: Fri, 3 Jan 2020 10:13:43 +0000 Subject: [PATCH 6/9] fixed tests --- .../alerting/server/routes/create.test.ts | 29 +------------------ .../alerting/server/routes/get.test.ts | 6 ++-- 2 files changed, 5 insertions(+), 30 deletions(-) diff --git a/x-pack/legacy/plugins/alerting/server/routes/create.test.ts b/x-pack/legacy/plugins/alerting/server/routes/create.test.ts index 51b9668b6131a..a614c70dc0271 100644 --- a/x-pack/legacy/plugins/alerting/server/routes/create.test.ts +++ b/x-pack/legacy/plugins/alerting/server/routes/create.test.ts @@ -58,34 +58,7 @@ test('creates an alert with proper parameters', async () => { expect(statusCode).toBe(200); const response = JSON.parse(payload); expect(new Date(response.createdAt)).toEqual(createdAt); - expect(omit(response, 'createdAt')).toMatchInlineSnapshot(` - Object { - "actions": Array [ - Object { - "actionTypeId": "test", - "group": "default", - "id": "2", - "params": Object { - "foo": true, - }, - }, - ], - "alertTypeId": "1", - "consumer": "bar", - "id": "123", - "name": "abc", - "params": Object { - "bar": true, - }, - "schedule": Object { - "interval": "10s", - }, - "tags": Array [ - "foo", - ], - "updatedAt": null, - } - `); + expect(omit(response, 'createdAt', 'updatedAt')).toMatchInlineSnapshot(); expect(alertsClient.create).toHaveBeenCalledTimes(1); expect(alertsClient.create.mock.calls[0]).toMatchInlineSnapshot(` Array [ diff --git a/x-pack/legacy/plugins/alerting/server/routes/get.test.ts b/x-pack/legacy/plugins/alerting/server/routes/get.test.ts index 2ac7d257139f3..5b1bdc7f69708 100644 --- a/x-pack/legacy/plugins/alerting/server/routes/get.test.ts +++ b/x-pack/legacy/plugins/alerting/server/routes/get.test.ts @@ -42,8 +42,10 @@ test('calls get with proper parameters', async () => { alertsClient.get.mockResolvedValueOnce(mockedAlert); const { payload, statusCode } = await server.inject(request); expect(statusCode).toBe(200); - const { createdAt, ...response } = JSON.parse(payload); - expect({ createdAt: new Date(createdAt), ...response }).toEqual(mockedAlert); + const { createdAt, updatedAt, ...response } = JSON.parse(payload); + expect({ createdAt: new Date(createdAt), updatedAt: new Date(updatedAt), ...response }).toEqual( + mockedAlert + ); expect(alertsClient.get).toHaveBeenCalledTimes(1); expect(alertsClient.get.mock.calls[0]).toMatchInlineSnapshot(` Array [ From 954a71a84c880a3cef2493dfd02e0677cc574f55 Mon Sep 17 00:00:00 2001 From: Gidi Meir Morris Date: Fri, 3 Jan 2020 10:14:36 +0000 Subject: [PATCH 7/9] fixed tests --- .../alerting/server/routes/create.test.ts | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/x-pack/legacy/plugins/alerting/server/routes/create.test.ts b/x-pack/legacy/plugins/alerting/server/routes/create.test.ts index a614c70dc0271..03b33b0bd40b0 100644 --- a/x-pack/legacy/plugins/alerting/server/routes/create.test.ts +++ b/x-pack/legacy/plugins/alerting/server/routes/create.test.ts @@ -58,7 +58,33 @@ test('creates an alert with proper parameters', async () => { expect(statusCode).toBe(200); const response = JSON.parse(payload); expect(new Date(response.createdAt)).toEqual(createdAt); - expect(omit(response, 'createdAt', 'updatedAt')).toMatchInlineSnapshot(); + expect(omit(response, 'createdAt', 'updatedAt')).toMatchInlineSnapshot(` + Object { + "actions": Array [ + Object { + "actionTypeId": "test", + "group": "default", + "id": "2", + "params": Object { + "foo": true, + }, + }, + ], + "alertTypeId": "1", + "consumer": "bar", + "id": "123", + "name": "abc", + "params": Object { + "bar": true, + }, + "schedule": Object { + "interval": "10s", + }, + "tags": Array [ + "foo", + ], + } + `); expect(alertsClient.create).toHaveBeenCalledTimes(1); expect(alertsClient.create.mock.calls[0]).toMatchInlineSnapshot(` Array [ From 924393f7f557adfe7f54cce93eb61bd287983652 Mon Sep 17 00:00:00 2001 From: Gidi Meir Morris Date: Fri, 3 Jan 2020 13:00:42 +0000 Subject: [PATCH 8/9] updatedAt should equal createdAt on creation --- .../spaces_only/tests/alerting/create.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/create.ts b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/create.ts index a34cf0b8beb72..e3741a8d3c18a 100644 --- a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/create.ts +++ b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/create.ts @@ -84,9 +84,7 @@ export default function createAlertTests({ getService }: FtrProviderContext) { }); expect(Date.parse(response.body.createdAt)).to.be.greaterThan(0); expect(Date.parse(response.body.updatedAt)).to.be.greaterThan(0); - expect(Date.parse(response.body.updatedAt)).to.be.greaterThan( - Date.parse(response.body.createdAt) - ); + expect(Date.parse(response.body.updatedAt)).to.eql(Date.parse(response.body.createdAt)); expect(typeof response.body.scheduledTaskId).to.be('string'); const { _source: taskRecord } = await getScheduledTask(response.body.scheduledTaskId); From 6dff233d59b5d2fefb6bbc4b157af41f64faad76 Mon Sep 17 00:00:00 2001 From: Gidi Meir Morris Date: Fri, 3 Jan 2020 15:08:48 +0000 Subject: [PATCH 9/9] removed assertions for updatedAt in non-update operations --- .../security_and_spaces/tests/alerting/create.ts | 4 +--- .../security_and_spaces/tests/alerting/find.ts | 2 -- .../security_and_spaces/tests/alerting/get.ts | 3 --- .../spaces_only/tests/alerting/create.ts | 1 - .../spaces_only/tests/alerting/find.ts | 1 - .../spaces_only/tests/alerting/get.ts | 3 --- 6 files changed, 1 insertion(+), 13 deletions(-) diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/create.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/create.ts index efc6506e009e7..003bb10c0adae 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/create.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/create.ts @@ -103,9 +103,7 @@ export default function createAlertTests({ getService }: FtrProviderContext) { expect(typeof response.body.scheduledTaskId).to.be('string'); expect(Date.parse(response.body.createdAt)).to.be.greaterThan(0); expect(Date.parse(response.body.updatedAt)).to.be.greaterThan(0); - expect(Date.parse(response.body.updatedAt)).to.be.greaterThan( - Date.parse(response.body.createdAt) - ); + const { _source: taskRecord } = await getScheduledTask(response.body.scheduledTaskId); expect(taskRecord.type).to.eql('task'); expect(taskRecord.task.taskType).to.eql('alerting:test.noop'); diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/find.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/find.ts index 4d47e0c936d00..d99ab794cd28f 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/find.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/find.ts @@ -76,7 +76,6 @@ export default function createFindTests({ getService }: FtrProviderContext) { }); expect(Date.parse(match.createdAt)).to.be.greaterThan(0); expect(Date.parse(match.updatedAt)).to.be.greaterThan(0); - expect(Date.parse(match.updatedAt)).to.be.greaterThan(Date.parse(match.createdAt)); break; default: throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); @@ -167,7 +166,6 @@ export default function createFindTests({ getService }: FtrProviderContext) { }); expect(Date.parse(match.createdAt)).to.be.greaterThan(0); expect(Date.parse(match.updatedAt)).to.be.greaterThan(0); - expect(Date.parse(match.updatedAt)).to.be.greaterThan(Date.parse(match.createdAt)); break; default: throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/get.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/get.ts index ad1890ad6beec..20eed4013d7dd 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/get.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/get.ts @@ -70,9 +70,6 @@ export default function createGetTests({ getService }: FtrProviderContext) { }); expect(Date.parse(response.body.createdAt)).to.be.greaterThan(0); expect(Date.parse(response.body.updatedAt)).to.be.greaterThan(0); - expect(Date.parse(response.body.updatedAt)).to.be.greaterThan( - Date.parse(response.body.createdAt) - ); break; default: throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); diff --git a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/create.ts b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/create.ts index e3741a8d3c18a..50e01c65b6a86 100644 --- a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/create.ts +++ b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/create.ts @@ -84,7 +84,6 @@ export default function createAlertTests({ getService }: FtrProviderContext) { }); expect(Date.parse(response.body.createdAt)).to.be.greaterThan(0); expect(Date.parse(response.body.updatedAt)).to.be.greaterThan(0); - expect(Date.parse(response.body.updatedAt)).to.eql(Date.parse(response.body.createdAt)); expect(typeof response.body.scheduledTaskId).to.be('string'); const { _source: taskRecord } = await getScheduledTask(response.body.scheduledTaskId); diff --git a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/find.ts b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/find.ts index 1e285acf09637..70935a462d03e 100644 --- a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/find.ts +++ b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/find.ts @@ -59,7 +59,6 @@ export default function createFindTests({ getService }: FtrProviderContext) { }); expect(Date.parse(match.createdAt)).to.be.greaterThan(0); expect(Date.parse(match.updatedAt)).to.be.greaterThan(0); - expect(Date.parse(match.updatedAt)).to.be.greaterThan(Date.parse(match.createdAt)); }); it(`shouldn't find alert from another space`, async () => { diff --git a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get.ts b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get.ts index a34e82ee888c1..30b5e43aee585 100644 --- a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get.ts +++ b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get.ts @@ -53,9 +53,6 @@ export default function createGetTests({ getService }: FtrProviderContext) { }); expect(Date.parse(response.body.createdAt)).to.be.greaterThan(0); expect(Date.parse(response.body.updatedAt)).to.be.greaterThan(0); - expect(Date.parse(response.body.updatedAt)).to.be.greaterThan( - Date.parse(response.body.createdAt) - ); }); it(`shouldn't find alert from another space`, async () => {