acl_specs.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import {describe, beforeEach, it, expect, sinon, angularMocks} from 'test/lib/common';
  2. import {AclCtrl} from '../acl';
  3. describe('AclCtrl', () => {
  4. const ctx: any = {};
  5. const backendSrv = {
  6. get: sinon.stub().returns(Promise.resolve([])),
  7. post: sinon.stub().returns(Promise.resolve([]))
  8. };
  9. const dashboardSrv = {
  10. getCurrent: sinon.stub().returns({id: 1, meta: { isFolder: false }})
  11. };
  12. beforeEach(angularMocks.module('grafana.core'));
  13. beforeEach(angularMocks.module('grafana.controllers'));
  14. beforeEach(angularMocks.inject(($rootScope, $controller, $q, $compile) => {
  15. ctx.$q = $q;
  16. ctx.scope = $rootScope.$new();
  17. AclCtrl.prototype.dashboard = {dashboard: {id: 1}};
  18. ctx.ctrl = $controller(AclCtrl, {
  19. $scope: ctx.scope,
  20. backendSrv: backendSrv,
  21. dashboardSrv: dashboardSrv
  22. }, {
  23. dismiss: () => { return; }
  24. });
  25. }));
  26. describe('when permissions are added', () => {
  27. beforeEach(() => {
  28. backendSrv.get.reset();
  29. backendSrv.post.reset();
  30. const userItem = {
  31. id: 2,
  32. login: 'user2',
  33. };
  34. ctx.ctrl.userPicked(userItem);
  35. const teamItem = {
  36. id: 2,
  37. name: 'ug1',
  38. };
  39. ctx.ctrl.groupPicked(teamItem);
  40. ctx.ctrl.newType = 'Editor';
  41. ctx.ctrl.typeChanged();
  42. ctx.ctrl.newType = 'Viewer';
  43. ctx.ctrl.typeChanged();
  44. });
  45. it('should sort the result by role, team and user', () => {
  46. expect(ctx.ctrl.items[0].role).to.eql('Viewer');
  47. expect(ctx.ctrl.items[1].role).to.eql('Editor');
  48. expect(ctx.ctrl.items[2].teamId).to.eql(2);
  49. expect(ctx.ctrl.items[3].userId).to.eql(2);
  50. });
  51. it('should save permissions to db', (done) => {
  52. ctx.ctrl.update().then(() => {
  53. done();
  54. });
  55. expect(backendSrv.post.getCall(0).args[0]).to.eql('/api/dashboards/id/1/acl');
  56. expect(backendSrv.post.getCall(0).args[1].items[0].role).to.eql('Viewer');
  57. expect(backendSrv.post.getCall(0).args[1].items[0].permission).to.eql(1);
  58. expect(backendSrv.post.getCall(0).args[1].items[1].role).to.eql('Editor');
  59. expect(backendSrv.post.getCall(0).args[1].items[1].permission).to.eql(1);
  60. expect(backendSrv.post.getCall(0).args[1].items[2].teamId).to.eql(2);
  61. expect(backendSrv.post.getCall(0).args[1].items[2].permission).to.eql(1);
  62. expect(backendSrv.post.getCall(0).args[1].items[3].userId).to.eql(2);
  63. expect(backendSrv.post.getCall(0).args[1].items[3].permission).to.eql(1);
  64. });
  65. });
  66. describe('when duplicate role permissions are added', () => {
  67. beforeEach(() => {
  68. backendSrv.get.reset();
  69. backendSrv.post.reset();
  70. ctx.ctrl.items = [];
  71. ctx.ctrl.newType = 'Editor';
  72. ctx.ctrl.typeChanged();
  73. ctx.ctrl.newType = 'Editor';
  74. ctx.ctrl.typeChanged();
  75. });
  76. it('should throw a validation error', () => {
  77. expect(ctx.ctrl.error).to.eql(ctx.ctrl.duplicateError);
  78. });
  79. it('should not add the duplicate permission', () => {
  80. expect(ctx.ctrl.items.length).to.eql(1);
  81. });
  82. });
  83. describe('when duplicate user permissions are added', () => {
  84. beforeEach(() => {
  85. backendSrv.get.reset();
  86. backendSrv.post.reset();
  87. ctx.ctrl.items = [];
  88. const userItem = {
  89. id: 2,
  90. login: 'user2',
  91. };
  92. ctx.ctrl.userPicked(userItem);
  93. ctx.ctrl.userPicked(userItem);
  94. });
  95. it('should throw a validation error', () => {
  96. expect(ctx.ctrl.error).to.eql(ctx.ctrl.duplicateError);
  97. });
  98. it('should not add the duplicate permission', () => {
  99. expect(ctx.ctrl.items.length).to.eql(1);
  100. });
  101. });
  102. describe('when duplicate team permissions are added', () => {
  103. beforeEach(() => {
  104. backendSrv.get.reset();
  105. backendSrv.post.reset();
  106. ctx.ctrl.items = [];
  107. const teamItem = {
  108. id: 2,
  109. name: 'ug1',
  110. };
  111. ctx.ctrl.groupPicked(teamItem);
  112. ctx.ctrl.groupPicked(teamItem);
  113. });
  114. it('should throw a validation error', () => {
  115. expect(ctx.ctrl.error).to.eql(ctx.ctrl.duplicateError);
  116. });
  117. it('should not add the duplicate permission', () => {
  118. expect(ctx.ctrl.items.length).to.eql(1);
  119. });
  120. });
  121. describe('when one inherited and one not inherited team permission are added', () => {
  122. beforeEach(() => {
  123. backendSrv.get.reset();
  124. backendSrv.post.reset();
  125. ctx.ctrl.items = [];
  126. const inheritedTeamItem = {
  127. id: 2,
  128. name: 'ug1',
  129. dashboardId: -1
  130. };
  131. ctx.ctrl.items.push(inheritedTeamItem);
  132. const teamItem = {
  133. id: 2,
  134. name: 'ug1',
  135. };
  136. ctx.ctrl.groupPicked(teamItem);
  137. });
  138. it('should not throw a validation error', () => {
  139. expect(ctx.ctrl.error).to.eql('');
  140. });
  141. it('should add both permissions', () => {
  142. expect(ctx.ctrl.items.length).to.eql(2);
  143. });
  144. });
  145. });