access.spec.jsx 4.8 KB

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