|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +import { AlertsClient, ConstructorOptions } from '../alerts_client'; |
| 7 | +import { savedObjectsClientMock, loggingSystemMock } from '../../../../../../src/core/server/mocks'; |
| 8 | +import { taskManagerMock } from '../../../../task_manager/server/mocks'; |
| 9 | +import { alertTypeRegistryMock } from '../../alert_type_registry.mock'; |
| 10 | +import { alertsAuthorizationMock } from '../../authorization/alerts_authorization.mock'; |
| 11 | +import { encryptedSavedObjectsMock } from '../../../../encrypted_saved_objects/server/mocks'; |
| 12 | +import { actionsAuthorizationMock } from '../../../../actions/server/mocks'; |
| 13 | +import { AlertsAuthorization } from '../../authorization/alerts_authorization'; |
| 14 | +import { ActionsAuthorization } from '../../../../actions/server'; |
| 15 | +import { getBeforeSetup, setGlobalDate } from './lib'; |
| 16 | +import { AlertExecutionStatusValues } from '../../types'; |
| 17 | + |
| 18 | +const taskManager = taskManagerMock.createStart(); |
| 19 | +const alertTypeRegistry = alertTypeRegistryMock.create(); |
| 20 | +const unsecuredSavedObjectsClient = savedObjectsClientMock.create(); |
| 21 | + |
| 22 | +const encryptedSavedObjects = encryptedSavedObjectsMock.createClient(); |
| 23 | +const authorization = alertsAuthorizationMock.create(); |
| 24 | +const actionsAuthorization = actionsAuthorizationMock.create(); |
| 25 | + |
| 26 | +const kibanaVersion = 'v7.10.0'; |
| 27 | +const alertsClientParams: jest.Mocked<ConstructorOptions> = { |
| 28 | + taskManager, |
| 29 | + alertTypeRegistry, |
| 30 | + unsecuredSavedObjectsClient, |
| 31 | + authorization: (authorization as unknown) as AlertsAuthorization, |
| 32 | + actionsAuthorization: (actionsAuthorization as unknown) as ActionsAuthorization, |
| 33 | + spaceId: 'default', |
| 34 | + namespace: 'default', |
| 35 | + getUserName: jest.fn(), |
| 36 | + createAPIKey: jest.fn(), |
| 37 | + invalidateAPIKey: jest.fn(), |
| 38 | + logger: loggingSystemMock.create().get(), |
| 39 | + encryptedSavedObjectsClient: encryptedSavedObjects, |
| 40 | + getActionsClient: jest.fn(), |
| 41 | + getEventLogClient: jest.fn(), |
| 42 | + kibanaVersion, |
| 43 | +}; |
| 44 | + |
| 45 | +beforeEach(() => { |
| 46 | + getBeforeSetup(alertsClientParams, taskManager, alertTypeRegistry); |
| 47 | +}); |
| 48 | + |
| 49 | +setGlobalDate(); |
| 50 | + |
| 51 | +describe('aggregate()', () => { |
| 52 | + const listedTypes = new Set([ |
| 53 | + { |
| 54 | + actionGroups: [], |
| 55 | + actionVariables: undefined, |
| 56 | + defaultActionGroupId: 'default', |
| 57 | + id: 'myType', |
| 58 | + name: 'myType', |
| 59 | + producer: 'myApp', |
| 60 | + }, |
| 61 | + ]); |
| 62 | + beforeEach(() => { |
| 63 | + authorization.getFindAuthorizationFilter.mockResolvedValue({ |
| 64 | + ensureAlertTypeIsAuthorized() {}, |
| 65 | + logSuccessfulAuthorization() {}, |
| 66 | + }); |
| 67 | + unsecuredSavedObjectsClient.find |
| 68 | + .mockResolvedValueOnce({ |
| 69 | + total: 10, |
| 70 | + per_page: 0, |
| 71 | + page: 1, |
| 72 | + saved_objects: [], |
| 73 | + }) |
| 74 | + .mockResolvedValueOnce({ |
| 75 | + total: 8, |
| 76 | + per_page: 0, |
| 77 | + page: 1, |
| 78 | + saved_objects: [], |
| 79 | + }) |
| 80 | + .mockResolvedValueOnce({ |
| 81 | + total: 6, |
| 82 | + per_page: 0, |
| 83 | + page: 1, |
| 84 | + saved_objects: [], |
| 85 | + }) |
| 86 | + .mockResolvedValueOnce({ |
| 87 | + total: 4, |
| 88 | + per_page: 0, |
| 89 | + page: 1, |
| 90 | + saved_objects: [], |
| 91 | + }) |
| 92 | + .mockResolvedValueOnce({ |
| 93 | + total: 2, |
| 94 | + per_page: 0, |
| 95 | + page: 1, |
| 96 | + saved_objects: [], |
| 97 | + }); |
| 98 | + alertTypeRegistry.list.mockReturnValue(listedTypes); |
| 99 | + authorization.filterByAlertTypeAuthorization.mockResolvedValue( |
| 100 | + new Set([ |
| 101 | + { |
| 102 | + id: 'myType', |
| 103 | + name: 'Test', |
| 104 | + actionGroups: [{ id: 'default', name: 'Default' }], |
| 105 | + defaultActionGroupId: 'default', |
| 106 | + producer: 'alerts', |
| 107 | + authorizedConsumers: { |
| 108 | + myApp: { read: true, all: true }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + ]) |
| 112 | + ); |
| 113 | + }); |
| 114 | + |
| 115 | + test('calls saved objects client with given params to perform aggregation', async () => { |
| 116 | + const alertsClient = new AlertsClient(alertsClientParams); |
| 117 | + const result = await alertsClient.aggregate({ options: {} }); |
| 118 | + expect(result).toMatchInlineSnapshot(` |
| 119 | + Object { |
| 120 | + "alertExecutionStatus": Object { |
| 121 | + "active": 8, |
| 122 | + "error": 6, |
| 123 | + "ok": 10, |
| 124 | + "pending": 4, |
| 125 | + "unknown": 2, |
| 126 | + }, |
| 127 | + } |
| 128 | + `); |
| 129 | + expect(unsecuredSavedObjectsClient.find).toHaveBeenCalledTimes( |
| 130 | + AlertExecutionStatusValues.length |
| 131 | + ); |
| 132 | + AlertExecutionStatusValues.forEach((status: string, ndx: number) => { |
| 133 | + expect(unsecuredSavedObjectsClient.find.mock.calls[ndx]).toEqual([ |
| 134 | + { |
| 135 | + fields: undefined, |
| 136 | + filter: `alert.attributes.executionStatus.status:(${status})`, |
| 137 | + page: 1, |
| 138 | + perPage: 0, |
| 139 | + type: 'alert', |
| 140 | + }, |
| 141 | + ]); |
| 142 | + }); |
| 143 | + }); |
| 144 | + |
| 145 | + test('supports filters when aggregating', async () => { |
| 146 | + const alertsClient = new AlertsClient(alertsClientParams); |
| 147 | + await alertsClient.aggregate({ options: { filter: 'someTerm' } }); |
| 148 | + |
| 149 | + expect(unsecuredSavedObjectsClient.find).toHaveBeenCalledTimes( |
| 150 | + AlertExecutionStatusValues.length |
| 151 | + ); |
| 152 | + AlertExecutionStatusValues.forEach((status: string, ndx: number) => { |
| 153 | + expect(unsecuredSavedObjectsClient.find.mock.calls[ndx]).toEqual([ |
| 154 | + { |
| 155 | + fields: undefined, |
| 156 | + filter: `someTerm and alert.attributes.executionStatus.status:(${status})`, |
| 157 | + page: 1, |
| 158 | + perPage: 0, |
| 159 | + type: 'alert', |
| 160 | + }, |
| 161 | + ]); |
| 162 | + }); |
| 163 | + }); |
| 164 | +}); |
0 commit comments