role.spec.tsx 4.1 KB

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