accountSecurity.spec.jsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. import React from 'react';
  2. import {mount} from '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. let wrapper = mount(
  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. let wrapper = mount(
  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-trash')).toHaveLength(1);
  52. expect(wrapper.find('CircleIndicator').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. let deleteMock = Client.addMockResponse({
  66. url: `${ENDPOINT}15/`,
  67. method: 'DELETE',
  68. });
  69. expect(deleteMock).not.toHaveBeenCalled();
  70. let wrapper = mount(
  71. <AccountSecurityWrapper>
  72. <AccountSecurity />
  73. </AccountSecurityWrapper>,
  74. TestStubs.routerContext()
  75. );
  76. expect(wrapper.find('CircleIndicator').prop('enabled')).toBe(true);
  77. // This will open confirm modal
  78. wrapper.find('Button .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('CircleIndicator').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. let deleteMock = Client.addMockResponse({
  108. url: `${ENDPOINT}15/`,
  109. method: 'DELETE',
  110. });
  111. expect(deleteMock).not.toHaveBeenCalled();
  112. let wrapper = mount(
  113. <AccountSecurityWrapper>
  114. <AccountSecurity />
  115. </AccountSecurityWrapper>,
  116. TestStubs.routerContext()
  117. );
  118. expect(
  119. wrapper
  120. .find('CircleIndicator')
  121. .first()
  122. .prop('enabled')
  123. ).toBe(true);
  124. // This will open confirm modal
  125. wrapper
  126. .find('Button .icon-trash')
  127. .first()
  128. .simulate('click');
  129. // Confirm
  130. wrapper
  131. .find('Modal Button')
  132. .last()
  133. .simulate('click');
  134. expect(deleteMock).toHaveBeenCalled();
  135. });
  136. it('can not remove last 2fa method when org requires 2fa', function() {
  137. Client.addMockResponse({
  138. url: ENDPOINT,
  139. body: [
  140. TestStubs.Authenticators().Totp({
  141. authId: '15',
  142. configureButton: 'Info',
  143. }),
  144. ],
  145. });
  146. Client.addMockResponse({
  147. url: ORG_ENDPOINT,
  148. body: TestStubs.Organizations({require2FA: true}),
  149. });
  150. let deleteMock = Client.addMockResponse({
  151. url: `${ENDPOINT}15/`,
  152. method: 'DELETE',
  153. });
  154. expect(deleteMock).not.toHaveBeenCalled();
  155. let wrapper = mount(
  156. <AccountSecurityWrapper>
  157. <AccountSecurity />
  158. </AccountSecurityWrapper>,
  159. TestStubs.routerContext()
  160. );
  161. expect(wrapper.find('CircleIndicator').prop('enabled')).toBe(true);
  162. // This will open confirm modal
  163. wrapper.find('Button .icon-trash').simulate('click');
  164. // Confirm
  165. expect(wrapper.find('Modal Button')).toHaveLength(0);
  166. expect(deleteMock).not.toHaveBeenCalled();
  167. });
  168. it('renders a primary interface that is not enrolled', function() {
  169. Client.addMockResponse({
  170. url: ENDPOINT,
  171. body: [TestStubs.Authenticators().Totp({isEnrolled: false})],
  172. });
  173. let wrapper = mount(
  174. <AccountSecurityWrapper>
  175. <AccountSecurity />
  176. </AccountSecurityWrapper>,
  177. TestStubs.routerContext()
  178. );
  179. expect(wrapper.find('AuthenticatorName').prop('children')).toBe('Authenticator App');
  180. // There should be an "Add" button
  181. expect(
  182. wrapper
  183. .find('Button[className="enroll-button"]')
  184. .first()
  185. .prop('children')
  186. ).toBe('Add');
  187. expect(wrapper.find('CircleIndicator').prop('enabled')).toBe(false);
  188. // user is not 2fa enrolled
  189. expect(wrapper.find('TwoFactorRequired')).toHaveLength(1);
  190. });
  191. it('renders a backup interface that is not enrolled', function() {
  192. Client.addMockResponse({
  193. url: ENDPOINT,
  194. body: [TestStubs.Authenticators().Recovery({isEnrolled: false})],
  195. });
  196. let wrapper = mount(
  197. <AccountSecurityWrapper>
  198. <AccountSecurity />
  199. </AccountSecurityWrapper>,
  200. TestStubs.routerContext()
  201. );
  202. expect(wrapper.find('AuthenticatorName').prop('children')).toBe('Recovery Codes');
  203. // There should be an View Codes button
  204. expect(wrapper.find('Button[className="details-button"]')).toHaveLength(0);
  205. expect(wrapper.find('CircleIndicator').prop('enabled')).toBe(false);
  206. // user is not 2fa enrolled
  207. expect(wrapper.find('TwoFactorRequired')).toHaveLength(1);
  208. });
  209. it('renders a backup interface that is enrolled', function() {
  210. Client.addMockResponse({
  211. url: ENDPOINT,
  212. body: [TestStubs.Authenticators().Recovery({isEnrolled: true})],
  213. });
  214. let wrapper = mount(
  215. <AccountSecurityWrapper>
  216. <AccountSecurity />
  217. </AccountSecurityWrapper>,
  218. TestStubs.routerContext()
  219. );
  220. expect(wrapper.find('AuthenticatorName').prop('children')).toBe('Recovery Codes');
  221. // There should be an View Codes button
  222. expect(
  223. wrapper
  224. .find('Button[className="details-button"]')
  225. .first()
  226. .prop('children')
  227. ).toBe('View Codes');
  228. expect(wrapper.find('CircleIndicator').prop('enabled')).toBe(true);
  229. });
  230. it('can change password', function() {
  231. Client.addMockResponse({
  232. url: ENDPOINT,
  233. body: [TestStubs.Authenticators().Recovery({isEnrolled: false})],
  234. });
  235. let url = '/users/me/password/';
  236. let mock = Client.addMockResponse({
  237. url,
  238. method: 'PUT',
  239. });
  240. let wrapper = mount(
  241. <AccountSecurityWrapper>
  242. <AccountSecurity />
  243. </AccountSecurityWrapper>,
  244. TestStubs.routerContext()
  245. );
  246. wrapper
  247. .find('PasswordForm input[name="password"]')
  248. .simulate('change', {target: {value: 'oldpassword'}});
  249. wrapper
  250. .find('PasswordForm input[name="passwordNew"]')
  251. .simulate('change', {target: {value: 'newpassword'}});
  252. wrapper
  253. .find('PasswordForm input[name="passwordVerify"]')
  254. .simulate('change', {target: {value: 'newpassword'}});
  255. wrapper.find('PasswordForm form').simulate('submit');
  256. expect(mock).toHaveBeenCalledWith(
  257. url,
  258. expect.objectContaining({
  259. method: 'PUT',
  260. data: {
  261. password: 'oldpassword',
  262. passwordNew: 'newpassword',
  263. passwordVerify: 'newpassword',
  264. },
  265. })
  266. );
  267. // user is not 2fa enrolled
  268. expect(wrapper.find('TwoFactorRequired')).toHaveLength(1);
  269. });
  270. it('requires current password to be entered', function() {
  271. Client.addMockResponse({
  272. url: ENDPOINT,
  273. body: [TestStubs.Authenticators().Recovery({isEnrolled: false})],
  274. });
  275. let url = '/users/me/password/';
  276. let mock = Client.addMockResponse({
  277. url,
  278. method: 'PUT',
  279. });
  280. let wrapper = mount(
  281. <AccountSecurityWrapper>
  282. <AccountSecurity />
  283. </AccountSecurityWrapper>,
  284. TestStubs.routerContext()
  285. );
  286. wrapper
  287. .find('PasswordForm input[name="passwordNew"]')
  288. .simulate('change', {target: {value: 'newpassword'}});
  289. wrapper
  290. .find('PasswordForm input[name="passwordVerify"]')
  291. .simulate('change', {target: {value: 'newpassword'}});
  292. wrapper.find('PasswordForm form').simulate('submit');
  293. expect(mock).not.toHaveBeenCalled();
  294. // user is not 2fa enrolled
  295. expect(wrapper.find('TwoFactorRequired')).toHaveLength(1);
  296. });
  297. it('can expire all sessions', function() {
  298. Client.addMockResponse({
  299. url: ENDPOINT,
  300. body: [TestStubs.Authenticators().Recovery({isEnrolled: false})],
  301. });
  302. let mock = Client.addMockResponse({
  303. url: AUTH_ENDPOINT,
  304. body: {all: true},
  305. method: 'DELETE',
  306. status: 204,
  307. });
  308. let wrapper = mount(
  309. <AccountSecurityWrapper>
  310. <AccountSecurity />
  311. </AccountSecurityWrapper>,
  312. TestStubs.routerContext()
  313. );
  314. wrapper.find('Button[data-test-id="signoutAll"]').simulate('click');
  315. expect(mock).toHaveBeenCalled();
  316. });
  317. });