#48: call authentication.logout for each revoked account

This commit is contained in:
SleepWalker
2016-11-15 07:55:15 +02:00
parent 9e7d5b8338
commit 5142d65b39
7 changed files with 214 additions and 73 deletions

View File

@@ -8,7 +8,8 @@ import {
add, ADD,
activate, ACTIVATE,
remove,
reset
reset,
logoutAll
} from 'components/accounts/actions';
import { SET_LOCALE } from 'components/i18n/actions';
@@ -116,58 +117,129 @@ describe('components/accounts/actions', () => {
});
describe('#revoke()', () => {
it('should switch next account if available', () => {
beforeEach(() => {
sinon.stub(authentication, 'logout').named('authentication.logout');
});
afterEach(() => {
authentication.logout.restore();
});
describe('when one account available', () => {
beforeEach(() => {
getState.returns({
accounts: {
active: account,
available: [account]
},
user
});
});
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
});
});
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
])
)
);
});
});
describe('#logoutAll()', () => {
const account2 = {...account, id: 2};
beforeEach(() => {
getState.returns({
accounts: {
active: account2,
available: [account]
available: [account, account2]
},
user
});
return revoke(account2)(dispatch, getState).then(() => {
expect(dispatch, 'to have a call satisfying', [
remove(account2)
]);
expect(dispatch, 'to have a call satisfying', [
activate(account)
]);
expect(dispatch, 'to have a call satisfying', [
updateUser({...user, isGuest: false})
]);
// expect(dispatch, 'to have calls satisfying', [
// [remove(account2)],
// [expect.it('to be a function')]
// // [authenticate(account2)] // TODO: this is not a plain action. How should we simplify its testing?
// ])
});
sinon.stub(authentication, 'logout').named('authentication.logout');
});
it('should logout if no other accounts available', () => {
getState.returns({
accounts: {
active: account,
available: []
},
user
});
afterEach(() => {
authentication.logout.restore();
});
revoke(account)(dispatch, getState).then(() => {
expect(dispatch, 'to have a call satisfying', [
{payload: {isGuest: true}}
// updateUser({isGuest: true})
]);
expect(dispatch, 'to have a call satisfying', [
reset()
]);
// 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?
// ])
});
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()
]);
});
});
});

View File

@@ -42,11 +42,16 @@ describe('components/user/actions', () => {
});
describe('user with jwt', () => {
const token = 'iLoveRockNRoll';
beforeEach(() => {
getState.returns({
user: {
token: 'iLoveRockNRoll',
lang: 'foo'
},
accounts: {
active: {token},
available: [{token}]
}
});
});
@@ -65,7 +70,7 @@ describe('components/user/actions', () => {
return callThunk(logout).then(() => {
expect(request.post, 'to have a call satisfying', [
'/api/authentication/logout'
'/api/authentication/logout', {}, {}
]);
});
});
@@ -75,11 +80,17 @@ describe('components/user/actions', () => {
testRedirectedToLogin();
});
describe('user without jwt', () => { // (a guest with partially filled user's state)
describe('user without jwt', () => {
// (a guest with partially filled user's state)
// DEPRECATED
beforeEach(() => {
getState.returns({
user: {
lang: 'foo'
},
accounts: {
active: null,
available: []
}
});
});

View File

@@ -17,6 +17,7 @@ describe('refreshTokenMiddleware', () => {
beforeEach(() => {
sinon.stub(authentication, 'requestToken').named('authentication.requestToken');
sinon.stub(authentication, 'logout').named('authentication.logout');
getState = sinon.stub().named('store.getState');
dispatch = sinon.spy((arg) =>
@@ -28,6 +29,7 @@ describe('refreshTokenMiddleware', () => {
afterEach(() => {
authentication.requestToken.restore();
authentication.logout.restore();
});
it('must be till 2100 to test with validToken', () =>
@@ -37,12 +39,14 @@ describe('refreshTokenMiddleware', () => {
describe('#before', () => {
describe('when token expired', () => {
beforeEach(() => {
const account = {
token: expiredToken,
refreshToken
};
getState.returns({
accounts: {
active: {
token: expiredToken,
refreshToken
}
active: account,
available: [account]
},
user: {}
});
@@ -104,12 +108,14 @@ describe('refreshTokenMiddleware', () => {
});
it('should if token can not be parsed', () => {
const account = {
token: 'realy bad token',
refreshToken
};
getState.returns({
accounts: {
active: {
token: 'realy bad token',
refreshToken
}
active: account,
available: [account]
},
user: {}
});
@@ -140,7 +146,8 @@ describe('refreshTokenMiddleware', () => {
beforeEach(() => {
getState.returns({
accounts: {
active: null
active: null,
available: []
},
user: {
token: expiredToken,
@@ -216,7 +223,8 @@ describe('refreshTokenMiddleware', () => {
beforeEach(() => {
getState.returns({
accounts: {
active: {refreshToken}
active: {refreshToken},
available: [{refreshToken}]
},
user: {}
});

View File

@@ -1,5 +1,6 @@
import expect from 'unexpected';
import request from 'services/request';
import authentication from 'services/api/authentication';
import accounts from 'services/api/accounts';
@@ -88,4 +89,38 @@ describe('authentication api', () => {
});
});
});
describe('#logout', () => {
beforeEach(() => {
sinon.stub(request, 'post').named('request.post');
});
afterEach(() => {
request.post.restore();
});
it('should request logout api', () => {
authentication.logout();
expect(request.post, 'to have a call satisfying', [
'/api/authentication/logout', {}, {}
]);
});
it('returns a promise', () => {
request.post.returns(Promise.resolve());
return expect(authentication.logout(), 'to be fulfilled');
});
it('overrides token if provided', () => {
const token = 'foo';
authentication.logout({token});
expect(request.post, 'to have a call satisfying', [
'/api/authentication/logout', {}, {token}
]);
});
});
});