|
| 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 | + |
| 7 | +import { Plugin } from './plugin'; |
| 8 | +import { coreMock } from '../../../../../src/core/server/mocks'; |
| 9 | +import { licensingMock } from '../../../../plugins/licensing/server/mocks'; |
| 10 | +import { encryptedSavedObjectsMock } from '../../../../plugins/encrypted_saved_objects/server/mocks'; |
| 11 | + |
| 12 | +describe('Alerting Plugin', () => { |
| 13 | + describe('start()', () => { |
| 14 | + /** |
| 15 | + * HACK: This test has put together to ensuire the function "getAlertsClientWithRequest" |
| 16 | + * throws when needed. There's a lot of blockers for writing a proper test like |
| 17 | + * misisng plugin start/setup mocks for taskManager and actions plugin, core.http.route |
| 18 | + * is actually not a function in Kibana Platform, etc. This test contains what is needed |
| 19 | + * to get to the necessary function within start(). |
| 20 | + */ |
| 21 | + describe('getAlertsClientWithRequest()', () => { |
| 22 | + it('throws error when encryptedSavedObjects plugin has usingEphemeralEncryptionKey set to true', async () => { |
| 23 | + const context = coreMock.createPluginInitializerContext(); |
| 24 | + const plugin = new Plugin(context); |
| 25 | + |
| 26 | + const coreSetup = coreMock.createSetup(); |
| 27 | + const encryptedSavedObjectsSetup = encryptedSavedObjectsMock.createSetup(); |
| 28 | + await plugin.setup( |
| 29 | + { |
| 30 | + ...coreSetup, |
| 31 | + http: { |
| 32 | + ...coreSetup.http, |
| 33 | + route: jest.fn(), |
| 34 | + }, |
| 35 | + } as any, |
| 36 | + { |
| 37 | + licensing: licensingMock.createSetup(), |
| 38 | + encryptedSavedObjects: encryptedSavedObjectsSetup, |
| 39 | + } as any |
| 40 | + ); |
| 41 | + |
| 42 | + const startContract = plugin.start( |
| 43 | + coreMock.createStart() as any, |
| 44 | + { |
| 45 | + actions: { |
| 46 | + execute: jest.fn(), |
| 47 | + getActionsClientWithRequest: jest.fn(), |
| 48 | + }, |
| 49 | + } as any |
| 50 | + ); |
| 51 | + |
| 52 | + expect(encryptedSavedObjectsSetup.usingEphemeralEncryptionKey).toEqual(true); |
| 53 | + expect(() => |
| 54 | + startContract.getAlertsClientWithRequest({} as any) |
| 55 | + ).toThrowErrorMatchingInlineSnapshot( |
| 56 | + `"Unable to create alerts client due to the Encrypted Saved Objects plugin using an ephemeral encryption key. Please set xpack.encryptedSavedObjects.encryptionKey in kibana.yml"` |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + it(`doesn't throw error when encryptedSavedObjects plugin has usingEphemeralEncryptionKey set to false`, async () => { |
| 61 | + const context = coreMock.createPluginInitializerContext(); |
| 62 | + const plugin = new Plugin(context); |
| 63 | + |
| 64 | + const coreSetup = coreMock.createSetup(); |
| 65 | + const encryptedSavedObjectsSetup = { |
| 66 | + ...encryptedSavedObjectsMock.createSetup(), |
| 67 | + usingEphemeralEncryptionKey: false, |
| 68 | + }; |
| 69 | + await plugin.setup( |
| 70 | + { |
| 71 | + ...coreSetup, |
| 72 | + http: { |
| 73 | + ...coreSetup.http, |
| 74 | + route: jest.fn(), |
| 75 | + }, |
| 76 | + } as any, |
| 77 | + { |
| 78 | + licensing: licensingMock.createSetup(), |
| 79 | + encryptedSavedObjects: encryptedSavedObjectsSetup, |
| 80 | + } as any |
| 81 | + ); |
| 82 | + |
| 83 | + const startContract = plugin.start( |
| 84 | + coreMock.createStart() as any, |
| 85 | + { |
| 86 | + actions: { |
| 87 | + execute: jest.fn(), |
| 88 | + getActionsClientWithRequest: jest.fn(), |
| 89 | + }, |
| 90 | + spaces: () => null, |
| 91 | + } as any |
| 92 | + ); |
| 93 | + |
| 94 | + const fakeRequest = { |
| 95 | + headers: {}, |
| 96 | + getBasePath: () => '', |
| 97 | + path: '/', |
| 98 | + route: { settings: {} }, |
| 99 | + url: { |
| 100 | + href: '/', |
| 101 | + }, |
| 102 | + raw: { |
| 103 | + req: { |
| 104 | + url: '/', |
| 105 | + }, |
| 106 | + }, |
| 107 | + getSavedObjectsClient: jest.fn(), |
| 108 | + }; |
| 109 | + await startContract.getAlertsClientWithRequest(fakeRequest as any); |
| 110 | + }); |
| 111 | + }); |
| 112 | + }); |
| 113 | +}); |
0 commit comments