accountSecurityEnroll.spec.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import {Authenticators} from 'sentry-fixture/authenticators';
  2. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  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. const authenticator = Authenticators().Totp({
  8. isEnrolled: false,
  9. qrcode: 'otpauth://totp/test%40sentry.io?issuer=Sentry&secret=secret',
  10. secret: 'secret',
  11. form: [
  12. {
  13. type: 'string',
  14. name: 'otp',
  15. label: 'OTP Code',
  16. },
  17. ],
  18. });
  19. const routerContext = TestStubs.routerContext([
  20. {
  21. router: {
  22. ...TestStubs.router(),
  23. params: {authId: authenticator.authId},
  24. },
  25. },
  26. ]);
  27. beforeEach(function () {
  28. MockApiClient.clearMockResponses();
  29. MockApiClient.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 = MockApiClient.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. MockApiClient.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. });