2019-12-07 16:58:52 +05:30
|
|
|
/* eslint @typescript-eslint/camelcase: off */
|
2017-05-26 00:41:57 +05:30
|
|
|
import sinon from 'sinon';
|
|
|
|
|
2019-12-08 00:32:00 +05:30
|
|
|
import OAuthState from 'app/services/authFlow/OAuthState';
|
|
|
|
import CompleteState from 'app/services/authFlow/CompleteState';
|
2016-03-21 11:46:37 +05:30
|
|
|
|
|
|
|
import { bootstrap, expectState, expectRun } from './helpers';
|
|
|
|
|
|
|
|
describe('OAuthState', () => {
|
2019-11-27 14:33:32 +05:30
|
|
|
let state;
|
|
|
|
let context;
|
|
|
|
let mock;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
state = new OAuthState();
|
|
|
|
|
|
|
|
const data = bootstrap();
|
|
|
|
context = data.context;
|
|
|
|
mock = data.mock;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.verify();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#enter', () => {
|
|
|
|
it('should run oAuthValidate', () => {
|
|
|
|
const query = {
|
|
|
|
client_id: 'client_id',
|
|
|
|
redirect_uri: 'redirect_uri',
|
|
|
|
response_type: 'response_type',
|
|
|
|
description: 'description',
|
2019-12-12 13:02:06 +05:30
|
|
|
scope: 'scope1 scope2',
|
2019-11-27 14:33:32 +05:30
|
|
|
prompt: 'none',
|
|
|
|
login_hint: '1',
|
|
|
|
state: 'state',
|
|
|
|
};
|
|
|
|
|
|
|
|
context.getRequest.returns({
|
|
|
|
query: new URLSearchParams(query),
|
|
|
|
params: {},
|
|
|
|
});
|
|
|
|
|
|
|
|
expectRun(
|
|
|
|
mock,
|
|
|
|
'oAuthValidate',
|
|
|
|
sinon.match({
|
|
|
|
clientId: query.client_id,
|
|
|
|
redirectUrl: query.redirect_uri,
|
|
|
|
responseType: query.response_type,
|
|
|
|
description: query.description,
|
|
|
|
scope: query.scope,
|
|
|
|
prompt: query.prompt,
|
|
|
|
loginHint: query.login_hint,
|
|
|
|
state: query.state,
|
|
|
|
}),
|
|
|
|
).returns({ then() {} });
|
|
|
|
|
|
|
|
state.enter(context);
|
|
|
|
});
|
2016-03-21 11:46:37 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should support clientId through route params', () => {
|
|
|
|
const clientId = 'client_id';
|
|
|
|
const query = {
|
|
|
|
redirect_uri: 'redirect_uri',
|
|
|
|
response_type: 'response_type',
|
2019-12-12 13:02:06 +05:30
|
|
|
scope: 'scope1 scope2',
|
2019-11-27 14:33:32 +05:30
|
|
|
state: 'state',
|
|
|
|
};
|
|
|
|
|
|
|
|
context.getRequest.returns({
|
|
|
|
query: new URLSearchParams(query),
|
|
|
|
params: { clientId },
|
|
|
|
});
|
|
|
|
|
|
|
|
expectRun(
|
|
|
|
mock,
|
|
|
|
'oAuthValidate',
|
|
|
|
sinon.match({
|
|
|
|
clientId,
|
|
|
|
redirectUrl: query.redirect_uri,
|
|
|
|
responseType: query.response_type,
|
|
|
|
scope: query.scope,
|
|
|
|
state: query.state,
|
|
|
|
}),
|
|
|
|
).returns({ then() {} });
|
|
|
|
|
|
|
|
state.enter(context);
|
2016-03-21 11:46:37 +05:30
|
|
|
});
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
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',
|
2019-12-12 13:02:06 +05:30
|
|
|
scope: 'scope1 scope2',
|
2019-11-27 14:33:32 +05:30
|
|
|
state: 'state',
|
|
|
|
};
|
|
|
|
|
|
|
|
context.getRequest.returns({
|
|
|
|
query: new URLSearchParams(query),
|
|
|
|
params: { clientId },
|
|
|
|
});
|
|
|
|
|
|
|
|
expectRun(
|
|
|
|
mock,
|
|
|
|
'oAuthValidate',
|
|
|
|
sinon.match({
|
|
|
|
clientId: query.client_id,
|
|
|
|
redirectUrl: query.redirect_uri,
|
|
|
|
responseType: query.response_type,
|
|
|
|
scope: query.scope,
|
|
|
|
state: query.state,
|
|
|
|
}),
|
|
|
|
).returns({ then() {} });
|
|
|
|
|
|
|
|
state.enter(context);
|
2016-03-21 11:46:37 +05:30
|
|
|
});
|
|
|
|
|
2019-12-12 13:02:06 +05:30
|
|
|
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',
|
|
|
|
state: 'state',
|
|
|
|
};
|
|
|
|
|
|
|
|
context.getRequest.returns({
|
|
|
|
query: new URLSearchParams(query),
|
|
|
|
});
|
|
|
|
|
|
|
|
expectRun(
|
|
|
|
mock,
|
|
|
|
'oAuthValidate',
|
|
|
|
sinon.match({
|
|
|
|
clientId: query.client_id,
|
|
|
|
redirectUrl: query.redirect_uri,
|
|
|
|
responseType: query.response_type,
|
|
|
|
scope: 'scope1 scope2',
|
|
|
|
state: query.state,
|
|
|
|
}),
|
|
|
|
).returns({ then() {} });
|
|
|
|
|
|
|
|
state.enter(context);
|
|
|
|
});
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
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
|
|
|
});
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
2016-03-21 11:46:37 +05:30
|
|
|
});
|