index.spec.tsx 8.9 KB

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