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

[APM] Custom links can still be created with a read only user. #87089

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -6,10 +6,20 @@
import React from 'react';
import { EuiButton } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { useApmPluginContext } from '../../../../../context/apm_plugin/use_apm_plugin_context';

export function CreateCustomLinkButton({ onClick }: { onClick: () => void }) {
const { core } = useApmPluginContext();
const canSave = core.application.capabilities.apm.save;
return (
<EuiButton color="primary" fill iconType="plusInCircle" onClick={onClick}>
<EuiButton
color="primary"
fill
iconType="plusInCircle"
onClick={onClick}
isDisabled={!canSave}
data-test-subj="createButton"
>
{i18n.translate(
'xpack.apm.settings.customizeUI.customLink.createCustomLink',
{ defaultMessage: 'Create custom link' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
EuiSpacer,
} from '@elastic/eui';
import { isEmpty } from 'lodash';
import { useApmPluginContext } from '../../../../../context/apm_plugin/use_apm_plugin_context';
import { CustomLink } from '../../../../../../common/custom_link/custom_link_types';
import { units, px } from '../../../../../style/variables';
import { ManagedTable } from '../../../../shared/ManagedTable';
Expand All @@ -26,6 +27,8 @@ interface Props {

export function CustomLinkTable({ items = [], onCustomLinkSelected }: Props) {
const [searchTerm, setSearchTerm] = useState('');
const { core } = useApmPluginContext();
const canSave = core.application.capabilities.apm.save;

const columns = [
{
Expand Down Expand Up @@ -61,22 +64,26 @@ export function CustomLinkTable({ items = [], onCustomLinkSelected }: Props) {
width: px(units.triple),
name: '',
actions: [
{
name: i18n.translate(
'xpack.apm.settings.customizeUI.customLink.table.editButtonLabel',
{ defaultMessage: 'Edit' }
),
description: i18n.translate(
'xpack.apm.settings.customizeUI.customLink.table.editButtonDescription',
{ defaultMessage: 'Edit this custom link' }
),
icon: 'pencil',
color: 'primary',
type: 'icon',
onClick: (customLink: CustomLink) => {
onCustomLinkSelected(customLink);
},
},
...(canSave
? [
{
name: i18n.translate(
'xpack.apm.settings.customizeUI.customLink.table.editButtonLabel',
{ defaultMessage: 'Edit' }
),
description: i18n.translate(
'xpack.apm.settings.customizeUI.customLink.table.editButtonDescription',
{ defaultMessage: 'Edit this custom link' }
),
icon: 'pencil',
color: 'primary',
type: 'icon',
onClick: (customLink: CustomLink) => {
onCustomLinkSelected(customLink);
},
},
]
: []),
],
},
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@
import {
fireEvent,
render,
waitFor,
RenderResult,
waitFor,
} from '@testing-library/react';
import React from 'react';
import { act } from 'react-dom/test-utils';
import * as apmApi from '../../../../../services/rest/createCallApmApi';
import { CustomLinkOverview } from '.';
import { License } from '../../../../../../../licensing/common/license';
import * as hooks from '../../../../../hooks/use_fetcher';
import { ApmPluginContextValue } from '../../../../../context/apm_plugin/apm_plugin_context';
import {
mockApmPluginContextValue,
MockApmPluginContextWrapper,
} from '../../../../../context/apm_plugin/mock_apm_plugin_context';
import { LicenseContext } from '../../../../../context/license/license_context';
import { CustomLinkOverview } from '.';
import * as hooks from '../../../../../hooks/use_fetcher';
import * as apmApi from '../../../../../services/rest/createCallApmApi';
import {
expectTextsInDocument,
expectTextsNotInDocument,
} from '../../../../../utils/testHelpers';
import * as saveCustomLink from './CreateEditCustomLinkFlyout/saveCustomLink';
import { MockApmPluginContextWrapper } from '../../../../../context/apm_plugin/mock_apm_plugin_context';

const data = [
{
Expand All @@ -39,6 +43,16 @@ const data = [
},
];

function getMockAPMContext({ canSave }: { canSave: boolean }) {
return ({
...mockApmPluginContextValue,
core: {
...mockApmPluginContextValue.core,
application: { capabilities: { apm: { save: canSave }, ml: {} } },
},
} as unknown) as ApmPluginContextValue;
}

describe('CustomLink', () => {
beforeAll(() => {
jest.spyOn(apmApi, 'callApmApi').mockResolvedValue({});
Expand Down Expand Up @@ -70,9 +84,11 @@ describe('CustomLink', () => {
});
it('shows when no link is available', () => {
const component = render(
<LicenseContext.Provider value={goldLicense}>
<CustomLinkOverview />
</LicenseContext.Provider>
<MockApmPluginContextWrapper>
<LicenseContext.Provider value={goldLicense}>
<CustomLinkOverview />
</LicenseContext.Provider>
</MockApmPluginContextWrapper>
);
expectTextsInDocument(component, ['No links found.']);
});
Expand All @@ -91,6 +107,34 @@ describe('CustomLink', () => {
jest.clearAllMocks();
});

it('enables create button when user has writte privileges', () => {
const mockContext = getMockAPMContext({ canSave: true });

const { getByTestId } = render(
<LicenseContext.Provider value={goldLicense}>
<MockApmPluginContextWrapper value={mockContext}>
<CustomLinkOverview />
</MockApmPluginContextWrapper>
</LicenseContext.Provider>
);
const createButton = getByTestId('createButton') as HTMLButtonElement;
expect(createButton.disabled).toBeFalsy();
});

it('enables edit button on custom link table when user has writte privileges', () => {
const mockContext = getMockAPMContext({ canSave: true });

const { getAllByText } = render(
<LicenseContext.Provider value={goldLicense}>
<MockApmPluginContextWrapper value={mockContext}>
<CustomLinkOverview />
</MockApmPluginContextWrapper>
</LicenseContext.Provider>
);

expect(getAllByText('Edit').length).toEqual(2);
});

it('shows a table with all custom link', () => {
const component = render(
<LicenseContext.Provider value={goldLicense}>
Expand All @@ -108,9 +152,11 @@ describe('CustomLink', () => {
});

it('checks if create custom link button is available and working', () => {
const mockContext = getMockAPMContext({ canSave: true });

const { queryByText, getByText } = render(
<LicenseContext.Provider value={goldLicense}>
<MockApmPluginContextWrapper>
<MockApmPluginContextWrapper value={mockContext}>
<CustomLinkOverview />
</MockApmPluginContextWrapper>
</LicenseContext.Provider>
Expand All @@ -137,9 +183,10 @@ describe('CustomLink', () => {
});

const openFlyout = () => {
const mockContext = getMockAPMContext({ canSave: true });
const component = render(
<LicenseContext.Provider value={goldLicense}>
<MockApmPluginContextWrapper>
<MockApmPluginContextWrapper value={mockContext}>
<CustomLinkOverview />
</MockApmPluginContextWrapper>
</LicenseContext.Provider>
Expand Down Expand Up @@ -173,9 +220,10 @@ describe('CustomLink', () => {
});

it('deletes a custom link', async () => {
const mockContext = getMockAPMContext({ canSave: true });
const component = render(
<LicenseContext.Provider value={goldLicense}>
<MockApmPluginContextWrapper>
<MockApmPluginContextWrapper value={mockContext}>
<CustomLinkOverview />
</MockApmPluginContextWrapper>
</LicenseContext.Provider>
Expand Down Expand Up @@ -356,4 +404,34 @@ describe('CustomLink', () => {
expectTextsNotInDocument(component, ['Start free 30-day trial']);
});
});

describe('with read-only user', () => {
it('disables create custom link button', () => {
const mockContext = getMockAPMContext({ canSave: false });

const { getByTestId } = render(
<LicenseContext.Provider value={goldLicense}>
<MockApmPluginContextWrapper value={mockContext}>
<CustomLinkOverview />
</MockApmPluginContextWrapper>
</LicenseContext.Provider>
);
const createButton = getByTestId('createButton') as HTMLButtonElement;
expect(createButton.disabled).toBeTruthy();
});

it('removes edit button on custom link table', () => {
const mockContext = getMockAPMContext({ canSave: false });

const { queryAllByText } = render(
<LicenseContext.Provider value={goldLicense}>
<MockApmPluginContextWrapper value={mockContext}>
<CustomLinkOverview />
</MockApmPluginContextWrapper>
</LicenseContext.Provider>
);

expect(queryAllByText('Edit').length).toEqual(0);
});
});
});