accountSecurityEnroll.spec.jsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import {Client} from 'sentry/api';
  3. import AccountSecurityEnroll from 'sentry/views/settings/account/accountSecurity/accountSecurityEnroll';
  4. const ENDPOINT = '/users/me/authenticators/';
  5. describe('AccountSecurityEnroll', function () {
  6. describe('Totp', function () {
  7. Client.clearMockResponses();
  8. const authenticator = TestStubs.Authenticators().Totp({
  9. isEnrolled: false,
  10. qrcode: 'otpauth://totp/test%40sentry.io?issuer=Sentry&secret=secret',
  11. secret: 'secret',
  12. form: [
  13. {
  14. type: 'string',
  15. name: 'otp',
  16. label: 'OTP Code',
  17. },
  18. ],
  19. });
  20. const routerContext = TestStubs.routerContext([
  21. {
  22. router: {
  23. ...TestStubs.router(),
  24. params: {authId: authenticator.authId},
  25. },
  26. },
  27. ]);
  28. beforeAll(function () {
  29. Client.addMockResponse({
  30. url: `${ENDPOINT}${authenticator.authId}/enroll/`,
  31. body: authenticator,
  32. });
  33. });
  34. it('does not have enrolled circle indicator', function () {
  35. render(<AccountSecurityEnroll />, {context: routerContext});
  36. expect(
  37. screen.getByRole('status', {name: 'Authentication Method Inactive'})
  38. ).toBeInTheDocument();
  39. });
  40. it('has qrcode component', function () {
  41. render(<AccountSecurityEnroll />, {context: routerContext});
  42. expect(screen.getByLabelText('Enrollment QR Code')).toBeInTheDocument();
  43. });
  44. it('can enroll', async function () {
  45. const enrollMock = Client.addMockResponse({
  46. url: `${ENDPOINT}${authenticator.authId}/enroll/`,
  47. method: 'POST',
  48. });
  49. render(<AccountSecurityEnroll />, {context: routerContext});
  50. await userEvent.type(screen.getByRole('textbox', {name: 'OTP Code'}), 'otp{enter}');
  51. expect(enrollMock).toHaveBeenCalledWith(
  52. `${ENDPOINT}15/enroll/`,
  53. expect.objectContaining({
  54. method: 'POST',
  55. data: expect.objectContaining({
  56. secret: 'secret',
  57. otp: 'otp',
  58. }),
  59. })
  60. );
  61. });
  62. it('can redirect with already enrolled error', function () {
  63. Client.addMockResponse({
  64. url: `${ENDPOINT}${authenticator.authId}/enroll/`,
  65. body: {details: 'Already enrolled'},
  66. statusCode: 400,
  67. });
  68. const pushMock = jest.fn();
  69. const routerContextWithMock = TestStubs.routerContext([
  70. {
  71. router: {
  72. ...TestStubs.router({push: pushMock}),
  73. params: {authId: authenticator.authId},
  74. },
  75. },
  76. ]);
  77. render(<AccountSecurityEnroll />, {context: routerContextWithMock});
  78. expect(pushMock).toHaveBeenCalledWith('/settings/account/security/');
  79. });
  80. });
  81. });