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