forked from Graylog2/graylog2-server
-
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.
Only display dashboards with write permissions in add to dashboard me…
…nu (Graylog2#4505) * Add tests for issue * Only display dashboards with write access in menu * Update current user permissions with the initial state * Make writableDashboards consistent with dashboards This store is using ImmutableJS to store the dashboards state, and it should do the same with the writableDashboards * Fix eslint errors on test
- Loading branch information
Showing
3 changed files
with
111 additions
and
5 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
106 changes: 106 additions & 0 deletions
106
graylog2-web-interface/src/components/dashboard/AddToDashboardMenu.test.jsx
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,106 @@ | ||
import 'jquery-ui/ui/version'; | ||
import 'jquery-ui/ui/effect'; | ||
import 'jquery-ui/ui/plugin'; | ||
import 'jquery-ui/ui/widget'; | ||
import 'jquery-ui/ui/widgets/mouse'; | ||
import React from 'react'; | ||
import Immutable from 'immutable'; | ||
import { mount } from 'enzyme'; | ||
import PermissionsMixin from 'util/PermissionsMixin'; | ||
import AddToDashboardMenu from './AddToDashboardMenu'; | ||
|
||
describe('<AddToDashboardMenu />', () => { | ||
const exampleProps = { | ||
widgetType: 'SEARCH_RESULT_CHART', | ||
title: 'Add to dashboard', | ||
permissions: [], | ||
}; | ||
|
||
const allDashboards = Immutable.List([ | ||
{ id: '1', title: 'One' }, | ||
{ id: '2', title: 'Two' }, | ||
{ id: '3', title: 'Three' }, | ||
]).sortBy(dashboard => dashboard.title); | ||
|
||
const filterDashboards = (dashboards, permissions) => ({ | ||
// Emulate API behaviour by filtering out dashboards the user cannot read | ||
dashboards: dashboards.filter(dashboard => PermissionsMixin.isPermitted(permissions, `dashboards:read:${dashboard.id}`)), | ||
writableDashboards: dashboards.filter(dashboard => PermissionsMixin.isPermitted(permissions, `dashboards:edit:${dashboard.id}`)), | ||
}); | ||
|
||
describe('admin users', () => { | ||
let wrapper; | ||
const permissions = ['*']; | ||
beforeEach(() => { | ||
wrapper = mount(<AddToDashboardMenu | ||
widgetType={exampleProps.widgetType} | ||
title={exampleProps.title} | ||
permissions={permissions} />); | ||
}); | ||
|
||
it('should see all dashboards', () => { | ||
const { dashboards, writableDashboards } = filterDashboards(allDashboards, permissions); | ||
wrapper.setState({ dashboards: dashboards, writableDashboards: writableDashboards }); | ||
wrapper.find('.dropdown-menu').find('MenuItem').forEach((node, idx) => { | ||
expect(node.prop('children')).toEqual(writableDashboards.get(idx).title); | ||
}); | ||
}); | ||
|
||
it('should see an option to create a new dashboard when there are none', () => { | ||
const { dashboards, writableDashboards } = filterDashboards(Immutable.List(), permissions); | ||
wrapper.setState({ dashboards: dashboards, writableDashboards: writableDashboards }); | ||
expect(wrapper.find('.dropdown-menu').find('MenuItem').prop('children')).toEqual('No dashboards, create one?'); | ||
}); | ||
}); | ||
|
||
describe('reader users', () => { | ||
describe('without dashboards:create permissions', () => { | ||
let wrapper; | ||
const permissions = ['dashboards:read:1', 'dashboards:read:2', 'dashboards:edit:2', 'dashboards:read:3']; | ||
beforeEach(() => { | ||
wrapper = mount(<AddToDashboardMenu | ||
widgetType={exampleProps.widgetType} | ||
title={exampleProps.title} | ||
permissions={permissions} />); | ||
}); | ||
|
||
it('should only see the dashboards that can edit', () => { | ||
const { dashboards, writableDashboards } = filterDashboards(allDashboards, permissions); | ||
wrapper.setState({ dashboards: dashboards, writableDashboards: writableDashboards }); | ||
expect(wrapper.find('.dropdown-menu').find('MenuItem').length).toEqual(1); | ||
expect(wrapper.find('.dropdown-menu').find('MenuItem').prop('children')).toEqual('Two'); | ||
}); | ||
|
||
it('should NOT see an option to create a new dashboard when there are none', () => { | ||
const { dashboards, writableDashboards } = filterDashboards(Immutable.List(), permissions); | ||
wrapper.setState({ dashboards: dashboards, writableDashboards: writableDashboards }); | ||
expect(wrapper.find('.dropdown-menu').find('MenuItem').prop('children')).not.toEqual('No dashboards, create one?'); | ||
expect(wrapper.find('.dropdown-menu').find('MenuItem').prop('children')).toEqual('No dashboards available'); | ||
}); | ||
}); | ||
|
||
describe('with dashboards:create permissions', () => { | ||
let wrapper; | ||
const permissions = ['dashboards:create', 'dashboards:read:1', 'dashboards:read:2', 'dashboards:edit:2', 'dashboards:read:3']; | ||
beforeEach(() => { | ||
wrapper = mount(<AddToDashboardMenu | ||
widgetType={exampleProps.widgetType} | ||
title={exampleProps.title} | ||
permissions={permissions} />); | ||
}); | ||
|
||
it('should only see the dashboards that can edit', () => { | ||
const { dashboards, writableDashboards } = filterDashboards(allDashboards, permissions); | ||
wrapper.setState({ dashboards: dashboards, writableDashboards: writableDashboards }); | ||
expect(wrapper.find('.dropdown-menu').find('MenuItem').length).toEqual(1); | ||
expect(wrapper.find('.dropdown-menu').find('MenuItem').prop('children')).toEqual('Two'); | ||
}); | ||
|
||
it('should see an option to create a new dashboard when there are none', () => { | ||
const { dashboards, writableDashboards } = filterDashboards(Immutable.List(), permissions); | ||
wrapper.setState({ dashboards: dashboards, writableDashboards: writableDashboards }); | ||
expect(wrapper.find('.dropdown-menu').find('MenuItem').prop('children')).toEqual('No dashboards, create one?'); | ||
}); | ||
}); | ||
}); | ||
}); |
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