index.spec.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. import {AccountEmails} from 'sentry-fixture/accountEmails';
  2. import {Authenticators} from 'sentry-fixture/authenticators';
  3. import {Organizations} from 'sentry-fixture/organizations';
  4. import {
  5. render,
  6. renderGlobalModal,
  7. screen,
  8. userEvent,
  9. waitFor,
  10. } from 'sentry-test/reactTestingLibrary';
  11. import ModalStore from 'sentry/stores/modalStore';
  12. import AccountSecurity from 'sentry/views/settings/account/accountSecurity';
  13. import AccountSecurityWrapper from 'sentry/views/settings/account/accountSecurity/accountSecurityWrapper';
  14. const ENDPOINT = '/users/me/authenticators/';
  15. const ORG_ENDPOINT = '/organizations/';
  16. const ACCOUNT_EMAILS_ENDPOINT = '/users/me/emails/';
  17. const AUTH_ENDPOINT = '/auth/';
  18. describe('AccountSecurity', function () {
  19. const router = TestStubs.router();
  20. beforeEach(function () {
  21. jest.spyOn(window.location, 'assign').mockImplementation(() => {});
  22. MockApiClient.clearMockResponses();
  23. MockApiClient.addMockResponse({
  24. url: ORG_ENDPOINT,
  25. body: Organizations(),
  26. });
  27. MockApiClient.addMockResponse({
  28. url: ACCOUNT_EMAILS_ENDPOINT,
  29. body: AccountEmails(),
  30. });
  31. });
  32. afterEach(function () {
  33. (window.location.assign as jest.Mock).mockRestore();
  34. });
  35. function renderComponent() {
  36. return render(
  37. <AccountSecurityWrapper
  38. location={router.location}
  39. route={router.routes[0]}
  40. routes={router.routes}
  41. router={router}
  42. routeParams={router.params}
  43. params={{...router.params, authId: '15'}}
  44. >
  45. <AccountSecurity
  46. deleteDisabled={false}
  47. authenticators={[]}
  48. hasVerifiedEmail
  49. countEnrolled={0}
  50. handleRefresh={jest.fn()}
  51. onDisable={jest.fn()}
  52. orgsRequire2fa={[]}
  53. location={router.location}
  54. route={router.routes[0]}
  55. routes={router.routes}
  56. router={router}
  57. routeParams={router.params}
  58. params={{...router.params, authId: '15'}}
  59. />
  60. </AccountSecurityWrapper>,
  61. {context: TestStubs.routerContext()}
  62. );
  63. }
  64. it('renders empty', async function () {
  65. MockApiClient.addMockResponse({
  66. url: ENDPOINT,
  67. body: [],
  68. });
  69. renderComponent();
  70. expect(
  71. await screen.findByText('No available authenticators to add')
  72. ).toBeInTheDocument();
  73. });
  74. it('renders a primary interface that is enrolled', async function () {
  75. MockApiClient.addMockResponse({
  76. url: ENDPOINT,
  77. body: [Authenticators().Totp({configureButton: 'Info'})],
  78. });
  79. renderComponent();
  80. expect(await screen.findByText('Authenticator App')).toBeInTheDocument();
  81. expect(screen.getByRole('button', {name: 'Info'})).toBeInTheDocument();
  82. expect(screen.getByRole('button', {name: 'Delete'})).toBeInTheDocument();
  83. expect(
  84. screen.getByRole('status', {name: 'Authentication Method Active'})
  85. ).toBeInTheDocument();
  86. });
  87. it('can delete enrolled authenticator', async function () {
  88. MockApiClient.addMockResponse({
  89. url: ENDPOINT,
  90. body: [
  91. Authenticators().Totp({
  92. authId: '15',
  93. configureButton: 'Info',
  94. }),
  95. ],
  96. });
  97. const deleteMock = MockApiClient.addMockResponse({
  98. url: `${ENDPOINT}15/`,
  99. method: 'DELETE',
  100. });
  101. renderComponent();
  102. expect(deleteMock).not.toHaveBeenCalled();
  103. expect(
  104. await screen.findByRole('status', {name: 'Authentication Method Active'})
  105. ).toBeInTheDocument();
  106. // next authenticators request should have totp disabled
  107. const authenticatorsMock = MockApiClient.addMockResponse({
  108. url: ENDPOINT,
  109. body: [
  110. Authenticators().Totp({
  111. isEnrolled: false,
  112. authId: '15',
  113. configureButton: 'Info',
  114. }),
  115. ],
  116. });
  117. await userEvent.click(screen.getByRole('button', {name: 'Delete'}));
  118. renderGlobalModal();
  119. await userEvent.click(screen.getByTestId('confirm-button'));
  120. // Should only have been called once
  121. await waitFor(() => expect(authenticatorsMock).toHaveBeenCalledTimes(1));
  122. expect(deleteMock).toHaveBeenCalled();
  123. expect(
  124. screen.getByRole('status', {name: 'Authentication Method Inactive'})
  125. ).toBeInTheDocument();
  126. });
  127. it('can remove one of multiple 2fa methods when org requires 2fa', async function () {
  128. MockApiClient.addMockResponse({
  129. url: ENDPOINT,
  130. body: [
  131. Authenticators().Totp({
  132. authId: '15',
  133. configureButton: 'Info',
  134. }),
  135. Authenticators().U2f(),
  136. ],
  137. });
  138. MockApiClient.addMockResponse({
  139. url: ORG_ENDPOINT,
  140. body: Organizations({require2FA: true}),
  141. });
  142. const deleteMock = MockApiClient.addMockResponse({
  143. url: `${ENDPOINT}15/`,
  144. method: 'DELETE',
  145. });
  146. expect(deleteMock).not.toHaveBeenCalled();
  147. renderComponent();
  148. expect(
  149. await screen.findAllByRole('status', {name: 'Authentication Method Active'})
  150. ).toHaveLength(2);
  151. await userEvent.click(screen.getAllByRole('button', {name: 'Delete'})[0]);
  152. renderGlobalModal();
  153. await userEvent.click(screen.getByTestId('confirm-button'));
  154. expect(deleteMock).toHaveBeenCalled();
  155. });
  156. it('can not remove last 2fa method when org requires 2fa', async function () {
  157. MockApiClient.addMockResponse({
  158. url: ENDPOINT,
  159. body: [
  160. Authenticators().Totp({
  161. authId: '15',
  162. configureButton: 'Info',
  163. }),
  164. ],
  165. });
  166. MockApiClient.addMockResponse({
  167. url: ORG_ENDPOINT,
  168. body: Organizations({require2FA: true}),
  169. });
  170. const deleteMock = MockApiClient.addMockResponse({
  171. url: `${ENDPOINT}15/`,
  172. method: 'DELETE',
  173. });
  174. renderComponent();
  175. expect(deleteMock).not.toHaveBeenCalled();
  176. expect(
  177. await screen.findByRole('status', {name: 'Authentication Method Active'})
  178. ).toBeInTheDocument();
  179. await userEvent.hover(screen.getByRole('button', {name: 'Delete'}));
  180. expect(screen.getByRole('button', {name: 'Delete'})).toBeDisabled();
  181. expect(
  182. await screen.findByText(
  183. 'Two-factor authentication is required for organization(s): test 1 and test 2.'
  184. )
  185. ).toBeInTheDocument();
  186. });
  187. it('cannot enroll without verified email', async function () {
  188. MockApiClient.addMockResponse({
  189. url: ENDPOINT,
  190. body: [Authenticators().Totp({isEnrolled: false})],
  191. });
  192. MockApiClient.addMockResponse({
  193. url: ACCOUNT_EMAILS_ENDPOINT,
  194. body: [
  195. {
  196. email: 'primary@example.com',
  197. isPrimary: true,
  198. isVerified: false,
  199. },
  200. ],
  201. });
  202. renderComponent();
  203. const openEmailModalFunc = jest.spyOn(ModalStore, 'openModal');
  204. expect(
  205. await screen.findByRole('status', {name: 'Authentication Method Inactive'})
  206. ).toBeInTheDocument();
  207. await userEvent.click(screen.getByRole('button', {name: 'Add'}));
  208. await waitFor(() => expect(openEmailModalFunc).toHaveBeenCalled());
  209. });
  210. it('renders a backup interface that is not enrolled', async function () {
  211. MockApiClient.addMockResponse({
  212. url: ENDPOINT,
  213. body: [Authenticators().Recovery({isEnrolled: false})],
  214. });
  215. renderComponent();
  216. expect(
  217. await screen.findByRole('status', {name: 'Authentication Method Inactive'})
  218. ).toBeInTheDocument();
  219. expect(screen.getByText('Recovery Codes')).toBeInTheDocument();
  220. });
  221. it('renders a primary interface that is not enrolled', async function () {
  222. MockApiClient.addMockResponse({
  223. url: ENDPOINT,
  224. body: [Authenticators().Totp({isEnrolled: false})],
  225. });
  226. renderComponent();
  227. expect(
  228. await screen.findByRole('status', {name: 'Authentication Method Inactive'})
  229. ).toBeInTheDocument();
  230. expect(screen.getByText('Authenticator App')).toBeInTheDocument();
  231. });
  232. it('does not render primary interface that disallows new enrollments', async function () {
  233. MockApiClient.addMockResponse({
  234. url: ENDPOINT,
  235. body: [
  236. Authenticators().Totp({disallowNewEnrollment: false}),
  237. Authenticators().U2f({disallowNewEnrollment: undefined}),
  238. Authenticators().Sms({disallowNewEnrollment: true}),
  239. ],
  240. });
  241. renderComponent();
  242. expect(await screen.findByText('Authenticator App')).toBeInTheDocument();
  243. expect(screen.getByText('U2F (Universal 2nd Factor)')).toBeInTheDocument();
  244. expect(screen.queryByText('Text Message')).not.toBeInTheDocument();
  245. });
  246. it('renders primary interface if new enrollments are disallowed, but we are enrolled', async function () {
  247. MockApiClient.addMockResponse({
  248. url: ENDPOINT,
  249. body: [Authenticators().Sms({isEnrolled: true, disallowNewEnrollment: true})],
  250. });
  251. renderComponent();
  252. // Should still render the authenticator since we are already enrolled
  253. expect(await screen.findByText('Text Message')).toBeInTheDocument();
  254. });
  255. it('renders a backup interface that is enrolled', async function () {
  256. MockApiClient.addMockResponse({
  257. url: ENDPOINT,
  258. body: [Authenticators().Recovery({isEnrolled: true})],
  259. });
  260. renderComponent();
  261. expect(await screen.findByText('Recovery Codes')).toBeInTheDocument();
  262. expect(screen.getByRole('button', {name: 'View Codes'})).toBeEnabled();
  263. });
  264. it('can change password', async function () {
  265. MockApiClient.addMockResponse({
  266. url: ENDPOINT,
  267. body: [Authenticators().Recovery({isEnrolled: false})],
  268. });
  269. const url = '/users/me/password/';
  270. const mock = MockApiClient.addMockResponse({
  271. url,
  272. method: 'PUT',
  273. });
  274. renderComponent();
  275. await userEvent.type(
  276. await screen.findByRole('textbox', {name: 'Current Password'}),
  277. 'oldpassword'
  278. );
  279. await userEvent.type(
  280. screen.getByRole('textbox', {name: 'New Password'}),
  281. 'newpassword'
  282. );
  283. await userEvent.type(
  284. screen.getByRole('textbox', {name: 'Verify New Password'}),
  285. 'newpassword'
  286. );
  287. await userEvent.click(screen.getByRole('button', {name: 'Change password'}));
  288. expect(mock).toHaveBeenCalledWith(
  289. url,
  290. expect.objectContaining({
  291. method: 'PUT',
  292. data: {
  293. password: 'oldpassword',
  294. passwordNew: 'newpassword',
  295. passwordVerify: 'newpassword',
  296. },
  297. })
  298. );
  299. });
  300. it('requires current password to be entered', async function () {
  301. MockApiClient.addMockResponse({
  302. url: ENDPOINT,
  303. body: [Authenticators().Recovery({isEnrolled: false})],
  304. });
  305. const url = '/users/me/password/';
  306. const mock = MockApiClient.addMockResponse({
  307. url,
  308. method: 'PUT',
  309. });
  310. renderComponent();
  311. await userEvent.type(
  312. await screen.findByRole('textbox', {name: 'New Password'}),
  313. 'newpassword'
  314. );
  315. await userEvent.type(
  316. screen.getByRole('textbox', {name: 'Verify New Password'}),
  317. 'newpassword'
  318. );
  319. await userEvent.click(screen.getByRole('button', {name: 'Change password'}));
  320. expect(mock).not.toHaveBeenCalled();
  321. });
  322. it('can expire all sessions', async function () {
  323. MockApiClient.addMockResponse({
  324. url: ENDPOINT,
  325. body: [Authenticators().Recovery({isEnrolled: false})],
  326. });
  327. const mock = MockApiClient.addMockResponse({
  328. url: AUTH_ENDPOINT,
  329. body: {all: true},
  330. method: 'DELETE',
  331. status: 204,
  332. });
  333. renderComponent();
  334. await userEvent.click(
  335. await screen.findByRole('button', {name: 'Sign out of all devices'})
  336. );
  337. expect(mock).toHaveBeenCalled();
  338. await waitFor(() =>
  339. expect(window.location.assign).toHaveBeenCalledWith('/auth/login/')
  340. );
  341. });
  342. });