index.spec.tsx 8.9 KB

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