access.spec.jsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import Access from 'app/components/acl/access';
  3. import ConfigStore from 'app/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. mountWithTheme(
  16. <Access access={['project:write', 'project:read', 'org:read']} requireAll={false}>
  17. {childrenMock}
  18. </Access>,
  19. routerContext
  20. );
  21. expect(childrenMock).toHaveBeenCalledWith({
  22. hasAccess: true,
  23. hasSuperuser: false,
  24. });
  25. });
  26. it('has access', function () {
  27. mountWithTheme(
  28. <Access access={['project:write', 'project:read']}>{childrenMock}</Access>,
  29. routerContext
  30. );
  31. expect(childrenMock).toHaveBeenCalledWith({
  32. hasAccess: true,
  33. hasSuperuser: false,
  34. });
  35. });
  36. it('has no access', function () {
  37. mountWithTheme(
  38. <Access access={['org:write']}>{childrenMock}</Access>,
  39. 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. mountWithTheme(
  49. <Access access={['org:write']} renderNoAccessMessage={noAccessRenderer}>
  50. {childrenMock}
  51. </Access>,
  52. routerContext
  53. );
  54. expect(childrenMock).not.toHaveBeenCalled();
  55. expect(noAccessRenderer).toHaveBeenCalled();
  56. });
  57. it('can specify org from props', function () {
  58. mountWithTheme(
  59. <Access
  60. organization={TestStubs.Organization({access: ['org:write']})}
  61. access={['org:write']}
  62. >
  63. {childrenMock}
  64. </Access>,
  65. routerContext
  66. );
  67. expect(childrenMock).toHaveBeenCalledWith({
  68. hasAccess: true,
  69. hasSuperuser: false,
  70. });
  71. });
  72. it('handles no org/project', function () {
  73. mountWithTheme(
  74. <Access access={['org:write']}>{childrenMock}</Access>,
  75. routerContext
  76. );
  77. expect(childrenMock).toHaveBeenCalledWith(
  78. expect.objectContaining({
  79. hasAccess: false,
  80. hasSuperuser: false,
  81. })
  82. );
  83. });
  84. it('handles no user', function () {
  85. // Regression test for the share sheet.
  86. ConfigStore.config = {
  87. user: null,
  88. };
  89. mountWithTheme(<Access>{childrenMock}</Access>, routerContext);
  90. expect(childrenMock).toHaveBeenCalledWith({
  91. hasAccess: true,
  92. hasSuperuser: false,
  93. });
  94. });
  95. it('is superuser', function () {
  96. ConfigStore.config = {
  97. user: {isSuperuser: true},
  98. };
  99. mountWithTheme(<Access isSuperuser>{childrenMock}</Access>, routerContext);
  100. expect(childrenMock).toHaveBeenCalledWith({
  101. hasAccess: true,
  102. hasSuperuser: true,
  103. });
  104. });
  105. it('is not superuser', function () {
  106. ConfigStore.config = {
  107. user: {isSuperuser: false},
  108. };
  109. mountWithTheme(<Access isSuperuser>{childrenMock}</Access>, routerContext);
  110. expect(childrenMock).toHaveBeenCalledWith({
  111. hasAccess: true,
  112. hasSuperuser: false,
  113. });
  114. });
  115. });
  116. describe('as React node', function () {
  117. let wrapper;
  118. it('has access', function () {
  119. wrapper = mountWithTheme(
  120. <Access access={['project:write']}>
  121. <div>The Child</div>
  122. </Access>,
  123. routerContext
  124. );
  125. expect(wrapper.find('Access div').text()).toBe('The Child');
  126. });
  127. it('has superuser', function () {
  128. ConfigStore.config = {
  129. user: {isSuperuser: true},
  130. };
  131. wrapper = mountWithTheme(
  132. <Access isSuperuser>
  133. <div>The Child</div>
  134. </Access>,
  135. routerContext
  136. );
  137. expect(wrapper.find('Access div').text()).toBe('The Child');
  138. });
  139. it('has no access', function () {
  140. wrapper = mountWithTheme(
  141. <Access access={['org:write']}>
  142. <div>The Child</div>
  143. </Access>,
  144. routerContext
  145. );
  146. expect(wrapper.find('Access div')).toHaveLength(0);
  147. });
  148. it('has no superuser', function () {
  149. ConfigStore.config = {
  150. user: {isSuperuser: false},
  151. };
  152. wrapper = mountWithTheme(
  153. <Access isSuperuser>
  154. <div>The Child</div>
  155. </Access>,
  156. routerContext
  157. );
  158. expect(wrapper.find('Access div')).toHaveLength(0);
  159. });
  160. });
  161. });