2016-03-21 11:46:37 +05:30
|
|
|
import OAuthState from 'services/authFlow/OAuthState';
|
|
|
|
import CompleteState from 'services/authFlow/CompleteState';
|
|
|
|
|
|
|
|
import { bootstrap, expectState, expectRun } from './helpers';
|
|
|
|
|
|
|
|
describe('OAuthState', () => {
|
|
|
|
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',
|
2016-08-23 10:00:06 +05:30
|
|
|
description: 'description',
|
2016-03-21 11:46:37 +05:30
|
|
|
scope: 'scope',
|
|
|
|
state: 'state'
|
|
|
|
};
|
|
|
|
|
2016-08-07 20:19:26 +05:30
|
|
|
context.getRequest.returns({query, params: {}});
|
|
|
|
|
|
|
|
expectRun(
|
|
|
|
mock,
|
|
|
|
'oAuthValidate',
|
|
|
|
sinon.match({
|
|
|
|
clientId: query.client_id,
|
|
|
|
redirectUrl: query.redirect_uri,
|
|
|
|
responseType: query.response_type,
|
2016-08-23 10:00:06 +05:30
|
|
|
description: query.description,
|
2016-08-07 20:19:26 +05:30
|
|
|
scope: query.scope,
|
|
|
|
state: query.state
|
|
|
|
})
|
|
|
|
).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: 'scope',
|
|
|
|
state: 'state'
|
|
|
|
};
|
|
|
|
|
|
|
|
context.getRequest.returns({
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
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: 'scope',
|
|
|
|
state: 'state'
|
|
|
|
};
|
|
|
|
|
|
|
|
context.getRequest.returns({
|
|
|
|
query,
|
|
|
|
params: {clientId}
|
|
|
|
});
|
2016-03-21 11:46:37 +05:30
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should transition to complete state on success', () => {
|
|
|
|
const promise = Promise.resolve();
|
|
|
|
|
2016-08-07 20:19:26 +05:30
|
|
|
context.getRequest.returns({query: {}, params: {}});
|
2016-03-21 11:46:37 +05:30
|
|
|
|
|
|
|
mock.expects('run').returns(promise);
|
|
|
|
expectState(mock, CompleteState);
|
|
|
|
|
|
|
|
state.enter(context);
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|