12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
- import AccountSecurityEnroll from 'sentry/views/settings/account/accountSecurity/accountSecurityEnroll';
- const ENDPOINT = '/users/me/authenticators/';
- describe('AccountSecurityEnroll', function () {
- describe('Totp', function () {
- const authenticator = TestStubs.Authenticators().Totp({
- isEnrolled: false,
- qrcode: 'otpauth://totp/test%40sentry.io?issuer=Sentry&secret=secret',
- secret: 'secret',
- form: [
- {
- type: 'string',
- name: 'otp',
- label: 'OTP Code',
- },
- ],
- });
- const routerContext = TestStubs.routerContext([
- {
- router: {
- ...TestStubs.router(),
- params: {authId: authenticator.authId},
- },
- },
- ]);
- beforeEach(function () {
- MockApiClient.clearMockResponses();
- MockApiClient.addMockResponse({
- url: `${ENDPOINT}${authenticator.authId}/enroll/`,
- body: authenticator,
- });
- });
- it('does not have enrolled circle indicator', function () {
- render(<AccountSecurityEnroll />, {context: routerContext});
- expect(
- screen.getByRole('status', {name: 'Authentication Method Inactive'})
- ).toBeInTheDocument();
- });
- it('has qrcode component', function () {
- render(<AccountSecurityEnroll />, {context: routerContext});
- expect(screen.getByLabelText('Enrollment QR Code')).toBeInTheDocument();
- });
- it('can enroll', async function () {
- const enrollMock = MockApiClient.addMockResponse({
- url: `${ENDPOINT}${authenticator.authId}/enroll/`,
- method: 'POST',
- });
- render(<AccountSecurityEnroll />, {context: routerContext});
- await userEvent.type(screen.getByRole('textbox', {name: 'OTP Code'}), 'otp{enter}');
- expect(enrollMock).toHaveBeenCalledWith(
- `${ENDPOINT}15/enroll/`,
- expect.objectContaining({
- method: 'POST',
- data: expect.objectContaining({
- secret: 'secret',
- otp: 'otp',
- }),
- })
- );
- });
- it('can redirect with already enrolled error', function () {
- MockApiClient.addMockResponse({
- url: `${ENDPOINT}${authenticator.authId}/enroll/`,
- body: {details: 'Already enrolled'},
- statusCode: 400,
- });
- const pushMock = jest.fn();
- const routerContextWithMock = TestStubs.routerContext([
- {
- router: {
- ...TestStubs.router({push: pushMock}),
- params: {authId: authenticator.authId},
- },
- },
- ]);
- render(<AccountSecurityEnroll />, {context: routerContextWithMock});
- expect(pushMock).toHaveBeenCalledWith('/settings/account/security/');
- });
- });
- });
|