2016-10-30 17:42:49 +05:30
|
|
|
import expect from 'unexpected';
|
2016-12-06 00:44:38 +05:30
|
|
|
import sinon from 'sinon';
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2017-05-09 01:04:50 +05:30
|
|
|
import { browserHistory } from 'react-router';
|
2017-01-04 11:22:46 +05:30
|
|
|
|
2017-02-24 11:20:32 +05:30
|
|
|
import logger from 'services/logger';
|
2017-02-26 17:10:07 +05:30
|
|
|
import { InternalServerError } from 'services/request';
|
2016-11-08 12:00:53 +05:30
|
|
|
import authentication from 'services/api/authentication';
|
2016-11-14 10:58:25 +05:30
|
|
|
import {
|
|
|
|
authenticate,
|
|
|
|
revoke,
|
2016-12-06 00:44:38 +05:30
|
|
|
logoutAll,
|
|
|
|
logoutStrangers
|
2016-11-14 10:58:25 +05:30
|
|
|
} from 'components/accounts/actions';
|
2017-01-27 11:59:20 +05:30
|
|
|
import {
|
|
|
|
add, ADD,
|
|
|
|
activate, ACTIVATE,
|
|
|
|
remove,
|
|
|
|
reset
|
|
|
|
} from 'components/accounts/actions/pure-actions';
|
2016-11-05 15:41:41 +05:30
|
|
|
import { SET_LOCALE } from 'components/i18n/actions';
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2017-01-04 11:22:46 +05:30
|
|
|
import { updateUser, setUser } from 'components/user/actions';
|
2017-01-31 11:35:36 +05:30
|
|
|
import { setAccountSwitcher } from 'components/auth/actions';
|
2016-10-30 17:42:49 +05:30
|
|
|
|
|
|
|
const account = {
|
|
|
|
id: 1,
|
|
|
|
username: 'username',
|
|
|
|
email: 'email@test.com',
|
|
|
|
token: 'foo',
|
2016-11-05 15:41:41 +05:30
|
|
|
refreshToken: 'bar'
|
2016-10-30 17:42:49 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
const user = {
|
|
|
|
id: 1,
|
|
|
|
username: 'username',
|
|
|
|
email: 'email@test.com',
|
2016-11-05 15:41:41 +05:30
|
|
|
lang: 'be'
|
2016-10-30 17:42:49 +05:30
|
|
|
};
|
|
|
|
|
2016-11-13 02:01:44 +05:30
|
|
|
describe('components/accounts/actions', () => {
|
2016-10-30 17:42:49 +05:30
|
|
|
let dispatch;
|
|
|
|
let getState;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2016-11-05 15:41:41 +05:30
|
|
|
dispatch = sinon.spy((arg) =>
|
|
|
|
typeof arg === 'function' ? arg(dispatch, getState) : arg
|
|
|
|
).named('store.dispatch');
|
|
|
|
getState = sinon.stub().named('store.getState');
|
2016-10-30 17:42:49 +05:30
|
|
|
|
|
|
|
getState.returns({
|
2017-01-06 11:34:14 +05:30
|
|
|
accounts: {
|
|
|
|
available: [],
|
|
|
|
active: null
|
|
|
|
},
|
2016-10-30 17:42:49 +05:30
|
|
|
user: {}
|
|
|
|
});
|
|
|
|
|
2016-11-08 12:00:53 +05:30
|
|
|
sinon.stub(authentication, 'validateToken').named('authentication.validateToken');
|
2017-02-24 11:20:32 +05:30
|
|
|
sinon.stub(logger, 'warn').named('logger.warn');
|
2016-11-08 12:00:53 +05:30
|
|
|
authentication.validateToken.returns(Promise.resolve({
|
|
|
|
token: account.token,
|
2017-01-06 11:34:14 +05:30
|
|
|
refreshToken: account.refreshToken,
|
|
|
|
user
|
2016-11-08 12:00:53 +05:30
|
|
|
}));
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2016-11-08 12:00:53 +05:30
|
|
|
authentication.validateToken.restore();
|
2017-02-24 11:20:32 +05:30
|
|
|
logger.warn.restore();
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
describe('#authenticate()', () => {
|
2016-11-05 15:41:41 +05:30
|
|
|
it('should request user state using token', () =>
|
2017-01-31 11:35:36 +05:30
|
|
|
authenticate(account)(dispatch, getState).then(() =>
|
2017-01-06 11:34:14 +05:30
|
|
|
expect(authentication.validateToken, 'to have a call satisfying', [
|
|
|
|
{token: account.token, refreshToken: account.refreshToken}
|
2016-11-05 15:41:41 +05:30
|
|
|
])
|
|
|
|
)
|
|
|
|
);
|
2016-10-30 17:42:49 +05:30
|
|
|
|
|
|
|
it(`dispatches ${ADD} action`, () =>
|
2017-01-31 11:35:36 +05:30
|
|
|
authenticate(account)(dispatch, getState).then(() =>
|
2016-10-30 17:42:49 +05:30
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
add(account)
|
|
|
|
])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
it(`dispatches ${ACTIVATE} action`, () =>
|
2017-01-31 11:35:36 +05:30
|
|
|
authenticate(account)(dispatch, getState).then(() =>
|
2016-10-30 17:42:49 +05:30
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
activate(account)
|
|
|
|
])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2016-11-05 15:41:41 +05:30
|
|
|
it(`dispatches ${SET_LOCALE} action`, () =>
|
2017-01-31 11:35:36 +05:30
|
|
|
authenticate(account)(dispatch, getState).then(() =>
|
2016-11-05 15:41:41 +05:30
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
{type: SET_LOCALE, payload: {locale: 'be'}}
|
|
|
|
])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2016-10-30 17:42:49 +05:30
|
|
|
it('should update user state', () =>
|
2017-01-31 11:35:36 +05:30
|
|
|
authenticate(account)(dispatch, getState).then(() =>
|
2016-10-30 17:42:49 +05:30
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
2016-11-05 15:41:41 +05:30
|
|
|
updateUser({...user, isGuest: false})
|
2016-10-30 17:42:49 +05:30
|
|
|
])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
it('resolves with account', () =>
|
2017-01-31 11:35:36 +05:30
|
|
|
authenticate(account)(dispatch, getState).then((resp) =>
|
2016-10-30 17:42:49 +05:30
|
|
|
expect(resp, 'to equal', account)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
it('rejects when bad auth data', () => {
|
2017-01-06 11:34:14 +05:30
|
|
|
authentication.validateToken.returns(Promise.reject({}));
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2017-01-31 11:35:36 +05:30
|
|
|
return expect(authenticate(account)(dispatch, getState), 'to be rejected').then(() => {
|
2017-02-24 11:20:32 +05:30
|
|
|
expect(logger.warn, 'to have a call satisfying', [
|
|
|
|
'Error validating token during auth', {}
|
|
|
|
]);
|
2017-01-06 11:34:14 +05:30
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
{payload: {isGuest: true}},
|
|
|
|
]);
|
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
reset()
|
|
|
|
]);
|
|
|
|
});
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|
2016-12-06 00:44:38 +05:30
|
|
|
|
2017-02-24 11:20:32 +05:30
|
|
|
it('rejects when 5xx without logouting', () => {
|
2017-02-26 17:10:07 +05:30
|
|
|
const resp = new InternalServerError(null, {status: 500});
|
2017-02-24 11:20:32 +05:30
|
|
|
|
|
|
|
authentication.validateToken.returns(Promise.reject(resp));
|
|
|
|
|
|
|
|
return expect(authenticate(account)(dispatch, getState), 'to be rejected with', resp).then(() => {
|
|
|
|
expect(dispatch, 'to have no calls satisfying', [
|
|
|
|
{payload: {isGuest: true}},
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(logger.warn, 'was not called');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-12-06 00:44:38 +05:30
|
|
|
it('marks user as stranger, if there is no refreshToken', () => {
|
|
|
|
const expectedKey = `stranger${account.id}`;
|
|
|
|
authentication.validateToken.returns(Promise.resolve({
|
2017-01-06 11:34:14 +05:30
|
|
|
token: account.token,
|
|
|
|
user
|
2016-12-06 00:44:38 +05:30
|
|
|
}));
|
|
|
|
|
|
|
|
sessionStorage.removeItem(expectedKey);
|
|
|
|
|
2017-01-31 11:35:36 +05:30
|
|
|
return authenticate(account)(dispatch, getState).then(() => {
|
2016-12-07 02:38:51 +05:30
|
|
|
expect(sessionStorage.getItem(expectedKey), 'not to be null');
|
2016-12-06 00:44:38 +05:30
|
|
|
sessionStorage.removeItem(expectedKey);
|
|
|
|
});
|
|
|
|
});
|
2017-01-31 11:35:36 +05:30
|
|
|
|
|
|
|
describe('when user authenticated during oauth', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
getState.returns({
|
|
|
|
accounts: {
|
|
|
|
available: [],
|
|
|
|
active: null
|
|
|
|
},
|
|
|
|
user: {},
|
|
|
|
auth: {
|
|
|
|
oauth: {
|
|
|
|
clientId: 'ely.by',
|
|
|
|
prompt: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should dispatch setAccountSwitcher', () =>
|
|
|
|
authenticate(account)(dispatch, getState).then(() =>
|
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
setAccountSwitcher(false)
|
|
|
|
])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
describe('#revoke()', () => {
|
2016-11-15 11:25:15 +05:30
|
|
|
beforeEach(() => {
|
|
|
|
sinon.stub(authentication, 'logout').named('authentication.logout');
|
|
|
|
});
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2016-11-15 11:25:15 +05:30
|
|
|
afterEach(() => {
|
|
|
|
authentication.logout.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when one account available', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
getState.returns({
|
|
|
|
accounts: {
|
|
|
|
active: account,
|
|
|
|
available: [account]
|
|
|
|
},
|
|
|
|
user
|
|
|
|
});
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|
|
|
|
|
2016-11-15 11:25:15 +05:30
|
|
|
it('should dispatch reset action', () =>
|
|
|
|
revoke(account)(dispatch, getState).then(() =>
|
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
reset()
|
|
|
|
])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
it('should call logout api method in background', () =>
|
|
|
|
revoke(account)(dispatch, getState).then(() =>
|
|
|
|
expect(authentication.logout, 'to have a call satisfying', [
|
|
|
|
account
|
|
|
|
])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
it('should update user state', () =>
|
|
|
|
revoke(account)(dispatch, getState).then(() =>
|
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
{payload: {isGuest: true}}
|
|
|
|
// updateUser({isGuest: true})
|
|
|
|
])
|
|
|
|
// expect(dispatch, 'to have calls satisfying', [
|
|
|
|
// [remove(account)],
|
|
|
|
// [expect.it('to be a function')]
|
|
|
|
// // [logout()] // TODO: this is not a plain action. How should we simplify its testing?
|
|
|
|
// ])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when multiple accounts available', () => {
|
|
|
|
const account2 = {...account, id: 2};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
getState.returns({
|
|
|
|
accounts: {
|
|
|
|
active: account2,
|
|
|
|
available: [account, account2]
|
|
|
|
},
|
|
|
|
user
|
|
|
|
});
|
2016-11-05 15:41:41 +05:30
|
|
|
});
|
2016-11-15 11:25:15 +05:30
|
|
|
|
|
|
|
it('should switch to the next account', () =>
|
|
|
|
revoke(account2)(dispatch, getState).then(() =>
|
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
activate(account)
|
|
|
|
])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
it('should remove current account', () =>
|
|
|
|
revoke(account2)(dispatch, getState).then(() =>
|
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
remove(account2)
|
|
|
|
])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
it('should call logout api method in background', () =>
|
|
|
|
revoke(account2)(dispatch, getState).then(() =>
|
|
|
|
expect(authentication.logout, 'to have a call satisfying', [
|
|
|
|
account2
|
|
|
|
])
|
|
|
|
)
|
|
|
|
);
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|
2016-11-15 11:25:15 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
describe('#logoutAll()', () => {
|
|
|
|
const account2 = {...account, id: 2};
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2016-11-15 11:25:15 +05:30
|
|
|
beforeEach(() => {
|
2016-11-13 02:01:44 +05:30
|
|
|
getState.returns({
|
|
|
|
accounts: {
|
2016-11-15 11:25:15 +05:30
|
|
|
active: account2,
|
|
|
|
available: [account, account2]
|
2016-11-13 02:01:44 +05:30
|
|
|
},
|
|
|
|
user
|
|
|
|
});
|
|
|
|
|
2016-11-15 11:25:15 +05:30
|
|
|
sinon.stub(authentication, 'logout').named('authentication.logout');
|
2017-05-09 01:04:50 +05:30
|
|
|
sinon.stub(browserHistory, 'push').named('browserHistory.push');
|
2016-11-15 11:25:15 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
authentication.logout.restore();
|
2017-05-09 01:04:50 +05:30
|
|
|
browserHistory.push.restore();
|
2016-11-15 11:25:15 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should call logout api method for each account', () => {
|
|
|
|
logoutAll()(dispatch, getState);
|
|
|
|
|
|
|
|
expect(authentication.logout, 'to have calls satisfying', [
|
|
|
|
[account],
|
|
|
|
[account2]
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should dispatch reset', () => {
|
|
|
|
logoutAll()(dispatch, getState);
|
|
|
|
|
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
reset()
|
|
|
|
]);
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|
2017-01-04 11:22:46 +05:30
|
|
|
|
|
|
|
it('should redirect to /login', () =>
|
|
|
|
logoutAll()(dispatch, getState).then(() => {
|
2017-05-09 01:04:50 +05:30
|
|
|
expect(browserHistory.push, 'to have a call satisfying', [
|
|
|
|
'/login'
|
2017-01-04 11:22:46 +05:30
|
|
|
]);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
it('should change user to guest', () =>
|
|
|
|
logoutAll()(dispatch, getState).then(() => {
|
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
setUser({
|
|
|
|
lang: user.lang,
|
|
|
|
isGuest: true
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
})
|
|
|
|
);
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|
2016-12-06 00:44:38 +05:30
|
|
|
|
|
|
|
describe('#logoutStrangers', () => {
|
|
|
|
const foreignAccount = {
|
|
|
|
...account,
|
|
|
|
id: 2,
|
|
|
|
refreshToken: undefined
|
|
|
|
};
|
|
|
|
|
|
|
|
const foreignAccount2 = {
|
|
|
|
...foreignAccount,
|
|
|
|
id: 3
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
getState.returns({
|
|
|
|
accounts: {
|
2017-01-12 10:59:39 +05:30
|
|
|
active: foreignAccount,
|
2016-12-06 00:44:38 +05:30
|
|
|
available: [account, foreignAccount, foreignAccount2]
|
|
|
|
},
|
|
|
|
user
|
|
|
|
});
|
|
|
|
|
|
|
|
sinon.stub(authentication, 'logout').named('authentication.logout');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
authentication.logout.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove stranger accounts', () => {
|
|
|
|
logoutStrangers()(dispatch, getState);
|
|
|
|
|
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
remove(foreignAccount)
|
|
|
|
]);
|
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
remove(foreignAccount2)
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should logout stranger accounts', () => {
|
|
|
|
logoutStrangers()(dispatch, getState);
|
|
|
|
|
|
|
|
expect(authentication.logout, 'to have calls satisfying', [
|
|
|
|
[foreignAccount],
|
|
|
|
[foreignAccount2]
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should activate another account if available', () =>
|
|
|
|
logoutStrangers()(dispatch, getState)
|
|
|
|
.then(() =>
|
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
activate(account)
|
|
|
|
])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2017-01-12 10:59:39 +05:30
|
|
|
it('should not activate another account if active account is already not a stranger', () => {
|
|
|
|
getState.returns({
|
|
|
|
accounts: {
|
|
|
|
active: account,
|
|
|
|
available: [account, foreignAccount]
|
|
|
|
},
|
|
|
|
user
|
|
|
|
});
|
|
|
|
|
|
|
|
return logoutStrangers()(dispatch, getState)
|
|
|
|
.then(() =>
|
|
|
|
expect(dispatch, 'was always called with',
|
|
|
|
expect.it('not to satisfy', activate(account)))
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not dispatch if no strangers', () => {
|
|
|
|
getState.returns({
|
|
|
|
accounts: {
|
|
|
|
active: account,
|
|
|
|
available: [account]
|
|
|
|
},
|
|
|
|
user
|
|
|
|
});
|
|
|
|
|
|
|
|
return logoutStrangers()(dispatch, getState)
|
|
|
|
.then(() =>
|
|
|
|
expect(dispatch, 'was not called')
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2016-12-06 00:44:38 +05:30
|
|
|
describe('when all accounts are strangers', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
getState.returns({
|
|
|
|
accounts: {
|
|
|
|
active: foreignAccount,
|
|
|
|
available: [foreignAccount, foreignAccount2]
|
|
|
|
},
|
|
|
|
user
|
|
|
|
});
|
|
|
|
|
|
|
|
logoutStrangers()(dispatch, getState);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('logouts all accounts', () => {
|
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
{payload: {isGuest: true}}
|
|
|
|
// updateUser({isGuest: true})
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
reset()
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-01-12 10:59:39 +05:30
|
|
|
describe('when a stranger has a mark in sessionStorage', () => {
|
2016-12-06 00:44:38 +05:30
|
|
|
const key = `stranger${foreignAccount.id}`;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
sessionStorage.setItem(key, 1);
|
|
|
|
|
|
|
|
logoutStrangers()(dispatch, getState);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
sessionStorage.removeItem(key);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not log out', () =>
|
2017-01-12 10:59:39 +05:30
|
|
|
expect(dispatch, 'was always called with',
|
|
|
|
expect.it('not to equal', {payload: foreignAccount})
|
|
|
|
)
|
2016-12-06 00:44:38 +05:30
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|