From f53e6f72a0bbebd97c5b5a2da3bccd087107c74e Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Wed, 12 Feb 2020 18:29:17 -0600 Subject: [PATCH] Account for spaces being disabled in ClientsService * Updates types to reflect that spaces may be unavailable * Adds a test for getSpaceId's behavior when spaces are disabled --- x-pack/legacy/plugins/siem/server/plugin.ts | 2 +- .../siem/server/services/clients.test.ts | 32 +++++++++++++++++++ .../plugins/siem/server/services/clients.ts | 10 +++--- 3 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 x-pack/legacy/plugins/siem/server/services/clients.test.ts diff --git a/x-pack/legacy/plugins/siem/server/plugin.ts b/x-pack/legacy/plugins/siem/server/plugin.ts index 3221adbf12539..e15248e5200ee 100644 --- a/x-pack/legacy/plugins/siem/server/plugin.ts +++ b/x-pack/legacy/plugins/siem/server/plugin.ts @@ -37,7 +37,7 @@ export interface SetupPlugins { encryptedSavedObjects: EncryptedSavedObjectsSetup; features: FeaturesSetup; security: SecuritySetup; - spaces: SpacesSetup; + spaces?: SpacesSetup; } export interface StartPlugins { diff --git a/x-pack/legacy/plugins/siem/server/services/clients.test.ts b/x-pack/legacy/plugins/siem/server/services/clients.test.ts new file mode 100644 index 0000000000000..7f63a8f5e949c --- /dev/null +++ b/x-pack/legacy/plugins/siem/server/services/clients.test.ts @@ -0,0 +1,32 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { coreMock, httpServerMock } from '../../../../../../src/core/server/mocks'; +import { actionsMock } from '../../../../../plugins/actions/server/mocks'; + +import { ClientsService } from './clients'; + +describe('ClientsService', () => { + describe('spacesClient', () => { + describe('#getSpaceId', () => { + it('returns the default spaceId if spaces are disabled', async () => { + const clients = new ClientsService(); + + const actions = actionsMock.createStart(); + const { elasticsearch } = coreMock.createSetup(); + const { savedObjects } = coreMock.createStart(); + const request = httpServerMock.createRawRequest(); + const spacesService = undefined; + + clients.setup(elasticsearch.dataClient, spacesService); + clients.start(savedObjects, actions); + + const { spacesClient } = await clients.createGetScoped()(request); + expect(spacesClient.getSpaceId()).toEqual('default'); + }); + }); + }); +}); diff --git a/x-pack/legacy/plugins/siem/server/services/clients.ts b/x-pack/legacy/plugins/siem/server/services/clients.ts index cb78f6e5c7838..ca50eda4e7a6c 100644 --- a/x-pack/legacy/plugins/siem/server/services/clients.ts +++ b/x-pack/legacy/plugins/siem/server/services/clients.ts @@ -13,7 +13,8 @@ import { } from '../../../../../../src/core/server'; import { ActionsClient } from '../../../../../plugins/actions/server'; import { AlertsClient } from '../../../../../legacy/plugins/alerting/server'; -import { CoreStart, StartPlugins, SetupPlugins } from '../plugin'; +import { SpacesServiceSetup } from '../../../../../plugins/spaces/server'; +import { CoreStart, StartPlugins } from '../plugin'; export interface Clients { actionsClient?: ActionsClient; @@ -30,12 +31,9 @@ export class ClientsService { private actions?: StartPlugins['actions']; private clusterClient?: IClusterClient; private savedObjects?: CoreStart['savedObjects']; - private spacesService?: SetupPlugins['spaces']['spacesService']; + private spacesService?: SpacesServiceSetup; - public setup( - clusterClient: IClusterClient, - spacesService: SetupPlugins['spaces']['spacesService'] - ) { + public setup(clusterClient: IClusterClient, spacesService?: SpacesServiceSetup) { this.clusterClient = clusterClient; this.spacesService = spacesService; }