twoFactorRequired.spec.jsx 3.6 KB

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