Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Reporting] Fix check for security and added jest test #109429

Merged
merged 2 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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