From 291dcd05eec92f344504856235fb215e2bcea66f Mon Sep 17 00:00:00 2001 From: Ahmad Bamieh Date: Mon, 31 May 2021 12:20:58 +0300 Subject: [PATCH] add tests to the service --- .../deprecations/deprecations_factory.test.ts | 2 +- .../deprecations_registry.test.ts | 2 +- .../deprecations/deprecations_registry.ts | 6 +- .../deprecations/deprecations_service.test.ts | 125 ++++++++++++++++++ .../deprecations/deprecations_service.ts | 4 +- src/core/server/deprecations/types.ts | 6 +- 6 files changed, 136 insertions(+), 9 deletions(-) create mode 100644 src/core/server/deprecations/deprecations_service.test.ts diff --git a/src/core/server/deprecations/deprecations_factory.test.ts b/src/core/server/deprecations/deprecations_factory.test.ts index 469451b0020c0..187f3880f9998 100644 --- a/src/core/server/deprecations/deprecations_factory.test.ts +++ b/src/core/server/deprecations/deprecations_factory.test.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { GetDeprecationsContext } from './types'; +import type { GetDeprecationsContext } from './types'; import { DeprecationsFactory } from './deprecations_factory'; import { loggerMock } from '../logging/logger.mock'; diff --git a/src/core/server/deprecations/deprecations_registry.test.ts b/src/core/server/deprecations/deprecations_registry.test.ts index 507677a531861..82b09beaa5123 100644 --- a/src/core/server/deprecations/deprecations_registry.test.ts +++ b/src/core/server/deprecations/deprecations_registry.test.ts @@ -7,7 +7,7 @@ */ /* eslint-disable dot-notation */ -import { RegisterDeprecationsConfig, GetDeprecationsContext } from './types'; +import type { RegisterDeprecationsConfig, GetDeprecationsContext } from './types'; import { DeprecationsRegistry } from './deprecations_registry'; describe('DeprecationsRegistry', () => { diff --git a/src/core/server/deprecations/deprecations_registry.ts b/src/core/server/deprecations/deprecations_registry.ts index f92d807514b82..cc05473923ac8 100644 --- a/src/core/server/deprecations/deprecations_registry.ts +++ b/src/core/server/deprecations/deprecations_registry.ts @@ -6,7 +6,11 @@ * Side Public License, v 1. */ -import { DeprecationsDetails, RegisterDeprecationsConfig, GetDeprecationsContext } from './types'; +import type { + DeprecationsDetails, + RegisterDeprecationsConfig, + GetDeprecationsContext, +} from './types'; export class DeprecationsRegistry { private readonly deprecationContexts: RegisterDeprecationsConfig[] = []; diff --git a/src/core/server/deprecations/deprecations_service.test.ts b/src/core/server/deprecations/deprecations_service.test.ts new file mode 100644 index 0000000000000..7de94e5c2e129 --- /dev/null +++ b/src/core/server/deprecations/deprecations_service.test.ts @@ -0,0 +1,125 @@ +/* + * 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 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +/* eslint-disable dot-notation */ +import { DeprecationsService } from './deprecations_service'; +import { httpServiceMock } from '../http/http_service.mock'; +import { mockRouter } from '../http/router/router.mock'; +import { mockCoreContext } from '../core_context.mock'; +import { DeprecationsFactory } from './deprecations_factory'; + +describe('DeprecationsService', () => { + const coreContext = mockCoreContext.create(); + describe('#setup', () => { + const http = httpServiceMock.createInternalSetupContract(); + const router = mockRouter.create(); + http.createRouter.mockReturnValue(router); + const deprecationsCoreSetupDeps = { http }; + + it('registers routes', () => { + const deprecationsService = new DeprecationsService(coreContext); + deprecationsService.setup(deprecationsCoreSetupDeps); + // Registers correct base api path + expect(http.createRouter).toBeCalledWith('/api/deprecations'); + // registers get route '/' + expect(router.get.mock.calls[0][0]).toEqual({ path: '/', validate: false }); + expect(router.get.mock.calls[0][1]).toBeInstanceOf(Function); + }); + + it('calls registerConfigDeprecationsInfo', () => { + const deprecationsService = new DeprecationsService(coreContext); + const mockRegisterConfigDeprecationsInfo = jest.fn(); + deprecationsService['registerConfigDeprecationsInfo'] = mockRegisterConfigDeprecationsInfo; + deprecationsService.setup(deprecationsCoreSetupDeps); + expect(mockRegisterConfigDeprecationsInfo).toBeCalledTimes(1); + }); + + it('returns setup contract', () => { + const deprecationsService = new DeprecationsService(coreContext); + const internalSetupContract = deprecationsService.setup(deprecationsCoreSetupDeps); + expect(internalSetupContract).toMatchInlineSnapshot(` + Object { + "getRegistry": [Function], + } + `); + const externalSetupContract = internalSetupContract.getRegistry('someDomain'); + + expect(externalSetupContract).toMatchInlineSnapshot(` + Object { + "registerDeprecations": [Function], + } + `); + }); + }); + + describe('#registerConfigDeprecationsInfo', () => { + it('registers config deprecations', () => { + const deprecationsService = new DeprecationsService(coreContext); + const mockGetHandledDeprecatedConfigs = jest.fn().mockReturnValue([ + [ + 'testDomain', + [ + { + message: 'testMessage', + documentationUrl: 'testDocUrl', + correctiveActions: { + manualSteps: [ + 'Using Kibana user management, change all users using the kibana_user role to the kibana_admin role.', + 'Using Kibana role-mapping management, change all role-mappings which assing the kibana_user role to the kibana_admin role.', + ], + }, + }, + ], + ], + ]); + const mockRegisterDeprecations = jest.fn(); + const mockGetRegistry = jest.fn().mockReturnValue({ + registerDeprecations: mockRegisterDeprecations, + }); + + coreContext.configService.getHandledDeprecatedConfigs = mockGetHandledDeprecatedConfigs; + const deprecationsFactory = new DeprecationsFactory({ + logger: deprecationsService['logger'], + }); + + deprecationsFactory.getRegistry = mockGetRegistry; + deprecationsService['registerConfigDeprecationsInfo'](deprecationsFactory); + + expect(mockGetHandledDeprecatedConfigs).toBeCalledTimes(1); + expect(mockGetRegistry).toBeCalledTimes(1); + expect(mockGetRegistry).toBeCalledWith('testDomain'); + expect(mockRegisterDeprecations).toBeCalledTimes(1); + + expect(mockRegisterDeprecations.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + Object { + "getDeprecations": [Function], + }, + ] + `); + + const configDeprecations = mockRegisterDeprecations.mock.calls[0][0].getDeprecations(); + expect(configDeprecations).toMatchInlineSnapshot(` + Array [ + Object { + "correctiveActions": Object { + "manualSteps": Array [ + "Using Kibana user management, change all users using the kibana_user role to the kibana_admin role.", + "Using Kibana role-mapping management, change all role-mappings which assing the kibana_user role to the kibana_admin role.", + ], + }, + "deprecationType": "config", + "documentationUrl": "testDocUrl", + "level": "critical", + "message": "testMessage", + }, + ] + `); + }); + }); +}); diff --git a/src/core/server/deprecations/deprecations_service.ts b/src/core/server/deprecations/deprecations_service.ts index 9fbc19a965247..6c3a445e753be 100644 --- a/src/core/server/deprecations/deprecations_service.ts +++ b/src/core/server/deprecations/deprecations_service.ts @@ -112,15 +112,13 @@ export interface InternalDeprecationsServiceSetup { /** @internal */ export interface DeprecationsSetupDeps { http: InternalHttpServiceSetup; - elasticsearch: InternalElasticsearchServiceSetup; - coreUsageData: CoreUsageDataSetup; } /** @internal */ export class DeprecationsService implements CoreService { private readonly logger: Logger; - constructor(private readonly coreContext: CoreContext) { + constructor(private readonly coreContext: Pick) { this.logger = coreContext.logger.get('deprecations-service'); } diff --git a/src/core/server/deprecations/types.ts b/src/core/server/deprecations/types.ts index 1c4579a152b44..50c947591fdf4 100644 --- a/src/core/server/deprecations/types.ts +++ b/src/core/server/deprecations/types.ts @@ -26,13 +26,13 @@ export interface DeprecationsDetails { */ level: 'warning' | 'critical' | 'fetch_error'; /** - * (optiional) Used to identify between different deprecation types. + * (optional) Used to identify between different deprecation types. * Example use case: in Upgrade Assistant, we may want to allow the user to sort by * deprecation type or show each type in a separate tab. * * Feel free to add new types if necessary. - * Predefined types are necessary to reduce similar definitions with different keywords - * across all kibana deprecations. + * Predefined types are necessary to reduce having similar definitions with different keywords + * across kibana deprecations. */ deprecationType?: 'config' | 'feature'; /* (optional) link to the documentation for more details on the deprecation. */