2016-07-30 16:14:43 +05:30
|
|
|
import expect from 'unexpected';
|
|
|
|
|
2016-07-29 01:21:47 +05:30
|
|
|
import { routeActions } from 'react-router-redux';
|
|
|
|
|
|
|
|
import request from 'services/request';
|
2016-11-14 10:58:25 +05:30
|
|
|
import { reset, RESET } from 'components/accounts/actions';
|
2016-07-29 01:21:47 +05:30
|
|
|
|
|
|
|
import {
|
|
|
|
logout,
|
|
|
|
setUser
|
|
|
|
} from 'components/user/actions';
|
|
|
|
|
|
|
|
|
|
|
|
describe('components/user/actions', () => {
|
2016-11-05 15:41:41 +05:30
|
|
|
const getState = sinon.stub().named('store.getState');
|
|
|
|
const dispatch = sinon.spy((arg) =>
|
|
|
|
typeof arg === 'function' ? arg(dispatch, getState) : arg
|
|
|
|
).named('store.dispatch');
|
2016-07-29 01:21:47 +05:30
|
|
|
|
|
|
|
const callThunk = function(fn, ...args) {
|
|
|
|
const thunk = fn(...args);
|
|
|
|
|
|
|
|
return thunk(dispatch, getState);
|
|
|
|
};
|
|
|
|
|
|
|
|
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-29 01:21:47 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
request.get.restore();
|
|
|
|
request.post.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#logout()', () => {
|
2016-08-07 22:56:29 +05:30
|
|
|
beforeEach(() => {
|
|
|
|
request.post.returns(Promise.resolve());
|
2016-07-29 01:21:47 +05:30
|
|
|
});
|
|
|
|
|
2016-08-07 22:56:29 +05:30
|
|
|
describe('user with jwt', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
getState.returns({
|
|
|
|
user: {
|
|
|
|
token: 'iLoveRockNRoll',
|
|
|
|
lang: 'foo'
|
|
|
|
}
|
|
|
|
});
|
2016-07-29 01:21:47 +05:30
|
|
|
});
|
|
|
|
|
2016-08-07 22:56:29 +05:30
|
|
|
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');
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
}, 0);
|
|
|
|
}));
|
|
|
|
|
|
|
|
return callThunk(logout).then(() => {
|
|
|
|
expect(request.post, 'to have a call satisfying', [
|
|
|
|
'/api/authentication/logout'
|
|
|
|
]);
|
|
|
|
});
|
2016-07-29 01:21:47 +05:30
|
|
|
});
|
2016-08-07 22:56:29 +05:30
|
|
|
|
|
|
|
testChangedToGuest();
|
2016-11-14 10:58:25 +05:30
|
|
|
testAccountsReset();
|
2016-08-07 22:56:29 +05:30
|
|
|
testRedirectedToLogin();
|
2016-07-29 01:21:47 +05:30
|
|
|
});
|
|
|
|
|
2016-08-07 22:56:29 +05:30
|
|
|
describe('user without jwt', () => { // (a guest with partially filled user's state)
|
|
|
|
beforeEach(() => {
|
|
|
|
getState.returns({
|
|
|
|
user: {
|
|
|
|
lang: 'foo'
|
|
|
|
}
|
|
|
|
});
|
2016-07-29 01:21:47 +05:30
|
|
|
});
|
|
|
|
|
2016-08-07 22:56:29 +05:30
|
|
|
it('should not post to /api/authentication/logout', () =>
|
|
|
|
callThunk(logout).then(() => {
|
|
|
|
expect(request.post, 'was not called');
|
|
|
|
})
|
|
|
|
);
|
2016-07-29 01:21:47 +05:30
|
|
|
|
2016-08-07 22:56:29 +05:30
|
|
|
testChangedToGuest();
|
2016-11-14 10:58:25 +05:30
|
|
|
testAccountsReset();
|
2016-08-07 22:56:29 +05:30
|
|
|
testRedirectedToLogin();
|
2016-07-29 01:21:47 +05:30
|
|
|
});
|
2016-08-07 22:56:29 +05:30
|
|
|
|
|
|
|
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')
|
|
|
|
]);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2016-11-14 10:58:25 +05:30
|
|
|
|
|
|
|
function testAccountsReset() {
|
|
|
|
it(`should dispatch ${RESET}`, () =>
|
|
|
|
callThunk(logout).then(() => {
|
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
reset()
|
|
|
|
]);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2016-07-29 01:21:47 +05:30
|
|
|
});
|
|
|
|
});
|