Playlists: Disable Create Playlist buttons for users with viewer role (#50840)

* Disable / hide buttons for creating playlist for users with viewer role

* Update tests

* Re-add mock for contextSrv module
This commit is contained in:
asymness 2022-06-17 16:59:33 +05:00 committed by GitHub
parent fd63ed540f
commit 43839dc40b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 7 deletions

View File

@ -1,6 +1,8 @@
import { render, waitFor } from '@testing-library/react';
import React from 'react';
import { contextSrv } from 'app/core/services/context_srv';
import { locationService } from '../../../../packages/grafana-runtime/src';
import { PlaylistPage, PlaylistPageProps } from './PlaylistPage';
@ -56,6 +58,22 @@ describe('PlaylistPage', () => {
const { getByText } = getTestContext();
await waitFor(() => getByText('There are no playlists created yet'));
});
describe('and signed in user is not a viewer', () => {
it('then create playlist button should not be disabled', async () => {
contextSrv.isEditor = true;
const { getByRole } = getTestContext();
const createPlaylistButton = await waitFor(() => getByRole('link', { name: /create playlist/i }));
expect(createPlaylistButton).not.toHaveStyle('pointer-events: none');
});
});
describe('and signed in user is a viewer', () => {
it('then create playlist button should be disabled', async () => {
contextSrv.isEditor = false;
const { getByRole } = getTestContext();
const createPlaylistButton = await waitFor(() => getByRole('link', { name: /create playlist/i }));
expect(createPlaylistButton).toHaveStyle('pointer-events: none');
});
});
});
describe('when mounted with a playlist', () => {
it('page should load', () => {
@ -75,12 +93,27 @@ describe('PlaylistPage', () => {
const { getByText } = getTestContext();
expect(getByText(/loading/i)).toBeInTheDocument();
});
it('then playlist title and buttons should appear on the page', async () => {
const { getByRole, getByText } = getTestContext();
await waitFor(() => getByText('A test playlist'));
expect(getByRole('button', { name: /Start playlist/i })).toBeInTheDocument();
expect(getByRole('link', { name: /Edit playlist/i })).toBeInTheDocument();
expect(getByRole('button', { name: /Delete playlist/i })).toBeInTheDocument();
describe('and signed in user is not a viewer', () => {
it('then playlist title and all playlist buttons should appear on the page', async () => {
contextSrv.isEditor = true;
const { getByRole, getByText } = getTestContext();
await waitFor(() => getByText('A test playlist'));
expect(getByRole('link', { name: /New playlist/i })).toBeInTheDocument();
expect(getByRole('button', { name: /Start playlist/i })).toBeInTheDocument();
expect(getByRole('link', { name: /Edit playlist/i })).toBeInTheDocument();
expect(getByRole('button', { name: /Delete playlist/i })).toBeInTheDocument();
});
});
describe('and signed in user is a viewer', () => {
it('then playlist title and only start playlist button should appear on the page', async () => {
contextSrv.isEditor = false;
const { getByRole, getByText, queryByRole } = getTestContext();
await waitFor(() => getByText('A test playlist'));
expect(queryByRole('link', { name: /New playlist/i })).not.toBeInTheDocument();
expect(getByRole('button', { name: /Start playlist/i })).toBeInTheDocument();
expect(queryByRole('link', { name: /Edit playlist/i })).not.toBeInTheDocument();
expect(queryByRole('button', { name: /Delete playlist/i })).not.toBeInTheDocument();
});
});
});
});

View File

@ -7,6 +7,7 @@ import { ConfirmModal } from '@grafana/ui';
import Page from 'app/core/components/Page/Page';
import PageActionBar from 'app/core/components/PageActionBar/PageActionBar';
import { getNavModel } from 'app/core/selectors/navModel';
import { contextSrv } from 'app/core/services/context_srv';
import { StoreState } from 'app/types';
import EmptyListCTA from '../../core/components/EmptyListCTA/EmptyListCTA';
@ -64,6 +65,7 @@ export const PlaylistPage: FC<PlaylistPageProps> = ({ navModel }) => {
buttonIcon="plus"
buttonLink="playlists/new"
buttonTitle="Create Playlist"
buttonDisabled={!contextSrv.isEditor}
proTip="You can use playlists to cycle dashboards on TVs without user control"
proTipLink="http://docs.grafana.org/reference/playlist/"
proTipLinkTitle="Learn more"
@ -79,7 +81,7 @@ export const PlaylistPage: FC<PlaylistPageProps> = ({ navModel }) => {
{showSearch && (
<PageActionBar
searchQuery={searchQuery}
linkButton={{ title: 'New playlist', href: '/playlists/new' }}
linkButton={contextSrv.isEditor && { title: 'New playlist', href: '/playlists/new' }}
setSearchQuery={setSearchQuery}
/>
)}