#181: fix logout for guests. Increase logout speed

This commit is contained in:
SleepWalker 2016-08-07 20:26:29 +03:00
parent 3d8c646a94
commit 322325d4ad
2 changed files with 87 additions and 56 deletions

View File

@ -49,14 +49,24 @@ export function setUser(payload) {
} }
export function logout() { export function logout() {
return (dispatch, getState) => return (dispatch, getState) => {
authentication.logout().then(() => { if (getState().user.token) {
dispatch(setUser({ authentication.logout();
lang: getState().user.lang, }
isGuest: true
})); return new Promise((resolve) => {
dispatch(routeActions.push('/login')); setTimeout(() => { // a tiny timeout to allow logout before user's token will be removed
dispatch(setUser({
lang: getState().user.lang,
isGuest: true
}));
dispatch(routeActions.push('/login'));
resolve();
}, 0);
}); });
};
} }
export function fetchUserData() { export function fetchUserData() {

View File

@ -34,62 +34,83 @@ describe('components/user/actions', () => {
}); });
describe('#logout()', () => { describe('#logout()', () => {
it('should post to /api/authentication/logout with user jwt', () => { beforeEach(() => {
getState.returns({
user: {
lang: 'foo'
}
});
request.post.returns(new Promise((resolve) => {
setTimeout(() => {
// we must not overwrite user's token till request starts
expect(dispatch, 'was not called');
resolve();
}, 0);
}));
return callThunk(logout).then(() => {
expect(request.post, 'to have a call satisfying', [
'/api/authentication/logout'
]);
});
});
it('should change user to guest', () => {
getState.returns({
user: {
lang: 'foo'
}
});
request.post.returns(Promise.resolve()); request.post.returns(Promise.resolve());
return callThunk(logout).then(() => {
expect(dispatch, 'to have a call satisfying', [
setUser({
lang: 'foo',
isGuest: true
})
]);
});
}); });
it('should redirect to /login', () => { describe('user with jwt', () => {
getState.returns({ beforeEach(() => {
user: { getState.returns({
lang: 'foo' user: {
} token: 'iLoveRockNRoll',
lang: 'foo'
}
});
}); });
request.post.returns(Promise.resolve()); it('should post to /api/authentication/logout with user jwt', () => {
// TODO: need an integration test with a middleware, because this
// test is not reliable to check, whether logout request will have token inside
request.post.returns(new Promise((resolve) => {
setTimeout(() => {
// we must not overwrite user's token till request starts
expect(dispatch, 'was not called');
return callThunk(logout).then(() => { resolve();
expect(dispatch, 'to have a call satisfying', [ }, 0);
routeActions.push('/login') }));
]);
return callThunk(logout).then(() => {
expect(request.post, 'to have a call satisfying', [
'/api/authentication/logout'
]);
});
}); });
testChangedToGuest();
testRedirectedToLogin();
}); });
describe('user without jwt', () => { // (a guest with partially filled user's state)
beforeEach(() => {
getState.returns({
user: {
lang: 'foo'
}
});
});
it('should not post to /api/authentication/logout', () =>
callThunk(logout).then(() => {
expect(request.post, 'was not called');
})
);
testChangedToGuest();
testRedirectedToLogin();
});
function testChangedToGuest() {
it('should change user to guest', () =>
callThunk(logout).then(() => {
expect(dispatch, 'to have a call satisfying', [
setUser({
lang: 'foo',
isGuest: true
})
]);
})
);
}
function testRedirectedToLogin() {
it('should redirect to /login', () =>
callThunk(logout).then(() => {
expect(dispatch, 'to have a call satisfying', [
routeActions.push('/login')
]);
})
);
}
}); });
}); });