accountSecurity.spec.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. import React from 'react';
  2. import {mountWithTheme} from 'sentry-test/enzyme';
  3. import {Client} from 'app/api';
  4. import AccountSecurity 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 AUTH_ENDPOINT = '/auth/';
  9. describe('AccountSecurity', function() {
  10. beforeEach(function() {
  11. Client.clearMockResponses();
  12. Client.addMockResponse({
  13. url: ORG_ENDPOINT,
  14. body: TestStubs.Organizations(),
  15. });
  16. });
  17. it('renders empty', function() {
  18. Client.addMockResponse({
  19. url: ENDPOINT,
  20. body: [],
  21. });
  22. const wrapper = mountWithTheme(
  23. <AccountSecurityWrapper>
  24. <AccountSecurity />
  25. </AccountSecurityWrapper>,
  26. TestStubs.routerContext()
  27. );
  28. expect(wrapper.find('EmptyMessage')).toHaveLength(1);
  29. expect(wrapper.find('TwoFactorRequired')).toHaveLength(0);
  30. });
  31. it('renders a primary interface that is enrolled', function() {
  32. Client.addMockResponse({
  33. url: ENDPOINT,
  34. body: [TestStubs.Authenticators().Totp({configureButton: 'Info'})],
  35. });
  36. const wrapper = mountWithTheme(
  37. <AccountSecurityWrapper>
  38. <AccountSecurity />
  39. </AccountSecurityWrapper>,
  40. TestStubs.routerContext()
  41. );
  42. expect(wrapper.find('AuthenticatorName').prop('children')).toBe('Authenticator App');
  43. // There should be an "Info" button
  44. expect(
  45. wrapper
  46. .find('Button[className="details-button"]')
  47. .first()
  48. .prop('children')
  49. ).toBe('Info');
  50. // Remove button
  51. expect(wrapper.find('Button[icon="icon-trash"]')).toHaveLength(1);
  52. expect(wrapper.find('AuthenticatorStatus').prop('enabled')).toBe(true);
  53. expect(wrapper.find('TwoFactorRequired')).toHaveLength(0);
  54. });
  55. it('can delete enrolled authenticator', function() {
  56. Client.addMockResponse({
  57. url: ENDPOINT,
  58. body: [
  59. TestStubs.Authenticators().Totp({
  60. authId: '15',
  61. configureButton: 'Info',
  62. }),
  63. ],
  64. });
  65. const deleteMock = Client.addMockResponse({
  66. url: `${ENDPOINT}15/`,
  67. method: 'DELETE',
  68. });
  69. expect(deleteMock).not.toHaveBeenCalled();
  70. const wrapper = mountWithTheme(
  71. <AccountSecurityWrapper>
  72. <AccountSecurity />
  73. </AccountSecurityWrapper>,
  74. TestStubs.routerContext()
  75. );
  76. expect(wrapper.find('AuthenticatorStatus').prop('enabled')).toBe(true);
  77. // This will open confirm modal
  78. wrapper.find('Button[icon="icon-trash"]').simulate('click');
  79. // Confirm
  80. wrapper
  81. .find('Modal Button')
  82. .last()
  83. .simulate('click');
  84. expect(deleteMock).toHaveBeenCalled();
  85. setTimeout(() => {
  86. wrapper.update();
  87. expect(wrapper.find('AuthenticatorStatus').prop('enabled')).toBe(false);
  88. }, 1);
  89. // still has another 2fa method
  90. expect(wrapper.find('TwoFactorRequired')).toHaveLength(0);
  91. });
  92. it('can remove one of multiple 2fa methods when org requires 2fa', function() {
  93. Client.addMockResponse({
  94. url: ENDPOINT,
  95. body: [
  96. TestStubs.Authenticators().Totp({
  97. authId: '15',
  98. configureButton: 'Info',
  99. }),
  100. TestStubs.Authenticators().U2f(),
  101. ],
  102. });
  103. Client.addMockResponse({
  104. url: ORG_ENDPOINT,
  105. body: TestStubs.Organizations({require2FA: true}),
  106. });
  107. const deleteMock = Client.addMockResponse({
  108. url: `${ENDPOINT}15/`,
  109. method: 'DELETE',
  110. });
  111. expect(deleteMock).not.toHaveBeenCalled();
  112. const wrapper = mountWithTheme(
  113. <AccountSecurityWrapper>
  114. <AccountSecurity />
  115. </AccountSecurityWrapper>,
  116. TestStubs.routerContext()
  117. );
  118. expect(
  119. wrapper
  120. .find('AuthenticatorStatus')
  121. .first()
  122. .prop('enabled')
  123. ).toBe(true);
  124. expect(
  125. wrapper
  126. .find('RemoveConfirm')
  127. .first()
  128. .prop('disabled')
  129. ).toBe(false);
  130. expect(
  131. wrapper
  132. .find('Tooltip')
  133. .first()
  134. .prop('disabled')
  135. ).toBe(true);
  136. // This will open confirm modal
  137. wrapper
  138. .find('Button[icon="icon-trash"]')
  139. .first()
  140. .simulate('click');
  141. // Confirm
  142. wrapper
  143. .find('Modal Button')
  144. .last()
  145. .simulate('click');
  146. expect(deleteMock).toHaveBeenCalled();
  147. });
  148. it('can not remove last 2fa method when org requires 2fa', function() {
  149. Client.addMockResponse({
  150. url: ENDPOINT,
  151. body: [
  152. TestStubs.Authenticators().Totp({
  153. authId: '15',
  154. configureButton: 'Info',
  155. }),
  156. ],
  157. });
  158. Client.addMockResponse({
  159. url: ORG_ENDPOINT,
  160. body: TestStubs.Organizations({require2FA: true}),
  161. });
  162. const deleteMock = Client.addMockResponse({
  163. url: `${ENDPOINT}15/`,
  164. method: 'DELETE',
  165. });
  166. expect(deleteMock).not.toHaveBeenCalled();
  167. const wrapper = mountWithTheme(
  168. <AccountSecurityWrapper>
  169. <AccountSecurity />
  170. </AccountSecurityWrapper>,
  171. TestStubs.routerContext()
  172. );
  173. expect(wrapper.find('AuthenticatorStatus').prop('enabled')).toBe(true);
  174. expect(wrapper.find('RemoveConfirm').prop('disabled')).toBe(true);
  175. expect(wrapper.find('Tooltip').prop('disabled')).toBe(false);
  176. expect(wrapper.find('Tooltip').prop('title')).toContain('test 1 and test 2');
  177. // This will open confirm modal
  178. wrapper.find('Button[icon="icon-trash"]').simulate('click');
  179. // Confirm
  180. expect(wrapper.find('Modal Button')).toHaveLength(0);
  181. expect(deleteMock).not.toHaveBeenCalled();
  182. });
  183. it('renders a primary interface that is not enrolled', function() {
  184. Client.addMockResponse({
  185. url: ENDPOINT,
  186. body: [TestStubs.Authenticators().Totp({isEnrolled: false})],
  187. });
  188. const wrapper = mountWithTheme(
  189. <AccountSecurityWrapper>
  190. <AccountSecurity />
  191. </AccountSecurityWrapper>,
  192. TestStubs.routerContext()
  193. );
  194. expect(wrapper.find('AuthenticatorName').prop('children')).toBe('Authenticator App');
  195. // There should be an "Add" button
  196. expect(
  197. wrapper
  198. .find('Button[className="enroll-button"]')
  199. .first()
  200. .prop('children')
  201. ).toBe('Add');
  202. expect(wrapper.find('AuthenticatorStatus').prop('enabled')).toBe(false);
  203. // user is not 2fa enrolled
  204. expect(wrapper.find('TwoFactorRequired')).toHaveLength(1);
  205. });
  206. it('renders a backup interface that is not enrolled', function() {
  207. Client.addMockResponse({
  208. url: ENDPOINT,
  209. body: [TestStubs.Authenticators().Recovery({isEnrolled: false})],
  210. });
  211. const wrapper = mountWithTheme(
  212. <AccountSecurityWrapper>
  213. <AccountSecurity />
  214. </AccountSecurityWrapper>,
  215. TestStubs.routerContext()
  216. );
  217. expect(wrapper.find('AuthenticatorName').prop('children')).toBe('Recovery Codes');
  218. // There should be an View Codes button
  219. expect(wrapper.find('Button[className="details-button"]')).toHaveLength(0);
  220. expect(wrapper.find('AuthenticatorStatus').prop('enabled')).toBe(false);
  221. // user is not 2fa enrolled
  222. expect(wrapper.find('TwoFactorRequired')).toHaveLength(1);
  223. });
  224. it('renders a backup interface that is enrolled', function() {
  225. Client.addMockResponse({
  226. url: ENDPOINT,
  227. body: [TestStubs.Authenticators().Recovery({isEnrolled: true})],
  228. });
  229. const wrapper = mountWithTheme(
  230. <AccountSecurityWrapper>
  231. <AccountSecurity />
  232. </AccountSecurityWrapper>,
  233. TestStubs.routerContext()
  234. );
  235. expect(wrapper.find('AuthenticatorName').prop('children')).toBe('Recovery Codes');
  236. // There should be an View Codes button
  237. expect(
  238. wrapper
  239. .find('Button[className="details-button"]')
  240. .first()
  241. .prop('children')
  242. ).toBe('View Codes');
  243. expect(wrapper.find('AuthenticatorStatus').prop('enabled')).toBe(true);
  244. });
  245. it('can change password', function() {
  246. Client.addMockResponse({
  247. url: ENDPOINT,
  248. body: [TestStubs.Authenticators().Recovery({isEnrolled: false})],
  249. });
  250. const url = '/users/me/password/';
  251. const mock = Client.addMockResponse({
  252. url,
  253. method: 'PUT',
  254. });
  255. const wrapper = mountWithTheme(
  256. <AccountSecurityWrapper>
  257. <AccountSecurity />
  258. </AccountSecurityWrapper>,
  259. TestStubs.routerContext()
  260. );
  261. wrapper
  262. .find('PasswordForm input[name="password"]')
  263. .simulate('change', {target: {value: 'oldpassword'}});
  264. wrapper
  265. .find('PasswordForm input[name="passwordNew"]')
  266. .simulate('change', {target: {value: 'newpassword'}});
  267. wrapper
  268. .find('PasswordForm input[name="passwordVerify"]')
  269. .simulate('change', {target: {value: 'newpassword'}});
  270. wrapper.find('PasswordForm form').simulate('submit');
  271. expect(mock).toHaveBeenCalledWith(
  272. url,
  273. expect.objectContaining({
  274. method: 'PUT',
  275. data: {
  276. password: 'oldpassword',
  277. passwordNew: 'newpassword',
  278. passwordVerify: 'newpassword',
  279. },
  280. })
  281. );
  282. // user is not 2fa enrolled
  283. expect(wrapper.find('TwoFactorRequired')).toHaveLength(1);
  284. });
  285. it('requires current password to be entered', function() {
  286. Client.addMockResponse({
  287. url: ENDPOINT,
  288. body: [TestStubs.Authenticators().Recovery({isEnrolled: false})],
  289. });
  290. const url = '/users/me/password/';
  291. const mock = Client.addMockResponse({
  292. url,
  293. method: 'PUT',
  294. });
  295. const wrapper = mountWithTheme(
  296. <AccountSecurityWrapper>
  297. <AccountSecurity />
  298. </AccountSecurityWrapper>,
  299. TestStubs.routerContext()
  300. );
  301. wrapper
  302. .find('PasswordForm input[name="passwordNew"]')
  303. .simulate('change', {target: {value: 'newpassword'}});
  304. wrapper
  305. .find('PasswordForm input[name="passwordVerify"]')
  306. .simulate('change', {target: {value: 'newpassword'}});
  307. wrapper.find('PasswordForm form').simulate('submit');
  308. expect(mock).not.toHaveBeenCalled();
  309. // user is not 2fa enrolled
  310. expect(wrapper.find('TwoFactorRequired')).toHaveLength(1);
  311. });
  312. it('can expire all sessions', function() {
  313. Client.addMockResponse({
  314. url: ENDPOINT,
  315. body: [TestStubs.Authenticators().Recovery({isEnrolled: false})],
  316. });
  317. const mock = Client.addMockResponse({
  318. url: AUTH_ENDPOINT,
  319. body: {all: true},
  320. method: 'DELETE',
  321. status: 204,
  322. });
  323. const wrapper = mountWithTheme(
  324. <AccountSecurityWrapper>
  325. <AccountSecurity />
  326. </AccountSecurityWrapper>,
  327. TestStubs.routerContext()
  328. );
  329. wrapper.find('Button[data-test-id="signoutAll"]').simulate('click');
  330. expect(mock).toHaveBeenCalled();
  331. });
  332. });