2017-06-15 20:21:14 +02:00
|
|
|
import {describe, beforeEach, it, expect, sinon, angularMocks} from 'test/lib/common';
|
|
|
|
|
import {AclCtrl} from '../acl';
|
|
|
|
|
|
|
|
|
|
describe('AclCtrl', () => {
|
2017-06-23 00:09:47 +02:00
|
|
|
const ctx: any = {};
|
|
|
|
|
const backendSrv = {
|
2017-06-15 20:21:14 +02:00
|
|
|
get: sinon.stub().returns(Promise.resolve([])),
|
|
|
|
|
post: sinon.stub().returns(Promise.resolve([]))
|
|
|
|
|
};
|
|
|
|
|
|
2017-06-23 00:09:47 +02:00
|
|
|
const dashboardSrv = {
|
2017-06-24 03:50:22 +02:00
|
|
|
getCurrent: sinon.stub().returns({id: 1, meta: { isFolder: false }})
|
2017-06-23 00:09:47 +02:00
|
|
|
};
|
|
|
|
|
|
2017-06-15 20:21:14 +02:00
|
|
|
beforeEach(angularMocks.module('grafana.core'));
|
|
|
|
|
beforeEach(angularMocks.module('grafana.controllers'));
|
|
|
|
|
|
|
|
|
|
beforeEach(angularMocks.inject(($rootScope, $controller, $q, $compile) => {
|
|
|
|
|
ctx.$q = $q;
|
|
|
|
|
ctx.scope = $rootScope.$new();
|
|
|
|
|
AclCtrl.prototype.dashboard = {dashboard: {id: 1}};
|
|
|
|
|
ctx.ctrl = $controller(AclCtrl, {
|
|
|
|
|
$scope: ctx.scope,
|
|
|
|
|
backendSrv: backendSrv,
|
2017-06-23 00:09:47 +02:00
|
|
|
dashboardSrv: dashboardSrv
|
2017-06-15 20:21:14 +02:00
|
|
|
}, {
|
2017-06-23 00:09:47 +02:00
|
|
|
dismiss: () => { return; }
|
2017-06-15 20:21:14 +02:00
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
|
2017-06-23 00:09:47 +02:00
|
|
|
describe('when permissions are added', () => {
|
|
|
|
|
beforeEach(() => {
|
2017-06-15 20:39:27 +02:00
|
|
|
backendSrv.get.reset();
|
|
|
|
|
backendSrv.post.reset();
|
2017-06-15 20:21:14 +02:00
|
|
|
|
2017-06-23 00:09:47 +02:00
|
|
|
const userItem = {
|
|
|
|
|
id: 2,
|
|
|
|
|
login: 'user2',
|
|
|
|
|
};
|
2017-06-15 20:21:14 +02:00
|
|
|
|
2017-06-23 00:09:47 +02:00
|
|
|
ctx.ctrl.userPicked(userItem);
|
2017-06-15 20:21:14 +02:00
|
|
|
|
2017-12-08 18:25:45 +03:00
|
|
|
const teamItem = {
|
2017-06-23 00:09:47 +02:00
|
|
|
id: 2,
|
|
|
|
|
name: 'ug1',
|
|
|
|
|
};
|
|
|
|
|
|
2017-12-08 18:25:45 +03:00
|
|
|
ctx.ctrl.groupPicked(teamItem);
|
2017-06-23 00:09:47 +02:00
|
|
|
|
|
|
|
|
ctx.ctrl.newType = 'Editor';
|
|
|
|
|
ctx.ctrl.typeChanged();
|
2017-06-15 20:21:14 +02:00
|
|
|
|
2017-06-23 00:09:47 +02:00
|
|
|
ctx.ctrl.newType = 'Viewer';
|
|
|
|
|
ctx.ctrl.typeChanged();
|
2017-06-15 20:21:14 +02:00
|
|
|
});
|
2017-06-15 20:39:27 +02:00
|
|
|
|
2017-06-23 00:09:47 +02:00
|
|
|
it('should sort the result by role, user group and user', () => {
|
|
|
|
|
expect(ctx.ctrl.items[0].role).to.eql('Viewer');
|
|
|
|
|
expect(ctx.ctrl.items[1].role).to.eql('Editor');
|
2017-12-08 18:25:45 +03:00
|
|
|
expect(ctx.ctrl.items[2].teamId).to.eql(2);
|
2017-06-23 00:09:47 +02:00
|
|
|
expect(ctx.ctrl.items[3].userId).to.eql(2);
|
|
|
|
|
});
|
2017-06-15 20:39:27 +02:00
|
|
|
|
2017-06-23 00:09:47 +02:00
|
|
|
it('should save permissions to db', (done) => {
|
|
|
|
|
ctx.ctrl.update().then(() => {
|
2017-06-15 20:39:27 +02:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
|
2017-06-21 01:19:30 +02:00
|
|
|
expect(backendSrv.post.getCall(0).args[0]).to.eql('/api/dashboards/id/1/acl');
|
2017-06-23 00:09:47 +02:00
|
|
|
expect(backendSrv.post.getCall(0).args[1].items[0].role).to.eql('Viewer');
|
|
|
|
|
expect(backendSrv.post.getCall(0).args[1].items[0].permission).to.eql(1);
|
|
|
|
|
expect(backendSrv.post.getCall(0).args[1].items[1].role).to.eql('Editor');
|
|
|
|
|
expect(backendSrv.post.getCall(0).args[1].items[1].permission).to.eql(1);
|
2017-12-08 18:25:45 +03:00
|
|
|
expect(backendSrv.post.getCall(0).args[1].items[2].teamId).to.eql(2);
|
2017-06-23 00:09:47 +02:00
|
|
|
expect(backendSrv.post.getCall(0).args[1].items[2].permission).to.eql(1);
|
|
|
|
|
expect(backendSrv.post.getCall(0).args[1].items[3].userId).to.eql(2);
|
|
|
|
|
expect(backendSrv.post.getCall(0).args[1].items[3].permission).to.eql(1);
|
2017-06-15 20:39:27 +02:00
|
|
|
});
|
|
|
|
|
});
|
2017-06-23 17:45:37 +02:00
|
|
|
|
|
|
|
|
describe('when duplicate role permissions are added', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
backendSrv.get.reset();
|
|
|
|
|
backendSrv.post.reset();
|
|
|
|
|
ctx.ctrl.items = [];
|
|
|
|
|
|
|
|
|
|
ctx.ctrl.newType = 'Editor';
|
|
|
|
|
ctx.ctrl.typeChanged();
|
|
|
|
|
|
|
|
|
|
ctx.ctrl.newType = 'Editor';
|
|
|
|
|
ctx.ctrl.typeChanged();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw a validation error', () => {
|
|
|
|
|
expect(ctx.ctrl.error).to.eql(ctx.ctrl.duplicateError);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should not add the duplicate permission', () => {
|
|
|
|
|
expect(ctx.ctrl.items.length).to.eql(1);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('when duplicate user permissions are added', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
backendSrv.get.reset();
|
|
|
|
|
backendSrv.post.reset();
|
|
|
|
|
ctx.ctrl.items = [];
|
|
|
|
|
|
|
|
|
|
const userItem = {
|
|
|
|
|
id: 2,
|
|
|
|
|
login: 'user2',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ctx.ctrl.userPicked(userItem);
|
|
|
|
|
ctx.ctrl.userPicked(userItem);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw a validation error', () => {
|
|
|
|
|
expect(ctx.ctrl.error).to.eql(ctx.ctrl.duplicateError);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should not add the duplicate permission', () => {
|
|
|
|
|
expect(ctx.ctrl.items.length).to.eql(1);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('when duplicate user group permissions are added', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
backendSrv.get.reset();
|
|
|
|
|
backendSrv.post.reset();
|
|
|
|
|
ctx.ctrl.items = [];
|
|
|
|
|
|
2017-12-08 18:25:45 +03:00
|
|
|
const teamItem = {
|
2017-06-23 17:45:37 +02:00
|
|
|
id: 2,
|
|
|
|
|
name: 'ug1',
|
|
|
|
|
};
|
|
|
|
|
|
2017-12-08 18:25:45 +03:00
|
|
|
ctx.ctrl.groupPicked(teamItem);
|
|
|
|
|
ctx.ctrl.groupPicked(teamItem);
|
2017-06-23 17:45:37 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw a validation error', () => {
|
|
|
|
|
expect(ctx.ctrl.error).to.eql(ctx.ctrl.duplicateError);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should not add the duplicate permission', () => {
|
|
|
|
|
expect(ctx.ctrl.items.length).to.eql(1);
|
|
|
|
|
});
|
|
|
|
|
});
|
2017-06-24 03:50:22 +02:00
|
|
|
|
|
|
|
|
describe('when one inherited and one not inherited user group permission are added', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
backendSrv.get.reset();
|
|
|
|
|
backendSrv.post.reset();
|
|
|
|
|
ctx.ctrl.items = [];
|
|
|
|
|
|
2017-12-08 18:25:45 +03:00
|
|
|
const inheritedTeamItem = {
|
2017-06-24 03:50:22 +02:00
|
|
|
id: 2,
|
|
|
|
|
name: 'ug1',
|
|
|
|
|
dashboardId: -1
|
|
|
|
|
};
|
|
|
|
|
|
2017-12-08 18:25:45 +03:00
|
|
|
ctx.ctrl.items.push(inheritedTeamItem);
|
2017-06-24 03:50:22 +02:00
|
|
|
|
2017-12-08 18:25:45 +03:00
|
|
|
const teamItem = {
|
2017-06-24 03:50:22 +02:00
|
|
|
id: 2,
|
|
|
|
|
name: 'ug1',
|
|
|
|
|
};
|
2017-12-08 18:25:45 +03:00
|
|
|
ctx.ctrl.groupPicked(teamItem);
|
2017-06-24 03:50:22 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should not throw a validation error', () => {
|
|
|
|
|
expect(ctx.ctrl.error).to.eql('');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should add both permissions', () => {
|
|
|
|
|
expect(ctx.ctrl.items.length).to.eql(2);
|
|
|
|
|
});
|
|
|
|
|
});
|
2017-06-15 20:21:14 +02:00
|
|
|
});
|