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

Browse: Hide dashboard actions if user does not have enough permission #55218

Merged
merged 2 commits into from Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 63 additions & 0 deletions public/app/features/search/components/ManageDashboards.test.tsx
@@ -0,0 +1,63 @@
import { render, screen, waitFor } from '@testing-library/react';
import React from 'react';
import { Provider } from 'react-redux';

import { contextSrv } from 'app/core/services/context_srv';
import { configureStore } from 'app/store/configureStore';
import { FolderDTO } from 'app/types';

import ManageDashboardsNew from './ManageDashboardsNew';

jest.mock('app/core/services/context_srv', () => {
const originMock = jest.requireActual('app/core/services/context_srv');

return {
...originMock,
contextSrv: {
...originMock.context_srv,
user: {},
hasAccess: jest.fn(() => false),
},
};
});

const setup = async (options?: { folder?: FolderDTO }) => {
const { folder = {} as FolderDTO } = options || {};
const store = configureStore();

const { rerender } = await waitFor(() =>
render(
<Provider store={store}>
<ManageDashboardsNew folder={folder} />
</Provider>
)
);

return { rerender, store };
};

jest.spyOn(console, 'error').mockImplementation();

describe('ManageDashboards', () => {
beforeEach(() => {
(contextSrv.hasAccess as jest.Mock).mockClear();
});
it('should show dashboard actions if user has correct permissions', async () => {
(contextSrv.hasAccess as jest.Mock).mockReturnValue(false);

const { rerender, store } = await setup();

expect(screen.queryByRole('button', { name: /new/i })).not.toBeInTheDocument();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicky but I didn't notice that this one test was both testing for having permissions and not having permissions, should we separate this into 2 tests so its easier to understand what each of them does? Or alternatively change the title to reflect that it is testing both scenarios


(contextSrv.hasAccess as jest.Mock).mockReturnValue(true);
await waitFor(() =>
rerender(
<Provider store={store}>
<ManageDashboardsNew folder={{ canEdit: true } as FolderDTO} />
</Provider>
)
);

expect(screen.getByRole('button', { name: /new/i })).toBeInTheDocument();
});
});
24 changes: 13 additions & 11 deletions public/app/features/search/components/ManageDashboardsNew.tsx
Expand Up @@ -29,15 +29,18 @@ export const ManageDashboardsNew = React.memo(({ folder }: Props) => {
const folderId = folder?.id;
// const folderUid = folder?.uid;
const canSave = folder?.canSave;
const { isEditor } = contextSrv;
const hasEditPermissionInFolders = folder ? canSave : contextSrv.hasEditPermissionInFolders;

const canCreateFolders = contextSrv.hasAccess(AccessControlAction.FoldersCreate, isEditor);
const canCreateDashboards = contextSrv.hasAccess(
AccessControlAction.DashboardsCreate,
hasEditPermissionInFolders || !!canSave
);
let [includePanels, setIncludePanels] = useLocalStorage<boolean>(SEARCH_PANELS_LOCAL_STORAGE_KEY, true);
if (!config.featureToggles.panelTitleSearch) {
includePanels = false;
}

const { isEditor } = contextSrv;

const onSearchQueryChange = (e: React.ChangeEvent<HTMLInputElement>) => {
onQueryChange(e.currentTarget.value);
};
Expand All @@ -57,14 +60,13 @@ export const ManageDashboardsNew = React.memo(({ folder }: Props) => {
suffix={false ? <Spinner /> : null}
/>
</div>
<DashboardActions
folderId={folderId}
canCreateFolders={contextSrv.hasAccess(AccessControlAction.FoldersCreate, isEditor)}
canCreateDashboards={contextSrv.hasAccess(
AccessControlAction.DashboardsCreate,
hasEditPermissionInFolders || !!canSave
)}
/>
{canCreateFolders && canCreateDashboards && (
<DashboardActions
folderId={folderId}
canCreateFolders={canCreateFolders}
canCreateDashboards={canCreateDashboards}
/>
)}
</div>

<SearchView
Expand Down