accountSecurityEnroll.spec.tsx 2.9 KB

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