PermissionsStore.jest.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { PermissionsStore } from './PermissionsStore';
  2. import { backendSrv } from 'test/mocks/common';
  3. describe('PermissionsStore', () => {
  4. let store;
  5. beforeEach(() => {
  6. backendSrv.get.mockReturnValue(
  7. Promise.resolve([
  8. { id: 2, dashboardId: 1, role: 'Viewer', permission: 1, permissionName: 'View' },
  9. { id: 3, dashboardId: 1, role: 'Editor', permission: 1, permissionName: 'Edit' },
  10. {
  11. id: 4,
  12. dashboardId: 10,
  13. permission: 1,
  14. permissionName: 'View',
  15. teamId: 1,
  16. teamName: 'MyTestTeam',
  17. },
  18. ])
  19. );
  20. backendSrv.post = jest.fn();
  21. store = PermissionsStore.create(
  22. {
  23. fetching: false,
  24. items: [],
  25. },
  26. {
  27. backendSrv: backendSrv,
  28. }
  29. );
  30. return store.load(1, false, false);
  31. });
  32. it('should save update on permission change', () => {
  33. expect(store.items[0].permission).toBe(1);
  34. expect(store.items[0].permissionName).toBe('View');
  35. store.updatePermissionOnIndex(0, 2, 'Edit');
  36. expect(store.items[0].permission).toBe(2);
  37. expect(store.items[0].permissionName).toBe('Edit');
  38. expect(backendSrv.post.mock.calls.length).toBe(1);
  39. expect(backendSrv.post.mock.calls[0][0]).toBe('/api/dashboards/id/1/acl');
  40. });
  41. it('should save newly added permissions automatically', () => {
  42. expect(store.items.length).toBe(3);
  43. const newItem = {
  44. userId: 10,
  45. userLogin: 'tester1',
  46. permission: 1,
  47. };
  48. store.addStoreItem(newItem);
  49. expect(store.items.length).toBe(4);
  50. expect(backendSrv.post.mock.calls.length).toBe(1);
  51. expect(backendSrv.post.mock.calls[0][0]).toBe('/api/dashboards/id/1/acl');
  52. });
  53. it('should save removed permissions automatically', () => {
  54. expect(store.items.length).toBe(3);
  55. store.removeStoreItem(2);
  56. expect(store.items.length).toBe(2);
  57. expect(backendSrv.post.mock.calls.length).toBe(1);
  58. expect(backendSrv.post.mock.calls[0][0]).toBe('/api/dashboards/id/1/acl');
  59. });
  60. describe('when duplicate user permissions are added', () => {
  61. beforeEach(() => {
  62. const newItem = {
  63. userId: 10,
  64. userLogin: 'tester1',
  65. permission: 1,
  66. dashboardId: 1,
  67. };
  68. store.addStoreItem(newItem);
  69. store.addStoreItem(newItem);
  70. });
  71. it('should return a validation error', () => {
  72. expect(store.items.length).toBe(4);
  73. expect(store.error).toBe('This permission exists already.');
  74. expect(backendSrv.post.mock.calls.length).toBe(1);
  75. });
  76. });
  77. describe('when duplicate team permissions are added', () => {
  78. beforeEach(() => {
  79. const newItem = {
  80. teamId: 1,
  81. teamName: 'testerteam',
  82. permission: 1,
  83. dashboardId: 1,
  84. };
  85. store.addStoreItem(newItem);
  86. store.addStoreItem(newItem);
  87. });
  88. it('should return a validation error', () => {
  89. expect(store.items.length).toBe(4);
  90. expect(store.error).toBe('This permission exists already.');
  91. expect(backendSrv.post.mock.calls.length).toBe(1);
  92. });
  93. });
  94. describe('when duplicate role permissions are added', () => {
  95. beforeEach(() => {
  96. const newItem = {
  97. team: 'MyTestTeam',
  98. teamId: 1,
  99. permission: 1,
  100. dashboardId: 1,
  101. };
  102. store.addStoreItem(newItem);
  103. store.addStoreItem(newItem);
  104. });
  105. it('should return a validation error', () => {
  106. expect(store.items.length).toBe(4);
  107. expect(store.error).toBe('This permission exists already.');
  108. expect(backendSrv.post.mock.calls.length).toBe(1);
  109. });
  110. });
  111. describe('when one inherited and one not inherited team permission are added', () => {
  112. beforeEach(() => {
  113. const teamItem = {
  114. team: 'MyTestTeam',
  115. dashboardId: 1,
  116. teamId: 1,
  117. permission: 2,
  118. };
  119. store.addStoreItem(teamItem);
  120. });
  121. it('should not throw a validation error', () => {
  122. expect(store.error).toBe(null);
  123. });
  124. it('should add both permissions', () => {
  125. expect(store.items.length).toBe(4);
  126. });
  127. });
  128. });