twoFactorRequired.spec.jsx 3.3 KB

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