Skip to content

Commit

Permalink
Add tests for legacy APIs (#95333)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecote authored Mar 26, 2021
1 parent 354e3ec commit 93067e5
Show file tree
Hide file tree
Showing 16 changed files with 703 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,75 @@ export default function createAggregateTests({ getService }: FtrProviderContext)
},
});
});

describe('legacy', () => {
it('should aggregate alert status totals', async () => {
const NumOkAlerts = 4;
const NumActiveAlerts = 1;
const NumErrorAlerts = 2;

await Promise.all(
[...Array(NumOkAlerts)].map(async () => {
const okAlertId = await createTestAlert(
{
rule_type_id: 'test.noop',
schedule: { interval: '1s' },
},
'ok'
);
objectRemover.add(Spaces.space1.id, okAlertId, 'rule', 'alerting');
})
);

await Promise.all(
[...Array(NumActiveAlerts)].map(async () => {
const activeAlertId = await createTestAlert(
{
rule_type_id: 'test.patternFiring',
schedule: { interval: '1s' },
params: {
pattern: { instance: new Array(100).fill(true) },
},
},
'active'
);
objectRemover.add(Spaces.space1.id, activeAlertId, 'rule', 'alerting');
})
);

await Promise.all(
[...Array(NumErrorAlerts)].map(async () => {
const activeAlertId = await createTestAlert(
{
rule_type_id: 'test.throw',
schedule: { interval: '1s' },
},
'error'
);
objectRemover.add(Spaces.space1.id, activeAlertId, 'rule', 'alerting');
})
);

// Adding delay to allow ES refresh cycle to run. Even when the waitForStatus
// calls are successful, the call to aggregate may return stale totals if called
// too early.
await delay(1000);
const reponse = await supertest.get(
`${getUrlPrefix(Spaces.space1.id)}/api/alerts/_aggregate`
);

expect(reponse.status).to.eql(200);
expect(reponse.body).to.eql({
alertExecutionStatus: {
ok: NumOkAlerts,
active: NumActiveAlerts,
error: NumErrorAlerts,
pending: 0,
unknown: 0,
},
});
});
});
});

const WaitForStatusIncrement = 500;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,93 @@ export default function createAlertTests({ getService }: FtrProviderContext) {
objectRemover.add(Spaces.space1.id, response.body.id, 'rule', 'alerting');
expect(response.body.scheduledTaskId).to.eql(undefined);
});

describe('legacy', () => {
it('should handle create alert request appropriately', async () => {
const { body: createdAction } = await supertest
.post(`${getUrlPrefix(Spaces.space1.id)}/api/actions/connector`)
.set('kbn-xsrf', 'foo')
.send({
name: 'MY action',
connector_type_id: 'test.noop',
config: {},
secrets: {},
})
.expect(200);

const {
rule_type_id: alertTypeId,
notify_when: notifyWhen,
...testAlert
} = getTestAlertData({
actions: [
{
id: createdAction.id,
group: 'default',
params: {},
},
],
});
const response = await supertest
.post(`${getUrlPrefix(Spaces.space1.id)}/api/alerts/alert`)
.set('kbn-xsrf', 'foo')
.send({
...testAlert,
alertTypeId,
notifyWhen,
});

expect(response.status).to.eql(200);
objectRemover.add(Spaces.space1.id, response.body.id, 'rule', 'alerting');
expect(response.body).to.eql({
id: response.body.id,
name: 'abc',
tags: ['foo'],
actions: [
{
id: createdAction.id,
actionTypeId: createdAction.connector_type_id,
group: 'default',
params: {},
},
],
enabled: true,
alertTypeId: 'test.noop',
consumer: 'alertsFixture',
params: {},
createdBy: null,
schedule: { interval: '1m' },
scheduledTaskId: response.body.scheduledTaskId,
updatedBy: null,
apiKeyOwner: null,
throttle: '1m',
notifyWhen: 'onThrottleInterval',
muteAll: false,
mutedInstanceIds: [],
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.eql(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');
expect(taskRecord.task.taskType).to.eql('alerting:test.noop');
expect(JSON.parse(taskRecord.task.params)).to.eql({
alertId: response.body.id,
spaceId: Spaces.space1.id,
});
// Ensure AAD isn't broken
await checkAAD({
supertest,
spaceId: Spaces.space1.id,
type: 'alert',
id: response.body.id,
});
});
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,27 @@ export default function createDeleteTests({ getService }: FtrProviderContext) {
message: `Saved object [alert/${createdAlert.id}] not found`,
});
});

describe('legacy', () => {
it('should handle delete alert request appropriately', async () => {
const { body: createdAlert } = await supertest
.post(`${getUrlPrefix(Spaces.space1.id)}/api/alerting/rule`)
.set('kbn-xsrf', 'foo')
.send(getTestAlertData())
.expect(200);

await supertest
.delete(`${getUrlPrefix(Spaces.space1.id)}/api/alerts/alert/${createdAlert.id}`)
.set('kbn-xsrf', 'foo')
.expect(204, '');

try {
await getScheduledTask(createdAlert.scheduledTaskId);
throw new Error('Should have removed scheduled task');
} catch (e) {
expect(e.status).to.eql(404);
}
});
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,36 @@ export default function createDisableAlertTests({ getService }: FtrProviderConte
message: `Saved object [alert/${createdAlert.id}] not found`,
});
});

describe('legacy', () => {
it('should handle disable alert request appropriately', async () => {
const { body: createdAlert } = await supertestWithoutAuth
.post(`${getUrlPrefix(Spaces.space1.id)}/api/alerting/rule`)
.set('kbn-xsrf', 'foo')
.send(getTestAlertData({ enabled: true }))
.expect(200);
objectRemover.add(Spaces.space1.id, createdAlert.id, 'rule', 'alerting');

await supertestWithoutAuth
.post(`${getUrlPrefix(Spaces.space1.id)}/api/alerts/alert/${createdAlert.id}/_disable`)
.set('kbn-xsrf', 'foo')
.expect(204);

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: supertestWithoutAuth,
spaceId: Spaces.space1.id,
type: 'alert',
id: createdAlert.id,
});
});
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,42 @@ export default function createEnableAlertTests({ getService }: FtrProviderContex
message: `Saved object [alert/${createdAlert.id}] not found`,
});
});

describe('legacy', () => {
it('should handle enable alert request appropriately', async () => {
const { body: createdAlert } = await supertestWithoutAuth
.post(`${getUrlPrefix(Spaces.space1.id)}/api/alerting/rule`)
.set('kbn-xsrf', 'foo')
.send(getTestAlertData({ enabled: false }))
.expect(200);
objectRemover.add(Spaces.space1.id, createdAlert.id, 'rule', 'alerting');

await supertestWithoutAuth
.post(`${getUrlPrefix(Spaces.space1.id)}/api/alerts/alert/${createdAlert.id}/_enable`)
.set('kbn-xsrf', 'foo')
.expect(204);

const { body: updatedAlert } = await supertestWithoutAuth
.get(`${getUrlPrefix(Spaces.space1.id)}/api/alerting/rule/${createdAlert.id}`)
.set('kbn-xsrf', 'foo')
.expect(200);
expect(typeof updatedAlert.scheduled_task_id).to.eql('string');
const { _source: taskRecord } = await getScheduledTask(updatedAlert.scheduled_task_id);
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: Spaces.space1.id,
});

// Ensure AAD isn't broken
await checkAAD({
supertest: supertestWithoutAuth,
spaceId: Spaces.space1.id,
type: 'alert',
id: createdAlert.id,
});
});
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,52 @@ export default function createFindTests({ getService }: FtrProviderContext) {
expect(response.body.total).to.equal(1);
expect(response.body.data[0].params.strValue).to.eql('my b');
});

describe('legacy', () => {
it('should handle find alert request appropriately', async () => {
const { body: createdAlert } = await supertest
.post(`${getUrlPrefix(Spaces.space1.id)}/api/alerting/rule`)
.set('kbn-xsrf', 'foo')
.send(getTestAlertData())
.expect(200);
objectRemover.add(Spaces.space1.id, createdAlert.id, 'rule', 'alerting');

const response = await supertest.get(
`${getUrlPrefix(
Spaces.space1.id
)}/api/alerts/_find?search=test.noop&search_fields=alertTypeId`
);

expect(response.status).to.eql(200);
expect(response.body.page).to.equal(1);
expect(response.body.perPage).to.be.greaterThan(0);
expect(response.body.total).to.be.greaterThan(0);
const match = response.body.data.find((obj: any) => obj.id === createdAlert.id);
expect(match).to.eql({
id: createdAlert.id,
name: 'abc',
tags: ['foo'],
alertTypeId: 'test.noop',
consumer: 'alertsFixture',
schedule: { interval: '1m' },
enabled: true,
actions: [],
params: {},
createdBy: null,
apiKeyOwner: null,
scheduledTaskId: match.scheduledTaskId,
updatedBy: null,
throttle: '1m',
notifyWhen: 'onThrottleInterval',
muteAll: false,
mutedInstanceIds: [],
createdAt: match.createdAt,
updatedAt: match.updatedAt,
executionStatus: match.executionStatus,
});
expect(Date.parse(match.createdAt)).to.be.greaterThan(0);
expect(Date.parse(match.updatedAt)).to.be.greaterThan(0);
});
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,46 @@ export default function createGetTests({ getService }: FtrProviderContext) {
message: 'Saved object [alert/1] not found',
});
});

describe('legacy', () => {
it('should handle get alert request appropriately', async () => {
const { body: createdAlert } = await supertest
.post(`${getUrlPrefix(Spaces.space1.id)}/api/alerting/rule`)
.set('kbn-xsrf', 'foo')
.send(getTestAlertData())
.expect(200);
objectRemover.add(Spaces.space1.id, createdAlert.id, 'rule', 'alerting');

const response = await supertest.get(
`${getUrlPrefix(Spaces.space1.id)}/api/alerts/alert/${createdAlert.id}`
);

expect(response.status).to.eql(200);
expect(response.body).to.eql({
id: createdAlert.id,
name: 'abc',
tags: ['foo'],
alertTypeId: 'test.noop',
consumer: 'alertsFixture',
schedule: { interval: '1m' },
enabled: true,
actions: [],
params: {},
createdBy: null,
scheduledTaskId: response.body.scheduledTaskId,
updatedBy: null,
apiKeyOwner: null,
throttle: '1m',
notifyWhen: 'onThrottleInterval',
muteAll: false,
mutedInstanceIds: [],
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);
});
});
});
}
Loading

0 comments on commit 93067e5

Please sign in to comment.