Skip to content

Commit

Permalink
fix check for security and added jest test (elastic#109429)
Browse files Browse the repository at this point in the history
  • Loading branch information
jloleysens authored and kibanamachine committed Aug 25, 2021
1 parent 9f2fe19 commit db02f70
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
83 changes: 83 additions & 0 deletions x-pack/plugins/reporting/server/routes/deprecations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { of } from 'rxjs';
import { UnwrapPromise } from '@kbn/utility-types';
import { setupServer } from 'src/core/server/test_utils';
import { API_GET_ILM_POLICY_STATUS } from '../../common/constants';
import { securityMock } from '../../../security/server/mocks';

import supertest from 'supertest';

import {
createMockConfigSchema,
createMockPluginSetup,
createMockReportingCore,
createMockLevelLogger,
} from '../test_helpers';

import { registerDeprecationsRoutes } from './deprecations';

type SetupServerReturn = UnwrapPromise<ReturnType<typeof setupServer>>;

describe(`GET ${API_GET_ILM_POLICY_STATUS}`, () => {
const reportingSymbol = Symbol('reporting');
let server: SetupServerReturn['server'];
let httpSetup: SetupServerReturn['httpSetup'];

const createReportingCore = ({
security,
}: {
security?: ReturnType<typeof securityMock.createSetup>;
}) =>
createMockReportingCore(
createMockConfigSchema({
queue: {
indexInterval: 'year',
timeout: 10000,
pollEnabled: true,
},
index: '.reporting',
}),
createMockPluginSetup({
security,
router: httpSetup.createRouter(''),
licensing: { license$: of({ isActive: true, isAvailable: true, type: 'gold' }) },
})
);

beforeEach(async () => {
jest.clearAllMocks();
({ server, httpSetup } = await setupServer(reportingSymbol));
});

it('correctly handles authz when security is unavailable', async () => {
const core = await createReportingCore({});

registerDeprecationsRoutes(core, createMockLevelLogger());
await server.start();

await supertest(httpSetup.server.listener)
.get(API_GET_ILM_POLICY_STATUS)
.expect(200)
.then(/* Ignore result */);
});

it('correctly handles authz when security is disabled', async () => {
const security = securityMock.createSetup();
security.license.isEnabled.mockReturnValue(false);
const core = await createReportingCore({ security });

registerDeprecationsRoutes(core, createMockLevelLogger());
await server.start();

await supertest(httpSetup.server.listener)
.get(API_GET_ILM_POLICY_STATUS)
.expect(200)
.then(/* Ignore result */);
});
});
2 changes: 1 addition & 1 deletion x-pack/plugins/reporting/server/routes/deprecations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const registerDeprecationsRoutes = (reporting: ReportingCore, logger: Log
const authzWrapper = <P, Q, B>(handler: RequestHandler<P, Q, B>): RequestHandler<P, Q, B> => {
return async (ctx, req, res) => {
const { security } = reporting.getPluginSetupDeps();
if (!security) {
if (!security?.license.isEnabled()) {
return handler(ctx, req, res);
}

Expand Down

0 comments on commit db02f70

Please sign in to comment.