From f87434b7b31dbda13522e5130fe1300b3aac769c Mon Sep 17 00:00:00 2001 From: Ying Mao Date: Tue, 1 Dec 2020 16:13:42 -0500 Subject: [PATCH 1/4] Adding delay between creating and updating alert to avoid 409 conflicts --- .../security_and_spaces/tests/alerting/delete.ts | 3 +++ .../security_and_spaces/tests/alerting/disable.ts | 3 +++ .../security_and_spaces/tests/alerting/enable.ts | 3 +++ .../security_and_spaces/tests/alerting/event_log.ts | 3 +++ .../security_and_spaces/tests/alerting/execution_status.ts | 3 +++ .../security_and_spaces/tests/alerting/update.ts | 3 +++ .../security_and_spaces/tests/alerting/update_api_key.ts | 3 +++ .../spaces_only/tests/alerting/execution_status.ts | 3 +++ 8 files changed, 24 insertions(+) diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/delete.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/delete.ts index fc453c8da72e7..3dbc29edd7490 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/delete.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/delete.ts @@ -301,6 +301,9 @@ export default function createDeleteTests({ getService }: FtrProviderContext) { .send(getTestAlertData()) .expect(200); + // Delay before performing update to avoid 409 errors + await new Promise((resolve) => setTimeout(resolve, 1000)); + await supertest .put( `${getUrlPrefix(space.id)}/api/alerts_fixture/saved_object/alert/${createdAlert.id}` diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/disable.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/disable.ts index 4e4f9053bd24f..39edc4e170adb 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/disable.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/disable.ts @@ -287,6 +287,9 @@ export default function createDisableAlertTests({ getService }: FtrProviderConte .expect(200); objectRemover.add(space.id, createdAlert.id, 'alert', 'alerts'); + // Delay before performing update to avoid 409 errors + await new Promise((resolve) => setTimeout(resolve, 1000)); + await supertest .put( `${getUrlPrefix(space.id)}/api/alerts_fixture/saved_object/alert/${createdAlert.id}` diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/enable.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/enable.ts index d7f6546bf34a9..34ef1a4c960f2 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/enable.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/enable.ts @@ -304,6 +304,9 @@ export default function createEnableAlertTests({ getService }: FtrProviderContex .expect(200); objectRemover.add(space.id, createdAlert.id, 'alert', 'alerts'); + // Delay before performing update to avoid 409 errors + await new Promise((resolve) => setTimeout(resolve, 1000)); + await supertest .put( `${getUrlPrefix(space.id)}/api/alerts_fixture/saved_object/alert/${createdAlert.id}` diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/event_log.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/event_log.ts index 385d8bfca4a9a..49db5c3650389 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/event_log.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/event_log.ts @@ -37,6 +37,9 @@ export default function eventLogTests({ getService }: FtrProviderContext) { const alertId = response.body.id; objectRemover.add(spaceId, alertId, 'alert', 'alerts'); + // Delay before performing update to avoid 409 errors + await new Promise((resolve) => setTimeout(resolve, 1000)); + // break AAD await supertest .put(`${getUrlPrefix(spaceId)}/api/alerts_fixture/saved_object/alert/${alertId}`) diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/execution_status.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/execution_status.ts index 4058b71356280..896525de9520d 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/execution_status.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/execution_status.ts @@ -37,6 +37,9 @@ export default function executionStatusAlertTests({ getService }: FtrProviderCon let executionStatus = await waitForStatus(alertId, new Set(['ok']), 10000); + // Delay before performing update to avoid 409 errors + await new Promise((resolve) => setTimeout(resolve, 1000)); + // break AAD await supertest .put(`${getUrlPrefix(spaceId)}/api/alerts_fixture/saved_object/alert/${alertId}`) 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 9c3d2801c0886..377b421e390d7 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 @@ -429,6 +429,9 @@ export default function createUpdateTests({ getService }: FtrProviderContext) { .expect(200); objectRemover.add(space.id, createdAlert.id, 'alert', 'alerts'); + // Delay before performing update to avoid 409 errors + await new Promise((resolve) => setTimeout(resolve, 1000)); + await supertest .put( `${getUrlPrefix(space.id)}/api/alerts_fixture/saved_object/alert/${createdAlert.id}` diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update_api_key.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update_api_key.ts index 7dea591b895ee..7f3a4c32ab532 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update_api_key.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update_api_key.ts @@ -301,6 +301,9 @@ export default function createUpdateApiKeyTests({ getService }: FtrProviderConte .expect(200); objectRemover.add(space.id, createdAlert.id, 'alert', 'alerts'); + // Delay before performing update to avoid 409 errors + await new Promise((resolve) => setTimeout(resolve, 1000)); + await supertest .put( `${getUrlPrefix(space.id)}/api/alerts_fixture/saved_object/alert/${createdAlert.id}` diff --git a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/execution_status.ts b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/execution_status.ts index 642173a7c2c6c..7fcfef3b1c678 100644 --- a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/execution_status.ts +++ b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/execution_status.ts @@ -196,6 +196,9 @@ export default function executionStatusAlertTests({ getService }: FtrProviderCon let executionStatus = await waitForStatus(alertId, new Set(['ok'])); + // Delay before performing update to avoid 409 errors + await new Promise((resolve) => setTimeout(resolve, 1000)); + // break the validation of the params await supertest .put(`${getUrlPrefix(Spaces.space1.id)}/api/alerts_fixture/saved_object/alert/${alertId}`) From 839d12f075b4de6a76987ad87c32640dfb900862 Mon Sep 17 00:00:00 2001 From: Ying Mao Date: Thu, 3 Dec 2020 08:13:01 -0500 Subject: [PATCH 2/4] Unskipping update test --- .../security_and_spaces/tests/alerting/update.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 377b421e390d7..61c07164bda73 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 @@ -31,8 +31,7 @@ export default function createUpdateTests({ getService }: FtrProviderContext) { .then((response: SupertestResponse) => response.body); } - // FLAKY: https://github.com/elastic/kibana/issues/82804 - describe.skip('update', () => { + describe('update', () => { const objectRemover = new ObjectRemover(supertest); after(() => objectRemover.removeAll()); From c7f7a25ff64d5ef3a9e392d72abefd1a27203582 Mon Sep 17 00:00:00 2001 From: Ying Mao Date: Fri, 4 Dec 2020 14:39:13 -0500 Subject: [PATCH 3/4] Using retry.try instead of delay --- .../tests/alerting/delete.ts | 102 +++++------ .../tests/alerting/disable.ts | 108 ++++++------ .../tests/alerting/enable.ts | 122 ++++++------- .../tests/alerting/event_log.ts | 79 +++++---- .../tests/alerting/execution_status.ts | 35 ++-- .../tests/alerting/update.ts | 161 +++++++++--------- .../tests/alerting/update_api_key.ts | 106 ++++++------ .../tests/alerting/execution_status.ts | 3 - 8 files changed, 358 insertions(+), 358 deletions(-) diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/delete.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/delete.ts index 3dbc29edd7490..5b099737d2999 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/delete.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/delete.ts @@ -19,6 +19,7 @@ import { FtrProviderContext } from '../../../common/ftr_provider_context'; export default function createDeleteTests({ getService }: FtrProviderContext) { const supertest = getService('supertest'); const es = getService('legacyEs'); + const retry = getService('retry'); const supertestWithoutAuth = getService('supertestWithoutAuth'); describe('delete', () => { @@ -301,60 +302,59 @@ export default function createDeleteTests({ getService }: FtrProviderContext) { .send(getTestAlertData()) .expect(200); - // Delay before performing update to avoid 409 errors - await new Promise((resolve) => setTimeout(resolve, 1000)); + await retry.try(async () => { + await supertest + .put( + `${getUrlPrefix(space.id)}/api/alerts_fixture/saved_object/alert/${createdAlert.id}` + ) + .set('kbn-xsrf', 'foo') + .send({ + attributes: { + name: 'bar', + }, + }) + .expect(200); - await supertest - .put( - `${getUrlPrefix(space.id)}/api/alerts_fixture/saved_object/alert/${createdAlert.id}` - ) - .set('kbn-xsrf', 'foo') - .send({ - attributes: { - name: 'bar', - }, - }) - .expect(200); + const response = await supertestWithoutAuth + .delete(`${getUrlPrefix(space.id)}/api/alerts/alert/${createdAlert.id}`) + .set('kbn-xsrf', 'foo') + .auth(user.username, user.password); - const response = await supertestWithoutAuth - .delete(`${getUrlPrefix(space.id)}/api/alerts/alert/${createdAlert.id}`) - .set('kbn-xsrf', 'foo') - .auth(user.username, user.password); - - switch (scenario.id) { - case 'no_kibana_privileges at space1': - case 'space_1_all at space2': - case 'global_read at space1': - expect(response.statusCode).to.eql(403); - expect(response.body).to.eql({ - error: 'Forbidden', - message: getConsumerUnauthorizedErrorMessage( - 'delete', - 'test.noop', - 'alertsFixture' - ), - statusCode: 403, - }); - objectRemover.add(space.id, createdAlert.id, 'alert', 'alerts'); - // Ensure task still exists - await getScheduledTask(createdAlert.scheduledTaskId); - break; - case 'superuser at space1': - case 'space_1_all at space1': - case 'space_1_all_alerts_none_actions at space1': - case 'space_1_all_with_restricted_fixture at space1': - expect(response.statusCode).to.eql(204); - expect(response.body).to.eql(''); - try { + switch (scenario.id) { + case 'no_kibana_privileges at space1': + case 'space_1_all at space2': + case 'global_read at space1': + expect(response.statusCode).to.eql(403); + expect(response.body).to.eql({ + error: 'Forbidden', + message: getConsumerUnauthorizedErrorMessage( + 'delete', + 'test.noop', + 'alertsFixture' + ), + statusCode: 403, + }); + objectRemover.add(space.id, createdAlert.id, 'alert', 'alerts'); + // Ensure task still exists await getScheduledTask(createdAlert.scheduledTaskId); - throw new Error('Should have removed scheduled task'); - } catch (e) { - expect(e.status).to.eql(404); - } - break; - default: - throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); - } + break; + case 'superuser at space1': + case 'space_1_all at space1': + case 'space_1_all_alerts_none_actions at space1': + case 'space_1_all_with_restricted_fixture at space1': + expect(response.statusCode).to.eql(204); + expect(response.body).to.eql(''); + try { + await getScheduledTask(createdAlert.scheduledTaskId); + throw new Error('Should have removed scheduled task'); + } catch (e) { + expect(e.status).to.eql(404); + } + break; + default: + throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); + } + }); }); }); } diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/disable.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/disable.ts index 39edc4e170adb..d4fe109782913 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/disable.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/disable.ts @@ -20,6 +20,7 @@ import { // eslint-disable-next-line import/no-default-export export default function createDisableAlertTests({ getService }: FtrProviderContext) { const es = getService('legacyEs'); + const retry = getService('retry'); const supertest = getService('supertest'); const supertestWithoutAuth = getService('supertestWithoutAuth'); @@ -287,63 +288,62 @@ export default function createDisableAlertTests({ getService }: FtrProviderConte .expect(200); objectRemover.add(space.id, createdAlert.id, 'alert', 'alerts'); - // Delay before performing update to avoid 409 errors - await new Promise((resolve) => setTimeout(resolve, 1000)); - - await supertest - .put( - `${getUrlPrefix(space.id)}/api/alerts_fixture/saved_object/alert/${createdAlert.id}` - ) - .set('kbn-xsrf', 'foo') - .send({ - attributes: { - name: 'bar', - }, - }) - .expect(200); + await retry.try(async () => { + await supertest + .put( + `${getUrlPrefix(space.id)}/api/alerts_fixture/saved_object/alert/${createdAlert.id}` + ) + .set('kbn-xsrf', 'foo') + .send({ + attributes: { + name: 'bar', + }, + }) + .expect(200); - const response = await alertUtils.getDisableRequest(createdAlert.id); + const response = await alertUtils.getDisableRequest(createdAlert.id); - switch (scenario.id) { - case 'no_kibana_privileges at space1': - case 'space_1_all at space2': - case 'global_read at space1': - expect(response.statusCode).to.eql(403); - expect(response.body).to.eql({ - error: 'Forbidden', - message: getConsumerUnauthorizedErrorMessage( - 'disable', - 'test.noop', - 'alertsFixture' - ), - statusCode: 403, - }); - // Ensure task still exists - await getScheduledTask(createdAlert.scheduledTaskId); - break; - case 'superuser at space1': - case 'space_1_all at space1': - case 'space_1_all_alerts_none_actions at space1': - case 'space_1_all_with_restricted_fixture at space1': - expect(response.statusCode).to.eql(204); - expect(response.body).to.eql(''); - try { + switch (scenario.id) { + case 'no_kibana_privileges at space1': + case 'space_1_all at space2': + case 'global_read at space1': + expect(response.statusCode).to.eql(403); + expect(response.body).to.eql({ + error: 'Forbidden', + message: getConsumerUnauthorizedErrorMessage( + 'disable', + 'test.noop', + 'alertsFixture' + ), + statusCode: 403, + }); + // Ensure task still exists await getScheduledTask(createdAlert.scheduledTaskId); - throw new Error('Should have removed scheduled task'); - } catch (e) { - expect(e.status).to.eql(404); - } - // Ensure AAD isn't broken - await checkAAD({ - supertest, - spaceId: space.id, - type: 'alert', - id: createdAlert.id, - }); - break; - default: - throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); - } + break; + case 'superuser at space1': + case 'space_1_all at space1': + case 'space_1_all_alerts_none_actions at space1': + case 'space_1_all_with_restricted_fixture at space1': + expect(response.statusCode).to.eql(204); + expect(response.body).to.eql(''); + try { + await getScheduledTask(createdAlert.scheduledTaskId); + throw new Error('Should have removed scheduled task'); + } catch (e) { + expect(e.status).to.eql(404); + } + // Ensure AAD isn't broken + await checkAAD({ + supertest, + spaceId: space.id, + type: 'alert', + id: createdAlert.id, + }); + break; + default: + throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); + } + }); }); it(`shouldn't disable alert from another space`, async () => { diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/enable.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/enable.ts index 34ef1a4c960f2..950bf87803eea 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/enable.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/enable.ts @@ -20,6 +20,7 @@ import { // eslint-disable-next-line import/no-default-export export default function createEnableAlertTests({ getService }: FtrProviderContext) { const es = getService('legacyEs'); + const retry = getService('retry'); const supertest = getService('supertest'); const supertestWithoutAuth = getService('supertestWithoutAuth'); @@ -304,68 +305,69 @@ export default function createEnableAlertTests({ getService }: FtrProviderContex .expect(200); objectRemover.add(space.id, createdAlert.id, 'alert', 'alerts'); - // Delay before performing update to avoid 409 errors - await new Promise((resolve) => setTimeout(resolve, 1000)); - - await supertest - .put( - `${getUrlPrefix(space.id)}/api/alerts_fixture/saved_object/alert/${createdAlert.id}` - ) - .set('kbn-xsrf', 'foo') - .send({ - attributes: { - name: 'bar', - }, - }) - .expect(200); + await retry.try(async () => { + await supertest + .put( + `${getUrlPrefix(space.id)}/api/alerts_fixture/saved_object/alert/${createdAlert.id}` + ) + .set('kbn-xsrf', 'foo') + .send({ + attributes: { + name: 'bar', + }, + }) + .expect(200); - const response = await alertUtils.getEnableRequest(createdAlert.id); + const response = await alertUtils.getEnableRequest(createdAlert.id); - switch (scenario.id) { - case 'no_kibana_privileges at space1': - case 'space_1_all at space2': - case 'global_read at space1': - expect(response.statusCode).to.eql(403); - expect(response.body).to.eql({ - error: 'Forbidden', - message: getConsumerUnauthorizedErrorMessage( - 'enable', - 'test.noop', - 'alertsFixture' - ), - statusCode: 403, - }); - break; - case 'superuser at space1': - case 'space_1_all at space1': - case 'space_1_all_alerts_none_actions at space1': - case 'space_1_all_with_restricted_fixture at space1': - expect(response.statusCode).to.eql(204); - expect(response.body).to.eql(''); - const { body: updatedAlert } = await supertestWithoutAuth - .get(`${getUrlPrefix(space.id)}/api/alerts/alert/${createdAlert.id}`) - .set('kbn-xsrf', 'foo') - .auth(user.username, user.password) - .expect(200); - expect(typeof updatedAlert.scheduledTaskId).to.eql('string'); - const { _source: taskRecord } = await getScheduledTask(updatedAlert.scheduledTaskId); - expect(taskRecord.type).to.eql('task'); - expect(taskRecord.task.taskType).to.eql('alerting:test.noop'); - expect(JSON.parse(taskRecord.task.params)).to.eql({ - alertId: createdAlert.id, - spaceId: space.id, - }); - // Ensure AAD isn't broken - await checkAAD({ - supertest, - spaceId: space.id, - type: 'alert', - id: createdAlert.id, - }); - break; - default: - throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); - } + switch (scenario.id) { + case 'no_kibana_privileges at space1': + case 'space_1_all at space2': + case 'global_read at space1': + expect(response.statusCode).to.eql(403); + expect(response.body).to.eql({ + error: 'Forbidden', + message: getConsumerUnauthorizedErrorMessage( + 'enable', + 'test.noop', + 'alertsFixture' + ), + statusCode: 403, + }); + break; + case 'superuser at space1': + case 'space_1_all at space1': + case 'space_1_all_alerts_none_actions at space1': + case 'space_1_all_with_restricted_fixture at space1': + expect(response.statusCode).to.eql(204); + expect(response.body).to.eql(''); + const { body: updatedAlert } = await supertestWithoutAuth + .get(`${getUrlPrefix(space.id)}/api/alerts/alert/${createdAlert.id}`) + .set('kbn-xsrf', 'foo') + .auth(user.username, user.password) + .expect(200); + expect(typeof updatedAlert.scheduledTaskId).to.eql('string'); + const { _source: taskRecord } = await getScheduledTask( + updatedAlert.scheduledTaskId + ); + expect(taskRecord.type).to.eql('task'); + expect(taskRecord.task.taskType).to.eql('alerting:test.noop'); + expect(JSON.parse(taskRecord.task.params)).to.eql({ + alertId: createdAlert.id, + spaceId: space.id, + }); + // Ensure AAD isn't broken + await checkAAD({ + supertest, + spaceId: space.id, + type: 'alert', + id: createdAlert.id, + }); + break; + default: + throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); + } + }); }); it(`shouldn't enable alert from another space`, async () => { diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/event_log.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/event_log.ts index 64614e545edef..64786cc642918 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/event_log.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/event_log.ts @@ -37,50 +37,49 @@ export default function eventLogTests({ getService }: FtrProviderContext) { const alertId = response.body.id; objectRemover.add(spaceId, alertId, 'alert', 'alerts'); - // Delay before performing update to avoid 409 errors - await new Promise((resolve) => setTimeout(resolve, 1000)); - - // break AAD - await supertest - .put(`${getUrlPrefix(spaceId)}/api/alerts_fixture/saved_object/alert/${alertId}`) - .set('kbn-xsrf', 'foo') - .send({ - attributes: { - name: 'bar', - }, - }) - .expect(200); + await retry.try(async () => { + // break AAD + await supertest + .put(`${getUrlPrefix(spaceId)}/api/alerts_fixture/saved_object/alert/${alertId}`) + .set('kbn-xsrf', 'foo') + .send({ + attributes: { + name: 'bar', + }, + }) + .expect(200); - const events = await retry.try(async () => { - // there can be a successful execute before the error one - const someEvents = await getEventLog({ - getService, - spaceId, - type: 'alert', - id: alertId, - provider: 'alerting', - actions: new Map([['execute', { gte: 1 }]]), + const events = await retry.try(async () => { + // there can be a successful execute before the error one + const someEvents = await getEventLog({ + getService, + spaceId, + type: 'alert', + id: alertId, + provider: 'alerting', + actions: new Map([['execute', { gte: 1 }]]), + }); + const errorEvents = someEvents.filter( + (event) => event?.kibana?.alerting?.status === 'error' + ); + if (errorEvents.length === 0) { + throw new Error('no execute/error events yet'); + } + return errorEvents; }); - const errorEvents = someEvents.filter( - (event) => event?.kibana?.alerting?.status === 'error' - ); - if (errorEvents.length === 0) { - throw new Error('no execute/error events yet'); - } - return errorEvents; - }); - const event = events[0]; - expect(event).to.be.ok(); + const event = events[0]; + expect(event).to.be.ok(); - validateEvent(event, { - spaceId, - savedObjects: [{ type: 'alert', id: alertId, rel: 'primary' }], - outcome: 'failure', - message: `test.noop:${alertId}: execution failed`, - errorMessage: 'Unable to decrypt attribute "apiKey"', - status: 'error', - reason: 'decrypt', + validateEvent(event, { + spaceId, + savedObjects: [{ type: 'alert', id: alertId, rel: 'primary' }], + outcome: 'failure', + message: `test.noop:${alertId}: execution failed`, + errorMessage: 'Unable to decrypt attribute "apiKey"', + status: 'error', + reason: 'decrypt', + }); }); }); }); diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/execution_status.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/execution_status.ts index 896525de9520d..7326392ba5ca8 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/execution_status.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/execution_status.ts @@ -12,6 +12,7 @@ import { FtrProviderContext } from '../../../common/ftr_provider_context'; // eslint-disable-next-line import/no-default-export export default function executionStatusAlertTests({ getService }: FtrProviderContext) { + const retry = getService('retry'); const supertest = getService('supertest'); const spaceId = Spaces[0].id; @@ -37,24 +38,26 @@ export default function executionStatusAlertTests({ getService }: FtrProviderCon let executionStatus = await waitForStatus(alertId, new Set(['ok']), 10000); - // Delay before performing update to avoid 409 errors - await new Promise((resolve) => setTimeout(resolve, 1000)); + await retry.try(async () => { + // Delay before performing update to avoid 409 errors + await new Promise((resolve) => setTimeout(resolve, 1000)); - // break AAD - await supertest - .put(`${getUrlPrefix(spaceId)}/api/alerts_fixture/saved_object/alert/${alertId}`) - .set('kbn-xsrf', 'foo') - .send({ - attributes: { - name: 'bar', - }, - }) - .expect(200); + // break AAD + await supertest + .put(`${getUrlPrefix(spaceId)}/api/alerts_fixture/saved_object/alert/${alertId}`) + .set('kbn-xsrf', 'foo') + .send({ + attributes: { + name: 'bar', + }, + }) + .expect(200); - executionStatus = await waitForStatus(alertId, new Set(['error'])); - expect(executionStatus.error).to.be.ok(); - expect(executionStatus.error.reason).to.be(AlertExecutionStatusErrorReasons.Decrypt); - expect(executionStatus.error.message).to.be('Unable to decrypt attribute "apiKey"'); + executionStatus = await waitForStatus(alertId, new Set(['error'])); + expect(executionStatus.error).to.be.ok(); + expect(executionStatus.error.reason).to.be(AlertExecutionStatusErrorReasons.Decrypt); + expect(executionStatus.error.message).to.be('Unable to decrypt attribute "apiKey"'); + }); }); }); 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 61c07164bda73..81ad52c36f693 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 @@ -428,89 +428,88 @@ export default function createUpdateTests({ getService }: FtrProviderContext) { .expect(200); objectRemover.add(space.id, createdAlert.id, 'alert', 'alerts'); - // Delay before performing update to avoid 409 errors - await new Promise((resolve) => setTimeout(resolve, 1000)); + await retry.try(async () => { + await supertest + .put( + `${getUrlPrefix(space.id)}/api/alerts_fixture/saved_object/alert/${createdAlert.id}` + ) + .set('kbn-xsrf', 'foo') + .send({ + attributes: { + name: 'bar', + }, + }) + .expect(200); - await supertest - .put( - `${getUrlPrefix(space.id)}/api/alerts_fixture/saved_object/alert/${createdAlert.id}` - ) - .set('kbn-xsrf', 'foo') - .send({ - attributes: { - name: 'bar', + const updatedData = { + name: 'bcd', + tags: ['bar'], + params: { + foo: true, }, - }) - .expect(200); - - const updatedData = { - name: 'bcd', - tags: ['bar'], - params: { - foo: true, - }, - schedule: { interval: '12s' }, - actions: [], - throttle: '1m', - }; - const response = await supertestWithoutAuth - .put(`${getUrlPrefix(space.id)}/api/alerts/alert/${createdAlert.id}`) - .set('kbn-xsrf', 'foo') - .auth(user.username, user.password) - .send(updatedData); - - switch (scenario.id) { - case 'no_kibana_privileges at space1': - case 'space_1_all at space2': - case 'global_read at space1': - expect(response.statusCode).to.eql(403); - expect(response.body).to.eql({ - error: 'Forbidden', - message: getConsumerUnauthorizedErrorMessage( - 'update', - 'test.noop', - 'alertsFixture' - ), - statusCode: 403, - }); - break; - case 'superuser at space1': - case 'space_1_all at space1': - case 'space_1_all_alerts_none_actions at space1': - case 'space_1_all_with_restricted_fixture at space1': - expect(response.statusCode).to.eql(200); - expect(response.body).to.eql({ - ...updatedData, - id: createdAlert.id, - alertTypeId: 'test.noop', - consumer: 'alertsFixture', - createdBy: 'elastic', - enabled: true, - updatedBy: user.username, - apiKeyOwner: user.username, - muteAll: false, - mutedInstanceIds: [], - scheduledTaskId: createdAlert.scheduledTaskId, - createdAt: response.body.createdAt, - updatedAt: response.body.updatedAt, - executionStatus: response.body.executionStatus, - }); - 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, - spaceId: space.id, - type: 'alert', - id: createdAlert.id, - }); - break; - default: - throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); - } + schedule: { interval: '12s' }, + actions: [], + throttle: '1m', + }; + const response = await supertestWithoutAuth + .put(`${getUrlPrefix(space.id)}/api/alerts/alert/${createdAlert.id}`) + .set('kbn-xsrf', 'foo') + .auth(user.username, user.password) + .send(updatedData); + + switch (scenario.id) { + case 'no_kibana_privileges at space1': + case 'space_1_all at space2': + case 'global_read at space1': + expect(response.statusCode).to.eql(403); + expect(response.body).to.eql({ + error: 'Forbidden', + message: getConsumerUnauthorizedErrorMessage( + 'update', + 'test.noop', + 'alertsFixture' + ), + statusCode: 403, + }); + break; + case 'superuser at space1': + case 'space_1_all at space1': + case 'space_1_all_alerts_none_actions at space1': + case 'space_1_all_with_restricted_fixture at space1': + expect(response.statusCode).to.eql(200); + expect(response.body).to.eql({ + ...updatedData, + id: createdAlert.id, + alertTypeId: 'test.noop', + consumer: 'alertsFixture', + createdBy: 'elastic', + enabled: true, + updatedBy: user.username, + apiKeyOwner: user.username, + muteAll: false, + mutedInstanceIds: [], + scheduledTaskId: createdAlert.scheduledTaskId, + createdAt: response.body.createdAt, + updatedAt: response.body.updatedAt, + executionStatus: response.body.executionStatus, + }); + 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, + spaceId: space.id, + type: 'alert', + id: createdAlert.id, + }); + break; + default: + throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); + } + }); }); it('should handle update alert request appropriately when alert name has leading and trailing whitespaces', async () => { diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update_api_key.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update_api_key.ts index 7f3a4c32ab532..5f608c2dd17b4 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update_api_key.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update_api_key.ts @@ -19,6 +19,7 @@ import { // eslint-disable-next-line import/no-default-export export default function createUpdateApiKeyTests({ getService }: FtrProviderContext) { + const retry = getService('retry'); const supertest = getService('supertest'); const supertestWithoutAuth = getService('supertestWithoutAuth'); @@ -301,61 +302,60 @@ export default function createUpdateApiKeyTests({ getService }: FtrProviderConte .expect(200); objectRemover.add(space.id, createdAlert.id, 'alert', 'alerts'); - // Delay before performing update to avoid 409 errors - await new Promise((resolve) => setTimeout(resolve, 1000)); - - await supertest - .put( - `${getUrlPrefix(space.id)}/api/alerts_fixture/saved_object/alert/${createdAlert.id}` - ) - .set('kbn-xsrf', 'foo') - .send({ - attributes: { - name: 'bar', - }, - }) - .expect(200); + await retry.try(async () => { + await supertest + .put( + `${getUrlPrefix(space.id)}/api/alerts_fixture/saved_object/alert/${createdAlert.id}` + ) + .set('kbn-xsrf', 'foo') + .send({ + attributes: { + name: 'bar', + }, + }) + .expect(200); - const response = await alertUtils.getUpdateApiKeyRequest(createdAlert.id); + const response = await alertUtils.getUpdateApiKeyRequest(createdAlert.id); - switch (scenario.id) { - case 'no_kibana_privileges at space1': - case 'space_1_all at space2': - case 'global_read at space1': - expect(response.statusCode).to.eql(403); - expect(response.body).to.eql({ - error: 'Forbidden', - message: getConsumerUnauthorizedErrorMessage( - 'updateApiKey', - 'test.noop', - 'alertsFixture' - ), - statusCode: 403, - }); - break; - case 'superuser at space1': - case 'space_1_all at space1': - case 'space_1_all_alerts_none_actions at space1': - case 'space_1_all_with_restricted_fixture at space1': - expect(response.statusCode).to.eql(204); - expect(response.body).to.eql(''); - const { body: updatedAlert } = await supertestWithoutAuth - .get(`${getUrlPrefix(space.id)}/api/alerts/alert/${createdAlert.id}`) - .set('kbn-xsrf', 'foo') - .auth(user.username, user.password) - .expect(200); - expect(updatedAlert.apiKeyOwner).to.eql(user.username); - // Ensure AAD isn't broken - await checkAAD({ - supertest, - spaceId: space.id, - type: 'alert', - id: createdAlert.id, - }); - break; - default: - throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); - } + switch (scenario.id) { + case 'no_kibana_privileges at space1': + case 'space_1_all at space2': + case 'global_read at space1': + expect(response.statusCode).to.eql(403); + expect(response.body).to.eql({ + error: 'Forbidden', + message: getConsumerUnauthorizedErrorMessage( + 'updateApiKey', + 'test.noop', + 'alertsFixture' + ), + statusCode: 403, + }); + break; + case 'superuser at space1': + case 'space_1_all at space1': + case 'space_1_all_alerts_none_actions at space1': + case 'space_1_all_with_restricted_fixture at space1': + expect(response.statusCode).to.eql(204); + expect(response.body).to.eql(''); + const { body: updatedAlert } = await supertestWithoutAuth + .get(`${getUrlPrefix(space.id)}/api/alerts/alert/${createdAlert.id}`) + .set('kbn-xsrf', 'foo') + .auth(user.username, user.password) + .expect(200); + expect(updatedAlert.apiKeyOwner).to.eql(user.username); + // Ensure AAD isn't broken + await checkAAD({ + supertest, + spaceId: space.id, + type: 'alert', + id: createdAlert.id, + }); + break; + default: + throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); + } + }); }); it(`shouldn't update alert api key from another space`, async () => { diff --git a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/execution_status.ts b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/execution_status.ts index 7fcfef3b1c678..642173a7c2c6c 100644 --- a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/execution_status.ts +++ b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/execution_status.ts @@ -196,9 +196,6 @@ export default function executionStatusAlertTests({ getService }: FtrProviderCon let executionStatus = await waitForStatus(alertId, new Set(['ok'])); - // Delay before performing update to avoid 409 errors - await new Promise((resolve) => setTimeout(resolve, 1000)); - // break the validation of the params await supertest .put(`${getUrlPrefix(Spaces.space1.id)}/api/alerts_fixture/saved_object/alert/${alertId}`) From d22f9676df9da84956caeaf7efe596bcad574d73 Mon Sep 17 00:00:00 2001 From: Ying Mao Date: Mon, 7 Dec 2020 09:37:27 -0500 Subject: [PATCH 4/4] PR fixes --- .../tests/alerting/delete.ts | 76 +++++----- .../tests/alerting/disable.ts | 82 +++++------ .../tests/alerting/enable.ts | 96 ++++++------ .../tests/alerting/event_log.ts | 56 +++---- .../tests/alerting/execution_status.ts | 13 +- .../tests/alerting/update.ts | 138 +++++++++--------- .../tests/alerting/update_api_key.ts | 80 +++++----- 7 files changed, 268 insertions(+), 273 deletions(-) diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/delete.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/delete.ts index 5b099737d2999..f55b930a264c9 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/delete.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/delete.ts @@ -314,47 +314,47 @@ export default function createDeleteTests({ getService }: FtrProviderContext) { }, }) .expect(200); + }); - const response = await supertestWithoutAuth - .delete(`${getUrlPrefix(space.id)}/api/alerts/alert/${createdAlert.id}`) - .set('kbn-xsrf', 'foo') - .auth(user.username, user.password); + const response = await supertestWithoutAuth + .delete(`${getUrlPrefix(space.id)}/api/alerts/alert/${createdAlert.id}`) + .set('kbn-xsrf', 'foo') + .auth(user.username, user.password); - switch (scenario.id) { - case 'no_kibana_privileges at space1': - case 'space_1_all at space2': - case 'global_read at space1': - expect(response.statusCode).to.eql(403); - expect(response.body).to.eql({ - error: 'Forbidden', - message: getConsumerUnauthorizedErrorMessage( - 'delete', - 'test.noop', - 'alertsFixture' - ), - statusCode: 403, - }); - objectRemover.add(space.id, createdAlert.id, 'alert', 'alerts'); - // Ensure task still exists + switch (scenario.id) { + case 'no_kibana_privileges at space1': + case 'space_1_all at space2': + case 'global_read at space1': + expect(response.statusCode).to.eql(403); + expect(response.body).to.eql({ + error: 'Forbidden', + message: getConsumerUnauthorizedErrorMessage( + 'delete', + 'test.noop', + 'alertsFixture' + ), + statusCode: 403, + }); + objectRemover.add(space.id, createdAlert.id, 'alert', 'alerts'); + // Ensure task still exists + await getScheduledTask(createdAlert.scheduledTaskId); + break; + case 'superuser at space1': + case 'space_1_all at space1': + case 'space_1_all_alerts_none_actions at space1': + case 'space_1_all_with_restricted_fixture at space1': + expect(response.statusCode).to.eql(204); + expect(response.body).to.eql(''); + try { await getScheduledTask(createdAlert.scheduledTaskId); - break; - case 'superuser at space1': - case 'space_1_all at space1': - case 'space_1_all_alerts_none_actions at space1': - case 'space_1_all_with_restricted_fixture at space1': - expect(response.statusCode).to.eql(204); - expect(response.body).to.eql(''); - try { - await getScheduledTask(createdAlert.scheduledTaskId); - throw new Error('Should have removed scheduled task'); - } catch (e) { - expect(e.status).to.eql(404); - } - break; - default: - throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); - } - }); + throw new Error('Should have removed scheduled task'); + } catch (e) { + expect(e.status).to.eql(404); + } + break; + default: + throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); + } }); }); } diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/disable.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/disable.ts index d4fe109782913..03d8f77893802 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/disable.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/disable.ts @@ -300,50 +300,50 @@ export default function createDisableAlertTests({ getService }: FtrProviderConte }, }) .expect(200); + }); - const response = await alertUtils.getDisableRequest(createdAlert.id); + const response = await alertUtils.getDisableRequest(createdAlert.id); - switch (scenario.id) { - case 'no_kibana_privileges at space1': - case 'space_1_all at space2': - case 'global_read at space1': - expect(response.statusCode).to.eql(403); - expect(response.body).to.eql({ - error: 'Forbidden', - message: getConsumerUnauthorizedErrorMessage( - 'disable', - 'test.noop', - 'alertsFixture' - ), - statusCode: 403, - }); - // Ensure task still exists + switch (scenario.id) { + case 'no_kibana_privileges at space1': + case 'space_1_all at space2': + case 'global_read at space1': + expect(response.statusCode).to.eql(403); + expect(response.body).to.eql({ + error: 'Forbidden', + message: getConsumerUnauthorizedErrorMessage( + 'disable', + 'test.noop', + 'alertsFixture' + ), + statusCode: 403, + }); + // Ensure task still exists + await getScheduledTask(createdAlert.scheduledTaskId); + break; + case 'superuser at space1': + case 'space_1_all at space1': + case 'space_1_all_alerts_none_actions at space1': + case 'space_1_all_with_restricted_fixture at space1': + expect(response.statusCode).to.eql(204); + expect(response.body).to.eql(''); + try { await getScheduledTask(createdAlert.scheduledTaskId); - break; - case 'superuser at space1': - case 'space_1_all at space1': - case 'space_1_all_alerts_none_actions at space1': - case 'space_1_all_with_restricted_fixture at space1': - expect(response.statusCode).to.eql(204); - expect(response.body).to.eql(''); - try { - await getScheduledTask(createdAlert.scheduledTaskId); - throw new Error('Should have removed scheduled task'); - } catch (e) { - expect(e.status).to.eql(404); - } - // Ensure AAD isn't broken - await checkAAD({ - supertest, - spaceId: space.id, - type: 'alert', - id: createdAlert.id, - }); - break; - default: - throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); - } - }); + throw new Error('Should have removed scheduled task'); + } catch (e) { + expect(e.status).to.eql(404); + } + // Ensure AAD isn't broken + await checkAAD({ + supertest, + spaceId: space.id, + type: 'alert', + id: createdAlert.id, + }); + break; + default: + throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); + } }); it(`shouldn't disable alert from another space`, async () => { diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/enable.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/enable.ts index 950bf87803eea..2444f37ea5b20 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/enable.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/enable.ts @@ -317,57 +317,55 @@ export default function createEnableAlertTests({ getService }: FtrProviderContex }, }) .expect(200); + }); - const response = await alertUtils.getEnableRequest(createdAlert.id); + const response = await alertUtils.getEnableRequest(createdAlert.id); - switch (scenario.id) { - case 'no_kibana_privileges at space1': - case 'space_1_all at space2': - case 'global_read at space1': - expect(response.statusCode).to.eql(403); - expect(response.body).to.eql({ - error: 'Forbidden', - message: getConsumerUnauthorizedErrorMessage( - 'enable', - 'test.noop', - 'alertsFixture' - ), - statusCode: 403, - }); - break; - case 'superuser at space1': - case 'space_1_all at space1': - case 'space_1_all_alerts_none_actions at space1': - case 'space_1_all_with_restricted_fixture at space1': - expect(response.statusCode).to.eql(204); - expect(response.body).to.eql(''); - const { body: updatedAlert } = await supertestWithoutAuth - .get(`${getUrlPrefix(space.id)}/api/alerts/alert/${createdAlert.id}`) - .set('kbn-xsrf', 'foo') - .auth(user.username, user.password) - .expect(200); - expect(typeof updatedAlert.scheduledTaskId).to.eql('string'); - const { _source: taskRecord } = await getScheduledTask( - updatedAlert.scheduledTaskId - ); - expect(taskRecord.type).to.eql('task'); - expect(taskRecord.task.taskType).to.eql('alerting:test.noop'); - expect(JSON.parse(taskRecord.task.params)).to.eql({ - alertId: createdAlert.id, - spaceId: space.id, - }); - // Ensure AAD isn't broken - await checkAAD({ - supertest, - spaceId: space.id, - type: 'alert', - id: createdAlert.id, - }); - break; - default: - throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); - } - }); + switch (scenario.id) { + case 'no_kibana_privileges at space1': + case 'space_1_all at space2': + case 'global_read at space1': + expect(response.statusCode).to.eql(403); + expect(response.body).to.eql({ + error: 'Forbidden', + message: getConsumerUnauthorizedErrorMessage( + 'enable', + 'test.noop', + 'alertsFixture' + ), + statusCode: 403, + }); + break; + case 'superuser at space1': + case 'space_1_all at space1': + case 'space_1_all_alerts_none_actions at space1': + case 'space_1_all_with_restricted_fixture at space1': + expect(response.statusCode).to.eql(204); + expect(response.body).to.eql(''); + const { body: updatedAlert } = await supertestWithoutAuth + .get(`${getUrlPrefix(space.id)}/api/alerts/alert/${createdAlert.id}`) + .set('kbn-xsrf', 'foo') + .auth(user.username, user.password) + .expect(200); + expect(typeof updatedAlert.scheduledTaskId).to.eql('string'); + const { _source: taskRecord } = await getScheduledTask(updatedAlert.scheduledTaskId); + expect(taskRecord.type).to.eql('task'); + expect(taskRecord.task.taskType).to.eql('alerting:test.noop'); + expect(JSON.parse(taskRecord.task.params)).to.eql({ + alertId: createdAlert.id, + spaceId: space.id, + }); + // Ensure AAD isn't broken + await checkAAD({ + supertest, + spaceId: space.id, + type: 'alert', + id: createdAlert.id, + }); + break; + default: + throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); + } }); it(`shouldn't enable alert from another space`, async () => { diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/event_log.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/event_log.ts index 64786cc642918..564ab7b832dd3 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/event_log.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/event_log.ts @@ -48,38 +48,38 @@ export default function eventLogTests({ getService }: FtrProviderContext) { }, }) .expect(200); + }); - const events = await retry.try(async () => { - // there can be a successful execute before the error one - const someEvents = await getEventLog({ - getService, - spaceId, - type: 'alert', - id: alertId, - provider: 'alerting', - actions: new Map([['execute', { gte: 1 }]]), - }); - const errorEvents = someEvents.filter( - (event) => event?.kibana?.alerting?.status === 'error' - ); - if (errorEvents.length === 0) { - throw new Error('no execute/error events yet'); - } - return errorEvents; + const events = await retry.try(async () => { + // there can be a successful execute before the error one + const someEvents = await getEventLog({ + getService, + spaceId, + type: 'alert', + id: alertId, + provider: 'alerting', + actions: new Map([['execute', { gte: 1 }]]), }); + const errorEvents = someEvents.filter( + (event) => event?.kibana?.alerting?.status === 'error' + ); + if (errorEvents.length === 0) { + throw new Error('no execute/error events yet'); + } + return errorEvents; + }); - const event = events[0]; - expect(event).to.be.ok(); + const event = events[0]; + expect(event).to.be.ok(); - validateEvent(event, { - spaceId, - savedObjects: [{ type: 'alert', id: alertId, rel: 'primary' }], - outcome: 'failure', - message: `test.noop:${alertId}: execution failed`, - errorMessage: 'Unable to decrypt attribute "apiKey"', - status: 'error', - reason: 'decrypt', - }); + validateEvent(event, { + spaceId, + savedObjects: [{ type: 'alert', id: alertId, rel: 'primary' }], + outcome: 'failure', + message: `test.noop:${alertId}: execution failed`, + errorMessage: 'Unable to decrypt attribute "apiKey"', + status: 'error', + reason: 'decrypt', }); }); }); diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/execution_status.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/execution_status.ts index 7326392ba5ca8..5295f5d90fb06 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/execution_status.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/execution_status.ts @@ -39,9 +39,6 @@ export default function executionStatusAlertTests({ getService }: FtrProviderCon let executionStatus = await waitForStatus(alertId, new Set(['ok']), 10000); await retry.try(async () => { - // Delay before performing update to avoid 409 errors - await new Promise((resolve) => setTimeout(resolve, 1000)); - // break AAD await supertest .put(`${getUrlPrefix(spaceId)}/api/alerts_fixture/saved_object/alert/${alertId}`) @@ -52,12 +49,12 @@ export default function executionStatusAlertTests({ getService }: FtrProviderCon }, }) .expect(200); - - executionStatus = await waitForStatus(alertId, new Set(['error'])); - expect(executionStatus.error).to.be.ok(); - expect(executionStatus.error.reason).to.be(AlertExecutionStatusErrorReasons.Decrypt); - expect(executionStatus.error.message).to.be('Unable to decrypt attribute "apiKey"'); }); + + executionStatus = await waitForStatus(alertId, new Set(['error'])); + expect(executionStatus.error).to.be.ok(); + expect(executionStatus.error.reason).to.be(AlertExecutionStatusErrorReasons.Decrypt); + expect(executionStatus.error.message).to.be('Unable to decrypt attribute "apiKey"'); }); }); 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 81ad52c36f693..6b03492432acc 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 @@ -440,76 +440,76 @@ export default function createUpdateTests({ getService }: FtrProviderContext) { }, }) .expect(200); - - const updatedData = { - name: 'bcd', - tags: ['bar'], - params: { - foo: true, - }, - schedule: { interval: '12s' }, - actions: [], - throttle: '1m', - }; - const response = await supertestWithoutAuth - .put(`${getUrlPrefix(space.id)}/api/alerts/alert/${createdAlert.id}`) - .set('kbn-xsrf', 'foo') - .auth(user.username, user.password) - .send(updatedData); - - switch (scenario.id) { - case 'no_kibana_privileges at space1': - case 'space_1_all at space2': - case 'global_read at space1': - expect(response.statusCode).to.eql(403); - expect(response.body).to.eql({ - error: 'Forbidden', - message: getConsumerUnauthorizedErrorMessage( - 'update', - 'test.noop', - 'alertsFixture' - ), - statusCode: 403, - }); - break; - case 'superuser at space1': - case 'space_1_all at space1': - case 'space_1_all_alerts_none_actions at space1': - case 'space_1_all_with_restricted_fixture at space1': - expect(response.statusCode).to.eql(200); - expect(response.body).to.eql({ - ...updatedData, - id: createdAlert.id, - alertTypeId: 'test.noop', - consumer: 'alertsFixture', - createdBy: 'elastic', - enabled: true, - updatedBy: user.username, - apiKeyOwner: user.username, - muteAll: false, - mutedInstanceIds: [], - scheduledTaskId: createdAlert.scheduledTaskId, - createdAt: response.body.createdAt, - updatedAt: response.body.updatedAt, - executionStatus: response.body.executionStatus, - }); - 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, - spaceId: space.id, - type: 'alert', - id: createdAlert.id, - }); - break; - default: - throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); - } }); + + const updatedData = { + name: 'bcd', + tags: ['bar'], + params: { + foo: true, + }, + schedule: { interval: '12s' }, + actions: [], + throttle: '1m', + }; + const response = await supertestWithoutAuth + .put(`${getUrlPrefix(space.id)}/api/alerts/alert/${createdAlert.id}`) + .set('kbn-xsrf', 'foo') + .auth(user.username, user.password) + .send(updatedData); + + switch (scenario.id) { + case 'no_kibana_privileges at space1': + case 'space_1_all at space2': + case 'global_read at space1': + expect(response.statusCode).to.eql(403); + expect(response.body).to.eql({ + error: 'Forbidden', + message: getConsumerUnauthorizedErrorMessage( + 'update', + 'test.noop', + 'alertsFixture' + ), + statusCode: 403, + }); + break; + case 'superuser at space1': + case 'space_1_all at space1': + case 'space_1_all_alerts_none_actions at space1': + case 'space_1_all_with_restricted_fixture at space1': + expect(response.statusCode).to.eql(200); + expect(response.body).to.eql({ + ...updatedData, + id: createdAlert.id, + alertTypeId: 'test.noop', + consumer: 'alertsFixture', + createdBy: 'elastic', + enabled: true, + updatedBy: user.username, + apiKeyOwner: user.username, + muteAll: false, + mutedInstanceIds: [], + scheduledTaskId: createdAlert.scheduledTaskId, + createdAt: response.body.createdAt, + updatedAt: response.body.updatedAt, + executionStatus: response.body.executionStatus, + }); + 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, + spaceId: space.id, + type: 'alert', + id: createdAlert.id, + }); + break; + default: + throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); + } }); it('should handle update alert request appropriately when alert name has leading and trailing whitespaces', async () => { diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update_api_key.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update_api_key.ts index 5f608c2dd17b4..0b525fbf7e809 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update_api_key.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update_api_key.ts @@ -314,48 +314,48 @@ export default function createUpdateApiKeyTests({ getService }: FtrProviderConte }, }) .expect(200); + }); - const response = await alertUtils.getUpdateApiKeyRequest(createdAlert.id); + const response = await alertUtils.getUpdateApiKeyRequest(createdAlert.id); - switch (scenario.id) { - case 'no_kibana_privileges at space1': - case 'space_1_all at space2': - case 'global_read at space1': - expect(response.statusCode).to.eql(403); - expect(response.body).to.eql({ - error: 'Forbidden', - message: getConsumerUnauthorizedErrorMessage( - 'updateApiKey', - 'test.noop', - 'alertsFixture' - ), - statusCode: 403, - }); - break; - case 'superuser at space1': - case 'space_1_all at space1': - case 'space_1_all_alerts_none_actions at space1': - case 'space_1_all_with_restricted_fixture at space1': - expect(response.statusCode).to.eql(204); - expect(response.body).to.eql(''); - const { body: updatedAlert } = await supertestWithoutAuth - .get(`${getUrlPrefix(space.id)}/api/alerts/alert/${createdAlert.id}`) - .set('kbn-xsrf', 'foo') - .auth(user.username, user.password) - .expect(200); - expect(updatedAlert.apiKeyOwner).to.eql(user.username); - // Ensure AAD isn't broken - await checkAAD({ - supertest, - spaceId: space.id, - type: 'alert', - id: createdAlert.id, - }); - break; - default: - throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); - } - }); + switch (scenario.id) { + case 'no_kibana_privileges at space1': + case 'space_1_all at space2': + case 'global_read at space1': + expect(response.statusCode).to.eql(403); + expect(response.body).to.eql({ + error: 'Forbidden', + message: getConsumerUnauthorizedErrorMessage( + 'updateApiKey', + 'test.noop', + 'alertsFixture' + ), + statusCode: 403, + }); + break; + case 'superuser at space1': + case 'space_1_all at space1': + case 'space_1_all_alerts_none_actions at space1': + case 'space_1_all_with_restricted_fixture at space1': + expect(response.statusCode).to.eql(204); + expect(response.body).to.eql(''); + const { body: updatedAlert } = await supertestWithoutAuth + .get(`${getUrlPrefix(space.id)}/api/alerts/alert/${createdAlert.id}`) + .set('kbn-xsrf', 'foo') + .auth(user.username, user.password) + .expect(200); + expect(updatedAlert.apiKeyOwner).to.eql(user.username); + // Ensure AAD isn't broken + await checkAAD({ + supertest, + spaceId: space.id, + type: 'alert', + id: createdAlert.id, + }); + break; + default: + throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); + } }); it(`shouldn't update alert api key from another space`, async () => {