index.spec.jsx 8.2 KB

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