2018-02-27 16:04:26 +01:00
|
|
|
import { PermissionsStore } from './PermissionsStore';
|
2018-01-26 14:02:22 +01:00
|
|
|
import { backendSrv } from 'test/mocks/common';
|
|
|
|
|
|
|
|
|
|
describe('PermissionsStore', () => {
|
|
|
|
|
let store;
|
|
|
|
|
|
2018-02-27 16:04:26 +01:00
|
|
|
beforeEach(async () => {
|
2018-01-26 14:02:22 +01:00
|
|
|
backendSrv.get.mockReturnValue(
|
|
|
|
|
Promise.resolve([
|
|
|
|
|
{ id: 2, dashboardId: 1, role: 'Viewer', permission: 1, permissionName: 'View' },
|
|
|
|
|
{ id: 3, dashboardId: 1, role: 'Editor', permission: 1, permissionName: 'Edit' },
|
|
|
|
|
{
|
|
|
|
|
id: 4,
|
2018-04-16 14:30:50 +02:00
|
|
|
dashboardId: 1,
|
2018-01-27 17:26:40 +01:00
|
|
|
permission: 1,
|
|
|
|
|
permissionName: 'View',
|
|
|
|
|
teamId: 1,
|
2018-04-16 14:30:50 +02:00
|
|
|
team: 'MyTestTeam',
|
2018-01-26 14:02:22 +01:00
|
|
|
},
|
2018-04-16 10:42:39 +02:00
|
|
|
{
|
|
|
|
|
id: 5,
|
2018-04-16 14:30:50 +02:00
|
|
|
dashboardId: 1,
|
2018-04-16 10:42:39 +02:00
|
|
|
permission: 1,
|
|
|
|
|
permissionName: 'View',
|
|
|
|
|
userId: 1,
|
2018-04-16 14:30:50 +02:00
|
|
|
userLogin: 'MyTestUser',
|
2018-04-16 10:42:39 +02:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: 6,
|
2018-04-16 14:30:50 +02:00
|
|
|
dashboardId: 1,
|
2018-04-16 10:42:39 +02:00
|
|
|
permission: 1,
|
|
|
|
|
permissionName: 'Edit',
|
|
|
|
|
teamId: 2,
|
2018-04-16 14:30:50 +02:00
|
|
|
team: 'MyTestTeam2',
|
2018-04-16 10:42:39 +02:00
|
|
|
},
|
2018-01-26 14:02:22 +01:00
|
|
|
])
|
|
|
|
|
);
|
|
|
|
|
|
2018-02-27 16:04:26 +01:00
|
|
|
backendSrv.post = jest.fn(() => Promise.resolve({}));
|
2018-01-26 14:02:22 +01:00
|
|
|
|
|
|
|
|
store = PermissionsStore.create(
|
|
|
|
|
{
|
|
|
|
|
fetching: false,
|
|
|
|
|
items: [],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
backendSrv: backendSrv,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2018-02-27 16:04:26 +01:00
|
|
|
await store.load(1, false, false);
|
2018-01-26 14:02:22 +01:00
|
|
|
});
|
|
|
|
|
|
2018-02-27 16:04:26 +01:00
|
|
|
it('should save update on permission change', async () => {
|
2018-01-26 14:02:22 +01:00
|
|
|
expect(store.items[0].permission).toBe(1);
|
2018-04-16 14:30:50 +02:00
|
|
|
expect(store.items[0].permissionName).toBe('Edit');
|
2018-01-26 14:02:22 +01:00
|
|
|
|
2018-02-27 16:04:26 +01:00
|
|
|
await store.updatePermissionOnIndex(0, 2, 'Edit');
|
2018-01-26 14:02:22 +01:00
|
|
|
|
|
|
|
|
expect(store.items[0].permission).toBe(2);
|
|
|
|
|
expect(store.items[0].permissionName).toBe('Edit');
|
|
|
|
|
expect(backendSrv.post.mock.calls.length).toBe(1);
|
2018-02-21 11:53:02 +01:00
|
|
|
expect(backendSrv.post.mock.calls[0][0]).toBe('/api/dashboards/id/1/permissions');
|
2018-01-26 14:02:22 +01:00
|
|
|
});
|
|
|
|
|
|
2018-02-27 16:04:26 +01:00
|
|
|
it('should save removed permissions automatically', async () => {
|
2018-04-16 14:30:50 +02:00
|
|
|
expect(store.items.length).toBe(5);
|
2018-01-26 14:02:22 +01:00
|
|
|
|
2018-02-27 16:04:26 +01:00
|
|
|
await store.removeStoreItem(2);
|
2018-01-26 14:02:22 +01:00
|
|
|
|
2018-04-16 14:30:50 +02:00
|
|
|
expect(store.items.length).toBe(4);
|
2018-01-26 14:02:22 +01:00
|
|
|
expect(backendSrv.post.mock.calls.length).toBe(1);
|
2018-02-21 11:53:02 +01:00
|
|
|
expect(backendSrv.post.mock.calls[0][0]).toBe('/api/dashboards/id/1/permissions');
|
2018-01-26 14:02:22 +01:00
|
|
|
});
|
2018-01-27 17:26:40 +01:00
|
|
|
|
2018-04-16 14:30:50 +02:00
|
|
|
it('should be sorted by sort rank and alphabetically', async () => {
|
|
|
|
|
expect(store.items[3].name).toBe('MyTestTeam2');
|
|
|
|
|
expect(store.items[4].name).toBe('MyTestUser');
|
|
|
|
|
});
|
|
|
|
|
|
2018-01-27 17:26:40 +01:00
|
|
|
describe('when one inherited and one not inherited team permission are added', () => {
|
2018-02-27 16:04:26 +01:00
|
|
|
beforeEach(async () => {
|
2018-02-01 14:02:14 +01:00
|
|
|
const overridingItemForChildDashboard = {
|
2018-01-27 17:26:40 +01:00
|
|
|
team: 'MyTestTeam',
|
|
|
|
|
dashboardId: 1,
|
|
|
|
|
teamId: 1,
|
|
|
|
|
permission: 2,
|
|
|
|
|
};
|
2018-01-31 16:12:51 +01:00
|
|
|
|
2018-02-01 14:02:14 +01:00
|
|
|
store.resetNewType();
|
|
|
|
|
store.newItem.setTeam(overridingItemForChildDashboard.teamId, overridingItemForChildDashboard.team);
|
|
|
|
|
store.newItem.setPermission(overridingItemForChildDashboard.permission);
|
2018-02-27 16:04:26 +01:00
|
|
|
await store.addStoreItem();
|
2018-01-27 17:26:40 +01:00
|
|
|
});
|
|
|
|
|
|
2018-02-01 14:02:14 +01:00
|
|
|
it('should add new overriding permission', () => {
|
2018-04-16 14:30:50 +02:00
|
|
|
expect(store.items.length).toBe(6);
|
2018-01-27 17:26:40 +01:00
|
|
|
});
|
|
|
|
|
});
|
2018-01-26 14:02:22 +01:00
|
|
|
});
|