twoFactorRequired.spec.jsx 3.7 KB

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