Merge pull request #1151 from gradido/catch-verify-login

fix: Catch Expired Session when Coming from Admin Interface
This commit is contained in:
Moriz Wahl 2021-12-06 15:47:09 +01:00 committed by GitHub
commit d5867a4a8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 8 deletions

View File

@ -399,7 +399,7 @@ jobs:
report_name: Coverage Frontend
type: lcov
result_path: ./coverage/lcov.info
min_coverage: 86
min_coverage: 87
token: ${{ github.token }}
##############################################################################

View File

@ -15,12 +15,19 @@ const addNavigationGuards = (router, store, apollo) => {
router.beforeEach(async (to, from, next) => {
if (to.path === '/authenticate' && to.query.token) {
store.commit('token', to.query.token)
const result = await apollo.query({
query: verifyLogin,
fetchPolicy: 'network-only',
})
store.dispatch('login', result.data.verifyLogin)
next({ path: '/overview' })
await apollo
.query({
query: verifyLogin,
fetchPolicy: 'network-only',
})
.then((result) => {
store.dispatch('login', result.data.verifyLogin)
next({ path: '/overview' })
})
.catch(() => {
store.dispatch('logout')
next()
})
} else {
next()
}

View File

@ -2,15 +2,28 @@ import addNavigationGuards from './guards'
import router from './router'
const storeCommitMock = jest.fn()
const storeDispatchMock = jest.fn()
const apolloQueryMock = jest.fn().mockResolvedValue({
data: {
verifyLogin: {
firstName: 'Peter',
},
},
})
const store = {
commit: storeCommitMock,
state: {
token: null,
},
dispatch: storeDispatchMock,
}
addNavigationGuards(router, store)
const apollo = {
query: apolloQueryMock,
}
addNavigationGuards(router, store, apollo)
describe('navigation guards', () => {
beforeEach(() => {
@ -29,6 +42,46 @@ describe('navigation guards', () => {
})
})
describe('authenticate', () => {
const navGuard = router.beforeHooks[1]
const next = jest.fn()
describe('with valid token', () => {
beforeEach(() => {
navGuard({ path: '/authenticate', query: { token: 'valid-token' } }, {}, next)
})
it('commts the token to the store', () => {
expect(storeCommitMock).toBeCalledWith('token', 'valid-token')
})
it('calls verifyLogin', () => {
expect(apolloQueryMock).toBeCalled()
})
it('commits login to the store', () => {
expect(storeDispatchMock).toBeCalledWith('login', { firstName: 'Peter' })
})
})
describe('with valid token and server error', () => {
beforeEach(() => {
apolloQueryMock.mockRejectedValue({
message: 'Ouch!',
})
navGuard({ path: '/authenticate', query: { token: 'valid-token' } }, {}, next)
})
it('dispatches logout to store', () => {
expect(storeDispatchMock).toBeCalledWith('logout')
})
it('calls next', () => {
expect(next).toBeCalledWith()
})
})
})
describe('authorization', () => {
const navGuard = router.beforeHooks[2]
const next = jest.fn()