acceptOrganizationInvite.spec.jsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import {browserHistory} from 'react-router';
  2. import {mountWithTheme} from 'sentry-test/enzyme';
  3. import {logout} from 'app/actionCreators/account';
  4. import React from 'react';
  5. import AcceptOrganizationInvite from 'app/views/acceptOrganizationInvite';
  6. jest.mock('app/actionCreators/account');
  7. const addMock = body =>
  8. MockApiClient.addMockResponse({
  9. url: '/accept-invite/1/abc/',
  10. method: 'GET',
  11. body,
  12. });
  13. describe('AcceptOrganizationInvite', function() {
  14. it('can accept invitation', async function() {
  15. addMock({
  16. orgSlug: 'test-org',
  17. needsAuthentication: false,
  18. needs2fa: false,
  19. needsSso: false,
  20. existingMember: false,
  21. });
  22. const wrapper = mountWithTheme(
  23. <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />,
  24. TestStubs.routerContext()
  25. );
  26. const joinButton = wrapper.find('Button[label="join-organization"]');
  27. expect(joinButton.exists()).toBe(true);
  28. expect(joinButton.text()).toBe('Join the test-org organization');
  29. const acceptMock = MockApiClient.addMockResponse({
  30. url: '/accept-invite/1/abc/',
  31. method: 'POST',
  32. });
  33. joinButton.simulate('click');
  34. expect(acceptMock).toHaveBeenCalled();
  35. expect(wrapper.find('Button[label="join-organization"]').props().disabled).toBe(true);
  36. await tick();
  37. expect(browserHistory.replace).toHaveBeenCalledWith('/test-org/');
  38. });
  39. it('requires authentication to join', function() {
  40. addMock({
  41. orgSlug: 'test-org',
  42. needsAuthentication: true,
  43. needs2fa: false,
  44. needsSso: false,
  45. existingMember: false,
  46. });
  47. const wrapper = mountWithTheme(
  48. <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />,
  49. TestStubs.routerContext()
  50. );
  51. const joinButton = wrapper.find('Button[label="join-organization"]');
  52. expect(joinButton.exists()).toBe(false);
  53. expect(wrapper.find('Button[label="create-account"]').exists()).toBe(true);
  54. });
  55. it('suggests sso authentication to login', function() {
  56. addMock({
  57. orgSlug: 'test-org',
  58. needsAuthentication: true,
  59. needs2fa: false,
  60. needsSso: true,
  61. existingMember: false,
  62. });
  63. const wrapper = mountWithTheme(
  64. <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />,
  65. TestStubs.routerContext()
  66. );
  67. const joinButton = wrapper.find('Button[label="join-organization"]');
  68. expect(joinButton.exists()).toBe(false);
  69. expect(wrapper.find('Button[label="create-account"]').exists()).toBe(false);
  70. expect(wrapper.find('[data-test-id="suggests-sso"]').exists()).toBe(true);
  71. expect(wrapper.find('Button[label="sso-login"]').exists()).toBe(true);
  72. });
  73. it('shows a logout button for existing members', async function() {
  74. addMock({
  75. orgSlug: 'test-org',
  76. needsAuthentication: false,
  77. needs2fa: false,
  78. needsSso: false,
  79. existingMember: true,
  80. });
  81. const wrapper = mountWithTheme(
  82. <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />,
  83. TestStubs.routerContext()
  84. );
  85. const existingMember = wrapper.find('[data-test-id="existing-member"]');
  86. expect(existingMember.exists()).toBe(true);
  87. const {replace} = window.location;
  88. window.location.replace = jest.fn();
  89. existingMember.find('Link').simulate('click');
  90. expect(logout).toHaveBeenCalled();
  91. await tick();
  92. expect(window.location.replace).toHaveBeenCalled();
  93. window.location.replace = replace;
  94. });
  95. it('shows 2fa warning', function() {
  96. addMock({
  97. orgSlug: 'test-org',
  98. needsAuthentication: false,
  99. needs2fa: true,
  100. needsSso: false,
  101. existingMember: false,
  102. });
  103. const wrapper = mountWithTheme(
  104. <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />,
  105. TestStubs.routerContext()
  106. );
  107. expect(wrapper.find('[data-test-id="2fa-warning"]').exists()).toBe(true);
  108. });
  109. });