fix check for security and added jest test (#109429) (#110013)

Co-authored-by: Jean-Louis Leysens <jloleysens@gmail.com>
This commit is contained in:
Kibana Machine 2021-08-25 07:03:16 -04:00 committed by GitHub
parent 4ea7a0cf40
commit a4f4a202a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 84 additions and 1 deletions

View file

@ -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 */);
});
});

View file

@ -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);
}