twoFactorRequired.spec.jsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import React from 'react';
  2. import {mount} from 'enzyme';
  3. import TwoFactorRequired from 'app/views/settings/account/accountSecurity/';
  4. import AccountSecurityWrapper from 'app/views/settings/account/accountSecurity/accountSecurityWrapper';
  5. const ENDPOINT = '/users/me/authenticators/';
  6. const ORG_ENDPOINT = '/organizations/';
  7. describe('TwoFactorRequired', function() {
  8. beforeEach(function() {
  9. MockApiClient.clearMockResponses();
  10. MockApiClient.addMockResponse({
  11. url: ENDPOINT,
  12. body: [TestStubs.Authenticators().Totp({isEnrolled: false})],
  13. });
  14. MockApiClient.addMockResponse({
  15. url: ORG_ENDPOINT,
  16. body: TestStubs.Organizations({require2FA: false}),
  17. });
  18. });
  19. it('renders empty', function() {
  20. MockApiClient.addMockResponse({
  21. url: ORG_ENDPOINT,
  22. body: [],
  23. });
  24. let wrapper = mount(
  25. <AccountSecurityWrapper>
  26. <TwoFactorRequired />
  27. </AccountSecurityWrapper>,
  28. TestStubs.routerContext()
  29. );
  30. expect(wrapper.find('TwoFactorRequired')).toHaveLength(1);
  31. expect(wrapper.find('StyledAlert[className="require-2fa"]')).toHaveLength(0);
  32. });
  33. it('does not render when 2FA is not required, not 2FA enrolled', function() {
  34. let wrapper = mount(
  35. <AccountSecurityWrapper>
  36. <TwoFactorRequired />
  37. </AccountSecurityWrapper>,
  38. TestStubs.routerContext()
  39. );
  40. expect(wrapper.find('StyledAlert[className="require-2fa"]')).toHaveLength(0);
  41. });
  42. it('does not render when 2FA is not required, 2FA is enrolled', function() {
  43. MockApiClient.addMockResponse({
  44. url: ENDPOINT,
  45. body: [TestStubs.Authenticators().Totp({isEnrolled: true})],
  46. });
  47. let wrapper = mount(
  48. <AccountSecurityWrapper>
  49. <TwoFactorRequired />
  50. </AccountSecurityWrapper>,
  51. TestStubs.routerContext()
  52. );
  53. expect(wrapper.find('StyledAlert[className="require-2fa"]')).toHaveLength(0);
  54. });
  55. it('does not render when 2FA is required, 2FA is enrolled', function() {
  56. MockApiClient.addMockResponse({
  57. url: ENDPOINT,
  58. body: [TestStubs.Authenticators().Totp({isEnrolled: true})],
  59. });
  60. MockApiClient.addMockResponse({
  61. url: ORG_ENDPOINT,
  62. body: TestStubs.Organizations({require2FA: true}),
  63. });
  64. let wrapper = mount(
  65. <AccountSecurityWrapper>
  66. <TwoFactorRequired />
  67. </AccountSecurityWrapper>,
  68. TestStubs.routerContext()
  69. );
  70. expect(wrapper.find('StyledAlert[className="require-2fa"]')).toHaveLength(0);
  71. });
  72. it('renders when 2FA is required for multiple orgs, 2FA is not enrolled', function() {
  73. MockApiClient.addMockResponse({
  74. url: ORG_ENDPOINT,
  75. body: TestStubs.Organizations({require2FA: true}),
  76. });
  77. let wrapper = mount(
  78. <AccountSecurityWrapper>
  79. <TwoFactorRequired />
  80. </AccountSecurityWrapper>,
  81. TestStubs.routerContext()
  82. );
  83. expect(wrapper.find('StyledAlert[className="require-2fa"]')).toHaveLength(1);
  84. expect(wrapper.find('StyledAlert[className="require-2fa"]').text()).toEqual(
  85. expect.stringContaining('Test 1 and Test 2 organizations')
  86. );
  87. });
  88. it('renders when 2FA is required for one org, 2FA is not enrolled', function() {
  89. MockApiClient.addMockResponse({
  90. url: ORG_ENDPOINT,
  91. body: [
  92. {
  93. id: '1',
  94. name: 'test 1',
  95. require2FA: true,
  96. },
  97. ],
  98. });
  99. let wrapper = mount(
  100. <AccountSecurityWrapper>
  101. <TwoFactorRequired />
  102. </AccountSecurityWrapper>,
  103. TestStubs.routerContext()
  104. );
  105. expect(wrapper.find('StyledAlert[className="require-2fa"]')).toHaveLength(1);
  106. expect(wrapper.find('StyledAlert[className="require-2fa"]').text()).toEqual(
  107. expect.stringContaining('Test 1 organization')
  108. );
  109. });
  110. });