twoFactorRequired.spec.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import Cookies from 'js-cookie';
  2. import * as qs from 'query-string';
  3. import {AccountEmails} from 'sentry-fixture/accountEmails';
  4. import {Authenticators} from 'sentry-fixture/authenticators';
  5. import {Organizations} from 'sentry-fixture/organizations';
  6. import {initializeOrg} from 'sentry-test/initializeOrg';
  7. import {render, screen} from 'sentry-test/reactTestingLibrary';
  8. import TwoFactorRequired from 'sentry/views/settings/account/accountSecurity/';
  9. import AccountSecurityWrapper from 'sentry/views/settings/account/accountSecurity/accountSecurityWrapper';
  10. const ENDPOINT = '/users/me/authenticators/';
  11. const ORG_ENDPOINT = '/organizations/';
  12. const INVITE_COOKIE = 'pending-invite';
  13. const ACCOUNT_EMAILS_ENDPOINT = '/users/me/emails/';
  14. describe('TwoFactorRequired', function () {
  15. beforeEach(function () {
  16. MockApiClient.clearMockResponses();
  17. MockApiClient.addMockResponse({
  18. url: ENDPOINT,
  19. body: [Authenticators().Totp({isEnrolled: false})],
  20. });
  21. MockApiClient.addMockResponse({
  22. url: ORG_ENDPOINT,
  23. body: Organizations(),
  24. });
  25. MockApiClient.addMockResponse({
  26. url: ACCOUNT_EMAILS_ENDPOINT,
  27. body: AccountEmails(),
  28. });
  29. });
  30. const {routerContext, routerProps} = initializeOrg();
  31. const baseProps = {
  32. authenticators: null,
  33. countEnrolled: 0,
  34. deleteDisabled: false,
  35. handleRefresh: () => {},
  36. hasVerifiedEmail: false,
  37. onDisable: () => {},
  38. orgsRequire2fa: [],
  39. ...routerProps,
  40. };
  41. it('renders empty', function () {
  42. MockApiClient.addMockResponse({
  43. url: ORG_ENDPOINT,
  44. body: [],
  45. });
  46. render(
  47. <AccountSecurityWrapper {...routerProps} params={{authId: ''}}>
  48. <TwoFactorRequired {...baseProps} />
  49. </AccountSecurityWrapper>,
  50. {context: routerContext}
  51. );
  52. expect(screen.queryByTestId('require-2fa')).not.toBeInTheDocument();
  53. });
  54. it('does not render when 2FA is disabled and no pendingInvite cookie', function () {
  55. render(
  56. <AccountSecurityWrapper {...routerProps} params={{authId: ''}}>
  57. <TwoFactorRequired {...baseProps} />
  58. </AccountSecurityWrapper>,
  59. {context: routerContext}
  60. );
  61. expect(screen.queryByTestId('require-2fa')).not.toBeInTheDocument();
  62. });
  63. it('does not render when 2FA is enrolled and no pendingInvite cookie', function () {
  64. MockApiClient.addMockResponse({
  65. url: ENDPOINT,
  66. body: [Authenticators().Totp({isEnrolled: true})],
  67. });
  68. render(
  69. <AccountSecurityWrapper {...routerProps} params={{authId: ''}}>
  70. <TwoFactorRequired {...baseProps} />
  71. </AccountSecurityWrapper>,
  72. {context: routerContext}
  73. );
  74. expect(screen.queryByTestId('require-2fa')).not.toBeInTheDocument();
  75. });
  76. it('does not render when 2FA is enrolled and has pendingInvite cookie', function () {
  77. const cookieData = {
  78. memberId: 5,
  79. token: 'abcde',
  80. url: '/accept/5/abcde/',
  81. };
  82. Cookies.set(INVITE_COOKIE, qs.stringify(cookieData));
  83. MockApiClient.addMockResponse({
  84. url: ENDPOINT,
  85. body: [Authenticators().Totp({isEnrolled: true})],
  86. });
  87. MockApiClient.addMockResponse({
  88. url: ORG_ENDPOINT,
  89. body: Organizations({require2FA: true}),
  90. });
  91. render(
  92. <AccountSecurityWrapper {...routerProps} params={{authId: ''}}>
  93. <TwoFactorRequired {...baseProps} />
  94. </AccountSecurityWrapper>,
  95. {context: routerContext}
  96. );
  97. expect(screen.queryByTestId('require-2fa')).not.toBeInTheDocument();
  98. Cookies.remove(INVITE_COOKIE);
  99. });
  100. it('renders when 2FA is disabled and has pendingInvite cookie', async function () {
  101. Cookies.set(INVITE_COOKIE, '/accept/5/abcde/');
  102. MockApiClient.addMockResponse({
  103. url: ORG_ENDPOINT,
  104. body: Organizations({require2FA: true}),
  105. });
  106. render(
  107. <AccountSecurityWrapper {...routerProps} params={{authId: ''}}>
  108. <TwoFactorRequired {...baseProps} />
  109. </AccountSecurityWrapper>,
  110. {context: routerContext}
  111. );
  112. expect(await screen.findByTestId('require-2fa')).toBeInTheDocument();
  113. Cookies.remove(INVITE_COOKIE);
  114. });
  115. });