twoFactorRequired.spec.tsx 3.9 KB

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