Test guards with roles array for verfiyLogin.

This commit is contained in:
elweyn 2023-07-23 11:09:39 +02:00
parent 8b2d07c0af
commit bed2b86ea5

View File

@ -5,7 +5,7 @@ const storeCommitMock = jest.fn()
const apolloQueryMock = jest.fn().mockResolvedValue({
data: {
verifyLogin: {
isAdmin: true,
roles: ['ADMIN'],
language: 'de',
},
},
@ -52,7 +52,12 @@ describe('navigation guards', () => {
})
it('commits the moderator to the store', () => {
expect(storeCommitMock).toBeCalledWith('moderator', { isAdmin: true, language: 'de' })
expect(storeCommitMock).toBeCalledWith('moderator', {
roles: ['ADMIN'],
isAdmin: true,
isModerator: false,
language: 'de',
})
})
it('redirects to /', () => {
@ -60,12 +65,49 @@ describe('navigation guards', () => {
})
})
describe('with valid token and not as admin', () => {
describe('with valid token and as moderator', () => {
beforeEach(async () => {
jest.clearAllMocks()
apolloQueryMock.mockResolvedValue({
data: {
verifyLogin: {
roles: ['MODERATOR'],
language: 'de',
},
},
})
await navGuard({ path: '/authenticate', query: { token: 'valid-token' } }, {}, next)
})
it('commits the token to the store', () => {
expect(storeCommitMock).toBeCalledWith('token', 'valid-token')
})
it.skip('sets the locale', () => {
expect(i18nLocaleMock).toBeCalledWith('de')
})
it('commits the moderator to the store', () => {
expect(storeCommitMock).toBeCalledWith('moderator', {
roles: ['MODERATOR'],
isAdmin: false,
isModerator: true,
language: 'de',
})
})
it('redirects to /', () => {
expect(next).toBeCalledWith({ path: '/' })
})
})
describe('with valid token and no roles', () => {
beforeEach(() => {
apolloQueryMock.mockResolvedValue({
data: {
verifyLogin: {
isAdmin: false,
roles: [],
language: 'de',
},
},
})
@ -128,17 +170,24 @@ describe('navigation guards', () => {
expect(next).toBeCalledWith({ path: '/not-found' })
})
it('redirects to not found with token in store and not moderator', () => {
it('redirects to not found with token in store and not admin or moderator', () => {
store.state.token = 'valid token'
navGuard({ path: '/' }, {}, next)
expect(next).toBeCalledWith({ path: '/not-found' })
})
it('does not redirect with token in store and as moderator', () => {
it('does not redirect with token in store and as admin', () => {
store.state.token = 'valid token'
store.state.moderator = { isAdmin: true }
navGuard({ path: '/' }, {}, next)
expect(next).toBeCalledWith()
})
it('does not redirect with token in store and as moderator', () => {
store.state.token = 'valid token'
store.state.moderator = { isModerator: true }
navGuard({ path: '/' }, {}, next)
expect(next).toBeCalledWith()
})
})
})