accountSecurityEnroll.spec.jsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  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. let wrapper;
  7. describe('Totp', function () {
  8. Client.clearMockResponses();
  9. const authenticator = TestStubs.Authenticators().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. },
  18. ],
  19. });
  20. beforeAll(function () {
  21. Client.addMockResponse({
  22. url: `${ENDPOINT}${authenticator.authId}/enroll/`,
  23. body: authenticator,
  24. });
  25. wrapper = mountWithTheme(
  26. <AccountSecurityEnroll />,
  27. TestStubs.routerContext([
  28. {
  29. router: {
  30. ...TestStubs.router(),
  31. params: {
  32. authId: authenticator.authId,
  33. },
  34. },
  35. },
  36. ])
  37. );
  38. });
  39. it('does not have enrolled circle indicator', function () {
  40. expect(wrapper.find('CircleIndicator').prop('enabled')).toBe(false);
  41. });
  42. it('has qrcode component', function () {
  43. expect(wrapper.find('QRCodeCanvas')).toHaveLength(1);
  44. });
  45. it('can enroll', function () {
  46. const enrollMock = Client.addMockResponse({
  47. url: `${ENDPOINT}${authenticator.authId}/enroll/`,
  48. method: 'POST',
  49. });
  50. wrapper.find('input[name="otp"]').simulate('change', {target: {value: 'otp'}});
  51. wrapper.find('Form').simulate('submit');
  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. Client.addMockResponse({
  65. url: `${ENDPOINT}${authenticator.authId}/enroll/`,
  66. body: {details: 'Already enrolled'},
  67. statusCode: 400,
  68. });
  69. const pushMock = jest.fn();
  70. wrapper = mountWithTheme(
  71. <AccountSecurityEnroll />,
  72. TestStubs.routerContext([
  73. {
  74. router: {
  75. ...TestStubs.router({
  76. push: pushMock,
  77. }),
  78. params: {
  79. authId: authenticator.authId,
  80. },
  81. },
  82. },
  83. ])
  84. );
  85. expect(pushMock).toHaveBeenCalledWith('/settings/account/security/');
  86. });
  87. });
  88. });