Skip to content

Commit 10becbb

Browse files
committed
Add more tests
1 parent 4a4aad9 commit 10becbb

File tree

1 file changed

+104
-1
lines changed

1 file changed

+104
-1
lines changed

x-pack/legacy/plugins/alerting/server/alerts_client.test.ts

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,41 @@ describe('enable()', () => {
976976
scope: ['alerting'],
977977
});
978978
});
979+
980+
test('swallows error when invalidate API key throws', async () => {
981+
const alertsClient = new AlertsClient(alertsClientParams);
982+
alertsClientParams.invalidateAPIKey.mockRejectedValueOnce(new Error('Fail'));
983+
encryptedSavedObjects.getDecryptedAsInternalUser.mockResolvedValueOnce({
984+
id: '1',
985+
type: 'alert',
986+
attributes: {
987+
schedule: { interval: '10s' },
988+
alertTypeId: '2',
989+
enabled: false,
990+
apiKey: Buffer.from('123:abc').toString('base64'),
991+
},
992+
version: '123',
993+
references: [],
994+
});
995+
taskManager.schedule.mockResolvedValueOnce({
996+
id: 'task-123',
997+
scheduledAt: new Date(),
998+
attempts: 0,
999+
status: TaskStatus.Idle,
1000+
runAt: new Date(),
1001+
state: {},
1002+
params: {},
1003+
taskType: '',
1004+
startedAt: null,
1005+
retryAt: null,
1006+
ownerId: null,
1007+
});
1008+
1009+
await alertsClient.enable({ id: '1' });
1010+
expect(alertsClientParams.logger.error).toHaveBeenCalledWith(
1011+
'Failed to invalidate API Key: Fail'
1012+
);
1013+
});
9791014
});
9801015

9811016
describe('disable()', () => {
@@ -1431,6 +1466,48 @@ describe('delete()', () => {
14311466
]
14321467
`);
14331468
});
1469+
1470+
test('swallows error when invalidate API key throws', async () => {
1471+
const alertsClient = new AlertsClient(alertsClientParams);
1472+
alertsClientParams.invalidateAPIKey.mockRejectedValueOnce(new Error('Fail'));
1473+
encryptedSavedObjects.getDecryptedAsInternalUser.mockResolvedValueOnce({
1474+
id: '1',
1475+
type: 'alert',
1476+
attributes: {
1477+
alertTypeId: '123',
1478+
schedule: { interval: '10s' },
1479+
params: {
1480+
bar: true,
1481+
},
1482+
apiKey: Buffer.from('123:abc').toString('base64'),
1483+
scheduledTaskId: 'task-123',
1484+
actions: [
1485+
{
1486+
group: 'default',
1487+
actionRef: 'action_0',
1488+
params: {
1489+
foo: true,
1490+
},
1491+
},
1492+
],
1493+
},
1494+
references: [
1495+
{
1496+
name: 'action_0',
1497+
type: 'action',
1498+
id: '1',
1499+
},
1500+
],
1501+
});
1502+
savedObjectsClient.delete.mockResolvedValueOnce({
1503+
success: true,
1504+
});
1505+
1506+
await alertsClient.delete({ id: '1' });
1507+
expect(alertsClientParams.logger.error).toHaveBeenCalledWith(
1508+
'Failed to invalidate API Key: Fail'
1509+
);
1510+
});
14341511
});
14351512

14361513
describe('update()', () => {
@@ -1968,7 +2045,7 @@ describe('update()', () => {
19682045
);
19692046
});
19702047

1971-
it('swallows error when invalidating API key throws', async () => {
2048+
it('swallows error when invalidate API key throws', async () => {
19722049
const alertsClient = new AlertsClient(alertsClientParams);
19732050
alertsClientParams.invalidateAPIKey.mockRejectedValueOnce(new Error('Fail'));
19742051
alertTypeRegistry.get.mockReturnValueOnce({
@@ -2312,4 +2389,30 @@ describe('updateApiKey()', () => {
23122389
{ version: '123' }
23132390
);
23142391
});
2392+
2393+
test('swallows error when invalidate API key throws', async () => {
2394+
const alertsClient = new AlertsClient(alertsClientParams);
2395+
alertsClientParams.invalidateAPIKey.mockRejectedValue(new Error('Fail'));
2396+
encryptedSavedObjects.getDecryptedAsInternalUser.mockResolvedValueOnce({
2397+
id: '1',
2398+
type: 'alert',
2399+
attributes: {
2400+
schedule: { interval: '10s' },
2401+
alertTypeId: '2',
2402+
enabled: true,
2403+
apiKey: Buffer.from('123:abc').toString('base64'),
2404+
},
2405+
version: '123',
2406+
references: [],
2407+
});
2408+
alertsClientParams.createAPIKey.mockResolvedValueOnce({
2409+
apiKeysEnabled: true,
2410+
result: { id: '123', api_key: 'abc' },
2411+
});
2412+
2413+
await alertsClient.updateApiKey({ id: '1' });
2414+
expect(alertsClientParams.logger.error).toHaveBeenCalledWith(
2415+
'Failed to invalidate API Key: Fail'
2416+
);
2417+
});
23152418
});

0 commit comments

Comments
 (0)