kibana/x-pack/plugins/actions/server/preconfigured_connectors/alert_history_es_index/create_alert_history_index_template.test.ts
ymao1 71ed148cfe
[Alerting] Preconfigured alert history index connector (#94909)
* Adding preconfigured alert history index

* Adding functions to build alert history document

* Adding functions to build alert history document

* Moving index template creation to plugin start

* Adding unit tests

* Adding unit tests

* Adding unit tests

* Simplifying

* Revert "Merge branch 'master' of https://github.com/elastic/kibana into alerting/default-es-index-schema"

This reverts commit 957c333aa4, reversing
changes made to 4b1b78761e.

* Reverting some changes

* Reverting some changes

* Adding index override

* Updating UI with index override

* Only allow indexOverride for preconfigured alert history connector

* Handling preconfigured connector id clashes

* Cleanup

* UI unit tests

* Fixing default schema shown in UI

* Fixing functional tests

* Adding functional test

* Fixing functional tests

* Adding docs and link to docs

* Adding config to docker allowlist

* Fixing wrong typescript operator

* Changing default for config to false

* Cleanup

* Adding note about index privileges to docs

* Fixing i18n

* PR fixes

* PR fixes

* PR fixes

* PR fixes - wording

* PR fixes

* Fixing unit and functional tests

* Fixing types check

* ES -> Elasticsearch

* Moving files

* Adding kibana- to beginning of prefix

* Namespacing alert data within schema with kibana

* Fix i18n

* Updating docs

* Fixing unit tests

* Fixing doc links

* Fixing types check

* PR fixes

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
2021-04-08 18:18:44 -04:00

52 lines
2 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { ElasticsearchClient } from 'src/core/server';
import { elasticsearchServiceMock, loggingSystemMock } from 'src/core/server/mocks';
import { DeeplyMockedKeys } from '@kbn/utility-types/jest';
import {
createAlertHistoryIndexTemplate,
getAlertHistoryIndexTemplate,
} from './create_alert_history_index_template';
type MockedLogger = ReturnType<typeof loggingSystemMock['createLogger']>;
describe('createAlertHistoryIndexTemplate', () => {
let logger: MockedLogger;
let clusterClient: DeeplyMockedKeys<ElasticsearchClient>;
beforeEach(() => {
logger = loggingSystemMock.createLogger();
clusterClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
});
test(`should create index template if it doesn't exist`, async () => {
// Response type for existsIndexTemplate is still TODO
clusterClient.indices.existsIndexTemplate.mockResolvedValue({
body: false,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);
await createAlertHistoryIndexTemplate({ client: clusterClient, logger });
expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalledWith({
name: `kibana-alert-history-template`,
body: getAlertHistoryIndexTemplate(),
create: true,
});
});
test(`shouldn't create index template if it already exists`, async () => {
// Response type for existsIndexTemplate is still TODO
clusterClient.indices.existsIndexTemplate.mockResolvedValue({
body: true,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);
await createAlertHistoryIndexTemplate({ client: clusterClient, logger });
expect(clusterClient.indices.putIndexTemplate).not.toHaveBeenCalled();
});
});