Skip to content

Commit

Permalink
fixed unit tests around routes in actions
Browse files Browse the repository at this point in the history
  • Loading branch information
gmmorris committed Jan 20, 2020
1 parent 4bfb25c commit b244516
Show file tree
Hide file tree
Showing 25 changed files with 1,180 additions and 512 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { extendRouteWithLicenseCheck } from './extend_route_with_license_check';
import { LicenseState } from './lib/license_state';
jest.mock('./lib/license_state', () => ({
verifyApiAccessFactory: () => {},
verifyApiAccess: () => {},
}));

describe('extendRouteWithLicenseCheck', () => {
Expand Down

This file was deleted.

26 changes: 0 additions & 26 deletions x-pack/plugins/actions/server/extend_route_with_license_check.ts

This file was deleted.

17 changes: 2 additions & 15 deletions x-pack/plugins/actions/server/lib/action_executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import Hapi from 'hapi';
import { KibanaRequest } from '../../../../../src/core/server';
import { schema } from '@kbn/config-schema';
import { ActionExecutor } from './action_executor';
import { actionTypeRegistryMock } from '../action_type_registry.mock';
Expand All @@ -29,20 +29,7 @@ const executeParams = {
params: {
foo: true,
},
request: {
headers: {},
getBasePath: () => '',
path: '/',
route: { settings: {} },
url: {
href: '/',
},
raw: {
req: {
url: '/',
},
},
} as Hapi.Request,
request: {} as KibanaRequest,
};

actionExecutor.initialize({
Expand Down
18 changes: 18 additions & 0 deletions x-pack/plugins/actions/server/lib/license_api_access.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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 Boom from 'boom';
import { LicenseState } from './license_state';

export function verifyApiAccess(licenseState: LicenseState) {
const licenseCheckResults = licenseState.getLicenseInformation();

if (licenseCheckResults.showAppLink && licenseCheckResults.enableAppLink) {
return null;
}

throw Boom.forbidden(licenseCheckResults.message);
}
38 changes: 38 additions & 0 deletions x-pack/plugins/actions/server/lib/license_state.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 { of } from 'rxjs';
import { LicenseState } from './license_state';
import { LICENSE_CHECK_STATE, ILicense } from '../../../licensing/server';

export const mockLicenseState = () => {
const license: ILicense = {
uid: '123',
status: 'active',
isActive: true,
signature: 'sig',
isAvailable: true,
toJSON: () => ({
signature: 'sig',
}),
getUnavailableReason: () => undefined,
hasAtLeast() {
return true;
},
check() {
return {
state: LICENSE_CHECK_STATE.Valid,
};
},
getFeature() {
return {
isAvailable: true,
isEnabled: true,
};
},
};
return new LicenseState(of(license));
};
11 changes: 0 additions & 11 deletions x-pack/plugins/actions/server/lib/license_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/

import Boom from 'boom';
import { i18n } from '@kbn/i18n';
import { Observable, Subscription } from 'rxjs';
import { assertNever } from '../../../../../src/core/utils';
Expand Down Expand Up @@ -79,13 +78,3 @@ export class LicenseState {
}
}
}

export function verifyApiAccessFactory(licenseState: LicenseState) {
const licenseCheckResults = licenseState.getLicenseInformation();

if (licenseCheckResults.showAppLink && licenseCheckResults.enableAppLink) {
return null;
}

throw Boom.forbidden(licenseCheckResults.message);
}
4 changes: 2 additions & 2 deletions x-pack/plugins/actions/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import {
getActionRoute,
updateActionRoute,
listActionTypesRoute,
getExecuteActionRoute,
executeActionRoute,
} from './routes';
import { LicenseState } from './lib/license_state';

Expand Down Expand Up @@ -148,7 +148,7 @@ export class ActionsPlugin implements Plugin<Promise<PluginSetupContract>, Plugi
findActionRoute(router, this.licenseState);
updateActionRoute(router, this.licenseState);
listActionTypesRoute(router, this.licenseState);
getExecuteActionRoute(router, this.licenseState, actionExecutor);
executeActionRoute(router, this.licenseState, actionExecutor);

return {
registerType: actionTypeRegistry.register.bind(actionTypeRegistry),
Expand Down
61 changes: 61 additions & 0 deletions x-pack/plugins/actions/server/routes/_mock_handler_arguments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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 { RequestHandlerContext, KibanaRequest, KibanaResponseFactory } from 'kibana/server';
import { identity } from 'lodash';

export function mockHandlerArguments(
{ actionsClient, listTypes: listTypesRes = [] }: any,
req: any,
res?: Array<MethodKeysOf<KibanaResponseFactory>>
): [RequestHandlerContext, KibanaRequest<any, any, any, any>, KibanaResponseFactory] {
const listTypes = jest.fn(() => listTypesRes);
return [
({
actions: {
listTypes,
getActionsClient() {
return (
actionsClient || {
get: jest.fn(),
delete: jest.fn(),
update: jest.fn(),
find: jest.fn(),
create: jest.fn(),
}
);
},
},
} as unknown) as RequestHandlerContext,
req as KibanaRequest<any, any, any, any>,
mockResponseFactory(res),
];
}

export const mockResponseFactory = (resToMock: Array<MethodKeysOf<KibanaResponseFactory>> = []) => {
const factory: jest.Mocked<KibanaResponseFactory> = {
ok: jest.fn(),
accepted: jest.fn(),
noContent: jest.fn(),
redirected: jest.fn(),
badRequest: jest.fn(),
unauthorized: jest.fn(),
forbidden: jest.fn(),
notFound: jest.fn(),
conflict: jest.fn(),
internalError: jest.fn(),
customError: jest.fn(),
custom: jest.fn(),
};
resToMock.forEach((key: string) => {
if (key in factory) {
Object.defineProperty(factory, key, {
value: jest.fn(identity),
});
}
});
return (factory as unknown) as KibanaResponseFactory;
};
65 changes: 0 additions & 65 deletions x-pack/plugins/actions/server/routes/_mock_server.ts

This file was deleted.

Loading

0 comments on commit b244516

Please sign in to comment.