2020-01-18 02:07:52 +05:30
|
|
|
import sinon, { SinonMock } from 'sinon';
|
2017-05-26 00:41:57 +05:30
|
|
|
|
2024-12-11 01:12:06 +05:30
|
|
|
import InitOAuthAuthCodeFlowState from './InitOAuthAuthCodeFlowState';
|
2019-12-08 00:32:00 +05:30
|
|
|
import CompleteState from 'app/services/authFlow/CompleteState';
|
2016-03-21 11:46:37 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
import { bootstrap, expectState, expectRun, MockedAuthContext } from './helpers';
|
2016-03-21 11:46:37 +05:30
|
|
|
|
|
|
|
describe('OAuthState', () => {
|
2024-12-11 01:12:06 +05:30
|
|
|
let state: InitOAuthAuthCodeFlowState;
|
2020-05-24 04:38:24 +05:30
|
|
|
let context: MockedAuthContext;
|
|
|
|
let mock: SinonMock;
|
2016-03-21 11:46:37 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
beforeEach(() => {
|
2024-12-11 01:12:06 +05:30
|
|
|
state = new InitOAuthAuthCodeFlowState();
|
2016-03-21 11:46:37 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
const data = bootstrap();
|
|
|
|
context = data.context;
|
|
|
|
mock = data.mock;
|
2016-03-21 11:46:37 +05:30
|
|
|
});
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
afterEach(() => {
|
|
|
|
mock.verify();
|
2019-12-12 13:02:06 +05:30
|
|
|
});
|
|
|
|
|
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;
|
|
|
|
});
|
2016-03-21 11:46:37 +05:30
|
|
|
});
|
|
|
|
});
|