role.spec.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import Cookies from 'js-cookie';
  2. import {Organization} from 'sentry-fixture/organization';
  3. import {render, screen} from 'sentry-test/reactTestingLibrary';
  4. import {Role} from 'sentry/components/acl/role';
  5. import ConfigStore from 'sentry/stores/configStore';
  6. describe('Role', function () {
  7. const organization = Organization({
  8. orgRole: 'admin',
  9. orgRoleList: [
  10. {
  11. id: 'member',
  12. name: 'Member',
  13. desc: '...',
  14. minimumTeamRole: 'contributor',
  15. },
  16. {
  17. id: 'admin',
  18. name: 'Admin',
  19. desc: '...',
  20. minimumTeamRole: 'admin',
  21. },
  22. {
  23. id: 'manager',
  24. name: 'Manager',
  25. desc: '...',
  26. minimumTeamRole: 'admin',
  27. },
  28. {
  29. id: 'owner',
  30. name: 'Owner',
  31. desc: '...',
  32. minimumTeamRole: 'admin',
  33. },
  34. ],
  35. });
  36. const routerContext = TestStubs.routerContext([
  37. {
  38. organization,
  39. },
  40. ]);
  41. describe('as render prop', function () {
  42. const childrenMock = jest.fn().mockReturnValue(null);
  43. beforeEach(function () {
  44. childrenMock.mockClear();
  45. });
  46. it('has a sufficient role', function () {
  47. render(<Role role="admin">{childrenMock}</Role>, {context: routerContext});
  48. expect(childrenMock).toHaveBeenCalledWith({
  49. hasRole: true,
  50. });
  51. });
  52. it('has an insufficient role', function () {
  53. render(<Role role="manager">{childrenMock}</Role>, {
  54. context: routerContext,
  55. });
  56. expect(childrenMock).toHaveBeenCalledWith({
  57. hasRole: false,
  58. });
  59. });
  60. it('gives access to a superuser with insufficient role', function () {
  61. ConfigStore.config.user = TestStubs.User({isSuperuser: true});
  62. Cookies.set = jest.fn();
  63. render(<Role role="owner">{childrenMock}</Role>, {context: routerContext});
  64. expect(childrenMock).toHaveBeenCalledWith({
  65. hasRole: true,
  66. });
  67. expect(Cookies.set).toHaveBeenCalledWith(
  68. 'su-test-cookie',
  69. 'set-in-isActiveSuperuser',
  70. {domain: '.sentry.io'}
  71. );
  72. ConfigStore.config.user = TestStubs.User({isSuperuser: false});
  73. });
  74. it('does not give access to a made up role', function () {
  75. render(<Role role="abcdefg">{childrenMock}</Role>, {
  76. context: routerContext,
  77. });
  78. expect(childrenMock).toHaveBeenCalledWith({
  79. hasRole: false,
  80. });
  81. });
  82. it('handles no user', function () {
  83. const user = {...ConfigStore.config.user};
  84. ConfigStore.config.user = undefined as any;
  85. render(<Role role="member">{childrenMock}</Role>, {context: routerContext});
  86. expect(childrenMock).toHaveBeenCalledWith({
  87. hasRole: false,
  88. });
  89. ConfigStore.config.user = user;
  90. });
  91. it('updates if user changes', function () {
  92. const user = {...ConfigStore.config.user};
  93. ConfigStore.config.user = undefined as any;
  94. const {rerender} = render(<Role role="member">{childrenMock}</Role>, {
  95. context: routerContext,
  96. });
  97. expect(childrenMock).toHaveBeenCalledWith({
  98. hasRole: false,
  99. });
  100. ConfigStore.config.user = user;
  101. rerender(<Role role="member">{childrenMock}</Role>);
  102. expect(childrenMock).toHaveBeenCalledWith({
  103. hasRole: true,
  104. });
  105. });
  106. it('handles no organization.orgRoleList', function () {
  107. render(
  108. <Role role="member" organization={{...organization, orgRoleList: []}}>
  109. {childrenMock}
  110. </Role>,
  111. {context: routerContext}
  112. );
  113. expect(childrenMock).toHaveBeenCalledWith({
  114. hasRole: false,
  115. });
  116. });
  117. });
  118. describe('as React node', function () {
  119. it('has a sufficient role', function () {
  120. render(
  121. <Role role="member">
  122. <div>The Child</div>
  123. </Role>,
  124. {context: routerContext}
  125. );
  126. expect(screen.getByText('The Child')).toBeInTheDocument();
  127. });
  128. it('has an insufficient role', function () {
  129. render(
  130. <Role role="owner">
  131. <div>The Child</div>
  132. </Role>,
  133. {context: routerContext}
  134. );
  135. expect(screen.queryByText('The Child')).not.toBeInTheDocument();
  136. });
  137. });
  138. });