accountSecurityEnroll.spec.tsx 2.9 KB

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