Save Queries: Remove entry points when user has Viewer role (#110244)

This commit is contained in:
Juan Cabanas
2025-09-01 14:18:02 -03:00
committed by GitHub
parent 9244d5f058
commit b47aece718
3 changed files with 74 additions and 1 deletions
@@ -0,0 +1,40 @@
import { screen } from '@testing-library/react';
import { render } from 'test/test-utils';
import { OrgRole } from '@grafana/data';
import { contextSrv } from 'app/core/core';
import { QueryLibraryContextProviderMock } from '../QueryLibrary/mocks';
import { RichHistoryAddToLibrary } from './RichHistoryAddToLibrary';
describe('RichHistoryAddToLibrary', () => {
it('should render button when save query is enabled', () => {
render(
<QueryLibraryContextProviderMock queryLibraryEnabled={true}>
<RichHistoryAddToLibrary query={{ refId: 'A' }} />
</QueryLibraryContextProviderMock>
);
expect(screen.getByRole('button', { name: /Save query/i })).toBeInTheDocument();
});
it('should not render button when save query is disabled', () => {
render(
<QueryLibraryContextProviderMock queryLibraryEnabled={false}>
<RichHistoryAddToLibrary query={{ refId: 'A' }} />
</QueryLibraryContextProviderMock>
);
expect(screen.queryByRole('button', { name: /Save query/i })).not.toBeInTheDocument();
});
it('should not render button when user has Viewer role', () => {
contextSrv.user.orgRole = OrgRole.Viewer;
render(
<QueryLibraryContextProviderMock queryLibraryEnabled={true}>
<RichHistoryAddToLibrary query={{ refId: 'A' }} />
</QueryLibraryContextProviderMock>
);
expect(screen.queryByRole('button', { name: /Save query/i })).not.toBeInTheDocument();
});
});
@@ -4,6 +4,7 @@ import { t } from '@grafana/i18n';
import { reportInteraction } from '@grafana/runtime';
import { DataQuery } from '@grafana/schema';
import { Button } from '@grafana/ui';
import { contextSrv } from 'app/core/services/context_srv';
import { useDispatch, useSelector } from 'app/types/store';
import { useQueryLibraryContext } from '../QueryLibrary/QueryLibraryContext';
@@ -30,6 +31,10 @@ export const RichHistoryAddToLibrary = ({ query }: Props) => {
const buttonLabel = t('explore.rich-history-card.add-to-library', 'Save query');
if (contextSrv.hasRole('Viewer')) {
return null;
}
return queryLibraryEnabled && !hasBeenSaved ? (
<>
<Button
@@ -51,5 +56,5 @@ export const RichHistoryAddToLibrary = ({ query }: Props) => {
{buttonLabel}
</Button>
</>
) : undefined;
) : null;
};
@@ -104,4 +104,32 @@ describe('SecondaryActions', () => {
await user.click(screen.getByRole('button', { name: /Query inspector/i }));
expect(onClickQueryInspector).toBeCalledTimes(1);
});
it('should render add from saved queries button when saved queries is enabled', () => {
render(
<QueryLibraryContextProviderMock queryLibraryEnabled={true}>
<SecondaryActions
onClickAddQueryRowButton={noop}
onClickQueryInspectorButton={noop}
onSelectQueryFromLibrary={noop}
/>
</QueryLibraryContextProviderMock>
);
expect(screen.getByRole('button', { name: /Add from saved queries/i })).toBeInTheDocument();
});
it('should not render add from saved queries button when saved queries is disabled', () => {
render(
<QueryLibraryContextProviderMock queryLibraryEnabled={false}>
<SecondaryActions
onClickAddQueryRowButton={noop}
onClickQueryInspectorButton={noop}
onSelectQueryFromLibrary={noop}
/>
</QueryLibraryContextProviderMock>
);
expect(screen.queryByRole('button', { name: /Add from saved queries/i })).not.toBeInTheDocument();
});
});