accounts-frontend/tests/components/user/middlewares/refreshTokenMiddleware.test.js

180 lines
5.9 KiB
JavaScript
Raw Normal View History

2016-08-10 00:47:49 +05:30
import expect from 'unexpected';
import refreshTokenMiddleware from 'components/user/middlewares/refreshTokenMiddleware';
import authentication from 'services/api/authentication';
const refreshToken = 'foo';
const expiredToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0NzA3NjE0NDMsImV4cCI6MTQ3MDc2MTQ0MywiaWF0IjoxNDcwNzYxNDQzLCJqdGkiOiJpZDEyMzQ1NiJ9.gWdnzfQQvarGpkbldUvB8qdJZSVkvdNtCbhbbl2yJW8';
// valid till 2100 year
const validToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0NzA3NjE5NzcsImV4cCI6NDEwMjQ0NDgwMCwiaWF0IjoxNDcwNzYxOTc3LCJqdGkiOiJpZDEyMzQ1NiJ9.M4KY4QgHOUzhpAZjWoHJbGsEJPR-RBsJ1c1BKyxvAoU';
describe('refreshTokenMiddleware', () => {
let middleware;
let getState;
let dispatch;
beforeEach(() => {
sinon.stub(authentication, 'requestToken').named('authentication.requestToken');
getState = sinon.stub().named('store.getState');
dispatch = sinon.stub().named('store.dispatch');
middleware = refreshTokenMiddleware({getState, dispatch});
});
afterEach(() => {
authentication.requestToken.restore();
});
describe('#before', () => {
describe('when token expired', () => {
beforeEach(() => {
getState.returns({
user: {
token: expiredToken,
refreshToken
}
});
2016-08-10 00:47:49 +05:30
});
it('should request new token', () => {
const data = {
url: 'foo',
options: {
headers: {}
}
};
2016-08-10 00:47:49 +05:30
authentication.requestToken.returns(Promise.resolve({token: validToken}));
return middleware.before(data).then((resp) => {
expect(resp, 'to satisfy', data);
expect(authentication.requestToken, 'to have a call satisfying', [
refreshToken
]);
});
});
it('should not apply to refresh-token request', () => {
const data = {url: '/refresh-token'};
const resp = middleware.before(data);
2016-08-10 00:47:49 +05:30
expect(resp, 'to satisfy', data);
expect(authentication.requestToken, 'was not called');
2016-08-10 00:47:49 +05:30
});
it('should not apply if options.autoRefreshToken === false', () => {
const data = {
url: 'foo',
options: {autoRefreshToken: false}
};
middleware.before(data);
expect(authentication.requestToken, 'was not called');
2016-08-10 00:47:49 +05:30
});
xit('should update user with new token'); // TODO: need a way to test, that action was called
xit('should logout if invalid token'); // TODO: need a way to test, that action was called
2016-08-10 00:47:49 +05:30
xit('should logout if token request failed', () => {
authentication.requestToken.returns(Promise.reject());
2016-08-10 00:47:49 +05:30
return middleware.before({url: 'foo'}).then((resp) => {
// TODO: need a way to test, that action was called
expect(dispatch, 'to have a call satisfying', logout);
});
});
2016-08-10 00:47:49 +05:30
});
it('should not be applied if no token', () => {
2016-08-10 00:47:49 +05:30
getState.returns({
user: {}
});
const data = {url: 'foo'};
2016-08-10 00:47:49 +05:30
const resp = middleware.before(data);
expect(resp, 'to satisfy', data);
expect(authentication.requestToken, 'was not called');
});
});
describe('#catch', () => {
const expiredResponse = {
name: 'Unauthorized',
message: 'Token expired',
code: 0,
status: 401,
type: 'yii\\web\\UnauthorizedHttpException'
};
const badTokenReponse = {
name: 'Unauthorized',
message: 'Token expired',
code: 0,
status: 401,
type: 'yii\\web\\UnauthorizedHttpException'
};
let restart;
beforeEach(() => {
2016-08-10 00:47:49 +05:30
getState.returns({
user: {
refreshToken
}
});
restart = sinon.stub().named('restart');
2016-08-10 00:47:49 +05:30
authentication.requestToken.returns(Promise.resolve({token: validToken}));
});
2016-08-10 00:47:49 +05:30
it('should request new token if expired', () =>
middleware.catch(expiredResponse, {options: {}}, restart).then(() => {
2016-08-10 00:47:49 +05:30
expect(authentication.requestToken, 'to have a call satisfying', [
refreshToken
]);
expect(restart, 'was called');
})
);
xit('should logout user if token cannot be refreshed', () => {
// TODO: need a way to test, that action was called
return middleware.catch(badTokenReponse, {options: {}}, restart).then(() => {
// TODO
2016-08-10 00:47:49 +05:30
});
});
it('should pass the request through if options.autoRefreshToken === false', () => {
const promise = middleware.catch(expiredResponse, {
options: {
autoRefreshToken: false
}
}, restart);
return expect(promise, 'to be rejected with', expiredResponse).then(() => {
expect(restart, 'was not called');
expect(authentication.requestToken, 'was not called');
});
});
2016-08-10 00:47:49 +05:30
it('should pass the rest of failed requests through', () => {
const resp = {};
const promise = middleware.catch(resp, {
options: {}
}, restart);
2016-08-10 00:47:49 +05:30
return expect(promise, 'to be rejected with', resp).then(() => {
expect(restart, 'was not called');
expect(authentication.requestToken, 'was not called');
2016-08-10 00:47:49 +05:30
});
});
});
});