twoFactorRequired.spec.jsx 3.8 KB

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