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

170 lines
5.1 KiB
TypeScript
Raw Normal View History

import sinon, { SinonMock } from 'sinon';
import InitOAuthAuthCodeFlowState from './InitOAuthAuthCodeFlowState';
import CompleteState from 'app/services/authFlow/CompleteState';
2020-05-24 04:38:24 +05:30
import { bootstrap, expectState, expectRun, MockedAuthContext } from './helpers';
describe('OAuthState', () => {
let state: InitOAuthAuthCodeFlowState;
2020-05-24 04:38:24 +05:30
let context: MockedAuthContext;
let mock: SinonMock;
2020-05-24 04:38:24 +05:30
beforeEach(() => {
state = new InitOAuthAuthCodeFlowState();
2020-05-24 04:38:24 +05:30
const data = bootstrap();
context = data.context;
mock = data.mock;
});
2020-05-24 04:38:24 +05:30
afterEach(() => {
mock.verify();
});
2020-05-24 04:38:24 +05:30
describe('#enter', () => {
it('should run oAuthValidate', () => {
const query = {
client_id: 'client_id',
redirect_uri: 'redirect_uri',
response_type: 'response_type',
description: 'description',
scope: 'scope1 scope2',
prompt: 'none',
login_hint: '1',
state: 'state',
};
context.getRequest.returns({
query: new URLSearchParams(query),
params: {},
});
expectRun(
mock,
'oAuthValidate',
sinon.match({
2024-12-23 17:03:41 +05:30
params: {
clientId: query.client_id,
redirectUrl: query.redirect_uri,
responseType: query.response_type,
scope: query.scope,
state: query.state,
},
2020-05-24 04:38:24 +05:30
description: query.description,
prompt: query.prompt,
loginHint: query.login_hint,
}),
).returns({ then() {} });
state.enter(context);
});
it('should support clientId through route params', () => {
const clientId = 'client_id';
const query = {
redirect_uri: 'redirect_uri',
response_type: 'response_type',
scope: 'scope1 scope2',
state: 'state',
};
context.getRequest.returns({
query: new URLSearchParams(query),
params: { clientId },
});
expectRun(
mock,
'oAuthValidate',
sinon.match({
2024-12-23 17:03:41 +05:30
params: {
clientId,
redirectUrl: query.redirect_uri,
responseType: query.response_type,
scope: query.scope,
state: query.state,
},
2020-05-24 04:38:24 +05:30
}),
).returns({ then() {} });
state.enter(context);
});
it('should give preference to client_id from query', () => {
const clientId = 'wrong_id';
const query = {
client_id: 'client_id',
redirect_uri: 'redirect_uri',
response_type: 'response_type',
scope: 'scope1 scope2',
state: 'state',
};
context.getRequest.returns({
query: new URLSearchParams(query),
params: { clientId },
});
expectRun(
mock,
'oAuthValidate',
sinon.match({
2024-12-23 17:03:41 +05:30
params: {
clientId: query.client_id,
redirectUrl: query.redirect_uri,
responseType: query.response_type,
scope: query.scope,
state: query.state,
},
2020-05-24 04:38:24 +05:30
}),
).returns({ then() {} });
state.enter(context);
});
it('should replace commas with spaces in scope param', () => {
const query = {
client_id: 'client_id',
redirect_uri: 'redirect_uri',
response_type: 'response_type',
scope: 'scope1,scope2,scope3',
state: 'state',
};
context.getRequest.returns({
query: new URLSearchParams(query),
});
expectRun(
mock,
'oAuthValidate',
sinon.match({
2024-12-23 17:03:41 +05:30
params: {
clientId: query.client_id,
redirectUrl: query.redirect_uri,
responseType: query.response_type,
scope: 'scope1 scope2 scope3',
state: query.state,
},
2020-05-24 04:38:24 +05:30
}),
).returns({ then() {} });
state.enter(context);
});
it('should transition to complete state on success', () => {
const promise = Promise.resolve();
context.getRequest.returns({ query: new URLSearchParams(), params: {} });
mock.expects('run').returns(promise);
expectState(mock, CompleteState);
state.enter(context);
return promise;
});
});
});