accounts-frontend/packages/app/services/authFlow/MfaState.test.ts

104 lines
2.8 KiB
TypeScript
Raw Normal View History

import expect from 'app/test/unexpected';
import sinon, { SinonMock } from 'sinon';
2017-08-23 00:09:08 +05:30
import MfaState from './MfaState';
import CompleteState from 'app/services/authFlow/CompleteState';
import PasswordState from 'app/services/authFlow/PasswordState';
2017-08-23 00:09:08 +05:30
2020-05-24 04:38:24 +05:30
import { bootstrap, expectState, expectNavigate, expectRun, MockedAuthContext } from './helpers';
2017-08-23 00:09:08 +05:30
describe('MfaState', () => {
2020-05-24 04:38:24 +05:30
let state: MfaState;
let context: MockedAuthContext;
let mock: SinonMock;
2017-08-23 00:09:08 +05:30
2020-05-24 04:38:24 +05:30
beforeEach(() => {
state = new MfaState();
2017-08-23 00:09:08 +05:30
2020-05-24 04:38:24 +05:30
const data = bootstrap();
context = data.context;
mock = data.mock;
});
2017-08-23 00:09:08 +05:30
2020-05-24 04:38:24 +05:30
afterEach(() => {
mock.verify();
});
2020-05-24 04:38:24 +05:30
describe('#enter', () => {
it('should navigate to /mfa', () => {
context.getState.returns({
auth: {
credentials: {
login: 'foo',
password: 'bar',
isTotpRequired: true,
},
},
});
expectNavigate(mock, '/mfa');
state.enter(context);
});
it('should transition to complete if no totp required', () => {
context.getState.returns({
auth: {
credentials: {
login: 'foo',
password: 'bar',
},
},
});
expectState(mock, CompleteState);
state.enter(context);
});
});
describe('#resolve', () => {
it('should call login with login and password', () => {
const expectedLogin = 'foo';
const expectedPassword = 'bar';
const expectedTotp = '111222';
const expectedRememberMe = true;
context.getState.returns({
auth: {
credentials: {
login: expectedLogin,
password: expectedPassword,
rememberMe: expectedRememberMe,
},
},
});
expectRun(mock, 'setAccountSwitcher', false);
2020-05-24 04:38:24 +05:30
expectRun(
mock,
'login',
sinon.match({
totp: expectedTotp,
login: expectedLogin,
password: expectedPassword,
rememberMe: expectedRememberMe,
}),
).returns(Promise.resolve());
expectState(mock, CompleteState);
const payload = { totp: expectedTotp };
return expect(state.resolve(context, payload), 'to be fulfilled');
});
2017-08-23 00:09:08 +05:30
});
2020-05-24 04:38:24 +05:30
describe('#goBack', () => {
it('should transition to login state', () => {
expectState(mock, PasswordState);
2017-08-23 00:09:08 +05:30
2020-05-24 04:38:24 +05:30
state.goBack(context);
});
2017-08-23 00:09:08 +05:30
});
});