accountSecurityEnroll.spec.tsx 2.7 KB

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