Merge branch 'master' into clean-up-frontend-links

This commit is contained in:
Moriz Wahl 2021-10-07 19:22:47 +02:00
commit 5bb0c2b5a5
7 changed files with 91 additions and 10 deletions

View File

@ -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 }}
##############################################################################

View File

@ -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,13 +50,7 @@ Vue.config.productionTip = false
loadAllRules(i18n)
router.beforeEach((to, from, next) => {
if (to.meta.requiresAuth && !store.state.token) {
next({ path: '/login' })
} else {
next()
}
})
addNavigationGuards(router, store)
/* eslint-disable no-new */
new Vue({

View File

@ -0,0 +1,18 @@
const addNavigationGuards = (router, store) => {
router.beforeEach((to, from, next) => {
// handle publisherId
const publisherId = to.query.pid
if (publisherId) {
store.commit('publisherId', publisherId)
delete to.query.pid
}
// handle authentication
if (to.meta.requiresAuth && !store.state.token) {
next({ path: '/login' })
} else {
next()
}
})
}
export default addNavigationGuards

View File

@ -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')
})
})
})

View File

@ -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) => {

View File

@ -29,6 +29,9 @@ export const mutations = {
newsletterState: (state, newsletterState) => {
state.newsletterState = newsletterState
},
publisherId: (state, publisherId) => {
state.publisherId = publisherId
},
community: (state, community) => {
state.community = community
},

View File

@ -10,6 +10,8 @@ const {
description,
coinanimation,
newsletterState,
publisherId,
community,
} = mutations
const { login, logout } = actions
@ -86,6 +88,32 @@ 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('community', () => {
it('sets the state of community', () => {
const state = {}
community(state, {
name: 'test12',
description: 'test community 12',
url: 'http://test12.test12/',
registerUrl: 'http://test12.test12/vue/register',
})
expect(state.community).toEqual({
name: 'test12',
description: 'test community 12',
url: 'http://test12.test12/',
registerUrl: 'http://test12.test12/vue/register',
})
})
})
})
describe('actions', () => {