access.spec.jsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import React from 'react';
  2. import {mount} from 'enzyme';
  3. import Access from 'app/components/acl/access';
  4. import ConfigStore from 'app/stores/configStore';
  5. describe('Access', function() {
  6. const organization = TestStubs.Organization({
  7. access: ['project:write', 'project:read'],
  8. });
  9. const routerContext = TestStubs.routerContext([{organization}]);
  10. describe('as render prop', function() {
  11. const childrenMock = jest.fn().mockReturnValue(null);
  12. beforeEach(function() {
  13. childrenMock.mockClear();
  14. });
  15. it('has access when requireAll is false', function() {
  16. mount(
  17. <Access access={['project:write', 'project:read', 'org:read']} requireAll={false}>
  18. {childrenMock}
  19. </Access>,
  20. routerContext
  21. );
  22. expect(childrenMock).toHaveBeenCalledWith({
  23. hasAccess: true,
  24. hasSuperuser: false,
  25. });
  26. });
  27. it('has accesss', function() {
  28. mount(
  29. <Access access={['project:write', 'project:read']}>{childrenMock}</Access>,
  30. routerContext
  31. );
  32. expect(childrenMock).toHaveBeenCalledWith({
  33. hasAccess: true,
  34. hasSuperuser: false,
  35. });
  36. });
  37. it('has no access', function() {
  38. mount(<Access access={['org:write']}>{childrenMock}</Access>, routerContext);
  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. mount(
  47. <Access access={['org:write']} renderNoAccessMessage={noAccessRenderer}>
  48. {childrenMock}
  49. </Access>,
  50. routerContext
  51. );
  52. expect(childrenMock).not.toHaveBeenCalled();
  53. expect(noAccessRenderer).toHaveBeenCalled();
  54. });
  55. it('can specify org from props', function() {
  56. mount(
  57. <Access
  58. organization={TestStubs.Organization({access: ['org:write']})}
  59. access={['org:write']}
  60. >
  61. {childrenMock}
  62. </Access>,
  63. routerContext
  64. );
  65. expect(childrenMock).toHaveBeenCalledWith({
  66. hasAccess: true,
  67. hasSuperuser: false,
  68. });
  69. });
  70. it('handles no org/project', function() {
  71. mount(
  72. <Access organization={null} project={null} access={['org:write']}>
  73. {childrenMock}
  74. </Access>,
  75. routerContext
  76. );
  77. expect(childrenMock).toHaveBeenCalledWith({
  78. hasAccess: false,
  79. hasSuperuser: false,
  80. });
  81. });
  82. it('is superuser', function() {
  83. ConfigStore.config = {
  84. user: {isSuperuser: true},
  85. };
  86. mount(<Access isSuperuser>{childrenMock}</Access>, routerContext);
  87. expect(childrenMock).toHaveBeenCalledWith({
  88. hasAccess: true,
  89. hasSuperuser: true,
  90. });
  91. });
  92. it('is not superuser', function() {
  93. ConfigStore.config = {
  94. user: {isSuperuser: false},
  95. };
  96. mount(<Access isSuperuser>{childrenMock}</Access>, routerContext);
  97. expect(childrenMock).toHaveBeenCalledWith({
  98. hasAccess: true,
  99. hasSuperuser: false,
  100. });
  101. });
  102. });
  103. describe('as React node', function() {
  104. let wrapper;
  105. it('has access', function() {
  106. wrapper = mount(
  107. <Access access={['project:write']}>
  108. <div>The Child</div>
  109. </Access>,
  110. routerContext
  111. );
  112. expect(wrapper.find('Access div').text()).toBe('The Child');
  113. });
  114. it('has superuser', function() {
  115. ConfigStore.config = {
  116. user: {isSuperuser: true},
  117. };
  118. wrapper = mount(
  119. <Access isSuperuser>
  120. <div>The Child</div>
  121. </Access>,
  122. routerContext
  123. );
  124. expect(wrapper.find('Access div').text()).toBe('The Child');
  125. });
  126. it('has no access', function() {
  127. wrapper = mount(
  128. <Access access={['org:write']}>
  129. <div>The Child</div>
  130. </Access>,
  131. routerContext
  132. );
  133. expect(wrapper.find('Access div')).toHaveLength(0);
  134. });
  135. it('has no superuser', function() {
  136. ConfigStore.config = {
  137. user: {isSuperuser: false},
  138. };
  139. wrapper = mount(
  140. <Access isSuperuser>
  141. <div>The Child</div>
  142. </Access>,
  143. routerContext
  144. );
  145. expect(wrapper.find('Access div')).toHaveLength(0);
  146. });
  147. });
  148. });