index.spec.tsx 9.4 KB

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