2018-01-31 16:12:51 +01:00
|
|
|
import { PermissionsStore, aclTypeValues } from './PermissionsStore';
|
2018-01-26 14:02:22 +01:00
|
|
|
import { backendSrv } from 'test/mocks/common';
|
|
|
|
|
|
|
|
|
|
describe('PermissionsStore', () => {
|
|
|
|
|
let store;
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
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-01-27 17:26:40 +01:00
|
|
|
dashboardId: 10,
|
|
|
|
|
permission: 1,
|
|
|
|
|
permissionName: 'View',
|
|
|
|
|
teamId: 1,
|
|
|
|
|
teamName: 'MyTestTeam',
|
2018-01-26 14:02:22 +01:00
|
|
|
},
|
|
|
|
|
])
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
backendSrv.post = jest.fn();
|
|
|
|
|
|
|
|
|
|
store = PermissionsStore.create(
|
|
|
|
|
{
|
|
|
|
|
fetching: false,
|
|
|
|
|
items: [],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
backendSrv: backendSrv,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2018-01-27 17:26:40 +01:00
|
|
|
return store.load(1, false);
|
2018-01-26 14:02:22 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should save update on permission change', () => {
|
|
|
|
|
expect(store.items[0].permission).toBe(1);
|
|
|
|
|
expect(store.items[0].permissionName).toBe('View');
|
|
|
|
|
|
|
|
|
|
store.updatePermissionOnIndex(0, 2, 'Edit');
|
|
|
|
|
|
|
|
|
|
expect(store.items[0].permission).toBe(2);
|
|
|
|
|
expect(store.items[0].permissionName).toBe('Edit');
|
|
|
|
|
expect(backendSrv.post.mock.calls.length).toBe(1);
|
|
|
|
|
expect(backendSrv.post.mock.calls[0][0]).toBe('/api/dashboards/id/1/acl');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should save removed permissions automatically', () => {
|
|
|
|
|
expect(store.items.length).toBe(3);
|
|
|
|
|
|
|
|
|
|
store.removeStoreItem(2);
|
|
|
|
|
|
|
|
|
|
expect(store.items.length).toBe(2);
|
|
|
|
|
expect(backendSrv.post.mock.calls.length).toBe(1);
|
|
|
|
|
expect(backendSrv.post.mock.calls[0][0]).toBe('/api/dashboards/id/1/acl');
|
|
|
|
|
});
|
2018-01-27 17:26:40 +01:00
|
|
|
|
|
|
|
|
describe('when duplicate team permissions are added', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
const newItem = {
|
2018-01-31 16:12:51 +01:00
|
|
|
teamId: 10,
|
|
|
|
|
team: 'tester-team',
|
2018-01-27 17:26:40 +01:00
|
|
|
permission: 1,
|
|
|
|
|
};
|
2018-01-31 16:12:51 +01:00
|
|
|
store.resetNewType();
|
|
|
|
|
store.newItem.setTeam(newItem.teamId, newItem.team);
|
|
|
|
|
store.newItem.setPermission(newItem.permission);
|
|
|
|
|
store.addStoreItem();
|
|
|
|
|
|
|
|
|
|
store.newItem.setTeam(newItem.teamId, newItem.team);
|
|
|
|
|
store.newItem.setPermission(newItem.permission);
|
|
|
|
|
store.addStoreItem();
|
2018-01-27 17:26:40 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return a validation error', () => {
|
|
|
|
|
expect(store.items.length).toBe(4);
|
|
|
|
|
expect(store.error).toBe('This permission exists already.');
|
|
|
|
|
expect(backendSrv.post.mock.calls.length).toBe(1);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2018-01-31 16:12:51 +01:00
|
|
|
describe('when duplicate user permissions are added', () => {
|
2018-01-27 17:26:40 +01:00
|
|
|
beforeEach(() => {
|
|
|
|
|
const newItem = {
|
2018-01-31 16:12:51 +01:00
|
|
|
userId: 10,
|
|
|
|
|
userLogin: 'tester1',
|
2018-01-27 17:26:40 +01:00
|
|
|
permission: 1,
|
|
|
|
|
};
|
2018-01-31 16:12:51 +01:00
|
|
|
store.setNewType(aclTypeValues.USER.value);
|
|
|
|
|
store.newItem.setUser(newItem.userId, newItem.userLogin);
|
|
|
|
|
store.newItem.setPermission(newItem.permission);
|
|
|
|
|
store.addStoreItem();
|
|
|
|
|
store.setNewType(aclTypeValues.USER.value);
|
|
|
|
|
store.newItem.setUser(newItem.userId, newItem.userLogin);
|
|
|
|
|
store.newItem.setPermission(newItem.permission);
|
|
|
|
|
store.addStoreItem();
|
2018-01-27 17:26:40 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return a validation error', () => {
|
|
|
|
|
expect(store.items.length).toBe(4);
|
|
|
|
|
expect(store.error).toBe('This permission exists already.');
|
|
|
|
|
expect(backendSrv.post.mock.calls.length).toBe(1);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2018-01-31 16:12:51 +01:00
|
|
|
// TODO: I dont get this one
|
|
|
|
|
// describe('when one inherited and one not inherited team permission are added', () => {
|
|
|
|
|
// beforeEach(() => {
|
|
|
|
|
// const teamItem = {
|
|
|
|
|
// team: 'MyTestTeam',
|
|
|
|
|
// dashboardId: 1,
|
|
|
|
|
// teamId: 1,
|
|
|
|
|
// permission: 2,
|
|
|
|
|
// };
|
|
|
|
|
// store.addStoreItem(teamItem);
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('should not throw a validation error', () => {
|
|
|
|
|
// expect(store.error).toBe(null);
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('should add both permissions', () => {
|
|
|
|
|
// expect(store.items.length).toBe(4);
|
|
|
|
|
// });
|
|
|
|
|
// });
|
2018-01-26 14:02:22 +01:00
|
|
|
});
|