Skip to content

Commit

Permalink
Updating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ymao1 committed Apr 23, 2021
1 parent 43fd392 commit 204bb91
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
12 changes: 12 additions & 0 deletions x-pack/plugins/alerting/server/alerts_client/tests/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1701,6 +1701,18 @@ describe('create()', () => {
);
});

test('throws an error if API key creation throws', async () => {
const data = getMockData();
alertsClientParams.createAPIKey.mockImplementation(() => {
throw new Error('no');
});
expect(
async () => await alertsClient.create({ data })
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Error creating rule: could not create API key - no"`
);
});

test('throws error when ensureActionTypeEnabled throws', async () => {
const data = getMockData();
alertTypeRegistry.ensureAlertTypeEnabled.mockImplementation(() => {
Expand Down
11 changes: 11 additions & 0 deletions x-pack/plugins/alerting/server/alerts_client/tests/enable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,17 @@ describe('enable()', () => {
);
});

test('throws an error if API key creation throws', async () => {
alertsClientParams.createAPIKey.mockImplementation(() => {
throw new Error('no');
});
expect(
async () => await alertsClient.enable({ id: '1' })
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Error enabling rule: could not create API key - no"`
);
});

test('falls back when failing to getDecryptedAsInternalUser', async () => {
encryptedSavedObjects.getDecryptedAsInternalUser.mockRejectedValue(new Error('Fail'));

Expand Down
47 changes: 47 additions & 0 deletions x-pack/plugins/alerting/server/alerts_client/tests/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,53 @@ describe('update()', () => {
`);
});

it('throws an error if API key creation throws', async () => {
alertsClientParams.createAPIKey.mockImplementation(() => {
throw new Error('no');
});
expect(
async () =>
await alertsClient.update({
id: '1',
data: {
schedule: { interval: '10s' },
name: 'abc',
tags: ['foo'],
params: {
bar: true,
},
throttle: null,
notifyWhen: 'onActiveAlert',
actions: [
{
group: 'default',
id: '1',
params: {
foo: true,
},
},
{
group: 'default',
id: '1',
params: {
foo: true,
},
},
{
group: 'default',
id: '2',
params: {
foo: true,
},
},
],
},
})
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Error updating rule: could not create API key - no"`
);
});

it('should validate params', async () => {
alertTypeRegistry.get.mockReturnValueOnce({
id: '123',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ describe('updateApiKey()', () => {
references: [],
});
encryptedSavedObjects.getDecryptedAsInternalUser.mockResolvedValue(existingEncryptedAlert);
});

test('updates the API key for the alert', async () => {
alertsClientParams.createAPIKey.mockResolvedValueOnce({
apiKeysEnabled: true,
result: { id: '234', name: '123', api_key: 'abc' },
});
});

test('updates the API key for the alert', async () => {
await alertsClient.updateApiKey({ id: '1' });
expect(unsecuredSavedObjectsClient.get).not.toHaveBeenCalled();
expect(encryptedSavedObjects.getDecryptedAsInternalUser).toHaveBeenCalledWith('alert', '1', {
Expand Down Expand Up @@ -145,7 +145,22 @@ describe('updateApiKey()', () => {
);
});

test('throws an error if API key creation throws', async () => {
alertsClientParams.createAPIKey.mockImplementation(() => {
throw new Error('no');
});
expect(
async () => await alertsClient.updateApiKey({ id: '1' })
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Error updating API key for rule: could not create API key - no"`
);
});

test('falls back to SOC when getDecryptedAsInternalUser throws an error', async () => {
alertsClientParams.createAPIKey.mockResolvedValueOnce({
apiKeysEnabled: true,
result: { id: '234', name: '123', api_key: 'abc' },
});
encryptedSavedObjects.getDecryptedAsInternalUser.mockRejectedValueOnce(new Error('Fail'));
unsecuredSavedObjectsClient.create.mockResolvedValueOnce({
id: '1',
Expand Down

0 comments on commit 204bb91

Please sign in to comment.