index.spec.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {RouteComponentPropsFixture} from 'sentry-fixture/routeComponentPropsFixture';
  3. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  4. import {logout} from 'sentry/actionCreators/account';
  5. import ConfigStore from 'sentry/stores/configStore';
  6. import {browserHistory} from 'sentry/utils/browserHistory';
  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 organization = OrganizationFixture({slug: 'org-slug'});
  23. const configState = ConfigStore.getState();
  24. afterEach(() => {
  25. ConfigStore.loadInitialData(configState);
  26. });
  27. it('can accept invitation', async function () {
  28. addMock({
  29. orgSlug: organization.slug,
  30. needsAuthentication: false,
  31. needs2fa: false,
  32. hasAuthProvider: false,
  33. requireSso: false,
  34. existingMember: false,
  35. });
  36. render(
  37. <AcceptOrganizationInvite
  38. {...RouteComponentPropsFixture()}
  39. params={{orgId: 'org-slug', memberId: '1', token: 'abc'}}
  40. />
  41. );
  42. const acceptMock = MockApiClient.addMockResponse({
  43. url: '/accept-invite/org-slug/1/abc/',
  44. method: 'POST',
  45. });
  46. const joinButton = getJoinButton();
  47. await userEvent.click(joinButton!);
  48. expect(acceptMock).toHaveBeenCalled();
  49. expect(browserHistory.replace).toHaveBeenCalledWith('/org-slug/');
  50. });
  51. it('can accept invitation on customer-domains', async function () {
  52. ConfigStore.set('customerDomain', {
  53. subdomain: 'org-slug',
  54. organizationUrl: 'https://org-slug.sentry.io',
  55. sentryUrl: 'https://sentry.io',
  56. });
  57. ConfigStore.set('links', {
  58. ...configState.links,
  59. sentryUrl: 'https://sentry.io',
  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. {...RouteComponentPropsFixture()}
  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. {...RouteComponentPropsFixture()}
  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. {...RouteComponentPropsFixture()}
  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. {...RouteComponentPropsFixture()}
  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. {...RouteComponentPropsFixture()}
  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. {...RouteComponentPropsFixture()}
  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. await waitFor(() => expect(logout).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. {...RouteComponentPropsFixture()}
  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. {...RouteComponentPropsFixture()}
  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. await waitFor(() => expect(logout).toHaveBeenCalled());
  247. });
  248. it('shows 2fa warning', function () {
  249. addMock({
  250. orgSlug: organization.slug,
  251. needsAuthentication: false,
  252. needs2fa: true,
  253. hasAuthProvider: false,
  254. requireSso: false,
  255. existingMember: false,
  256. });
  257. render(
  258. <AcceptOrganizationInvite
  259. {...RouteComponentPropsFixture()}
  260. params={{orgId: 'org-slug', memberId: '1', token: 'abc'}}
  261. />
  262. );
  263. expect(
  264. screen.getByRole('button', {name: 'Configure Two-Factor Auth'})
  265. ).toBeInTheDocument();
  266. });
  267. });