2019-12-08 00:32:00 +05:30
|
|
|
import expect from 'app/test/unexpected';
|
2016-12-06 00:44:38 +05:30
|
|
|
import sinon from 'sinon';
|
2019-12-08 00:32:00 +05:30
|
|
|
import { browserHistory } from 'app/services/history';
|
|
|
|
import { InternalServerError } from 'app/services/request';
|
|
|
|
import { sessionStorage } from 'app/services/localStorage';
|
|
|
|
import * as authentication from 'app/services/api/authentication';
|
2016-11-14 10:58:25 +05:30
|
|
|
import {
|
2019-11-27 14:33:32 +05:30
|
|
|
authenticate,
|
|
|
|
revoke,
|
|
|
|
logoutAll,
|
|
|
|
logoutStrangers,
|
2019-12-08 00:32:00 +05:30
|
|
|
} from 'app/components/accounts/actions';
|
2017-01-27 11:59:20 +05:30
|
|
|
import {
|
2019-11-27 14:33:32 +05:30
|
|
|
add,
|
|
|
|
ADD,
|
|
|
|
activate,
|
|
|
|
ACTIVATE,
|
|
|
|
remove,
|
|
|
|
reset,
|
2019-12-08 00:32:00 +05:30
|
|
|
} from 'app/components/accounts/actions/pure-actions';
|
|
|
|
import { SET_LOCALE } from 'app/components/i18n/actions';
|
|
|
|
import { updateUser, setUser } from 'app/components/user/actions';
|
|
|
|
import { setLogin, setAccountSwitcher } from 'app/components/auth/actions';
|
|
|
|
import { Dispatch, RootState } from 'app/reducers';
|
2019-12-07 16:58:52 +05:30
|
|
|
|
|
|
|
import { Account } from './reducer';
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
const token =
|
|
|
|
'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJlbHl8MSJ9.pRJ7vakt2eIscjqwG__KhSxKb3qwGsdBBeDbBffJs_I';
|
|
|
|
const legacyToken =
|
|
|
|
'eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOjF9.cRF-sQNrwWQ94xCb3vWioVdjxAZeefEE7GMGwh7708o';
|
2019-01-28 00:42:58 +05:30
|
|
|
|
2016-10-30 17:42:49 +05:30
|
|
|
const account = {
|
2019-11-27 14:33:32 +05:30
|
|
|
id: 1,
|
|
|
|
username: 'username',
|
|
|
|
email: 'email@test.com',
|
|
|
|
token,
|
|
|
|
refreshToken: 'bar',
|
2016-10-30 17:42:49 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
const user = {
|
2019-11-27 14:33:32 +05:30
|
|
|
id: 1,
|
|
|
|
username: 'username',
|
|
|
|
email: 'email@test.com',
|
|
|
|
lang: 'be',
|
2016-10-30 17:42:49 +05:30
|
|
|
};
|
|
|
|
|
2016-11-13 02:01:44 +05:30
|
|
|
describe('components/accounts/actions', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
let dispatch: Dispatch;
|
|
|
|
let getState: () => RootState;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
dispatch = sinon
|
|
|
|
.spy(arg => (typeof arg === 'function' ? arg(dispatch, getState) : arg))
|
|
|
|
.named('store.dispatch');
|
|
|
|
getState = sinon.stub().named('store.getState');
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
(getState as any).returns({
|
2019-11-27 14:33:32 +05:30
|
|
|
accounts: {
|
|
|
|
available: [],
|
|
|
|
active: null,
|
|
|
|
},
|
|
|
|
auth: {
|
|
|
|
credentials: {},
|
|
|
|
},
|
|
|
|
user: {},
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
sinon
|
|
|
|
.stub(authentication, 'validateToken')
|
|
|
|
.named('authentication.validateToken');
|
|
|
|
sinon.stub(browserHistory, 'push').named('browserHistory.push');
|
|
|
|
sinon.stub(authentication, 'logout').named('authentication.logout');
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
(authentication.logout as any).returns(Promise.resolve());
|
|
|
|
(authentication.validateToken as any).returns(
|
2019-11-27 14:33:32 +05:30
|
|
|
Promise.resolve({
|
|
|
|
token: account.token,
|
|
|
|
refreshToken: account.refreshToken,
|
|
|
|
user,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2019-12-07 16:58:52 +05:30
|
|
|
(authentication.validateToken as any).restore();
|
|
|
|
(authentication.logout as any).restore();
|
|
|
|
(browserHistory.push as any).restore();
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
describe('#authenticate()', () => {
|
|
|
|
it('should request user state using token', () =>
|
2019-12-07 16:58:52 +05:30
|
|
|
authenticate(account)(dispatch, getState, undefined).then(() =>
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(authentication.validateToken, 'to have a call satisfying', [
|
|
|
|
account.id,
|
|
|
|
account.token,
|
|
|
|
account.refreshToken,
|
|
|
|
]),
|
|
|
|
));
|
|
|
|
|
|
|
|
it('should request user by extracting id from token', () =>
|
2019-12-07 16:58:52 +05:30
|
|
|
authenticate({ token } as Account)(
|
|
|
|
dispatch,
|
|
|
|
getState,
|
|
|
|
undefined,
|
|
|
|
).then(() =>
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(authentication.validateToken, 'to have a call satisfying', [
|
|
|
|
1,
|
|
|
|
token,
|
|
|
|
undefined,
|
|
|
|
]),
|
|
|
|
));
|
|
|
|
|
|
|
|
it('should request user by extracting id from legacy token', () =>
|
2019-12-07 16:58:52 +05:30
|
|
|
authenticate({ token: legacyToken } as Account)(
|
|
|
|
dispatch,
|
|
|
|
getState,
|
|
|
|
undefined,
|
|
|
|
).then(() =>
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(authentication.validateToken, 'to have a call satisfying', [
|
|
|
|
1,
|
|
|
|
legacyToken,
|
|
|
|
undefined,
|
|
|
|
]),
|
|
|
|
));
|
|
|
|
|
|
|
|
it(`dispatches ${ADD} action`, () =>
|
2019-12-07 16:58:52 +05:30
|
|
|
authenticate(account)(dispatch, getState, undefined).then(() =>
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(dispatch, 'to have a call satisfying', [add(account)]),
|
|
|
|
));
|
|
|
|
|
|
|
|
it(`dispatches ${ACTIVATE} action`, () =>
|
2019-12-07 16:58:52 +05:30
|
|
|
authenticate(account)(dispatch, getState, undefined).then(() =>
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(dispatch, 'to have a call satisfying', [activate(account)]),
|
|
|
|
));
|
|
|
|
|
|
|
|
it(`dispatches ${SET_LOCALE} action`, () =>
|
2019-12-07 16:58:52 +05:30
|
|
|
authenticate(account)(dispatch, getState, undefined).then(() =>
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
{ type: SET_LOCALE, payload: { locale: 'be' } },
|
|
|
|
]),
|
|
|
|
));
|
|
|
|
|
|
|
|
it('should update user state', () =>
|
2019-12-07 16:58:52 +05:30
|
|
|
authenticate(account)(dispatch, getState, undefined).then(() =>
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
updateUser({ ...user, isGuest: false }),
|
|
|
|
]),
|
|
|
|
));
|
|
|
|
|
|
|
|
it('resolves with account', () =>
|
2019-12-07 16:58:52 +05:30
|
|
|
authenticate(account)(dispatch, getState, undefined).then(resp =>
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(resp, 'to equal', account),
|
|
|
|
));
|
|
|
|
|
|
|
|
it('rejects when bad auth data', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
(authentication.validateToken as any).returns(Promise.reject({}));
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
return expect(
|
2019-12-07 16:58:52 +05:30
|
|
|
authenticate(account)(dispatch, getState, undefined),
|
2019-11-27 14:33:32 +05:30
|
|
|
'to be rejected',
|
|
|
|
).then(() => {
|
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
setLogin(account.email),
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(browserHistory.push, 'to have a call satisfying', ['/login']);
|
|
|
|
});
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('rejects when 5xx without logouting', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
const resp = new InternalServerError('500', { status: 500 });
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
(authentication.validateToken as any).rejects(resp);
|
2016-11-05 15:41:41 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return expect(
|
2019-12-07 16:58:52 +05:30
|
|
|
authenticate(account)(dispatch, getState, undefined),
|
2019-11-27 14:33:32 +05:30
|
|
|
'to be rejected with',
|
|
|
|
resp,
|
|
|
|
).then(() =>
|
|
|
|
expect(dispatch, 'to have no calls satisfying', [
|
|
|
|
{ payload: { isGuest: true } },
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
});
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('marks user as stranger, if there is no refreshToken', () => {
|
|
|
|
const expectedKey = `stranger${account.id}`;
|
2019-12-07 16:58:52 +05:30
|
|
|
(authentication.validateToken as any).resolves({
|
2019-11-27 14:33:32 +05:30
|
|
|
token: account.token,
|
|
|
|
user,
|
|
|
|
});
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
sessionStorage.removeItem(expectedKey);
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
return authenticate(account)(dispatch, getState, undefined).then(() => {
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(sessionStorage.getItem(expectedKey), 'not to be null');
|
|
|
|
sessionStorage.removeItem(expectedKey);
|
|
|
|
});
|
|
|
|
});
|
2017-12-31 00:34:31 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
describe('when user authenticated during oauth', () => {
|
|
|
|
beforeEach(() => {
|
2019-12-07 16:58:52 +05:30
|
|
|
(getState as any).returns({
|
2019-11-27 14:33:32 +05:30
|
|
|
accounts: {
|
|
|
|
available: [],
|
|
|
|
active: null,
|
|
|
|
},
|
|
|
|
user: {},
|
|
|
|
auth: {
|
|
|
|
oauth: {
|
|
|
|
clientId: 'ely.by',
|
|
|
|
prompt: [],
|
|
|
|
},
|
|
|
|
},
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should dispatch setAccountSwitcher', () =>
|
2019-12-07 16:58:52 +05:30
|
|
|
authenticate(account)(dispatch, getState, undefined).then(() =>
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
setAccountSwitcher(false),
|
|
|
|
]),
|
|
|
|
));
|
|
|
|
});
|
2016-12-06 00:44:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
describe('when one account available', () => {
|
|
|
|
beforeEach(() => {
|
2019-12-07 16:58:52 +05:30
|
|
|
(getState as any).returns({
|
2019-11-27 14:33:32 +05:30
|
|
|
accounts: {
|
|
|
|
active: account.id,
|
|
|
|
available: [account],
|
|
|
|
},
|
|
|
|
auth: {
|
|
|
|
credentials: {},
|
|
|
|
},
|
|
|
|
user,
|
2017-02-24 11:20:32 +05:30
|
|
|
});
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
2017-02-24 11:20:32 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should activate account before auth api call', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
(authentication.validateToken as any).returns(
|
|
|
|
Promise.reject({ error: 'foo' }),
|
|
|
|
);
|
2016-12-06 00:44:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return expect(
|
2019-12-07 16:58:52 +05:30
|
|
|
authenticate(account)(dispatch, getState, undefined),
|
2019-11-27 14:33:32 +05:30
|
|
|
'to be rejected with',
|
|
|
|
{ error: 'foo' },
|
|
|
|
).then(() =>
|
|
|
|
expect(dispatch, 'to have a call satisfying', [activate(account)]),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-12-06 00:44:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
describe('#revoke()', () => {
|
|
|
|
describe('when one account available', () => {
|
|
|
|
beforeEach(() => {
|
2019-12-07 16:58:52 +05:30
|
|
|
(getState as any).returns({
|
2019-11-27 14:33:32 +05:30
|
|
|
accounts: {
|
|
|
|
active: account.id,
|
|
|
|
available: [account],
|
|
|
|
},
|
|
|
|
auth: {
|
|
|
|
credentials: {},
|
|
|
|
},
|
|
|
|
user,
|
2016-12-06 00:44:38 +05:30
|
|
|
});
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should dispatch reset action', () =>
|
2019-12-07 16:58:52 +05:30
|
|
|
revoke(account)(dispatch, getState, undefined).then(() =>
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(dispatch, 'to have a call satisfying', [reset()]),
|
|
|
|
));
|
|
|
|
|
|
|
|
it('should call logout api method in background', () =>
|
2019-12-07 16:58:52 +05:30
|
|
|
revoke(account)(dispatch, getState, undefined).then(() =>
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(authentication.logout, 'to have a call satisfying', [
|
|
|
|
account.token,
|
|
|
|
]),
|
|
|
|
));
|
|
|
|
|
|
|
|
it('should update user state', () =>
|
2019-12-07 16:58:52 +05:30
|
|
|
revoke(account)(dispatch, getState, undefined).then(
|
2019-11-27 14:33:32 +05:30
|
|
|
() =>
|
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
setUser({ 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?
|
|
|
|
// ])
|
|
|
|
));
|
|
|
|
});
|
2017-01-31 11:35:36 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
describe('when multiple accounts available', () => {
|
|
|
|
const account2 = { ...account, id: 2 };
|
2018-02-28 02:47:31 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
beforeEach(() => {
|
2019-12-07 16:58:52 +05:30
|
|
|
(getState as any).returns({
|
2019-11-27 14:33:32 +05:30
|
|
|
accounts: {
|
|
|
|
active: account2.id,
|
|
|
|
available: [account, account2],
|
|
|
|
},
|
|
|
|
user,
|
2018-02-28 02:47:31 +05:30
|
|
|
});
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should switch to the next account', () =>
|
2019-12-07 16:58:52 +05:30
|
|
|
revoke(account2)(dispatch, getState, undefined).then(() =>
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(dispatch, 'to have a call satisfying', [activate(account)]),
|
|
|
|
));
|
|
|
|
|
|
|
|
it('should remove current account', () =>
|
2019-12-07 16:58:52 +05:30
|
|
|
revoke(account2)(dispatch, getState, undefined).then(() =>
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(dispatch, 'to have a call satisfying', [remove(account2)]),
|
|
|
|
));
|
|
|
|
|
|
|
|
it('should call logout api method in background', () =>
|
2019-12-07 16:58:52 +05:30
|
|
|
revoke(account2)(dispatch, getState, undefined).then(() =>
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(authentication.logout, 'to have a call satisfying', [
|
|
|
|
account2.token,
|
|
|
|
]),
|
|
|
|
));
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
describe('#logoutAll()', () => {
|
|
|
|
const account2 = { ...account, id: 2 };
|
2016-11-15 11:25:15 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
beforeEach(() => {
|
2019-12-07 16:58:52 +05:30
|
|
|
(getState as any).returns({
|
2019-11-27 14:33:32 +05:30
|
|
|
accounts: {
|
|
|
|
active: account2.id,
|
|
|
|
available: [account, account2],
|
|
|
|
},
|
|
|
|
auth: {
|
|
|
|
credentials: {},
|
|
|
|
},
|
|
|
|
user,
|
|
|
|
});
|
2016-11-15 11:25:15 +05:30
|
|
|
});
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should call logout api method for each account', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
logoutAll()(dispatch, getState, undefined);
|
2016-11-15 11:25:15 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(authentication.logout, 'to have calls satisfying', [
|
|
|
|
[account.token],
|
|
|
|
[account2.token],
|
|
|
|
]);
|
|
|
|
});
|
2016-11-15 11:25:15 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should dispatch reset', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
logoutAll()(dispatch, getState, undefined);
|
2016-11-15 11:25:15 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(dispatch, 'to have a call satisfying', [reset()]);
|
|
|
|
});
|
2016-11-15 11:25:15 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should redirect to /login', () =>
|
2019-12-07 16:58:52 +05:30
|
|
|
logoutAll()(dispatch, getState, undefined).then(() => {
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(browserHistory.push, 'to have a call satisfying', ['/login']);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should change user to guest', () =>
|
2019-12-07 16:58:52 +05:30
|
|
|
logoutAll()(dispatch, getState, undefined).then(() => {
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
setUser({
|
|
|
|
lang: user.lang,
|
|
|
|
isGuest: true,
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#logoutStrangers', () => {
|
|
|
|
const foreignAccount = {
|
|
|
|
...account,
|
|
|
|
id: 2,
|
2019-12-07 16:58:52 +05:30
|
|
|
refreshToken: null,
|
2019-11-27 14:33:32 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
const foreignAccount2 = {
|
|
|
|
...foreignAccount,
|
|
|
|
id: 3,
|
|
|
|
};
|
2017-01-04 11:22:46 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
beforeEach(() => {
|
2019-12-07 16:58:52 +05:30
|
|
|
(getState as any).returns({
|
2019-11-27 14:33:32 +05:30
|
|
|
accounts: {
|
|
|
|
active: foreignAccount.id,
|
|
|
|
available: [account, foreignAccount, foreignAccount2],
|
|
|
|
},
|
|
|
|
user,
|
|
|
|
});
|
|
|
|
});
|
2017-01-04 11:22:46 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should remove stranger accounts', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
logoutStrangers()(dispatch, getState, undefined);
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
expect(dispatch, 'to have a call satisfying', [remove(foreignAccount)]);
|
|
|
|
expect(dispatch, 'to have a call satisfying', [remove(foreignAccount2)]);
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|
2016-12-06 00:44:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should logout stranger accounts', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
logoutStrangers()(dispatch, getState, undefined);
|
2016-12-06 00:44:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(authentication.logout, 'to have calls satisfying', [
|
|
|
|
[foreignAccount.token],
|
|
|
|
[foreignAccount2.token],
|
|
|
|
]);
|
|
|
|
});
|
2016-12-06 00:44:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should activate another account if available', () =>
|
2019-12-07 16:58:52 +05:30
|
|
|
logoutStrangers()(dispatch, getState, undefined).then(() =>
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(dispatch, 'to have a call satisfying', [activate(account)]),
|
|
|
|
));
|
|
|
|
|
|
|
|
it('should not activate another account if active account is already not a stranger', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
(getState as any).returns({
|
2019-11-27 14:33:32 +05:30
|
|
|
accounts: {
|
|
|
|
active: account.id,
|
|
|
|
available: [account, foreignAccount],
|
|
|
|
},
|
|
|
|
user,
|
|
|
|
});
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
return logoutStrangers()(dispatch, getState, undefined).then(() =>
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(dispatch, 'not to have calls satisfying', [activate(account)]),
|
|
|
|
);
|
|
|
|
});
|
2016-12-06 00:44:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should not dispatch if no strangers', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
(getState as any).returns({
|
2019-11-27 14:33:32 +05:30
|
|
|
accounts: {
|
|
|
|
active: account.id,
|
|
|
|
available: [account],
|
|
|
|
},
|
|
|
|
user,
|
|
|
|
});
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
return logoutStrangers()(dispatch, getState, undefined).then(() =>
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(dispatch, 'was not called'),
|
|
|
|
);
|
|
|
|
});
|
2016-12-06 00:44:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
describe('when all accounts are strangers', () => {
|
|
|
|
beforeEach(() => {
|
2019-12-07 16:58:52 +05:30
|
|
|
(getState as any).returns({
|
2019-11-27 14:33:32 +05:30
|
|
|
accounts: {
|
|
|
|
active: foreignAccount.id,
|
|
|
|
available: [foreignAccount, foreignAccount2],
|
|
|
|
},
|
|
|
|
auth: {
|
|
|
|
credentials: {},
|
|
|
|
},
|
|
|
|
user,
|
2016-12-06 00:44:38 +05:30
|
|
|
});
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
logoutStrangers()(dispatch, getState, undefined);
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
2016-12-06 00:44:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('logouts all accounts', () => {
|
|
|
|
expect(authentication.logout, 'to have calls satisfying', [
|
|
|
|
[foreignAccount.token],
|
|
|
|
[foreignAccount2.token],
|
|
|
|
]);
|
2017-01-12 10:59:39 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(dispatch, 'to have a call satisfying', [
|
|
|
|
setUser({ isGuest: true }),
|
|
|
|
]);
|
2017-01-12 10:59:39 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(dispatch, 'to have a call satisfying', [reset()]);
|
|
|
|
});
|
|
|
|
});
|
2016-12-06 00:44:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
describe('when a stranger has a mark in sessionStorage', () => {
|
|
|
|
const key = `stranger${foreignAccount.id}`;
|
2016-12-06 00:44:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
beforeEach(() => {
|
|
|
|
sessionStorage.setItem(key, 1);
|
2016-12-06 00:44:38 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
logoutStrangers()(dispatch, getState, undefined);
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
2016-12-06 00:44:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
afterEach(() => {
|
|
|
|
sessionStorage.removeItem(key);
|
|
|
|
});
|
2016-12-06 00:44:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should not log out', () =>
|
|
|
|
expect(dispatch, 'not to have calls satisfying', [
|
|
|
|
{ payload: foreignAccount },
|
|
|
|
]));
|
2016-12-06 00:44:38 +05:30
|
|
|
});
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|