PermissionsStore.jest.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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);
  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. };
  67. store.addStoreItem(newItem);
  68. store.addStoreItem(newItem);
  69. });
  70. it('should return a validation error', () => {
  71. expect(store.items.length).toBe(4);
  72. expect(store.error).toBe('This permission exists already.');
  73. expect(backendSrv.post.mock.calls.length).toBe(1);
  74. });
  75. });
  76. describe('when duplicate team permissions are added', () => {
  77. beforeEach(() => {
  78. const newItem = {
  79. teamId: 1,
  80. teamName: 'testerteam',
  81. permission: 1,
  82. };
  83. store.addStoreItem(newItem);
  84. store.addStoreItem(newItem);
  85. });
  86. it('should return a validation error', () => {
  87. expect(store.items.length).toBe(4);
  88. expect(store.error).toBe('This permission exists already.');
  89. expect(backendSrv.post.mock.calls.length).toBe(1);
  90. });
  91. });
  92. describe('when duplicate role permissions are added', () => {
  93. beforeEach(() => {
  94. const newItem = {
  95. team: 'MyTestTeam',
  96. teamId: 1,
  97. permission: 1,
  98. };
  99. store.addStoreItem(newItem);
  100. store.addStoreItem(newItem);
  101. });
  102. it('should return a validation error', () => {
  103. expect(store.items.length).toBe(4);
  104. expect(store.error).toBe('This permission exists already.');
  105. expect(backendSrv.post.mock.calls.length).toBe(1);
  106. });
  107. });
  108. describe('when one inherited and one not inherited team permission are added', () => {
  109. beforeEach(() => {
  110. const teamItem = {
  111. team: 'MyTestTeam',
  112. dashboardId: 1,
  113. teamId: 1,
  114. permission: 2,
  115. };
  116. store.addStoreItem(teamItem);
  117. });
  118. it('should not throw a validation error', () => {
  119. expect(store.error).toBe(null);
  120. });
  121. it('should add both permissions', () => {
  122. expect(store.items.length).toBe(4);
  123. });
  124. });
  125. });