index.spec.tsx 3.9 KB

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