acceptOrganizationInvite.spec.jsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import {browserHistory} from 'react-router';
  2. import {mountWithTheme} from 'sentry-test/enzyme';
  3. import {logout} from 'sentry/actionCreators/account';
  4. import AcceptOrganizationInvite from 'sentry/views/acceptOrganizationInvite';
  5. jest.mock('sentry/actionCreators/account');
  6. const addMock = body =>
  7. MockApiClient.addMockResponse({
  8. url: '/accept-invite/1/abc/',
  9. method: 'GET',
  10. body,
  11. });
  12. describe('AcceptOrganizationInvite', function () {
  13. it('can accept invitation', async function () {
  14. addMock({
  15. orgSlug: 'test-org',
  16. needsAuthentication: false,
  17. needs2fa: false,
  18. hasAuthProvider: false,
  19. requireSso: false,
  20. existingMember: false,
  21. });
  22. const wrapper = mountWithTheme(
  23. <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />
  24. );
  25. const joinButton = wrapper.find('Button[aria-label="join-organization"]');
  26. expect(joinButton.exists()).toBe(true);
  27. expect(joinButton.text()).toBe('Join the test-org organization');
  28. const acceptMock = MockApiClient.addMockResponse({
  29. url: '/accept-invite/1/abc/',
  30. method: 'POST',
  31. });
  32. joinButton.simulate('click');
  33. expect(acceptMock).toHaveBeenCalled();
  34. expect(wrapper.find('Button[aria-label="join-organization"]').props().disabled).toBe(
  35. true
  36. );
  37. await tick();
  38. expect(browserHistory.replace).toHaveBeenCalledWith('/test-org/');
  39. });
  40. it('requires authentication to join', function () {
  41. addMock({
  42. orgSlug: 'test-org',
  43. needsAuthentication: true,
  44. needs2fa: false,
  45. hasAuthProvider: false,
  46. requireSso: false,
  47. existingMember: false,
  48. });
  49. const wrapper = mountWithTheme(
  50. <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />
  51. );
  52. const joinButton = wrapper.find('Button[aria-label="join-organization"]');
  53. expect(joinButton.exists()).toBe(false);
  54. expect(wrapper.find('[data-test-id="action-info-general"]').exists()).toBe(true);
  55. expect(wrapper.find('[data-test-id="action-info-sso"]').exists()).toBe(false);
  56. expect(wrapper.find('Button[aria-label="sso-login"]').exists()).toBe(false);
  57. expect(wrapper.find('Button[aria-label="create-account"]').exists()).toBe(true);
  58. expect(wrapper.find('[data-test-id="link-with-existing"]').exists()).toBe(true);
  59. });
  60. it('suggests sso authentication to login', function () {
  61. addMock({
  62. orgSlug: 'test-org',
  63. needsAuthentication: true,
  64. needs2fa: false,
  65. hasAuthProvider: true,
  66. requireSso: false,
  67. existingMember: false,
  68. });
  69. const wrapper = mountWithTheme(
  70. <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />
  71. );
  72. const joinButton = wrapper.find('Button[aria-label="join-organization"]');
  73. expect(joinButton.exists()).toBe(false);
  74. expect(wrapper.find('[data-test-id="action-info-general"]').exists()).toBe(true);
  75. expect(wrapper.find('[data-test-id="action-info-sso"]').exists()).toBe(true);
  76. expect(wrapper.find('Button[aria-label="sso-login"]').exists()).toBe(true);
  77. expect(wrapper.find('Button[aria-label="create-account"]').exists()).toBe(true);
  78. expect(wrapper.find('[data-test-id="link-with-existing"]').exists()).toBe(true);
  79. });
  80. it('enforce required sso authentication', function () {
  81. addMock({
  82. orgSlug: 'test-org',
  83. needsAuthentication: true,
  84. needs2fa: false,
  85. hasAuthProvider: true,
  86. requireSso: true,
  87. existingMember: false,
  88. });
  89. const wrapper = mountWithTheme(
  90. <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />
  91. );
  92. const joinButton = wrapper.find('Button[aria-label="join-organization"]');
  93. expect(joinButton.exists()).toBe(false);
  94. expect(wrapper.find('[data-test-id="action-info-general"]').exists()).toBe(false);
  95. expect(wrapper.find('[data-test-id="action-info-sso"]').exists()).toBe(true);
  96. expect(wrapper.find('Button[aria-label="sso-login"]').exists()).toBe(true);
  97. expect(wrapper.find('Button[aria-label="create-account"]').exists()).toBe(false);
  98. expect(wrapper.find('[data-test-id="link-with-existing"]').exists()).toBe(false);
  99. });
  100. it('enforce required sso authentication for logged in users', function () {
  101. addMock({
  102. orgSlug: 'test-org',
  103. needsAuthentication: false,
  104. needs2fa: false,
  105. hasAuthProvider: true,
  106. requireSso: true,
  107. existingMember: false,
  108. });
  109. const wrapper = mountWithTheme(
  110. <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />
  111. );
  112. const joinButton = wrapper.find('Button[aria-label="join-organization"]');
  113. expect(joinButton.exists()).toBe(false);
  114. expect(wrapper.find('[data-test-id="action-info-general"]').exists()).toBe(false);
  115. expect(wrapper.find('[data-test-id="action-info-sso"]').exists()).toBe(true);
  116. expect(wrapper.find('Button[aria-label="sso-login"]').exists()).toBe(true);
  117. expect(wrapper.find('Button[aria-label="create-account"]').exists()).toBe(false);
  118. expect(wrapper.find('[data-test-id="link-with-existing"]').exists()).toBe(false);
  119. });
  120. it('show logout button for logged in users w/ sso and membership', async function () {
  121. addMock({
  122. orgSlug: 'test-org',
  123. needsAuthentication: false,
  124. needs2fa: false,
  125. hasAuthProvider: true,
  126. requireSso: true,
  127. existingMember: true,
  128. });
  129. const wrapper = mountWithTheme(
  130. <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />
  131. );
  132. const existingMember = wrapper.find('[data-test-id="existing-member"]');
  133. expect(existingMember.exists()).toBe(true);
  134. const {replace} = window.location;
  135. window.location.replace = jest.fn();
  136. existingMember
  137. .find('[data-test-id="existing-member-link"]')
  138. .hostNodes()
  139. .simulate('click');
  140. expect(logout).toHaveBeenCalled();
  141. await tick();
  142. expect(window.location.replace).toHaveBeenCalled();
  143. window.location.replace = replace;
  144. });
  145. it('shows right options for logged in user and optional SSO', function () {
  146. addMock({
  147. orgSlug: 'test-org',
  148. needsAuthentication: false,
  149. needs2fa: false,
  150. hasAuthProvider: true,
  151. requireSso: false,
  152. existingMember: false,
  153. });
  154. const wrapper = mountWithTheme(
  155. <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />
  156. );
  157. const ssoLink = wrapper.find('[data-test-id="action-info-sso"]');
  158. expect(ssoLink.exists()).toBe(true);
  159. const joinButton = wrapper.find('Button[aria-label="join-organization"]');
  160. expect(joinButton.exists()).toBe(true);
  161. });
  162. it('shows a logout button for existing members', async function () {
  163. addMock({
  164. orgSlug: 'test-org',
  165. needsAuthentication: false,
  166. needs2fa: false,
  167. hasAuthProvider: false,
  168. requireSso: false,
  169. existingMember: true,
  170. });
  171. const wrapper = mountWithTheme(
  172. <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />
  173. );
  174. const existingMember = wrapper.find('[data-test-id="existing-member"]');
  175. expect(existingMember.exists()).toBe(true);
  176. const {replace} = window.location;
  177. window.location.replace = jest.fn();
  178. existingMember
  179. .find('[data-test-id="existing-member-link"]')
  180. .hostNodes()
  181. .simulate('click');
  182. expect(logout).toHaveBeenCalled();
  183. await tick();
  184. expect(window.location.replace).toHaveBeenCalled();
  185. window.location.replace = replace;
  186. });
  187. it('shows 2fa warning', function () {
  188. addMock({
  189. orgSlug: 'test-org',
  190. needsAuthentication: false,
  191. needs2fa: true,
  192. hasAuthProvider: false,
  193. requireSso: false,
  194. existingMember: false,
  195. });
  196. const wrapper = mountWithTheme(
  197. <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />
  198. );
  199. expect(wrapper.find('[data-test-id="2fa-warning"]').exists()).toBe(true);
  200. });
  201. });