access.spec.jsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import {Organization} from 'fixtures/js-stubs/organization';
  2. import {routerContext} from 'fixtures/js-stubs/routerContext';
  3. import {render, screen} from 'sentry-test/reactTestingLibrary';
  4. import Access from 'sentry/components/acl/access';
  5. import ConfigStore from 'sentry/stores/configStore';
  6. describe('Access', function () {
  7. const organization = Organization({
  8. access: ['project:write', 'project:read'],
  9. });
  10. const routerContext = routerContext([{organization}]);
  11. describe('as render prop', function () {
  12. const childrenMock = jest.fn().mockReturnValue(null);
  13. beforeEach(function () {
  14. childrenMock.mockClear();
  15. });
  16. it('has access when requireAll is false', function () {
  17. render(
  18. <Access access={['project:write', 'project:read', 'org:read']} requireAll={false}>
  19. {childrenMock}
  20. </Access>,
  21. {context: routerContext}
  22. );
  23. expect(childrenMock).toHaveBeenCalledWith({
  24. hasAccess: true,
  25. hasSuperuser: false,
  26. });
  27. });
  28. it('has access', function () {
  29. render(<Access access={['project:write', 'project:read']}>{childrenMock}</Access>, {
  30. context: routerContext,
  31. });
  32. expect(childrenMock).toHaveBeenCalledWith({
  33. hasAccess: true,
  34. hasSuperuser: false,
  35. });
  36. });
  37. it('has no access', function () {
  38. render(<Access access={['org:write']}>{childrenMock}</Access>, {
  39. context: routerContext,
  40. });
  41. expect(childrenMock).toHaveBeenCalledWith({
  42. hasAccess: false,
  43. hasSuperuser: false,
  44. });
  45. });
  46. it('calls render function when no access', function () {
  47. const noAccessRenderer = jest.fn(() => null);
  48. render(
  49. <Access access={['org:write']} renderNoAccessMessage={noAccessRenderer}>
  50. {childrenMock}
  51. </Access>,
  52. {context: routerContext}
  53. );
  54. expect(childrenMock).not.toHaveBeenCalled();
  55. expect(noAccessRenderer).toHaveBeenCalled();
  56. });
  57. it('can specify org from props', function () {
  58. render(
  59. <Access
  60. organization={Organization({access: ['org:write']})}
  61. access={['org:write']}
  62. >
  63. {childrenMock}
  64. </Access>,
  65. {context: routerContext}
  66. );
  67. expect(childrenMock).toHaveBeenCalledWith({
  68. hasAccess: true,
  69. hasSuperuser: false,
  70. });
  71. });
  72. it('handles no org/project', function () {
  73. render(<Access access={['org:write']}>{childrenMock}</Access>, {
  74. context: routerContext,
  75. });
  76. expect(childrenMock).toHaveBeenCalledWith(
  77. expect.objectContaining({
  78. hasAccess: false,
  79. hasSuperuser: false,
  80. })
  81. );
  82. });
  83. it('handles no user', function () {
  84. // Regression test for the share sheet.
  85. ConfigStore.config = {
  86. user: null,
  87. };
  88. render(<Access>{childrenMock}</Access>, {context: routerContext});
  89. expect(childrenMock).toHaveBeenCalledWith({
  90. hasAccess: true,
  91. hasSuperuser: false,
  92. });
  93. });
  94. it('is superuser', function () {
  95. ConfigStore.config = {
  96. user: {isSuperuser: true},
  97. };
  98. render(<Access isSuperuser>{childrenMock}</Access>, {
  99. context: routerContext,
  100. });
  101. expect(childrenMock).toHaveBeenCalledWith({
  102. hasAccess: true,
  103. hasSuperuser: true,
  104. });
  105. });
  106. it('is not superuser', function () {
  107. ConfigStore.config = {
  108. user: {isSuperuser: false},
  109. };
  110. render(<Access isSuperuser>{childrenMock}</Access>, {
  111. context: routerContext,
  112. });
  113. expect(childrenMock).toHaveBeenCalledWith({
  114. hasAccess: true,
  115. hasSuperuser: false,
  116. });
  117. });
  118. });
  119. describe('as React node', function () {
  120. it('has access', function () {
  121. render(
  122. <Access access={['project:write']}>
  123. <p>The Child</p>
  124. </Access>,
  125. {context: routerContext}
  126. );
  127. expect(screen.getByText('The Child')).toBeInTheDocument();
  128. });
  129. it('has superuser', function () {
  130. ConfigStore.config = {
  131. user: {isSuperuser: true},
  132. };
  133. render(
  134. <Access isSuperuser>
  135. <p>The Child</p>
  136. </Access>,
  137. {context: routerContext}
  138. );
  139. expect(screen.getByText('The Child')).toBeInTheDocument();
  140. });
  141. it('has no access', function () {
  142. render(
  143. <Access access={['org:write']}>
  144. <p>The Child</p>
  145. </Access>,
  146. {context: routerContext}
  147. );
  148. expect(screen.queryByText('The Child')).not.toBeInTheDocument();
  149. });
  150. it('has no superuser', function () {
  151. ConfigStore.config = {
  152. user: {isSuperuser: false},
  153. };
  154. render(
  155. <Access isSuperuser>
  156. <p>The Child</p>
  157. </Access>,
  158. {context: routerContext}
  159. );
  160. expect(screen.queryByRole('The Child')).not.toBeInTheDocument();
  161. });
  162. });
  163. });