forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SecuritySolution] Check visualize.save on added to library (elastic#…
…194689) ## Summary Previously we use `canUseEditor` to check if we should disable the `Added to Library` action. It's not ideal as it checks `core.application.capabilities.visualize?.show` under the hood. We should use `application.capabilities.visualize?.save` to make sure `Added to Library` is disabled when they have no rights to save a visualization. Steps to verify: 1. Create a role with read visualization privilege, and assign it to a user: <img width="2556" alt="Screenshot 2024-10-02 at 13 20 44" src="https://github.com/user-attachments/assets/1ab9ddcf-96fd-4fd1-bdad-7382573350fb"> 2. Login with the user and check `Add to Library` should be disabled: <img width="2556" alt="Screenshot 2024-10-02 at 13 20 11" src="https://github.com/user-attachments/assets/9681b121-77e6-47c1-9a99-57d53f5d0b07"> ### Checklist Delete any items that are not applicable to this PR. - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios --------- Co-authored-by: kibanamachine <[email protected]>
- Loading branch information
1 parent
fe83c0d
commit b2d821a
Showing
3 changed files
with
119 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...rity_solution/public/common/components/visualization_actions/use_save_to_library.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* 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, act } from '@testing-library/react-hooks'; | ||
import { toMountPoint } from '@kbn/react-kibana-mount'; | ||
import { useSaveToLibrary } from './use_save_to_library'; | ||
import { useKibana } from '../../lib/kibana'; | ||
import { kpiHostMetricLensAttributes } from './lens_attributes/hosts/kpi_host_metric'; | ||
|
||
jest.mock('../../lib/kibana', () => ({ | ||
useKibana: jest.fn(), | ||
})); | ||
|
||
jest.mock('./use_redirect_to_dashboard_from_lens', () => ({ | ||
useRedirectToDashboardFromLens: jest.fn().mockReturnValue({ | ||
redirectTo: jest.fn(), | ||
getEditOrCreateDashboardPath: jest.fn().mockReturnValue('mockDashboardPath'), | ||
}), | ||
})); | ||
|
||
jest.mock('../link_to', () => ({ | ||
useGetSecuritySolutionUrl: jest.fn(), | ||
})); | ||
|
||
jest.mock('@kbn/react-kibana-mount', () => ({ | ||
toMountPoint: jest.fn().mockReturnValue(jest.fn()), | ||
})); | ||
|
||
const mockUseKibana = useKibana as jest.Mock; | ||
|
||
describe('useSaveToLibrary hook', () => { | ||
const mockStartServices = { | ||
application: { capabilities: { visualize: { save: true } } }, | ||
lens: { SaveModalComponent: jest.fn() }, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
mockUseKibana.mockReturnValue({ services: mockStartServices }); | ||
}); | ||
|
||
it('should open the save visualization flyout when openSaveVisualizationFlyout is called', () => { | ||
const { result } = renderHook(() => | ||
useSaveToLibrary({ attributes: kpiHostMetricLensAttributes }) | ||
); | ||
|
||
act(() => { | ||
result.current.openSaveVisualizationFlyout(); | ||
}); | ||
|
||
expect(toMountPoint).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should disable visualizations if user cannot save', () => { | ||
const noSaveCapabilities = { | ||
...mockStartServices, | ||
application: { capabilities: { visualize: { save: false } } }, | ||
}; | ||
mockUseKibana.mockReturnValue({ services: noSaveCapabilities }); | ||
|
||
const { result } = renderHook(() => | ||
useSaveToLibrary({ attributes: kpiHostMetricLensAttributes }) | ||
); | ||
expect(result.current.disableVisualizations).toBe(true); | ||
}); | ||
|
||
it('should disable visualizations if attributes are missing', () => { | ||
mockUseKibana.mockReturnValue({ services: mockStartServices }); | ||
const { result } = renderHook(() => useSaveToLibrary({ attributes: null })); | ||
expect(result.current.disableVisualizations).toBe(true); | ||
}); | ||
|
||
it('should enable visualizations if user can save and attributes are present', () => { | ||
const { result } = renderHook(() => | ||
useSaveToLibrary({ attributes: kpiHostMetricLensAttributes }) | ||
); | ||
expect(result.current.disableVisualizations).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters