accountSecurityDetails.spec.jsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import React from 'react';
  2. import {mountWithTheme} from 'sentry-test/enzyme';
  3. import {Client} from 'app/api';
  4. import AccountSecurityDetails from 'app/views/settings/account/accountSecurity/accountSecurityDetails';
  5. import AccountSecurityWrapper from 'app/views/settings/account/accountSecurity/accountSecurityWrapper';
  6. const ENDPOINT = '/users/me/authenticators/';
  7. const ORG_ENDPOINT = '/organizations/';
  8. describe('AccountSecurityDetails', function() {
  9. let wrapper;
  10. describe('Totp', function() {
  11. Client.clearMockResponses();
  12. beforeAll(function() {
  13. Client.addMockResponse({
  14. url: ENDPOINT,
  15. body: TestStubs.AllAuthenticators(),
  16. });
  17. Client.addMockResponse({
  18. url: ORG_ENDPOINT,
  19. body: TestStubs.Organizations(),
  20. });
  21. Client.addMockResponse({
  22. url: `${ENDPOINT}15/`,
  23. body: TestStubs.Authenticators().Totp(),
  24. });
  25. wrapper = mountWithTheme(
  26. <AccountSecurityWrapper>
  27. <AccountSecurityDetails />
  28. </AccountSecurityWrapper>,
  29. TestStubs.routerContext([
  30. {
  31. router: {
  32. ...TestStubs.router(),
  33. params: {
  34. authId: 15,
  35. },
  36. },
  37. },
  38. ])
  39. );
  40. });
  41. it('has enrolled circle indicator', function() {
  42. expect(wrapper.find('CircleIndicator').prop('enabled')).toBe(true);
  43. });
  44. it('has created and last used dates', function() {
  45. expect(wrapper.find('AuthenticatorDate')).toHaveLength(2);
  46. });
  47. it('can remove method', function() {
  48. const deleteMock = Client.addMockResponse({
  49. url: `${ENDPOINT}15/`,
  50. method: 'DELETE',
  51. });
  52. wrapper.find('RemoveConfirm Button').simulate('click');
  53. wrapper
  54. .find('Modal Button')
  55. .last()
  56. .simulate('click');
  57. expect(deleteMock).toHaveBeenCalled();
  58. });
  59. it('can remove one of multiple 2fa methods when org requires 2fa', function() {
  60. Client.addMockResponse({
  61. url: ORG_ENDPOINT,
  62. body: TestStubs.Organizations({require2FA: true}),
  63. });
  64. const deleteMock = Client.addMockResponse({
  65. url: `${ENDPOINT}15/`,
  66. method: 'DELETE',
  67. });
  68. wrapper = mountWithTheme(
  69. <AccountSecurityWrapper>
  70. <AccountSecurityDetails />
  71. </AccountSecurityWrapper>,
  72. TestStubs.routerContext([
  73. {
  74. router: {
  75. ...TestStubs.router(),
  76. params: {
  77. authId: 15,
  78. },
  79. },
  80. },
  81. ])
  82. );
  83. wrapper.find('RemoveConfirm Button').simulate('click');
  84. wrapper
  85. .find('Modal Button')
  86. .last()
  87. .simulate('click');
  88. expect(deleteMock).toHaveBeenCalled();
  89. });
  90. it('can not remove last 2fa method when org requires 2fa', function() {
  91. Client.addMockResponse({
  92. url: ORG_ENDPOINT,
  93. body: TestStubs.Organizations({require2FA: true}),
  94. });
  95. Client.addMockResponse({
  96. url: ENDPOINT,
  97. body: [TestStubs.Authenticators().Totp()],
  98. });
  99. const deleteMock = Client.addMockResponse({
  100. url: `${ENDPOINT}15/`,
  101. method: 'DELETE',
  102. });
  103. wrapper = mountWithTheme(
  104. <AccountSecurityWrapper>
  105. <AccountSecurityDetails />
  106. </AccountSecurityWrapper>,
  107. TestStubs.routerContext([
  108. {
  109. router: {
  110. ...TestStubs.router(),
  111. params: {
  112. authId: 15,
  113. },
  114. },
  115. },
  116. ])
  117. );
  118. wrapper.find('RemoveConfirm Button').simulate('click');
  119. expect(wrapper.find('Modal Button')).toHaveLength(0);
  120. expect(deleteMock).not.toHaveBeenCalled();
  121. });
  122. });
  123. describe('Recovery', function() {
  124. beforeEach(function() {
  125. Client.clearMockResponses();
  126. Client.addMockResponse({
  127. url: ENDPOINT,
  128. body: TestStubs.AllAuthenticators(),
  129. });
  130. Client.addMockResponse({
  131. url: ORG_ENDPOINT,
  132. body: TestStubs.Organizations(),
  133. });
  134. Client.addMockResponse({
  135. url: `${ENDPOINT}16/`,
  136. body: TestStubs.Authenticators().Recovery(),
  137. });
  138. wrapper = mountWithTheme(
  139. <AccountSecurityWrapper>
  140. <AccountSecurityDetails />
  141. </AccountSecurityWrapper>,
  142. TestStubs.routerContext([
  143. {
  144. router: {
  145. ...TestStubs.router(),
  146. params: {
  147. authId: 16,
  148. },
  149. },
  150. },
  151. ])
  152. );
  153. });
  154. it('has enrolled circle indicator', function() {
  155. expect(wrapper.find('CircleIndicator').prop('enabled')).toBe(true);
  156. });
  157. it('has created and last used dates', function() {
  158. expect(wrapper.find('AuthenticatorDate')).toHaveLength(2);
  159. });
  160. it('does not have remove button', function() {
  161. expect(wrapper.find('RemoveConfirm')).toHaveLength(0);
  162. });
  163. it('regenerates codes', function() {
  164. const deleteMock = Client.addMockResponse({
  165. url: `${ENDPOINT}16/`,
  166. method: 'PUT',
  167. });
  168. wrapper.find('RecoveryCodes').prop('onRegenerateBackupCodes')();
  169. expect(deleteMock).toHaveBeenCalled();
  170. });
  171. it('has copy, print and download buttons', function() {
  172. const codes = 'ABCD-1234 \nEFGH-5678';
  173. const downloadCodes = `Button[href="data:text/plain;charset=utf-8,${codes}"]`;
  174. expect(wrapper.find(downloadCodes)).toHaveLength(1);
  175. wrapper.find(downloadCodes).simulate('click');
  176. expect(wrapper.find('Button InlineSvg[src="icon-print"]')).toHaveLength(1);
  177. expect(wrapper.find('iframe[name="printable"]')).toHaveLength(1);
  178. expect(wrapper.find(`Clipboard[value="${codes}"]`)).toHaveLength(1);
  179. });
  180. });
  181. });