acceptOrganizationInvite.spec.jsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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/org-slug/1/abc/',
  9. method: 'GET',
  10. body,
  11. });
  12. const getJoinButton = () =>
  13. screen.queryByRole('button', {name: 'Join the org-slug organization'});
  14. describe('AcceptOrganizationInvite', function () {
  15. const organization = TestStubs.Organization({slug: 'org-slug'});
  16. const initialData = window.__initialData;
  17. afterEach(() => {
  18. window.__initialData = initialData;
  19. });
  20. it('can accept invitation', async function () {
  21. addMock({
  22. orgSlug: organization.slug,
  23. needsAuthentication: false,
  24. needs2fa: false,
  25. hasAuthProvider: false,
  26. requireSso: false,
  27. existingMember: false,
  28. });
  29. render(
  30. <AcceptOrganizationInvite
  31. params={{orgId: 'org-slug', memberId: '1', token: 'abc'}}
  32. />
  33. );
  34. const acceptMock = MockApiClient.addMockResponse({
  35. url: '/accept-invite/org-slug/1/abc/',
  36. method: 'POST',
  37. });
  38. const joinButton = getJoinButton();
  39. userEvent.click(joinButton);
  40. expect(acceptMock).toHaveBeenCalled();
  41. expect(joinButton).toBeDisabled();
  42. await waitFor(() =>
  43. expect(browserHistory.replace).toHaveBeenCalledWith('/org-slug/')
  44. );
  45. });
  46. it('can accept invitation on customer-domains', async function () {
  47. window.__initialData = {
  48. customerDomain: {
  49. subdomain: 'org-slug',
  50. organizationUrl: 'https://org-slug.sentry.io',
  51. sentryUrl: 'https://sentry.io',
  52. },
  53. links: {
  54. sentryUrl: 'https://sentry.io',
  55. },
  56. };
  57. addMock({
  58. orgSlug: organization.slug,
  59. needsAuthentication: false,
  60. needs2fa: false,
  61. hasAuthProvider: false,
  62. requireSso: false,
  63. existingMember: false,
  64. });
  65. render(<AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />);
  66. const acceptMock = MockApiClient.addMockResponse({
  67. url: '/accept-invite/org-slug/1/abc/',
  68. method: 'POST',
  69. });
  70. const joinButton = getJoinButton();
  71. userEvent.click(joinButton);
  72. expect(acceptMock).toHaveBeenCalled();
  73. expect(joinButton).toBeDisabled();
  74. await waitFor(() =>
  75. expect(browserHistory.replace).toHaveBeenCalledWith('/org-slug/')
  76. );
  77. });
  78. it('requires authentication to join', function () {
  79. addMock({
  80. orgSlug: organization.slug,
  81. needsAuthentication: true,
  82. needs2fa: false,
  83. hasAuthProvider: false,
  84. requireSso: false,
  85. existingMember: false,
  86. });
  87. render(
  88. <AcceptOrganizationInvite
  89. params={{orgId: 'org-slug', memberId: '1', token: 'abc'}}
  90. />
  91. );
  92. expect(getJoinButton()).not.toBeInTheDocument();
  93. expect(screen.getByTestId('action-info-general')).toBeInTheDocument();
  94. expect(screen.queryByTestId('action-info-sso')).not.toBeInTheDocument();
  95. expect(
  96. screen.getByRole('button', {name: 'Create a new account'})
  97. ).toBeInTheDocument();
  98. expect(
  99. screen.getByRole('link', {name: 'Login using an existing account'})
  100. ).toBeInTheDocument();
  101. });
  102. it('suggests sso authentication to login', function () {
  103. addMock({
  104. orgSlug: organization.slug,
  105. needsAuthentication: true,
  106. needs2fa: false,
  107. hasAuthProvider: true,
  108. requireSso: false,
  109. existingMember: false,
  110. ssoProvider: 'SSO',
  111. });
  112. render(
  113. <AcceptOrganizationInvite
  114. params={{orgId: 'org-slug', memberId: '1', token: 'abc'}}
  115. />
  116. );
  117. expect(getJoinButton()).not.toBeInTheDocument();
  118. expect(screen.getByTestId('action-info-general')).toBeInTheDocument();
  119. expect(screen.getByTestId('action-info-sso')).toBeInTheDocument();
  120. expect(screen.getByRole('button', {name: 'Join with SSO'})).toBeInTheDocument();
  121. expect(
  122. screen.getByRole('button', {name: 'Create a new account'})
  123. ).toBeInTheDocument();
  124. expect(
  125. screen.getByRole('link', {name: 'Login using an existing account'})
  126. ).toBeInTheDocument();
  127. });
  128. it('enforce required sso authentication', function () {
  129. addMock({
  130. orgSlug: organization.slug,
  131. needsAuthentication: true,
  132. needs2fa: false,
  133. hasAuthProvider: true,
  134. requireSso: true,
  135. existingMember: false,
  136. ssoProvider: 'SSO',
  137. });
  138. render(
  139. <AcceptOrganizationInvite
  140. params={{orgId: 'org-slug', memberId: '1', token: 'abc'}}
  141. />
  142. );
  143. expect(getJoinButton()).not.toBeInTheDocument();
  144. expect(screen.queryByTestId('action-info-general')).not.toBeInTheDocument();
  145. expect(screen.getByTestId('action-info-sso')).toBeInTheDocument();
  146. expect(screen.getByRole('button', {name: 'Join with SSO'})).toBeInTheDocument();
  147. expect(
  148. screen.queryByRole('button', {name: 'Create a new account'})
  149. ).not.toBeInTheDocument();
  150. expect(
  151. screen.queryByRole('link', {name: 'Login using an existing account'})
  152. ).not.toBeInTheDocument();
  153. });
  154. it('enforce required sso authentication for logged in users', function () {
  155. addMock({
  156. orgSlug: organization.slug,
  157. needsAuthentication: false,
  158. needs2fa: false,
  159. hasAuthProvider: true,
  160. requireSso: true,
  161. existingMember: false,
  162. ssoProvider: 'SSO',
  163. });
  164. render(
  165. <AcceptOrganizationInvite
  166. params={{orgId: 'org-slug', memberId: '1', token: 'abc'}}
  167. />
  168. );
  169. expect(getJoinButton()).not.toBeInTheDocument();
  170. expect(screen.queryByTestId('action-info-general')).not.toBeInTheDocument();
  171. expect(screen.getByTestId('action-info-sso')).toBeInTheDocument();
  172. expect(screen.getByRole('button', {name: 'Join with SSO'})).toBeInTheDocument();
  173. expect(
  174. screen.queryByRole('button', {name: 'Create a new account'})
  175. ).not.toBeInTheDocument();
  176. expect(
  177. screen.queryByRole('link', {name: 'Login using an existing account'})
  178. ).not.toBeInTheDocument();
  179. });
  180. it('show logout button for logged in users w/ sso and membership', async function () {
  181. addMock({
  182. orgSlug: organization.slug,
  183. needsAuthentication: false,
  184. needs2fa: false,
  185. hasAuthProvider: true,
  186. requireSso: true,
  187. existingMember: true,
  188. ssoProvider: 'SSO',
  189. });
  190. render(
  191. <AcceptOrganizationInvite
  192. params={{orgId: 'org-slug', memberId: '1', 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 right options for logged in user and optional SSO', function () {
  201. addMock({
  202. orgSlug: organization.slug,
  203. needsAuthentication: false,
  204. needs2fa: false,
  205. hasAuthProvider: true,
  206. requireSso: false,
  207. existingMember: false,
  208. ssoProvider: 'SSO',
  209. });
  210. render(
  211. <AcceptOrganizationInvite
  212. params={{orgId: 'org-slug', memberId: '1', token: 'abc'}}
  213. />
  214. );
  215. expect(screen.getByTestId('action-info-sso')).toBeInTheDocument();
  216. expect(getJoinButton()).toBeInTheDocument();
  217. });
  218. it('shows a logout button for existing members', async function () {
  219. addMock({
  220. orgSlug: organization.slug,
  221. needsAuthentication: false,
  222. needs2fa: false,
  223. hasAuthProvider: false,
  224. requireSso: false,
  225. existingMember: true,
  226. });
  227. render(
  228. <AcceptOrganizationInvite
  229. params={{orgId: 'org-slug', memberId: '1', token: 'abc'}}
  230. />
  231. );
  232. expect(screen.getByTestId('existing-member')).toBeInTheDocument();
  233. userEvent.click(screen.getByTestId('existing-member-link'));
  234. expect(logout).toHaveBeenCalled();
  235. await waitFor(() => expect(window.location.replace).toHaveBeenCalled());
  236. });
  237. it('shows 2fa warning', function () {
  238. addMock({
  239. orgSlug: organization.slug,
  240. needsAuthentication: false,
  241. needs2fa: true,
  242. hasAuthProvider: false,
  243. requireSso: false,
  244. existingMember: false,
  245. });
  246. render(
  247. <AcceptOrganizationInvite
  248. params={{orgId: 'org-slug', memberId: '1', token: 'abc'}}
  249. />
  250. );
  251. expect(
  252. screen.getByRole('button', {name: 'Configure Two-Factor Auth'})
  253. ).toBeInTheDocument();
  254. });
  255. });