From 6665c17f0b7838caba1e0a012f90b69d0a769777 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Wed, 6 Oct 2021 14:47:03 +0200 Subject: [PATCH 1/6] feat: Global Catch of Publisher ID to Store --- frontend/src/main.js | 5 +++++ frontend/src/store/store.js | 3 +++ frontend/src/store/store.test.js | 9 +++++++++ 3 files changed, 17 insertions(+) diff --git a/frontend/src/main.js b/frontend/src/main.js index ba7015049..32a15103e 100755 --- a/frontend/src/main.js +++ b/frontend/src/main.js @@ -50,6 +50,11 @@ Vue.config.productionTip = false loadAllRules(i18n) router.beforeEach((to, from, next) => { + const publisherId = to.query.pid + if (publisherId) { + store.commit('publisherId', publisherId) + to.query.pid = undefined + } if (to.meta.requiresAuth && !store.state.token) { next({ path: '/login' }) } else { diff --git a/frontend/src/store/store.js b/frontend/src/store/store.js index 5bb55c9d8..cfaafec1e 100644 --- a/frontend/src/store/store.js +++ b/frontend/src/store/store.js @@ -29,6 +29,9 @@ export const mutations = { newsletterState: (state, newsletterState) => { state.newsletterState = newsletterState }, + publisherId: (state, publisherId) => { + state.publisherId = publisherId + }, } export const actions = { diff --git a/frontend/src/store/store.test.js b/frontend/src/store/store.test.js index 7ae3344d9..f40f25d92 100644 --- a/frontend/src/store/store.test.js +++ b/frontend/src/store/store.test.js @@ -9,6 +9,7 @@ const { lastName, description, newsletterState, + publisherId, } = mutations const { login, logout } = actions @@ -77,6 +78,14 @@ describe('Vuex store', () => { expect(state.newsletterState).toEqual(true) }) }) + + describe('publisherId', () => { + it('sets the state of publisherId', () => { + const state = {} + publisherId(state, 42) + expect(state.publisherId).toEqual(42) + }) + }) }) describe('actions', () => { From 20466d311cab46d0cfcf5eba4d92589964769987 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Wed, 6 Oct 2021 15:01:02 +0200 Subject: [PATCH 2/6] delete pid from query object --- frontend/src/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/src/main.js b/frontend/src/main.js index 32a15103e..3cbbc15fe 100755 --- a/frontend/src/main.js +++ b/frontend/src/main.js @@ -53,7 +53,7 @@ router.beforeEach((to, from, next) => { const publisherId = to.query.pid if (publisherId) { store.commit('publisherId', publisherId) - to.query.pid = undefined + delete to.query.pid } if (to.meta.requiresAuth && !store.state.token) { next({ path: '/login' }) From 505c2f030b38ed6ae7bdeb40a5205dafbb68bfb6 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Wed, 6 Oct 2021 16:21:29 +0200 Subject: [PATCH 3/6] test storing pid from routes --- frontend/src/main.js | 14 ++--------- frontend/src/routes/guards.js | 16 +++++++++++++ frontend/src/routes/guards.test.js | 38 ++++++++++++++++++++++++++++++ frontend/src/routes/router.js | 3 +-- 4 files changed, 57 insertions(+), 14 deletions(-) create mode 100644 frontend/src/routes/guards.js create mode 100644 frontend/src/routes/guards.test.js diff --git a/frontend/src/main.js b/frontend/src/main.js index 3cbbc15fe..ad5c6e1db 100755 --- a/frontend/src/main.js +++ b/frontend/src/main.js @@ -6,6 +6,7 @@ import { loadAllRules } from './validation-rules' import { ApolloClient, ApolloLink, InMemoryCache, HttpLink } from 'apollo-boost' import VueApollo from 'vue-apollo' import CONFIG from './config' +import addNavigationGuards from './routes/guards' import { store } from './store/store' @@ -49,18 +50,7 @@ Vue.config.productionTip = false loadAllRules(i18n) -router.beforeEach((to, from, next) => { - const publisherId = to.query.pid - if (publisherId) { - store.commit('publisherId', publisherId) - delete to.query.pid - } - if (to.meta.requiresAuth && !store.state.token) { - next({ path: '/login' }) - } else { - next() - } -}) +addNavigationGuards(router, store) /* eslint-disable no-new */ new Vue({ diff --git a/frontend/src/routes/guards.js b/frontend/src/routes/guards.js new file mode 100644 index 000000000..91e341370 --- /dev/null +++ b/frontend/src/routes/guards.js @@ -0,0 +1,16 @@ +const addNavigationGuards = (router, store) => { + router.beforeEach((to, from, next) => { + const publisherId = to.query.pid + if (publisherId) { + store.commit('publisherId', publisherId) + delete to.query.pid + } + if (to.meta.requiresAuth && !store.state.token) { + next({ path: '/login' }) + } else { + next() + } + }) +} + +export default addNavigationGuards diff --git a/frontend/src/routes/guards.test.js b/frontend/src/routes/guards.test.js new file mode 100644 index 000000000..a016ca111 --- /dev/null +++ b/frontend/src/routes/guards.test.js @@ -0,0 +1,38 @@ +import addNavigationGuards from './guards' +import router from './router' + +const storeCommitMock = jest.fn() + +const store = { + commit: storeCommitMock, + state: { + token: null, + }, +} + +addNavigationGuards(router, store) + +describe('navigation guards', () => { + beforeEach(() => { + jest.clearAllMocks() + }) + + describe('publisher ID', () => { + it('commits the pid to the store when present', () => { + router.push({ path: 'login', query: { pid: 42 } }) + expect(storeCommitMock).toBeCalledWith('publisherId', '42') + }) + + it('does not commit the pid when not present', () => { + router.push({ path: 'register' }) + expect(storeCommitMock).not.toBeCalled() + }) + }) + + describe('authorization', () => { + it.skip('redirects to login when not authorized', async () => { + router.push({ path: 'overview' }) + expect(router.history.current.path).toBe('/login') + }) + }) +}) diff --git a/frontend/src/routes/router.js b/frontend/src/routes/router.js index 189516242..f32c15fc1 100644 --- a/frontend/src/routes/router.js +++ b/frontend/src/routes/router.js @@ -5,10 +5,9 @@ import CONFIG from '../config' Vue.use(VueRouter) -// configure router const router = new VueRouter({ base: '/vue', - routes, // short for routes: routes + routes, linkActiveClass: 'active', mode: 'history', scrollBehavior: (to, from, savedPosition) => { From 61075e149373d76fa9fb315d14c51a1b41f4be5a Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 7 Oct 2021 14:03:31 +0200 Subject: [PATCH 4/6] add comment for pid Co-authored-by: Ulf Gebhardt --- frontend/src/routes/guards.js | 1 + 1 file changed, 1 insertion(+) diff --git a/frontend/src/routes/guards.js b/frontend/src/routes/guards.js index 91e341370..791978352 100644 --- a/frontend/src/routes/guards.js +++ b/frontend/src/routes/guards.js @@ -1,5 +1,6 @@ const addNavigationGuards = (router, store) => { router.beforeEach((to, from, next) => { + // handle publisherId const publisherId = to.query.pid if (publisherId) { store.commit('publisherId', publisherId) From 8ae46474fccd8167fd2d183a3f2f68c38df3788e Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 7 Oct 2021 14:06:43 +0200 Subject: [PATCH 5/6] add comment for authentication Co-authored-by: Ulf Gebhardt --- frontend/src/routes/guards.js | 1 + 1 file changed, 1 insertion(+) diff --git a/frontend/src/routes/guards.js b/frontend/src/routes/guards.js index 791978352..eebd6976e 100644 --- a/frontend/src/routes/guards.js +++ b/frontend/src/routes/guards.js @@ -6,6 +6,7 @@ const addNavigationGuards = (router, store) => { store.commit('publisherId', publisherId) delete to.query.pid } + // handle authentication if (to.meta.requiresAuth && !store.state.token) { next({ path: '/login' }) } else { From 500b3580629c0cba0b71905b6286f88932b46f7b Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 7 Oct 2021 14:39:51 +0200 Subject: [PATCH 6/6] frontend test coverage to 76% --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dae41fc1c..de9939101 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -344,7 +344,7 @@ jobs: report_name: Coverage Frontend type: lcov result_path: ./coverage/lcov.info - min_coverage: 75 + min_coverage: 76 token: ${{ github.token }} ##############################################################################