index.spec.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. import {AccountEmailsFixture} from 'sentry-fixture/accountEmails';
  2. import {AuthenticatorsFixture} from 'sentry-fixture/authenticators';
  3. import {OrganizationsFixture} from 'sentry-fixture/organizations';
  4. import {RouterFixture} from 'sentry-fixture/routerFixture';
  5. import {
  6. render,
  7. renderGlobalModal,
  8. screen,
  9. userEvent,
  10. waitFor,
  11. } from 'sentry-test/reactTestingLibrary';
  12. import ModalStore from 'sentry/stores/modalStore';
  13. import AccountSecurity from 'sentry/views/settings/account/accountSecurity';
  14. import AccountSecurityWrapper from 'sentry/views/settings/account/accountSecurity/accountSecurityWrapper';
  15. const ENDPOINT = '/users/me/authenticators/';
  16. const ORG_ENDPOINT = '/organizations/';
  17. const ACCOUNT_EMAILS_ENDPOINT = '/users/me/emails/';
  18. const AUTH_ENDPOINT = '/auth/';
  19. describe('AccountSecurity', function () {
  20. const router = RouterFixture();
  21. beforeEach(function () {
  22. jest.spyOn(window.location, 'assign').mockImplementation(() => {});
  23. MockApiClient.clearMockResponses();
  24. MockApiClient.addMockResponse({
  25. url: ORG_ENDPOINT,
  26. body: OrganizationsFixture(),
  27. });
  28. MockApiClient.addMockResponse({
  29. url: ACCOUNT_EMAILS_ENDPOINT,
  30. body: AccountEmailsFixture(),
  31. });
  32. });
  33. afterEach(function () {
  34. jest.mocked(window.location.assign).mockRestore();
  35. });
  36. function renderComponent() {
  37. return render(
  38. <AccountSecurityWrapper
  39. location={router.location}
  40. route={router.routes[0]}
  41. routes={router.routes}
  42. router={router}
  43. routeParams={router.params}
  44. params={{...router.params, authId: '15'}}
  45. >
  46. <AccountSecurity
  47. deleteDisabled={false}
  48. authenticators={[]}
  49. hasVerifiedEmail
  50. countEnrolled={0}
  51. handleRefresh={jest.fn()}
  52. onDisable={jest.fn()}
  53. orgsRequire2fa={[]}
  54. location={router.location}
  55. route={router.routes[0]}
  56. routes={router.routes}
  57. router={router}
  58. routeParams={router.params}
  59. params={{...router.params, authId: '15'}}
  60. />
  61. </AccountSecurityWrapper>
  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: [AuthenticatorsFixture().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. AuthenticatorsFixture().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. AuthenticatorsFixture().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. AuthenticatorsFixture().Totp({
  132. authId: '15',
  133. configureButton: 'Info',
  134. }),
  135. AuthenticatorsFixture().U2f(),
  136. ],
  137. });
  138. MockApiClient.addMockResponse({
  139. url: ORG_ENDPOINT,
  140. body: OrganizationsFixture({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. AuthenticatorsFixture().Totp({
  161. authId: '15',
  162. configureButton: 'Info',
  163. }),
  164. ],
  165. });
  166. MockApiClient.addMockResponse({
  167. url: ORG_ENDPOINT,
  168. body: OrganizationsFixture({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: [AuthenticatorsFixture().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: [AuthenticatorsFixture().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: [AuthenticatorsFixture().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. AuthenticatorsFixture().Totp({disallowNewEnrollment: false}),
  237. AuthenticatorsFixture().U2f({disallowNewEnrollment: undefined}),
  238. AuthenticatorsFixture().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: [
  250. AuthenticatorsFixture().Sms({isEnrolled: true, disallowNewEnrollment: true}),
  251. ],
  252. });
  253. renderComponent();
  254. // Should still render the authenticator since we are already enrolled
  255. expect(await screen.findByText('Text Message')).toBeInTheDocument();
  256. });
  257. it('renders a backup interface that is enrolled', async function () {
  258. MockApiClient.addMockResponse({
  259. url: ENDPOINT,
  260. body: [AuthenticatorsFixture().Recovery({isEnrolled: true})],
  261. });
  262. renderComponent();
  263. expect(await screen.findByText('Recovery Codes')).toBeInTheDocument();
  264. expect(screen.getByRole('button', {name: 'View Codes'})).toBeEnabled();
  265. });
  266. it('can change password', async function () {
  267. MockApiClient.addMockResponse({
  268. url: ENDPOINT,
  269. body: [AuthenticatorsFixture().Recovery({isEnrolled: false})],
  270. });
  271. const url = '/users/me/password/';
  272. const mock = MockApiClient.addMockResponse({
  273. url,
  274. method: 'PUT',
  275. });
  276. renderComponent();
  277. await userEvent.type(
  278. await screen.findByRole('textbox', {name: 'Current Password'}),
  279. 'oldpassword'
  280. );
  281. await userEvent.type(
  282. screen.getByRole('textbox', {name: 'New Password'}),
  283. 'newpassword'
  284. );
  285. await userEvent.type(
  286. screen.getByRole('textbox', {name: 'Verify New Password'}),
  287. 'newpassword'
  288. );
  289. await userEvent.click(screen.getByRole('button', {name: 'Change password'}));
  290. expect(mock).toHaveBeenCalledWith(
  291. url,
  292. expect.objectContaining({
  293. method: 'PUT',
  294. data: {
  295. password: 'oldpassword',
  296. passwordNew: 'newpassword',
  297. passwordVerify: 'newpassword',
  298. },
  299. })
  300. );
  301. });
  302. it('requires current password to be entered', async function () {
  303. MockApiClient.addMockResponse({
  304. url: ENDPOINT,
  305. body: [AuthenticatorsFixture().Recovery({isEnrolled: false})],
  306. });
  307. const url = '/users/me/password/';
  308. const mock = MockApiClient.addMockResponse({
  309. url,
  310. method: 'PUT',
  311. });
  312. renderComponent();
  313. await userEvent.type(
  314. await screen.findByRole('textbox', {name: 'New Password'}),
  315. 'newpassword'
  316. );
  317. await userEvent.type(
  318. screen.getByRole('textbox', {name: 'Verify New Password'}),
  319. 'newpassword'
  320. );
  321. await userEvent.click(screen.getByRole('button', {name: 'Change password'}));
  322. expect(mock).not.toHaveBeenCalled();
  323. });
  324. it('can expire all sessions', async function () {
  325. MockApiClient.addMockResponse({
  326. url: ENDPOINT,
  327. body: [AuthenticatorsFixture().Recovery({isEnrolled: false})],
  328. });
  329. const mock = MockApiClient.addMockResponse({
  330. url: AUTH_ENDPOINT,
  331. body: {all: true},
  332. method: 'DELETE',
  333. status: 204,
  334. });
  335. renderComponent();
  336. await userEvent.click(
  337. await screen.findByRole('button', {name: 'Sign out of all devices'})
  338. );
  339. expect(mock).toHaveBeenCalled();
  340. await waitFor(() =>
  341. expect(window.location.assign).toHaveBeenCalledWith('/auth/login/')
  342. );
  343. });
  344. });