Skip to content

Commit

Permalink
add tests to the service
Browse files Browse the repository at this point in the history
  • Loading branch information
Bamieh committed May 31, 2021
1 parent 3f1056d commit 291dcd0
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/core/server/deprecations/deprecations_factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
2 changes: 1 addition & 1 deletion src/core/server/deprecations/deprecations_registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
6 changes: 5 additions & 1 deletion src/core/server/deprecations/deprecations_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand Down
125 changes: 125 additions & 0 deletions src/core/server/deprecations/deprecations_service.test.ts
Original file line number Diff line number Diff line change
@@ -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",
},
]
`);
});
});
});
4 changes: 1 addition & 3 deletions src/core/server/deprecations/deprecations_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,13 @@ export interface InternalDeprecationsServiceSetup {
/** @internal */
export interface DeprecationsSetupDeps {
http: InternalHttpServiceSetup;
elasticsearch: InternalElasticsearchServiceSetup;
coreUsageData: CoreUsageDataSetup;
}

/** @internal */
export class DeprecationsService implements CoreService<InternalDeprecationsServiceSetup> {
private readonly logger: Logger;

constructor(private readonly coreContext: CoreContext) {
constructor(private readonly coreContext: Pick<CoreContext, 'logger' | 'configService'>) {
this.logger = coreContext.logger.get('deprecations-service');
}

Expand Down
6 changes: 3 additions & 3 deletions src/core/server/deprecations/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down

0 comments on commit 291dcd0

Please sign in to comment.