#167: fix bug /logout request failed due to missing jwt. Added corresponding tests

This commit is contained in:
SleepWalker 2016-07-28 22:51:47 +03:00
parent 3804c1c143
commit d9d4f55426
2 changed files with 103 additions and 16 deletions

View File

@ -7,8 +7,8 @@ import { setLocale } from 'components/i18n/actions';
export const UPDATE = 'USER_UPDATE';
/**
* @param {string|Object} payload jwt token or user object
* @return {Object} action definition
* @param {string|object} payload jwt token or user object
* @return {object} action definition
*/
export function updateUser(payload) {
return {
@ -45,14 +45,14 @@ export function setUser(payload) {
}
export function logout() {
return (dispatch, getState) => {
authentication.logout();
dispatch(setUser({
lang: getState().user.lang,
isGuest: true
}));
dispatch(routeActions.push('/login'));
};
return (dispatch, getState) =>
authentication.logout().then(() => {
dispatch(setUser({
lang: getState().user.lang,
isGuest: true
}));
dispatch(routeActions.push('/login'));
});
}
export function fetchUserData() {
@ -127,10 +127,10 @@ function requestAccessToken(refreshToken, dispatch) {
/**
* Ensures, that all user's requests have fresh access token
*
* @param {Function} dispatch
* @param {Function} getState
* @param {function} dispatch
* @param {function} getState
*
* @return {Object} middleware
* @return {object} middleware
*/
function tokenCheckMiddleware(dispatch, getState) {
return {
@ -187,10 +187,10 @@ function tokenCheckMiddleware(dispatch, getState) {
/**
* Applies Bearer header for all requests
*
* @param {Function} dispatch
* @param {Function} getState
* @param {function} dispatch
* @param {function} getState
*
* @return {Object} middleware
* @return {object} middleware
*/
function tokenApplyMiddleware(dispatch, getState) {
return {

View File

@ -0,0 +1,87 @@
import { routeActions } from 'react-router-redux';
import request from 'services/request';
import {
logout,
setUser
} from 'components/user/actions';
describe('components/user/actions', () => {
const dispatch = sinon.stub();
const getState = sinon.stub();
const callThunk = function(fn, ...args) {
const thunk = fn(...args);
return thunk(dispatch, getState);
};
beforeEach(() => {
dispatch.reset();
getState.reset();
getState.returns({});
sinon.stub(request, 'get');
sinon.stub(request, 'post');
});
afterEach(() => {
request.get.restore();
request.post.restore();
});
describe('#logout()', () => {
it('should post to /api/authentication/logout with user jwt', () => {
getState.returns({
user: {
lang: 'foo'
}
});
request.post.returns(new Promise((resolve) => {
setTimeout(() => {
// we must not overwrite user's token till request starts
sinon.assert.notCalled(dispatch);
resolve();
}, 0);
}));
return callThunk(logout).then(() => {
sinon.assert.calledWith(request.post, '/api/authentication/logout');
});
});
it('should change user to guest', () => {
getState.returns({
user: {
lang: 'foo'
}
});
request.post.returns(Promise.resolve());
return callThunk(logout).then(() => {
sinon.assert.calledWith(dispatch, setUser({
lang: 'foo',
isGuest: true
}));
});
});
it('should redirect to /login', () => {
getState.returns({
user: {
lang: 'foo'
}
});
request.post.returns(Promise.resolve());
return callThunk(logout).then(() => {
sinon.assert.calledWith(dispatch, routeActions.push('/login'));
});
});
});
});