index.spec.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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('renders error message', function () {
  85. MockApiClient.addMockResponse({
  86. url: '/accept-invite/1/abc/',
  87. method: 'GET',
  88. statusCode: 400,
  89. body: {detail: 'uh oh'},
  90. });
  91. render(
  92. <AcceptOrganizationInvite
  93. {...RouteComponentPropsFixture()}
  94. params={{memberId: '1', token: 'abc'}}
  95. />
  96. );
  97. expect(getJoinButton()).not.toBeInTheDocument();
  98. expect(
  99. screen.getByRole('link', {name: 'sign in with a different account'})
  100. ).toBeInTheDocument();
  101. });
  102. it('requires authentication to join', function () {
  103. addMock({
  104. orgSlug: organization.slug,
  105. needsAuthentication: true,
  106. needs2fa: false,
  107. hasAuthProvider: false,
  108. requireSso: false,
  109. existingMember: false,
  110. });
  111. render(
  112. <AcceptOrganizationInvite
  113. {...RouteComponentPropsFixture()}
  114. params={{orgId: 'org-slug', memberId: '1', token: 'abc'}}
  115. />
  116. );
  117. expect(getJoinButton()).not.toBeInTheDocument();
  118. expect(screen.getByTestId('action-info-general')).toBeInTheDocument();
  119. expect(screen.queryByTestId('action-info-sso')).not.toBeInTheDocument();
  120. expect(
  121. screen.getByRole('button', {name: 'Create a new account'})
  122. ).toBeInTheDocument();
  123. expect(
  124. screen.getByRole('link', {name: 'Login using an existing account'})
  125. ).toBeInTheDocument();
  126. });
  127. it('suggests sso authentication to login', function () {
  128. addMock({
  129. orgSlug: organization.slug,
  130. needsAuthentication: true,
  131. needs2fa: false,
  132. hasAuthProvider: true,
  133. requireSso: false,
  134. existingMember: false,
  135. ssoProvider: 'SSO',
  136. });
  137. render(
  138. <AcceptOrganizationInvite
  139. {...RouteComponentPropsFixture()}
  140. params={{orgId: 'org-slug', memberId: '1', token: 'abc'}}
  141. />
  142. );
  143. expect(getJoinButton()).not.toBeInTheDocument();
  144. expect(screen.getByTestId('action-info-general')).toBeInTheDocument();
  145. expect(screen.getByTestId('action-info-sso')).toBeInTheDocument();
  146. expect(screen.getByRole('button', {name: 'Join with SSO'})).toBeInTheDocument();
  147. expect(
  148. screen.getByRole('button', {name: 'Create a new account'})
  149. ).toBeInTheDocument();
  150. expect(
  151. screen.getByRole('link', {name: 'Login using an existing account'})
  152. ).toBeInTheDocument();
  153. });
  154. it('enforce required sso authentication', function () {
  155. addMock({
  156. orgSlug: organization.slug,
  157. needsAuthentication: true,
  158. needs2fa: false,
  159. hasAuthProvider: true,
  160. requireSso: true,
  161. existingMember: false,
  162. ssoProvider: 'SSO',
  163. });
  164. render(
  165. <AcceptOrganizationInvite
  166. {...RouteComponentPropsFixture()}
  167. params={{orgId: 'org-slug', memberId: '1', token: 'abc'}}
  168. />
  169. );
  170. expect(getJoinButton()).not.toBeInTheDocument();
  171. expect(screen.queryByTestId('action-info-general')).not.toBeInTheDocument();
  172. expect(screen.getByTestId('action-info-sso')).toBeInTheDocument();
  173. expect(screen.getByRole('button', {name: 'Join with SSO'})).toBeInTheDocument();
  174. expect(
  175. screen.queryByRole('button', {name: 'Create a new account'})
  176. ).not.toBeInTheDocument();
  177. expect(
  178. screen.queryByRole('link', {name: 'Login using an existing account'})
  179. ).not.toBeInTheDocument();
  180. });
  181. it('enforce required sso authentication for logged in users', function () {
  182. addMock({
  183. orgSlug: organization.slug,
  184. needsAuthentication: false,
  185. needs2fa: false,
  186. hasAuthProvider: true,
  187. requireSso: true,
  188. existingMember: false,
  189. ssoProvider: 'SSO',
  190. });
  191. render(
  192. <AcceptOrganizationInvite
  193. {...RouteComponentPropsFixture()}
  194. params={{orgId: 'org-slug', memberId: '1', token: 'abc'}}
  195. />
  196. );
  197. expect(getJoinButton()).not.toBeInTheDocument();
  198. expect(screen.queryByTestId('action-info-general')).not.toBeInTheDocument();
  199. expect(screen.getByTestId('action-info-sso')).toBeInTheDocument();
  200. expect(screen.getByRole('button', {name: 'Join with SSO'})).toBeInTheDocument();
  201. expect(
  202. screen.queryByRole('button', {name: 'Create a new account'})
  203. ).not.toBeInTheDocument();
  204. expect(
  205. screen.queryByRole('link', {name: 'Login using an existing account'})
  206. ).not.toBeInTheDocument();
  207. });
  208. it('show logout button for logged in users w/ sso and membership', async function () {
  209. addMock({
  210. orgSlug: organization.slug,
  211. needsAuthentication: false,
  212. needs2fa: false,
  213. hasAuthProvider: true,
  214. requireSso: true,
  215. existingMember: true,
  216. ssoProvider: 'SSO',
  217. });
  218. render(
  219. <AcceptOrganizationInvite
  220. {...RouteComponentPropsFixture()}
  221. params={{orgId: 'org-slug', memberId: '1', token: 'abc'}}
  222. />
  223. );
  224. expect(screen.getByTestId('existing-member')).toBeInTheDocument();
  225. await userEvent.click(screen.getByTestId('existing-member-link'));
  226. await waitFor(() => expect(logout).toHaveBeenCalled());
  227. });
  228. it('shows right options for logged in user and optional SSO', function () {
  229. addMock({
  230. orgSlug: organization.slug,
  231. needsAuthentication: false,
  232. needs2fa: false,
  233. hasAuthProvider: true,
  234. requireSso: false,
  235. existingMember: false,
  236. ssoProvider: 'SSO',
  237. });
  238. render(
  239. <AcceptOrganizationInvite
  240. {...RouteComponentPropsFixture()}
  241. params={{orgId: 'org-slug', memberId: '1', token: 'abc'}}
  242. />
  243. );
  244. expect(screen.getByTestId('action-info-sso')).toBeInTheDocument();
  245. expect(getJoinButton()).toBeInTheDocument();
  246. });
  247. it('shows a logout button for existing members', async function () {
  248. addMock({
  249. orgSlug: organization.slug,
  250. needsAuthentication: false,
  251. needs2fa: false,
  252. hasAuthProvider: false,
  253. requireSso: false,
  254. existingMember: true,
  255. });
  256. render(
  257. <AcceptOrganizationInvite
  258. {...RouteComponentPropsFixture()}
  259. params={{orgId: 'org-slug', memberId: '1', token: 'abc'}}
  260. />
  261. );
  262. expect(screen.getByTestId('existing-member')).toBeInTheDocument();
  263. await userEvent.click(screen.getByTestId('existing-member-link'));
  264. await waitFor(() => expect(logout).toHaveBeenCalled());
  265. });
  266. it('shows 2fa warning', function () {
  267. addMock({
  268. orgSlug: organization.slug,
  269. needsAuthentication: false,
  270. needs2fa: true,
  271. hasAuthProvider: false,
  272. requireSso: false,
  273. existingMember: false,
  274. });
  275. render(
  276. <AcceptOrganizationInvite
  277. {...RouteComponentPropsFixture()}
  278. params={{orgId: 'org-slug', memberId: '1', token: 'abc'}}
  279. />
  280. );
  281. expect(
  282. screen.getByRole('button', {name: 'Configure Two-Factor Auth'})
  283. ).toBeInTheDocument();
  284. });
  285. });