PermissionsStore.jest.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { PermissionsStore } from './PermissionsStore';
  2. import { backendSrv } from 'test/mocks/common';
  3. describe('PermissionsStore', () => {
  4. let store;
  5. beforeEach(async () => {
  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. team: 'MyTestTeam',
  17. },
  18. {
  19. id: 5,
  20. dashboardId: 1,
  21. permission: 1,
  22. permissionName: 'View',
  23. userId: 1,
  24. userLogin: 'MyTestUser',
  25. },
  26. {
  27. id: 6,
  28. dashboardId: 1,
  29. permission: 1,
  30. permissionName: 'Edit',
  31. teamId: 2,
  32. team: 'MyTestTeam2',
  33. },
  34. ])
  35. );
  36. backendSrv.post = jest.fn(() => Promise.resolve({}));
  37. store = PermissionsStore.create(
  38. {
  39. fetching: false,
  40. items: [],
  41. },
  42. {
  43. backendSrv: backendSrv,
  44. }
  45. );
  46. await store.load(1, false, false);
  47. });
  48. it('should save update on permission change', async () => {
  49. expect(store.items[0].permission).toBe(1);
  50. expect(store.items[0].permissionName).toBe('View');
  51. await store.updatePermissionOnIndex(0, 2, 'Edit');
  52. expect(store.items[0].permission).toBe(2);
  53. expect(store.items[0].permissionName).toBe('Edit');
  54. expect(backendSrv.post.mock.calls.length).toBe(1);
  55. expect(backendSrv.post.mock.calls[0][0]).toBe('/api/dashboards/id/1/permissions');
  56. });
  57. it('should save removed permissions automatically', async () => {
  58. expect(store.items.length).toBe(5);
  59. await store.removeStoreItem(2);
  60. expect(store.items.length).toBe(4);
  61. expect(backendSrv.post.mock.calls.length).toBe(1);
  62. expect(backendSrv.post.mock.calls[0][0]).toBe('/api/dashboards/id/1/permissions');
  63. });
  64. it('should be sorted by sort rank and alphabetically', async () => {
  65. expect(store.items[0].name).toBe('MyTestTeam');
  66. expect(store.items[0].dashboardId).toBe(10);
  67. expect(store.items[1].name).toBe('Editor');
  68. expect(store.items[2].name).toBe('Viewer');
  69. expect(store.items[3].name).toBe('MyTestTeam2');
  70. expect(store.items[4].name).toBe('MyTestUser');
  71. });
  72. describe('when one inherited and one not inherited team permission are added', () => {
  73. beforeEach(async () => {
  74. const overridingItemForChildDashboard = {
  75. team: 'MyTestTeam',
  76. dashboardId: 1,
  77. teamId: 1,
  78. permission: 2,
  79. };
  80. store.resetNewType();
  81. store.newItem.setTeam(overridingItemForChildDashboard.teamId, overridingItemForChildDashboard.team);
  82. store.newItem.setPermission(overridingItemForChildDashboard.permission);
  83. await store.addStoreItem();
  84. });
  85. it('should add new overriding permission', () => {
  86. expect(store.items.length).toBe(6);
  87. });
  88. it('should be sorted by sort rank and alphabetically', async () => {
  89. expect(store.items[0].name).toBe('MyTestTeam');
  90. expect(store.items[0].dashboardId).toBe(10);
  91. expect(store.items[1].name).toBe('Editor');
  92. expect(store.items[2].name).toBe('Viewer');
  93. expect(store.items[3].name).toBe('MyTestTeam');
  94. expect(store.items[3].dashboardId).toBe(1);
  95. expect(store.items[4].name).toBe('MyTestTeam2');
  96. expect(store.items[5].name).toBe('MyTestUser');
  97. });
  98. });
  99. });