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';
|
2016-11-05 15:41:41 +05:30
|
|
|
import { updateToken } from 'components/accounts/actions';
|
2016-08-10 00:47:49 +05:30
|
|
|
|
|
|
|
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');
|
2016-11-15 11:25:15 +05:30
|
|
|
sinon.stub(authentication, 'logout').named('authentication.logout');
|
2016-08-10 00:47:49 +05:30
|
|
|
|
|
|
|
getState = sinon.stub().named('store.getState');
|
2016-11-05 15:41:41 +05:30
|
|
|
dispatch = sinon.spy((arg) =>
|
|
|
|
typeof arg === 'function' ? arg(dispatch, getState) : arg
|
|
|
|
).named('store.dispatch');
|
2016-08-10 00:47:49 +05:30
|
|
|
|
|
|
|
middleware = refreshTokenMiddleware({getState, dispatch});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
authentication.requestToken.restore();
|
2016-11-15 11:25:15 +05:30
|
|
|
authentication.logout.restore();
|
2016-08-10 00:47:49 +05:30
|
|
|
});
|
|
|
|
|
2016-11-05 15:41:41 +05:30
|
|
|
it('must be till 2100 to test with validToken', () =>
|
|
|
|
expect(new Date().getFullYear(), 'to be less than', 2100)
|
|
|
|
);
|
|
|
|
|
2016-08-10 00:47:49 +05:30
|
|
|
describe('#before', () => {
|
2016-10-30 17:42:49 +05:30
|
|
|
describe('when token expired', () => {
|
|
|
|
beforeEach(() => {
|
2016-11-15 11:25:15 +05:30
|
|
|
const account = {
|
|
|
|
token: expiredToken,
|
|
|
|
refreshToken
|
|
|
|
};
|
2016-10-30 17:42:49 +05:30
|
|
|
getState.returns({
|
2016-11-05 15:41:41 +05:30
|
|
|
accounts: {
|
2016-11-15 11:25:15 +05:30
|
|
|
active: account,
|
|
|
|
available: [account]
|
2016-11-05 15:41:41 +05:30
|
|
|
},
|
|
|
|
user: {}
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|
2016-08-10 00:47:49 +05:30
|
|
|
});
|
|
|
|
|
2016-10-30 17:42:49 +05:30
|
|
|
it('should request new token', () => {
|
|
|
|
const data = {
|
|
|
|
url: 'foo',
|
|
|
|
options: {
|
|
|
|
headers: {}
|
|
|
|
}
|
|
|
|
};
|
2016-08-10 00:47:49 +05:30
|
|
|
|
2016-10-30 17:42: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', () => {
|
2016-11-13 02:01:44 +05:30
|
|
|
const data = {url: '/refresh-token', options: {}};
|
2016-10-30 17:42:49 +05:30
|
|
|
const resp = middleware.before(data);
|
2016-08-10 00:47:49 +05:30
|
|
|
|
|
|
|
expect(resp, 'to satisfy', data);
|
|
|
|
|
2016-10-30 17:42:49 +05:30
|
|
|
expect(authentication.requestToken, 'was not called');
|
2016-08-10 00:47:49 +05:30
|
|
|
});
|
|
|
|
|
2016-11-08 12:00:53 +05:30
|
|
|
it('should not auto refresh token if options.token specified', () => {
|
2016-10-30 17:42:49 +05:30
|
|
|
const data = {
|
|
|
|
url: 'foo',
|
2016-11-08 12:00:53 +05:30
|
|
|
options: {token: 'foo'}
|
2016-10-30 17:42:49 +05:30
|
|
|
};
|
|
|
|
middleware.before(data);
|
|
|
|
|
|
|
|
expect(authentication.requestToken, 'was not called');
|
2016-08-10 00:47:49 +05:30
|
|
|
});
|
|
|
|
|
2016-11-05 15:41:41 +05:30
|
|
|
it('should update user with new token', () => {
|
|
|
|
const data = {
|
|
|
|
url: 'foo',
|
|
|
|
options: {
|
|
|
|
headers: {}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
authentication.requestToken.returns(Promise.resolve({token: validToken}));
|
|
|
|
|
|
|
|
return middleware.before(data).then(() =>
|
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
updateToken(validToken)
|
|
|
|
])
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should if token can not be parsed', () => {
|
2016-11-15 11:25:15 +05:30
|
|
|
const account = {
|
|
|
|
token: 'realy bad token',
|
|
|
|
refreshToken
|
|
|
|
};
|
2016-11-05 15:41:41 +05:30
|
|
|
getState.returns({
|
|
|
|
accounts: {
|
2016-11-15 11:25:15 +05:30
|
|
|
active: account,
|
|
|
|
available: [account]
|
2016-11-05 15:41:41 +05:30
|
|
|
},
|
|
|
|
user: {}
|
|
|
|
});
|
|
|
|
|
|
|
|
const req = {url: 'foo', options: {}};
|
|
|
|
|
|
|
|
return expect(middleware.before(req), 'to be fulfilled with', req).then(() => {
|
|
|
|
expect(authentication.requestToken, 'was not called');
|
2016-08-10 00:47:49 +05:30
|
|
|
|
2016-11-05 15:41:41 +05:30
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
{payload: {isGuest: true}}
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should logout if token request failed', () => {
|
2016-10-30 17:42:49 +05:30
|
|
|
authentication.requestToken.returns(Promise.reject());
|
2016-08-10 00:47:49 +05:30
|
|
|
|
2016-11-05 15:41:41 +05:30
|
|
|
return expect(middleware.before({url: 'foo', options: {}}), 'to be fulfilled').then(() =>
|
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
{payload: {isGuest: true}}
|
|
|
|
])
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when token expired legacy user', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
getState.returns({
|
|
|
|
accounts: {
|
2016-11-15 11:25:15 +05:30
|
|
|
active: null,
|
|
|
|
available: []
|
2016-11-05 15:41:41 +05:30
|
|
|
},
|
|
|
|
user: {
|
|
|
|
token: expiredToken,
|
|
|
|
refreshToken
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should request new token', () => {
|
|
|
|
const data = {
|
|
|
|
url: 'foo',
|
|
|
|
options: {
|
|
|
|
headers: {}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
]);
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|
|
|
|
});
|
2016-08-10 00:47:49 +05:30
|
|
|
});
|
|
|
|
|
2016-10-30 17:42:49 +05:30
|
|
|
it('should not be applied if no token', () => {
|
2016-08-10 00:47:49 +05:30
|
|
|
getState.returns({
|
2016-11-05 15:41:41 +05:30
|
|
|
accounts: {
|
|
|
|
active: null
|
|
|
|
},
|
2016-08-10 00:47:49 +05:30
|
|
|
user: {}
|
|
|
|
});
|
|
|
|
|
2016-10-30 17:42:49 +05:30
|
|
|
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', () => {
|
2016-10-30 17:42:49 +05:30
|
|
|
const expiredResponse = {
|
|
|
|
name: 'Unauthorized',
|
|
|
|
message: 'Token expired',
|
|
|
|
code: 0,
|
|
|
|
status: 401,
|
|
|
|
type: 'yii\\web\\UnauthorizedHttpException'
|
|
|
|
};
|
|
|
|
|
|
|
|
const badTokenReponse = {
|
|
|
|
name: 'Unauthorized',
|
2016-11-05 15:41:41 +05:30
|
|
|
message: 'You are requesting with an invalid credential.',
|
|
|
|
code: 0,
|
|
|
|
status: 401,
|
|
|
|
type: 'yii\\web\\UnauthorizedHttpException'
|
|
|
|
};
|
|
|
|
|
|
|
|
const incorrectTokenReponse = {
|
|
|
|
name: 'Unauthorized',
|
|
|
|
message: 'Incorrect token',
|
2016-10-30 17:42:49 +05:30
|
|
|
code: 0,
|
|
|
|
status: 401,
|
|
|
|
type: 'yii\\web\\UnauthorizedHttpException'
|
|
|
|
};
|
|
|
|
|
|
|
|
let restart;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2016-08-10 00:47:49 +05:30
|
|
|
getState.returns({
|
2016-11-05 15:41:41 +05:30
|
|
|
accounts: {
|
2016-11-15 11:25:15 +05:30
|
|
|
active: {refreshToken},
|
|
|
|
available: [{refreshToken}]
|
2016-11-05 15:41:41 +05:30
|
|
|
},
|
|
|
|
user: {}
|
2016-08-10 00:47:49 +05:30
|
|
|
});
|
|
|
|
|
2016-10-30 17:42:49 +05:30
|
|
|
restart = sinon.stub().named('restart');
|
2016-08-10 00:47:49 +05:30
|
|
|
|
|
|
|
authentication.requestToken.returns(Promise.resolve({token: validToken}));
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|
2016-08-10 00:47:49 +05:30
|
|
|
|
2016-10-30 17:42: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');
|
2016-10-30 17:42:49 +05:30
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2016-11-05 15:41:41 +05:30
|
|
|
it('should logout user if invalid credential', () =>
|
|
|
|
expect(
|
|
|
|
middleware.catch(badTokenReponse, {options: {}}, restart),
|
|
|
|
'to be rejected'
|
|
|
|
).then(() =>
|
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
{payload: {isGuest: true}}
|
|
|
|
])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
it('should logout user if token is incorrect', () =>
|
|
|
|
expect(
|
|
|
|
middleware.catch(incorrectTokenReponse, {options: {}}, restart),
|
|
|
|
'to be rejected'
|
|
|
|
).then(() =>
|
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
{payload: {isGuest: true}}
|
|
|
|
])
|
|
|
|
)
|
|
|
|
);
|
2016-08-10 00:47:49 +05:30
|
|
|
|
2016-11-08 12:00:53 +05:30
|
|
|
it('should pass the request through if options.token specified', () => {
|
2016-10-30 17:42:49 +05:30
|
|
|
const promise = middleware.catch(expiredResponse, {
|
|
|
|
options: {
|
2016-11-08 12:00:53 +05:30
|
|
|
token: 'foo'
|
2016-10-30 17:42:49 +05:30
|
|
|
}
|
|
|
|
}, 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 = {};
|
|
|
|
|
2016-10-30 17:42:49 +05:30
|
|
|
const promise = middleware.catch(resp, {
|
|
|
|
options: {}
|
|
|
|
}, restart);
|
2016-08-10 00:47:49 +05:30
|
|
|
|
2016-10-30 17:42: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
|
|
|
});
|
|
|
|
});
|
2016-11-05 15:41:41 +05:30
|
|
|
|
|
|
|
describe('legacy user.refreshToken', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
getState.returns({
|
|
|
|
accounts: {
|
|
|
|
active: null
|
|
|
|
},
|
|
|
|
user: {refreshToken}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should request new token if expired', () =>
|
|
|
|
middleware.catch(expiredResponse, {options: {}}, restart).then(() => {
|
|
|
|
expect(authentication.requestToken, 'to have a call satisfying', [
|
|
|
|
refreshToken
|
|
|
|
]);
|
|
|
|
expect(restart, 'was called');
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
2016-08-10 00:47:49 +05:30
|
|
|
});
|
|
|
|
});
|