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

653 lines
22 KiB
TypeScript
Raw Normal View History

import expect from 'app/test/unexpected';
import sinon, { SinonMock } from 'sinon';
2024-12-23 19:23:49 +05:30
import { SynchronousPromise } from 'synchronous-promise';
2016-07-30 16:14:43 +05:30
import CompleteState from 'app/services/authFlow/CompleteState';
import LoginState from 'app/services/authFlow/LoginState';
import ActivationState from 'app/services/authFlow/ActivationState';
import AcceptRulesState from 'app/services/authFlow/AcceptRulesState';
import FinishState from 'app/services/authFlow/FinishState';
import PermissionsState from 'app/services/authFlow/PermissionsState';
import ChooseAccountState from 'app/services/authFlow/ChooseAccountState';
import { Account } from 'app/components/accounts/reducer';
import AbstractState from './AbstractState';
2020-05-24 04:38:24 +05:30
import { bootstrap, expectState, expectNavigate, expectRun, MockedAuthContext } from './helpers';
describe('CompleteState', () => {
2020-05-24 04:38:24 +05:30
let state: CompleteState;
let context: MockedAuthContext;
let mock: SinonMock;
2020-05-24 04:38:24 +05:30
beforeEach(() => {
state = new CompleteState();
2020-05-24 04:38:24 +05:30
const data = bootstrap();
({ context, mock } = data);
});
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 / for authenticated', () => {
context.getState.returns({
user: {
isGuest: false,
},
auth: {
credentials: {},
},
2020-05-24 04:38:24 +05:30
});
2020-05-24 04:38:24 +05:30
expectNavigate(mock, '/');
2020-05-24 04:38:24 +05:30
state.enter(context);
});
2020-05-24 04:38:24 +05:30
it('should transition to login for guests', () => {
context.getState.returns({
user: {
isGuest: true,
},
auth: {
credentials: {},
},
2020-05-24 04:38:24 +05:30
});
2020-05-24 04:38:24 +05:30
expectState(mock, LoginState);
2020-05-24 04:38:24 +05:30
state.enter(context);
});
2020-05-24 04:38:24 +05:30
it('should transition to activation if account is not activated', () => {
context.getState.returns({
user: {
isGuest: false,
},
auth: {
credentials: {
isActivationRequired: true,
},
},
2020-05-24 04:38:24 +05:30
});
2020-05-24 04:38:24 +05:30
expectState(mock, ActivationState);
2020-05-24 04:38:24 +05:30
state.enter(context);
});
2016-08-03 00:29:29 +05:30
it('should navigate to the / if account is deleted', () => {
context.getState.returns({
user: {
isGuest: false,
shouldAcceptRules: true,
isDeleted: true,
},
auth: {
credentials: {},
},
});
expectNavigate(mock, '/');
state.enter(context);
});
2020-05-24 04:38:24 +05:30
it('should transition to accept-rules if shouldAcceptRules', () => {
context.getState.returns({
user: {
shouldAcceptRules: true,
isGuest: false,
},
auth: {
credentials: {},
},
2020-05-24 04:38:24 +05:30
});
2016-08-03 00:29:29 +05:30
2020-05-24 04:38:24 +05:30
expectState(mock, AcceptRulesState);
2016-08-03 00:29:29 +05:30
2020-05-24 04:38:24 +05:30
state.enter(context);
});
2016-08-03 00:29:29 +05:30
2020-05-24 04:38:24 +05:30
it('should transition to activation with higher priority than shouldAcceptRules', () => {
context.getState.returns({
user: {
shouldAcceptRules: true,
isGuest: false,
},
auth: {
credentials: {
isActivationRequired: true,
},
},
2020-05-24 04:38:24 +05:30
});
2016-08-03 00:29:29 +05:30
2020-05-24 04:38:24 +05:30
expectState(mock, ActivationState);
2016-08-03 00:29:29 +05:30
2020-05-24 04:38:24 +05:30
state.enter(context);
});
describe('oauth', () => {
it('should transition to finish state if code is present', () => {
context.getState.returns({
user: {
isGuest: false,
2020-05-24 04:38:24 +05:30
},
auth: {
credentials: {},
oauth: {
2024-12-23 17:03:41 +05:30
params: {
clientId: 'ely.by',
},
code: 'XXX',
},
2020-05-24 04:38:24 +05:30
},
});
2020-05-24 04:38:24 +05:30
expectState(mock, FinishState);
state.enter(context);
2020-05-24 04:38:24 +05:30
});
describe('permissions', () => {
it('should transition to permissions state if acceptRequired', () => {
context.getState.returns({
user: {
isGuest: false,
},
auth: {
credentials: {},
oauth: {
2024-12-23 17:03:41 +05:30
params: {
clientId: 'ely.by',
},
acceptRequired: true,
},
},
});
expectState(mock, PermissionsState);
state.enter(context);
});
it('should transition to permissions state if prompt=consent', () => {
context.getState.returns({
user: {
isGuest: false,
},
auth: {
credentials: {},
oauth: {
2024-12-23 17:03:41 +05:30
params: {
clientId: 'ely.by',
},
prompt: ['consent'],
},
},
});
expectState(mock, PermissionsState);
state.enter(context);
});
2020-05-24 04:38:24 +05:30
});
describe('account switcher', () => {
it('should transition to ChooseAccountState if user has multiple accs and switcher enabled', () => {
context.getState.returns({
user: {
isGuest: false,
},
accounts: {
available: [{ id: 1 }, { id: 2 }],
active: 1,
},
auth: {
credentials: {},
isSwitcherEnabled: true,
oauth: {
2024-12-23 17:03:41 +05:30
params: {
clientId: 'ely.by',
},
prompt: [],
},
},
});
expectState(mock, ChooseAccountState);
state.enter(context);
});
it('should transition to ChooseAccountState if user isDeleted', () => {
context.getState.returns({
user: {
isDeleted: true,
isGuest: false,
},
accounts: {
available: [{ id: 1 }],
active: 1,
},
auth: {
isSwitcherEnabled: true,
credentials: {},
oauth: {
2024-12-23 17:03:41 +05:30
params: {
clientId: 'ely.by',
},
prompt: [],
},
},
});
expectState(mock, ChooseAccountState);
state.enter(context);
});
it('should NOT transition to ChooseAccountState if user has multiple accs and switcher disabled', () => {
context.getState.returns({
user: {
isGuest: false,
},
accounts: {
available: [{ id: 1 }, { id: 2 }],
active: 1,
},
auth: {
isSwitcherEnabled: false,
credentials: {},
oauth: {
2024-12-23 17:03:41 +05:30
params: {
clientId: 'ely.by',
},
prompt: [],
},
},
});
2024-12-23 17:03:41 +05:30
expectRun(mock, 'oAuthComplete', {}).returns(Promise.resolve());
state.enter(context);
});
it('should transition to ChooseAccountState if prompt=select_account and switcher enabled', () => {
context.getState.returns({
user: {
isGuest: false,
},
accounts: {
available: [{ id: 1 }],
active: 1,
},
auth: {
isSwitcherEnabled: true,
credentials: {},
oauth: {
2024-12-23 17:03:41 +05:30
params: {
clientId: 'ely.by',
},
prompt: ['select_account'],
},
},
});
expectState(mock, ChooseAccountState);
state.enter(context);
});
it('should NOT transition to ChooseAccountState if prompt=select_account and switcher disabled', () => {
context.getState.returns({
user: {
isGuest: false,
},
accounts: {
available: [{ id: 1 }],
active: 1,
},
auth: {
isSwitcherEnabled: false,
credentials: {},
oauth: {
2024-12-23 17:03:41 +05:30
params: {
clientId: 'ely.by',
},
prompt: ['select_account'],
},
},
});
2024-12-23 17:03:41 +05:30
expectRun(mock, 'oAuthComplete', {}).returns(Promise.resolve());
state.enter(context);
});
2020-05-24 04:38:24 +05:30
});
});
});
2020-05-24 04:38:24 +05:30
describe('when user completes oauth', () => {
it('should run oAuthComplete', () => {
context.getState.returns({
user: {
isGuest: false,
},
auth: {
credentials: {},
2020-05-24 04:38:24 +05:30
oauth: {
2024-12-23 17:03:41 +05:30
params: {
clientId: 'ely.by',
},
2020-05-24 04:38:24 +05:30
prompt: [],
},
},
});
2024-12-23 17:03:41 +05:30
expectRun(mock, 'oAuthComplete', sinon.match.object).returns(Promise.resolve());
2020-05-24 04:38:24 +05:30
state.enter(context);
});
2020-05-24 04:38:24 +05:30
it('should run redirect by default', () => {
const expectedUrl = 'foo/bar';
const promise = Promise.resolve({ redirectUri: expectedUrl });
context.getState.returns({
user: {
isGuest: false,
},
auth: {
credentials: {},
2020-05-24 04:38:24 +05:30
oauth: {
2024-12-23 17:03:41 +05:30
params: {
clientId: 'ely.by',
},
2020-05-24 04:38:24 +05:30
prompt: [],
},
},
});
expectRun(mock, 'oAuthComplete', sinon.match.object).returns(promise);
expectRun(mock, 'redirect', expectedUrl);
state.enter(context);
return promise.catch(mock.verify.bind(mock));
});
2020-05-24 04:38:24 +05:30
const testOAuth = (
type: 'resolve' | 'reject',
resp: Record<string, any>,
expectedInstance: typeof AbstractState,
) => {
2024-12-23 19:23:49 +05:30
const promise = SynchronousPromise[type](resp);
2020-05-24 04:38:24 +05:30
context.getState.returns({
user: {
isGuest: false,
},
auth: {
credentials: {},
2020-05-24 04:38:24 +05:30
oauth: {
2024-12-23 17:03:41 +05:30
params: {
clientId: 'ely.by',
},
2020-05-24 04:38:24 +05:30
prompt: [],
},
},
});
expectRun(mock, 'oAuthComplete', sinon.match.object).returns(promise);
expectState(mock, expectedInstance);
state.enter(context);
return promise.catch(mock.verify.bind(mock));
};
2020-05-24 04:38:24 +05:30
it('should transition to finish state if rejected with static_page', () =>
testOAuth('resolve', { redirectUri: 'static_page' }, FinishState));
it('should transition to finish state if rejected with static_page_with_code', () =>
testOAuth('resolve', { redirectUri: 'static_page_with_code' }, FinishState));
it('should transition to login state if rejected with unauthorized', () =>
testOAuth('reject', { unauthorized: true }, LoginState));
it('should transition to permissions state if rejected with acceptRequired', () =>
testOAuth('reject', { acceptRequired: true }, PermissionsState));
describe('when loginHint is set', () => {
const testSuccessLoginHint = (field: keyof Account) => {
const account: Account = {
id: 9,
email: 'some@email.com',
username: 'thatUsername',
token: '',
refreshToken: '',
isDeleted: false,
2020-05-24 04:38:24 +05:30
};
context.getState.returns({
user: {
isGuest: false,
},
accounts: {
available: [account],
active: 100,
},
auth: {
credentials: {},
2020-05-24 04:38:24 +05:30
oauth: {
2024-12-23 17:03:41 +05:30
params: {
clientId: 'ely.by',
},
2020-05-24 04:38:24 +05:30
loginHint: account[field],
prompt: [],
},
},
});
expectRun(mock, 'setAccountSwitcher', false);
expectRun(mock, 'authenticate', account).returns(Promise.resolve());
expectState(mock, CompleteState);
return expect(state.enter(context), 'to be fulfilled');
};
it('should authenticate account if id matches', () => testSuccessLoginHint('id'));
it('should authenticate account if email matches', () => testSuccessLoginHint('email'));
it('should authenticate account if username matches', () => testSuccessLoginHint('username'));
it('should not authenticate if account is already authenticated', () => {
const account = {
id: 9,
email: 'some@email.com',
username: 'thatUsername',
};
context.getState.returns({
user: {
isGuest: false,
},
accounts: {
available: [account],
active: account.id,
},
auth: {
credentials: {},
2020-05-24 04:38:24 +05:30
oauth: {
2024-12-23 17:03:41 +05:30
params: {
clientId: 'ely.by',
},
2020-05-24 04:38:24 +05:30
loginHint: account.id,
prompt: [],
},
},
});
expectRun(mock, 'setAccountSwitcher', false);
2024-12-23 19:23:49 +05:30
expectRun(mock, 'oAuthComplete', {}).returns(SynchronousPromise.resolve());
2020-05-24 04:38:24 +05:30
return expect(state.enter(context), 'to be fulfilled');
});
it('should not authenticate if account was not found and continue auth', () => {
const account = {
id: 9,
email: 'some@email.com',
username: 'thatUsername',
};
context.getState.returns({
user: {
isGuest: false,
},
accounts: {
available: [{ id: 1 }],
active: 1,
},
auth: {
credentials: {},
2020-05-24 04:38:24 +05:30
oauth: {
2024-12-23 17:03:41 +05:30
params: {
clientId: 'ely.by',
},
2020-05-24 04:38:24 +05:30
loginHint: account.id,
prompt: [],
},
},
});
2024-12-23 17:03:41 +05:30
expectRun(mock, 'oAuthComplete', {}).returns(Promise.resolve());
2020-05-24 04:38:24 +05:30
return expect(state.enter(context), 'to be fulfilled');
});
});
});
2020-05-24 04:38:24 +05:30
describe('permissions accept', () => {
it('should set flags, when user accepted permissions', () => {
state = new CompleteState();
expect(state.isPermissionsAccepted, 'to be undefined');
2020-05-24 04:38:24 +05:30
state = new CompleteState({ accept: undefined });
expect(state.isPermissionsAccepted, 'to be undefined');
2020-05-24 04:38:24 +05:30
state = new CompleteState({ accept: true });
expect(state.isPermissionsAccepted, 'to be true');
2020-05-24 04:38:24 +05:30
state = new CompleteState({ accept: false });
expect(state.isPermissionsAccepted, 'to be false');
});
2020-05-24 04:38:24 +05:30
it('should run oAuthComplete passing accept: true', () => {
const expected = { accept: true };
state = new CompleteState(expected);
context.getState.returns({
user: {
isGuest: false,
},
auth: {
credentials: {},
2020-05-24 04:38:24 +05:30
oauth: {
2024-12-23 17:03:41 +05:30
params: {
clientId: 'ely.by',
},
2020-05-24 04:38:24 +05:30
prompt: [],
},
},
});
2024-12-23 17:03:41 +05:30
mock.expects('run').once().withExactArgs('oAuthComplete', sinon.match(expected)).returns(Promise.resolve());
2020-05-24 04:38:24 +05:30
state.enter(context);
});
2020-05-24 04:38:24 +05:30
it('should run oAuthComplete passing accept: false', () => {
const expected = { accept: false };
state = new CompleteState(expected);
context.getState.returns({
user: {
isGuest: false,
},
auth: {
credentials: {},
2020-05-24 04:38:24 +05:30
oauth: {
2024-12-23 17:03:41 +05:30
params: {
clientId: 'ely.by',
},
2020-05-24 04:38:24 +05:30
prompt: [],
},
},
});
2024-12-23 17:03:41 +05:30
expectRun(mock, 'oAuthComplete', sinon.match(expected)).returns(Promise.resolve());
2020-05-24 04:38:24 +05:30
state.enter(context);
});
2020-05-24 04:38:24 +05:30
it('should run oAuthComplete passing accept: true, while acceptRequired: true', () => {
// acceptRequired may block user accept/decline actions, so we need
// to check that they are accessible
const expected = { accept: true };
state = new CompleteState(expected);
context.getState.returns({
user: {
isGuest: false,
},
auth: {
credentials: {},
2020-05-24 04:38:24 +05:30
oauth: {
2024-12-23 17:03:41 +05:30
params: {
clientId: 'ely.by',
},
2020-05-24 04:38:24 +05:30
prompt: [],
acceptRequired: true,
},
},
});
2024-12-23 17:03:41 +05:30
expectRun(mock, 'oAuthComplete', sinon.match(expected)).returns(Promise.resolve());
2020-05-24 04:38:24 +05:30
state.enter(context);
});
2020-05-24 04:38:24 +05:30
it('should run oAuthComplete passing accept: false, while acceptRequired: true', () => {
// acceptRequired may block user accept/decline actions, so we need
// to check that they are accessible
const expected = { accept: false };
state = new CompleteState(expected);
context.getState.returns({
user: {
isGuest: false,
},
auth: {
credentials: {},
2020-05-24 04:38:24 +05:30
oauth: {
2024-12-23 17:03:41 +05:30
params: {
clientId: 'ely.by',
},
2020-05-24 04:38:24 +05:30
prompt: [],
acceptRequired: true,
},
},
});
2024-12-23 17:03:41 +05:30
expectRun(mock, 'oAuthComplete', sinon.match(expected)).returns(Promise.resolve());
2020-05-24 04:38:24 +05:30
state.enter(context);
});
});
});