role.spec.tsx 4.0 KB

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