PermissionsStore.jest.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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: 1,
  13. userId: 2,
  14. userLogin: 'danlimerick',
  15. userEmail: 'dan.limerick@gmail.com',
  16. permission: 4,
  17. permissionName: 'Admin',
  18. },
  19. ])
  20. );
  21. backendSrv.post = jest.fn();
  22. store = PermissionsStore.create(
  23. {
  24. fetching: false,
  25. canUpdate: false,
  26. items: [],
  27. },
  28. {
  29. backendSrv: backendSrv,
  30. }
  31. );
  32. return store.load(1, true);
  33. });
  34. it('should save update on permission change', () => {
  35. expect(store.items[0].permission).toBe(1);
  36. expect(store.items[0].permissionName).toBe('View');
  37. store.updatePermissionOnIndex(0, 2, 'Edit');
  38. expect(store.items[0].permission).toBe(2);
  39. expect(store.items[0].permissionName).toBe('Edit');
  40. expect(backendSrv.post.mock.calls.length).toBe(1);
  41. expect(backendSrv.post.mock.calls[0][0]).toBe('/api/dashboards/id/1/acl');
  42. });
  43. it('should save newly added permissions automatically', () => {
  44. expect(store.items.length).toBe(3);
  45. const newItem = {
  46. userId: 10,
  47. userLogin: 'tester1',
  48. permission: 1,
  49. };
  50. store.addStoreItem(newItem);
  51. expect(store.items.length).toBe(4);
  52. expect(backendSrv.post.mock.calls.length).toBe(1);
  53. expect(backendSrv.post.mock.calls[0][0]).toBe('/api/dashboards/id/1/acl');
  54. });
  55. it('should save removed permissions automatically', () => {
  56. expect(store.items.length).toBe(3);
  57. store.removeStoreItem(2);
  58. expect(store.items.length).toBe(2);
  59. expect(backendSrv.post.mock.calls.length).toBe(1);
  60. expect(backendSrv.post.mock.calls[0][0]).toBe('/api/dashboards/id/1/acl');
  61. });
  62. });