organizationSecurityAndPrivacy.spec.jsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {mountGlobalModal} from 'sentry-test/modal';
  4. import OrganizationSecurityAndPrivacy from 'sentry/views/settings/organizationSecurityAndPrivacy';
  5. describe('OrganizationSecurityAndPrivacy', function () {
  6. let organization;
  7. let routerContext;
  8. beforeEach(function () {
  9. ({organization, routerContext} = initializeOrg());
  10. MockApiClient.addMockResponse({
  11. url: `/organizations/${organization.slug}/auth-provider/`,
  12. method: 'GET',
  13. });
  14. MockApiClient.addMockResponse({
  15. url: `/organizations/${organization.slug}/`,
  16. method: 'GET',
  17. });
  18. });
  19. it('shows require2fa switch', async function () {
  20. const wrapper = mountWithTheme(
  21. <OrganizationSecurityAndPrivacy
  22. params={{orgId: organization.slug}}
  23. organization={organization}
  24. />,
  25. routerContext
  26. );
  27. await tick();
  28. wrapper.update();
  29. expect(wrapper.find('Switch[name="require2FA"]')).toHaveLength(1);
  30. });
  31. it('returns to "off" if switch enable fails (e.g. API error)', async function () {
  32. MockApiClient.addMockResponse({
  33. url: '/organizations/org-slug/',
  34. method: 'PUT',
  35. statusCode: 500,
  36. });
  37. const wrapper = mountWithTheme(
  38. <OrganizationSecurityAndPrivacy
  39. params={{orgId: organization.slug}}
  40. organization={organization}
  41. />,
  42. routerContext
  43. );
  44. await tick();
  45. wrapper.update();
  46. wrapper.find('Switch[name="require2FA"]').simulate('click');
  47. // hide console.error for this test
  48. jest.spyOn(console, 'error').mockImplementation(() => {});
  49. // Confirm but has API failure
  50. const modal = await mountGlobalModal();
  51. modal.find('Button[priority="primary"]').simulate('click');
  52. await tick();
  53. wrapper.update();
  54. expect(wrapper.find('Switch[name="require2FA"]').prop('isActive')).toBe(false);
  55. // eslint-disable-next-line no-console
  56. console.error.mockRestore();
  57. });
  58. it('renders join request switch', async function () {
  59. const wrapper = mountWithTheme(
  60. <OrganizationSecurityAndPrivacy params={{orgId: organization.slug}} />,
  61. TestStubs.routerContext([{organization}])
  62. );
  63. await tick();
  64. wrapper.update();
  65. expect(wrapper.find('Switch[name="allowJoinRequests"]').exists()).toBe(true);
  66. });
  67. it('enables require2fa but cancels confirm modal', async function () {
  68. const mock = MockApiClient.addMockResponse({
  69. url: '/organizations/org-slug/',
  70. method: 'PUT',
  71. });
  72. const wrapper = mountWithTheme(
  73. <OrganizationSecurityAndPrivacy
  74. params={{orgId: organization.slug}}
  75. organization={organization}
  76. />,
  77. routerContext
  78. );
  79. await tick();
  80. wrapper.update();
  81. expect(wrapper.find('Switch[name="require2FA"]')).toHaveLength(1);
  82. wrapper.find('Switch[name="require2FA"]').simulate('click');
  83. // Cancel
  84. const modal = await mountGlobalModal();
  85. modal.find('Button').first().simulate('click');
  86. await tick();
  87. wrapper.update();
  88. expect(wrapper.find('Switch[name="require2FA"]').prop('isActive')).toBe(false);
  89. expect(mock).not.toHaveBeenCalled();
  90. });
  91. it('enables require2fa with confirm modal', async function () {
  92. const mock = MockApiClient.addMockResponse({
  93. url: '/organizations/org-slug/',
  94. method: 'PUT',
  95. });
  96. const wrapper = mountWithTheme(
  97. <OrganizationSecurityAndPrivacy
  98. params={{orgId: organization.slug}}
  99. organization={organization}
  100. />,
  101. routerContext
  102. );
  103. await tick();
  104. wrapper.update();
  105. expect(wrapper.find('Switch[name="require2FA"]')).toHaveLength(1);
  106. wrapper.find('Switch[name="require2FA"]').simulate('click');
  107. // Confirm
  108. const modal = await mountGlobalModal();
  109. modal.find('Button[priority="primary"]').simulate('click');
  110. await tick();
  111. wrapper.update();
  112. expect(wrapper.find('Switch[name="require2FA"]').prop('isActive')).toBe(true);
  113. expect(mock).toHaveBeenCalledWith(
  114. '/organizations/org-slug/',
  115. expect.objectContaining({
  116. method: 'PUT',
  117. data: {
  118. require2FA: true,
  119. },
  120. })
  121. );
  122. });
  123. });