acl_specs.ts 4.9 KB

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