useRole.spec.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {UserFixture} from 'sentry-fixture/user';
  3. import {renderHook} from 'sentry-test/reactTestingLibrary';
  4. import {useRole} from 'sentry/components/acl/useRole';
  5. import ConfigStore from 'sentry/stores/configStore';
  6. import OrganizationStore from 'sentry/stores/organizationStore';
  7. import type {Organization} from 'sentry/types/organization';
  8. import {OrganizationContext} from 'sentry/views/organizationContext';
  9. function createWrapper(organization: Organization) {
  10. return function ({children}: {children: React.ReactNode}) {
  11. return (
  12. <OrganizationContext.Provider value={organization}>
  13. {children}
  14. </OrganizationContext.Provider>
  15. );
  16. };
  17. }
  18. describe('useRole', () => {
  19. const organization = OrganizationFixture({
  20. // User is an admin of this test org
  21. orgRole: 'admin',
  22. // For these tests, attachments will require an admin role
  23. attachmentsRole: 'admin',
  24. debugFilesRole: 'member',
  25. });
  26. beforeEach(() => {
  27. ConfigStore.set('user', UserFixture());
  28. // OrganizationStore is still called directly in isActiveSuperuser()
  29. OrganizationStore.init();
  30. OrganizationStore.onUpdate(organization, {replace: true});
  31. });
  32. it('has a sufficient role', () => {
  33. const {result} = renderHook(() => useRole({role: 'attachmentsRole'}), {
  34. wrapper: createWrapper(organization),
  35. });
  36. expect(result.current.hasRole).toBe(true);
  37. expect(result.current.roleRequired).toBe('admin');
  38. });
  39. it('has an insufficient role', () => {
  40. const org = OrganizationFixture({
  41. ...organization,
  42. orgRole: 'member',
  43. });
  44. OrganizationStore.onUpdate(org, {replace: true});
  45. const {result} = renderHook(() => useRole({role: 'attachmentsRole'}), {
  46. wrapper: createWrapper(org),
  47. });
  48. expect(result.current.hasRole).toBe(false);
  49. });
  50. it('gives access to a superuser with insufficient role', () => {
  51. const org = OrganizationFixture({
  52. ...organization,
  53. orgRole: 'member',
  54. access: ['org:superuser'],
  55. });
  56. OrganizationStore.onUpdate(org, {replace: true});
  57. const {result} = renderHook(() => useRole({role: 'attachmentsRole'}), {
  58. wrapper: createWrapper(org),
  59. });
  60. expect(result.current.hasRole).toBe(true);
  61. });
  62. it('handles no organization.orgRoleList', () => {
  63. const org = {...organization, orgRoleList: []};
  64. OrganizationStore.onUpdate(org, {replace: true});
  65. const {result} = renderHook(() => useRole({role: 'attachmentsRole'}), {
  66. wrapper: createWrapper(org),
  67. });
  68. expect(result.current.hasRole).toBe(false);
  69. });
  70. });