From bed2b86ea58de86452465ec924973612de98b325 Mon Sep 17 00:00:00 2001 From: elweyn Date: Sun, 23 Jul 2023 11:09:39 +0200 Subject: [PATCH] Test guards with roles array for verfiyLogin. --- admin/src/router/guards.test.js | 61 +++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/admin/src/router/guards.test.js b/admin/src/router/guards.test.js index da4dd5969..19a1881ef 100644 --- a/admin/src/router/guards.test.js +++ b/admin/src/router/guards.test.js @@ -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() + }) }) })