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

[Synthetics] Check if license management plugin is enabled before linking #178773

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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"ml",
"serverless",
"spaces",
"telemetry"
"telemetry",
"licenseManagement"
],
"requiredBundles": [
"data",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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 { renderHook } from '@testing-library/react-hooks';
import React from 'react';
import { render, WrappedHelper } from '../utils/testing';
import { useSyntheticsPrivileges } from './use_synthetics_priviliges';

jest.mock('../../../hooks/use_capabilities', () => ({
useCanReadSyntheticsIndex: jest.fn().mockReturnValue({ canRead: true, loading: false }),
}));

jest.mock('react-redux', () => {
const actual = jest.requireActual('react-redux');
return {
...actual,
useSelector: jest.fn().mockReturnValue({ error: { body: { message: 'License not active' } } }),
};
});

function wrapper({ children }: { children: React.ReactElement }) {
return <WrappedHelper>{children}</WrappedHelper>;
}

describe('useSyntheticsPrivileges', () => {
it.each([
[true, null],
[false, ''],
])(
'should correctly set the disabled prop of the license nav button if `licenseManagement` enabled is %s',
(enabled, expectedDisabledAttribute) => {
const {
result: { current },
} = renderHook(() => useSyntheticsPrivileges(), { wrapper });

expect(current).not.toBeUndefined();

const { getByLabelText } = render(current!, {
core: {
licenseManagement: { enabled },
},
});

const licenseNavButton = getByLabelText('Navigate to license management');

expect(licenseNavButton);

// there should only be an href if the license management is enabled, otherwise we render a disabled button with no handler
expect(!!licenseNavButton.getAttribute('href')).toEqual(enabled);
expect(licenseNavButton.getAttribute('disabled')).toEqual(expectedDisabledAttribute);
}
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
EuiMarkdownFormat,
} from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n-react';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import { css } from '@emotion/react';
import { i18n } from '@kbn/i18n';
import { selectOverviewStatus } from '../state/overview_status';
Expand All @@ -27,6 +28,7 @@ import {
SYNTHETICS_INDEX_PATTERN,
} from '../../../../common/constants';
import { useSyntheticsSettingsContext } from '../contexts';
import { ClientPluginsStart } from '../../../plugin';

export const useSyntheticsPrivileges = () => {
const { canRead: canReadSyntheticsIndex, loading: isCanReadLoading } =
Expand Down Expand Up @@ -107,7 +109,11 @@ const Unprivileged = ({ unprivilegedIndices }: { unprivilegedIndices: string[] }
);

const LicenseExpired = () => {
const licenseManagementEnabled =
useKibana<ClientPluginsStart>().services.licenseManagement?.enabled;

const { basePath } = useSyntheticsSettingsContext();

return (
<EuiEmptyPrompt
data-test-subj="syntheticsUnprivileged"
Expand All @@ -131,7 +137,11 @@ const LicenseExpired = () => {
}
actions={[
<EuiButton
aria-label={i18n.translate('xpack.synthetics.invalidLicense.manageYourLicenseButton', {
defaultMessage: 'Navigate to license management',
})}
data-test-subj="apmInvalidLicenseNotificationManageYourLicenseButton"
isDisabled={!licenseManagementEnabled}
href={basePath + '/app/management/stack/license_management'}
>
{i18n.translate('xpack.synthetics.invalidLicense.licenseManagementLink', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export const PageRouter: FC = () => {

apiService.addInspectorRequest = addInspectorRequest;

const isUnPrivileged = useSyntheticsPrivileges();
const isUnprivileged = useSyntheticsPrivileges();

return (
<Routes>
Expand All @@ -205,12 +205,12 @@ export const PageRouter: FC = () => {
<div className={APP_WRAPPER_CLASS} data-test-subj={dataTestSubj}>
<RouteInit title={title} path={path} />
<SyntheticsPageTemplateComponent
pageHeader={isUnPrivileged ? undefined : pageHeader}
pageHeader={isUnprivileged ? undefined : pageHeader}
data-test-subj={'synthetics-page-template'}
isPageDataLoaded={true}
{...pageTemplateProps}
>
{isUnPrivileged || <RouteComponent />}
{isUnprivileged || <RouteComponent />}
</SyntheticsPageTemplateComponent>
</div>
</Route>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import type {
ObservabilitySharedPluginSetup,
ObservabilitySharedPluginStart,
} from '@kbn/observability-shared-plugin/public';
import { LicenseManagementUIPluginSetup } from '@kbn/license-management-plugin/public/plugin';
import {
ObservabilityAIAssistantPublicSetup,
ObservabilityAIAssistantPublicStart,
Expand Down Expand Up @@ -100,6 +101,7 @@ export interface ClientPluginsStart {
uiSettings: CoreStart['uiSettings'];
usageCollection: UsageCollectionStart;
serverless: ServerlessPluginStart;
licenseManagement?: LicenseManagementUIPluginSetup;
}

export interface UptimePluginServices extends Partial<CoreStart> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
"@kbn/code-editor-mock",
"@kbn/serverless",
"@kbn/repo-info",
"@kbn/index-management-plugin"
"@kbn/index-management-plugin",
"@kbn/license-management-plugin"
],
"exclude": ["target/**/*"]
}