acceptOrganizationInvite.spec.jsx 7.1 KB

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