index.spec.tsx 8.8 KB

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