index.spec.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import {Fragment} from 'react';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  4. import GlobalModal from 'sentry/components/globalModal';
  5. import OrganizationSecurityAndPrivacy from 'sentry/views/settings/organizationSecurityAndPrivacy';
  6. describe('OrganizationSecurityAndPrivacy', function () {
  7. const {organization} = initializeOrg();
  8. beforeEach(() => {
  9. MockApiClient.addMockResponse({
  10. url: `/organizations/${organization.slug}/auth-provider/`,
  11. method: 'GET',
  12. body: {},
  13. });
  14. });
  15. it('shows require2fa switch', async function () {
  16. render(<OrganizationSecurityAndPrivacy />);
  17. expect(
  18. await screen.findByRole('checkbox', {
  19. name: 'Enable to require and enforce two-factor authentication for all members',
  20. })
  21. ).toBeInTheDocument();
  22. });
  23. it('returns to "off" if switch enable fails (e.g. API error)', async function () {
  24. MockApiClient.addMockResponse({
  25. url: `/organizations/${organization.slug}/`,
  26. method: 'PUT',
  27. statusCode: 500,
  28. });
  29. render(
  30. <Fragment>
  31. <GlobalModal />
  32. <OrganizationSecurityAndPrivacy />
  33. </Fragment>
  34. );
  35. await userEvent.click(
  36. await screen.findByRole('checkbox', {
  37. name: 'Enable to require and enforce two-factor authentication for all members',
  38. })
  39. );
  40. // Hide console.error for this test
  41. jest.spyOn(console, 'error').mockImplementation(() => {});
  42. // Confirm but has API failure
  43. await userEvent.click(screen.getByRole('button', {name: 'Confirm'}));
  44. expect(
  45. await screen.findByRole('checkbox', {
  46. name: 'Enable to require and enforce two-factor authentication for all members',
  47. })
  48. ).not.toBeChecked();
  49. });
  50. it('renders join request switch', async function () {
  51. render(<OrganizationSecurityAndPrivacy />);
  52. expect(
  53. await screen.findByRole('checkbox', {
  54. name: 'Enable to allow users to request to join your organization',
  55. })
  56. ).toBeInTheDocument();
  57. });
  58. it('enables require2fa but cancels confirm modal', async function () {
  59. const mock = MockApiClient.addMockResponse({
  60. url: `/organizations/${organization.slug}/`,
  61. method: 'PUT',
  62. });
  63. render(
  64. <Fragment>
  65. <GlobalModal />
  66. <OrganizationSecurityAndPrivacy />
  67. </Fragment>
  68. );
  69. await userEvent.click(
  70. await screen.findByRole('checkbox', {
  71. name: 'Enable to require and enforce two-factor authentication for all members',
  72. })
  73. );
  74. // Cancel
  75. await userEvent.click(screen.getByRole('button', {name: 'Cancel'}));
  76. expect(
  77. screen.getByRole('checkbox', {
  78. name: 'Enable to require and enforce two-factor authentication for all members',
  79. })
  80. ).not.toBeChecked();
  81. expect(mock).not.toHaveBeenCalled();
  82. });
  83. it('enables require2fa with confirm modal', async function () {
  84. const mock = MockApiClient.addMockResponse({
  85. url: `/organizations/${organization.slug}/`,
  86. method: 'PUT',
  87. });
  88. render(
  89. <Fragment>
  90. <GlobalModal />
  91. <OrganizationSecurityAndPrivacy />
  92. </Fragment>
  93. );
  94. await userEvent.click(
  95. await screen.findByRole('checkbox', {
  96. name: 'Enable to require and enforce two-factor authentication for all members',
  97. })
  98. );
  99. await userEvent.click(screen.getByRole('button', {name: 'Confirm'}));
  100. expect(
  101. screen.getByRole('checkbox', {
  102. name: 'Enable to require and enforce two-factor authentication for all members',
  103. })
  104. ).toBeChecked();
  105. expect(mock).toHaveBeenCalledWith(
  106. '/organizations/org-slug/',
  107. expect.objectContaining({
  108. method: 'PUT',
  109. data: {
  110. require2FA: true,
  111. },
  112. })
  113. );
  114. });
  115. });