2017-06-08 01:55:15 +05:30
|
|
|
import sinon from 'sinon';
|
2019-12-08 00:32:00 +05:30
|
|
|
import expect from 'app/test/unexpected';
|
2016-07-30 16:14:43 +05:30
|
|
|
|
2019-12-08 00:32:00 +05:30
|
|
|
import request from 'app/services/request';
|
2016-07-27 23:57:21 +05:30
|
|
|
|
|
|
|
import {
|
2019-11-27 14:33:32 +05:30
|
|
|
setLoadingState,
|
|
|
|
oAuthValidate,
|
|
|
|
oAuthComplete,
|
|
|
|
setClient,
|
|
|
|
setOAuthRequest,
|
|
|
|
setScopes,
|
|
|
|
setOAuthCode,
|
|
|
|
requirePermissionsAccept,
|
|
|
|
login,
|
|
|
|
setLogin,
|
2019-12-08 00:32:00 +05:30
|
|
|
} from 'app/components/auth/actions';
|
2016-07-27 23:57:21 +05:30
|
|
|
|
|
|
|
const oauthData = {
|
2019-11-27 14:33:32 +05:30
|
|
|
clientId: '',
|
|
|
|
redirectUrl: '',
|
|
|
|
responseType: '',
|
|
|
|
scope: '',
|
|
|
|
state: '',
|
2016-07-27 23:57:21 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
describe('components/auth/actions', () => {
|
2019-11-27 14:33:32 +05:30
|
|
|
const dispatch = sinon.stub().named('store.dispatch');
|
|
|
|
const getState = sinon.stub().named('store.getState');
|
|
|
|
|
|
|
|
function callThunk(fn, ...args) {
|
|
|
|
const thunk = fn(...args);
|
|
|
|
|
|
|
|
return thunk(dispatch, getState);
|
|
|
|
}
|
|
|
|
|
|
|
|
function expectDispatchCalls(calls) {
|
|
|
|
expect(
|
|
|
|
dispatch,
|
|
|
|
'to have calls satisfying',
|
|
|
|
[[setLoadingState(true)]]
|
|
|
|
.concat(calls)
|
|
|
|
.concat([[setLoadingState(false)]]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
dispatch.reset();
|
|
|
|
getState.reset();
|
|
|
|
getState.returns({});
|
|
|
|
sinon.stub(request, 'get').named('request.get');
|
|
|
|
sinon.stub(request, 'post').named('request.post');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2019-12-07 16:58:52 +05:30
|
|
|
(request.get as any).restore();
|
|
|
|
(request.post as any).restore();
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
describe('#oAuthValidate()', () => {
|
|
|
|
let resp;
|
2016-07-27 23:57:21 +05:30
|
|
|
|
|
|
|
beforeEach(() => {
|
2019-11-27 14:33:32 +05:30
|
|
|
resp = {
|
|
|
|
client: { id: 123 },
|
|
|
|
oAuth: { state: 123 },
|
|
|
|
session: {
|
|
|
|
scopes: ['scopes'],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
(request.get as any).returns(Promise.resolve(resp));
|
2016-07-27 23:57:21 +05:30
|
|
|
});
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should send get request to an api', () =>
|
|
|
|
callThunk(oAuthValidate, oauthData).then(() => {
|
|
|
|
expect(request.get, 'to have a call satisfying', [
|
|
|
|
'/api/oauth2/v1/validate',
|
|
|
|
{},
|
|
|
|
]);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should dispatch setClient, setOAuthRequest and setScopes', () =>
|
|
|
|
callThunk(oAuthValidate, oauthData).then(() => {
|
|
|
|
expectDispatchCalls([
|
|
|
|
[setClient(resp.client)],
|
|
|
|
[
|
|
|
|
setOAuthRequest({
|
|
|
|
...resp.oAuth,
|
|
|
|
prompt: 'none',
|
|
|
|
loginHint: undefined,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
[setScopes(resp.session.scopes)],
|
|
|
|
]);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#oAuthComplete()', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
getState.returns({
|
|
|
|
auth: {
|
|
|
|
oauth: oauthData,
|
|
|
|
},
|
|
|
|
});
|
2016-07-27 23:57:21 +05:30
|
|
|
});
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should post to api/oauth2/complete', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
(request.post as any).returns(
|
2019-11-27 14:33:32 +05:30
|
|
|
Promise.resolve({
|
|
|
|
redirectUri: '',
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
return callThunk(oAuthComplete).then(() => {
|
|
|
|
expect(request.post, 'to have a call satisfying', [
|
|
|
|
'/api/oauth2/v1/complete?client_id=&redirect_uri=&response_type=&description=&scope=&prompt=&login_hint=&state=',
|
|
|
|
{},
|
|
|
|
]);
|
|
|
|
});
|
2016-07-27 23:57:21 +05:30
|
|
|
});
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should dispatch setOAuthCode for static_page redirect', () => {
|
|
|
|
const resp = {
|
|
|
|
success: true,
|
|
|
|
redirectUri: 'static_page?code=123&state=',
|
|
|
|
};
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
(request.post as any).returns(Promise.resolve(resp));
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
return callThunk(oAuthComplete).then(() => {
|
|
|
|
expectDispatchCalls([
|
|
|
|
[
|
|
|
|
setOAuthCode({
|
|
|
|
success: true,
|
|
|
|
code: '123',
|
|
|
|
displayCode: false,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
2016-07-27 23:57:21 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
it('should resolve to with success false and redirectUri for access_denied', async () => {
|
2019-11-27 14:33:32 +05:30
|
|
|
const resp = {
|
|
|
|
statusCode: 401,
|
|
|
|
error: 'access_denied',
|
|
|
|
redirectUri: 'redirectUri',
|
|
|
|
};
|
2016-07-30 16:14:43 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
(request.post as any).returns(Promise.reject(resp));
|
2016-07-27 23:57:21 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
const data = await callThunk(oAuthComplete);
|
|
|
|
|
|
|
|
expect(data, 'to equal', {
|
|
|
|
success: false,
|
|
|
|
redirectUri: 'redirectUri',
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
|
|
|
});
|
2016-07-27 23:57:21 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should dispatch requirePermissionsAccept if accept_required', () => {
|
|
|
|
const resp = {
|
|
|
|
statusCode: 401,
|
|
|
|
error: 'accept_required',
|
|
|
|
};
|
2016-07-27 23:57:21 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
(request.post as any).returns(Promise.reject(resp));
|
2016-07-27 23:57:21 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
return callThunk(oAuthComplete).catch(error => {
|
|
|
|
expect(error.acceptRequired, 'to be true');
|
2019-11-27 14:33:32 +05:30
|
|
|
expectDispatchCalls([[requirePermissionsAccept()]]);
|
|
|
|
});
|
2016-07-27 23:57:21 +05:30
|
|
|
});
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
describe('#login()', () => {
|
|
|
|
describe('when correct login was entered', () => {
|
|
|
|
beforeEach(() => {
|
2019-12-07 16:58:52 +05:30
|
|
|
(request.post as any).returns(
|
2019-11-27 14:33:32 +05:30
|
|
|
Promise.reject({
|
|
|
|
errors: {
|
|
|
|
password: 'error.password_required',
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
2016-11-13 02:01:44 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should set login', () =>
|
|
|
|
callThunk(login, { login: 'foo' }).then(() => {
|
|
|
|
expectDispatchCalls([[setLogin('foo')]]);
|
|
|
|
}));
|
2016-11-13 02:01:44 +05:30
|
|
|
});
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
2016-07-27 23:57:21 +05:30
|
|
|
});
|