twoFactorRequired.spec.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import Cookies from 'js-cookie';
  2. import * as qs from 'query-string';
  3. import {AccountEmailsFixture} from 'sentry-fixture/accountEmails';
  4. import {AuthenticatorsFixture} from 'sentry-fixture/authenticators';
  5. import {OrganizationsFixture} 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: [AuthenticatorsFixture().Totp({isEnrolled: false})],
  20. });
  21. MockApiClient.addMockResponse({
  22. url: ORG_ENDPOINT,
  23. body: OrganizationsFixture(),
  24. });
  25. MockApiClient.addMockResponse({
  26. url: ACCOUNT_EMAILS_ENDPOINT,
  27. body: AccountEmailsFixture(),
  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', async 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(await screen.findByText('Your current password')).toBeInTheDocument();
  53. expect(screen.queryByTestId('require-2fa')).not.toBeInTheDocument();
  54. });
  55. it('does not render when 2FA is disabled and no pendingInvite cookie', async function () {
  56. render(
  57. <AccountSecurityWrapper {...routerProps} params={{authId: ''}}>
  58. <TwoFactorRequired {...baseProps} />
  59. </AccountSecurityWrapper>,
  60. {context: routerContext}
  61. );
  62. expect(await screen.findByText('Your current password')).toBeInTheDocument();
  63. expect(screen.queryByTestId('require-2fa')).not.toBeInTheDocument();
  64. });
  65. it('does not render when 2FA is enrolled and no pendingInvite cookie', async function () {
  66. MockApiClient.addMockResponse({
  67. url: ENDPOINT,
  68. body: [AuthenticatorsFixture().Totp({isEnrolled: true})],
  69. });
  70. render(
  71. <AccountSecurityWrapper {...routerProps} params={{authId: ''}}>
  72. <TwoFactorRequired {...baseProps} />
  73. </AccountSecurityWrapper>,
  74. {context: routerContext}
  75. );
  76. expect(await screen.findByText('Your current password')).toBeInTheDocument();
  77. expect(screen.queryByTestId('require-2fa')).not.toBeInTheDocument();
  78. });
  79. it('does not render when 2FA is enrolled and has pendingInvite cookie', async function () {
  80. const cookieData = {
  81. memberId: 5,
  82. token: 'abcde',
  83. url: '/accept/5/abcde/',
  84. };
  85. Cookies.set(INVITE_COOKIE, qs.stringify(cookieData));
  86. MockApiClient.addMockResponse({
  87. url: ENDPOINT,
  88. body: [AuthenticatorsFixture().Totp({isEnrolled: true})],
  89. });
  90. MockApiClient.addMockResponse({
  91. url: ORG_ENDPOINT,
  92. body: OrganizationsFixture({require2FA: true}),
  93. });
  94. render(
  95. <AccountSecurityWrapper {...routerProps} params={{authId: ''}}>
  96. <TwoFactorRequired {...baseProps} />
  97. </AccountSecurityWrapper>,
  98. {context: routerContext}
  99. );
  100. expect(await screen.findByText('Your current password')).toBeInTheDocument();
  101. expect(screen.queryByTestId('require-2fa')).not.toBeInTheDocument();
  102. Cookies.remove(INVITE_COOKIE);
  103. });
  104. it('renders when 2FA is disabled and has pendingInvite cookie', async function () {
  105. Cookies.set(INVITE_COOKIE, '/accept/5/abcde/');
  106. MockApiClient.addMockResponse({
  107. url: ORG_ENDPOINT,
  108. body: OrganizationsFixture({require2FA: true}),
  109. });
  110. render(
  111. <AccountSecurityWrapper {...routerProps} params={{authId: ''}}>
  112. <TwoFactorRequired {...baseProps} />
  113. </AccountSecurityWrapper>,
  114. {context: routerContext}
  115. );
  116. expect(await screen.findByTestId('require-2fa')).toBeInTheDocument();
  117. Cookies.remove(INVITE_COOKIE);
  118. });
  119. });