acceptOrganizationInvite.spec.jsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import {browserHistory} from 'react-router';
  2. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  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. const getJoinButton = () =>
  13. screen.queryByRole('button', {name: 'Join the test-org organization'});
  14. describe('AcceptOrganizationInvite', function () {
  15. it('can accept invitation', async function () {
  16. addMock({
  17. orgSlug: 'test-org',
  18. needsAuthentication: false,
  19. needs2fa: false,
  20. hasAuthProvider: false,
  21. requireSso: false,
  22. existingMember: false,
  23. });
  24. render(<AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />);
  25. const acceptMock = MockApiClient.addMockResponse({
  26. url: '/accept-invite/1/abc/',
  27. method: 'POST',
  28. });
  29. const joinButton = getJoinButton();
  30. userEvent.click(joinButton);
  31. expect(acceptMock).toHaveBeenCalled();
  32. expect(joinButton).toBeDisabled();
  33. await waitFor(() =>
  34. expect(browserHistory.replace).toHaveBeenCalledWith('/test-org/')
  35. );
  36. });
  37. it('requires authentication to join', function () {
  38. addMock({
  39. orgSlug: 'test-org',
  40. needsAuthentication: true,
  41. needs2fa: false,
  42. hasAuthProvider: false,
  43. requireSso: false,
  44. existingMember: false,
  45. });
  46. render(<AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />);
  47. expect(getJoinButton()).not.toBeInTheDocument();
  48. expect(screen.getByTestId('action-info-general')).toBeInTheDocument();
  49. expect(screen.queryByTestId('action-info-sso')).not.toBeInTheDocument();
  50. expect(
  51. screen.getByRole('button', {name: 'Create a new account'})
  52. ).toBeInTheDocument();
  53. expect(
  54. screen.getByRole('link', {name: 'Login using an existing account'})
  55. ).toBeInTheDocument();
  56. });
  57. it('suggests sso authentication to login', function () {
  58. addMock({
  59. orgSlug: 'test-org',
  60. needsAuthentication: true,
  61. needs2fa: false,
  62. hasAuthProvider: true,
  63. requireSso: false,
  64. existingMember: false,
  65. ssoProvider: 'SSO',
  66. });
  67. render(<AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />);
  68. expect(getJoinButton()).not.toBeInTheDocument();
  69. expect(screen.getByTestId('action-info-general')).toBeInTheDocument();
  70. expect(screen.getByTestId('action-info-sso')).toBeInTheDocument();
  71. expect(screen.getByRole('button', {name: 'Join with SSO'})).toBeInTheDocument();
  72. expect(
  73. screen.getByRole('button', {name: 'Create a new account'})
  74. ).toBeInTheDocument();
  75. expect(
  76. screen.getByRole('link', {name: 'Login using an existing account'})
  77. ).toBeInTheDocument();
  78. });
  79. it('enforce required sso authentication', function () {
  80. addMock({
  81. orgSlug: 'test-org',
  82. needsAuthentication: true,
  83. needs2fa: false,
  84. hasAuthProvider: true,
  85. requireSso: true,
  86. existingMember: false,
  87. ssoProvider: 'SSO',
  88. });
  89. render(<AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />);
  90. expect(getJoinButton()).not.toBeInTheDocument();
  91. expect(screen.queryByTestId('action-info-general')).not.toBeInTheDocument();
  92. expect(screen.getByTestId('action-info-sso')).toBeInTheDocument();
  93. expect(screen.getByRole('button', {name: 'Join with SSO'})).toBeInTheDocument();
  94. expect(
  95. screen.queryByRole('button', {name: 'Create a new account'})
  96. ).not.toBeInTheDocument();
  97. expect(
  98. screen.queryByRole('link', {name: 'Login using an existing account'})
  99. ).not.toBeInTheDocument();
  100. });
  101. it('enforce required sso authentication for logged in users', function () {
  102. addMock({
  103. orgSlug: 'test-org',
  104. needsAuthentication: false,
  105. needs2fa: false,
  106. hasAuthProvider: true,
  107. requireSso: true,
  108. existingMember: false,
  109. ssoProvider: 'SSO',
  110. });
  111. render(<AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />);
  112. expect(getJoinButton()).not.toBeInTheDocument();
  113. expect(screen.queryByTestId('action-info-general')).not.toBeInTheDocument();
  114. expect(screen.getByTestId('action-info-sso')).toBeInTheDocument();
  115. expect(screen.getByRole('button', {name: 'Join with SSO'})).toBeInTheDocument();
  116. expect(
  117. screen.queryByRole('button', {name: 'Create a new account'})
  118. ).not.toBeInTheDocument();
  119. expect(
  120. screen.queryByRole('link', {name: 'Login using an existing account'})
  121. ).not.toBeInTheDocument();
  122. });
  123. it('show logout button for logged in users w/ sso and membership', async function () {
  124. addMock({
  125. orgSlug: 'test-org',
  126. needsAuthentication: false,
  127. needs2fa: false,
  128. hasAuthProvider: true,
  129. requireSso: true,
  130. existingMember: true,
  131. ssoProvider: 'SSO',
  132. });
  133. render(<AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />);
  134. expect(screen.getByTestId('existing-member')).toBeInTheDocument();
  135. const {replace} = window.location;
  136. window.location.replace = jest.fn();
  137. userEvent.click(screen.getByTestId('existing-member-link'));
  138. expect(logout).toHaveBeenCalled();
  139. await waitFor(() => expect(window.location.replace).toHaveBeenCalled());
  140. window.location.replace = replace;
  141. });
  142. it('shows right options for logged in user and optional SSO', function () {
  143. addMock({
  144. orgSlug: 'test-org',
  145. needsAuthentication: false,
  146. needs2fa: false,
  147. hasAuthProvider: true,
  148. requireSso: false,
  149. existingMember: false,
  150. ssoProvider: 'SSO',
  151. });
  152. render(<AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />);
  153. expect(screen.getByTestId('action-info-sso')).toBeInTheDocument();
  154. expect(getJoinButton()).toBeInTheDocument();
  155. });
  156. it('shows a logout button for existing members', async function () {
  157. addMock({
  158. orgSlug: 'test-org',
  159. needsAuthentication: false,
  160. needs2fa: false,
  161. hasAuthProvider: false,
  162. requireSso: false,
  163. existingMember: true,
  164. });
  165. render(<AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />);
  166. expect(screen.getByTestId('existing-member')).toBeInTheDocument();
  167. const {replace} = window.location;
  168. window.location.replace = jest.fn();
  169. userEvent.click(screen.getByTestId('existing-member-link'));
  170. expect(logout).toHaveBeenCalled();
  171. await waitFor(() => expect(window.location.replace).toHaveBeenCalled());
  172. window.location.replace = replace;
  173. });
  174. it('shows 2fa warning', function () {
  175. addMock({
  176. orgSlug: 'test-org',
  177. needsAuthentication: false,
  178. needs2fa: true,
  179. hasAuthProvider: false,
  180. requireSso: false,
  181. existingMember: false,
  182. });
  183. render(<AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />);
  184. expect(
  185. screen.getByRole('button', {name: 'Configure Two-Factor Auth'})
  186. ).toBeInTheDocument();
  187. });
  188. });