From fb26219354a5767fa11141c8fa5bda5fc42dded8 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Fri, 19 Apr 2024 12:09:04 +0200 Subject: [PATCH 1/7] refs #7190 feat: useTokenConfig --- src/composables/useState.js | 12 ++++++++++++ src/composables/useTokenConfig.js | 26 ++++++++++++++++++++++++++ src/i18n/locale/en.yml | 1 + src/i18n/locale/es.yml | 1 + 4 files changed, 40 insertions(+) create mode 100644 src/composables/useTokenConfig.js diff --git a/src/composables/useState.js b/src/composables/useState.js index e0b742a73..e671d41bd 100644 --- a/src/composables/useState.js +++ b/src/composables/useState.js @@ -13,6 +13,7 @@ const user = ref({ }); const roles = ref([]); +const tokenConfig = ref({}); const drawer = ref(true); const headerMounted = ref(false); @@ -52,6 +53,15 @@ export function useState() { function setRoles(data) { roles.value = data; } + function getTokenConfig() { + return computed(() => { + return tokenConfig.value; + }); + } + + function setTokenConfig(data) { + tokenConfig.value = data; + } function set(name, data) { state.value[name] = ref(data); @@ -70,6 +80,8 @@ export function useState() { setUser, getRoles, setRoles, + getTokenConfig, + setTokenConfig, set, get, unset, diff --git a/src/composables/useTokenConfig.js b/src/composables/useTokenConfig.js new file mode 100644 index 000000000..3119a6aa6 --- /dev/null +++ b/src/composables/useTokenConfig.js @@ -0,0 +1,26 @@ +import axios from 'axios'; +import { useState } from './useState'; +import useNotify from './useNotify'; + +export function useTokenConfig() { + const state = useState(); + const { notify } = useNotify(); + + async function fetch() { + try { + const tokenConfig = await axios.get('AccessTokenConfigs/findOne', { + filter: { fields: ['renewInterval', 'renewPeriod'] }, + }); + if (!tokenConfig) return; + state.setTokenConfig(tokenConfig); + return tokenConfig; + } catch (error) { + notify('errors.tokenConfig', 'negative'); + console.error('Error fetching token config:', error); + } + } + + return { + fetch, + }; +} diff --git a/src/i18n/locale/en.yml b/src/i18n/locale/en.yml index db34b393c..bcdc6dca1 100644 --- a/src/i18n/locale/en.yml +++ b/src/i18n/locale/en.yml @@ -96,6 +96,7 @@ errors: statusBadGateway: It seems that the server has fall down statusGatewayTimeout: Could not contact the server userConfig: Error fetching user config + tokenConfig: Error fetching token config writeRequest: The requested operation could not be completed login: title: Login diff --git a/src/i18n/locale/es.yml b/src/i18n/locale/es.yml index cb5876dd3..73eb70e60 100644 --- a/src/i18n/locale/es.yml +++ b/src/i18n/locale/es.yml @@ -96,6 +96,7 @@ errors: statusBadGateway: Parece ser que el servidor ha caído statusGatewayTimeout: No se ha podido contactar con el servidor userConfig: Error al obtener configuración de usuario + tokenConfig: Error al obtener configuración de token writeRequest: No se pudo completar la operación solicitada login: title: Inicio de sesión From aecea5f273110c90d820c728d402b9b911f71836 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Fri, 19 Apr 2024 12:26:43 +0200 Subject: [PATCH 2/7] refs #7190 feat: renewToken --- src/composables/useSession.js | 48 ++++++++++++++++++++++++++++++- src/composables/useTokenConfig.js | 3 ++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/composables/useSession.js b/src/composables/useSession.js index 8583e10d4..b4c5e4abb 100644 --- a/src/composables/useSession.js +++ b/src/composables/useSession.js @@ -3,11 +3,13 @@ import { useRole } from './useRole'; import { useUserConfig } from './useUserConfig'; import axios from 'axios'; import useNotify from './useNotify'; +import { useTokenConfig } from './useTokenConfig'; const TOKEN_MULTIMEDIA = 'tokenMultimedia'; const TOKEN = 'token'; export function useSession() { const { notify } = useNotify(); + let isCheckingToken = false; function getToken() { const localToken = localStorage.getItem(TOKEN); @@ -23,9 +25,12 @@ export function useSession() { } function setToken(data) { - const storage = data.keepLogin ? localStorage : sessionStorage; + let keepLogin = data.keepLogin ?? sessionStorage.getItem('keepLogin'); + if (!keepLogin) keepLogin = false; + const storage = keepLogin ? localStorage : sessionStorage; storage.setItem(TOKEN, data.token); storage.setItem(TOKEN_MULTIMEDIA, data.tokenMultimedia); + sessionStorage.setItem('keepLogin', keepLogin); } async function destroyToken(url, storage, key) { if (storage.getItem(key)) { @@ -59,6 +64,8 @@ export function useSession() { lang: '', darkMode: null, }); + + stopRenewer(); } async function login(token, tokenMultimedia, keepLogin) { @@ -66,6 +73,9 @@ export function useSession() { await useRole().fetch(); await useUserConfig().fetch(); + await useTokenConfig().fetch(); + + await checkValidity(); } function isLoggedIn() { @@ -75,6 +85,42 @@ export function useSession() { return !!(localToken || sessionToken); } + function stopRenewer() { + clearInterval(this.intervalId); + } + + async function renewToken() { + const _token = getToken(); + const token = await axios.post('VnUsers/renewToken', { + headers: { Authorization: _token }, + }); + const _tokenMultimedia = getTokenMultimedia(); + const tokenMultimedia = await axios.post('VnUsers/renewToken', { + headers: { Authorization: _tokenMultimedia }, + }); + setToken({ token, tokenMultimedia }); + } + + async function checkValidity() { + const { getTokenConfig } = useState(); + + const tokenConfig = getTokenConfig(); + const created = +localStorage.getItem('created'); + const ttl = localStorage.getItem('ttl'); + + if (isCheckingToken || !created) return; + isCheckingToken = true; + + const renewPeriodInSeconds = Math.min(ttl, tokenConfig.value.renewPeriod) * 1000; + const maxDate = created + renewPeriodInSeconds; + const now = new Date(); + + if (now.getTime() <= maxDate) return (isCheckingToken = false); + + await renewToken(); + isCheckingToken = false; + } + return { getToken, getTokenMultimedia, diff --git a/src/composables/useTokenConfig.js b/src/composables/useTokenConfig.js index 3119a6aa6..afe7a5272 100644 --- a/src/composables/useTokenConfig.js +++ b/src/composables/useTokenConfig.js @@ -8,6 +8,9 @@ export function useTokenConfig() { async function fetch() { try { + let tokenConfigState = state.getTokenConfig(); + if (tokenConfigState) return tokenConfigState; + const tokenConfig = await axios.get('AccessTokenConfigs/findOne', { filter: { fields: ['renewInterval', 'renewPeriod'] }, }); From 2b24e18f755c135ae7f94e284fcf58d0cdba1b98 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Mon, 22 Apr 2024 07:37:35 +0200 Subject: [PATCH 3/7] FEAT: SETiNTERVAL WITH RENEWpERIOD --- src/composables/useSession.js | 68 +++++++++++++++++++++++-------- src/composables/useTokenConfig.js | 12 +++--- src/pages/Login/LoginMain.vue | 8 +++- src/router/index.js | 2 + 4 files changed, 64 insertions(+), 26 deletions(-) diff --git a/src/composables/useSession.js b/src/composables/useSession.js index b4c5e4abb..f308bf3e2 100644 --- a/src/composables/useSession.js +++ b/src/composables/useSession.js @@ -10,6 +10,7 @@ const TOKEN = 'token'; export function useSession() { const { notify } = useNotify(); let isCheckingToken = false; + let intervalId = null; function getToken() { const localToken = localStorage.getItem(TOKEN); @@ -24,14 +25,25 @@ export function useSession() { return localTokenMultimedia || sessionTokenMultimedia || ''; } - function setToken(data) { - let keepLogin = data.keepLogin ?? sessionStorage.getItem('keepLogin'); - if (!keepLogin) keepLogin = false; + function setSession(data) { + let keepLogin = data.keepLogin; const storage = keepLogin ? localStorage : sessionStorage; storage.setItem(TOKEN, data.token); storage.setItem(TOKEN_MULTIMEDIA, data.tokenMultimedia); + storage.setItem('created', data.created); + storage.setItem('ttl', data.ttl); sessionStorage.setItem('keepLogin', keepLogin); } + + function keepLogin() { + return sessionStorage.getItem('keepLogin'); + } + + function setToken({ token, tokenMultimedia }) { + const storage = keepLogin() ? localStorage : sessionStorage; + storage.setItem(TOKEN, token); + storage.setItem(TOKEN_MULTIMEDIA, tokenMultimedia); + } async function destroyToken(url, storage, key) { if (storage.getItem(key)) { try { @@ -50,11 +62,15 @@ export function useSession() { tokenMultimedia: 'Accounts/logout', token: 'VnUsers/logout', }; + const storage = keepLogin() ? localStorage : sessionStorage; + for (const [key, url] of Object.entries(tokens)) { - await destroyToken(url, localStorage, key); - await destroyToken(url, sessionStorage, key); + await destroyToken(url, storage, key); } + localStorage.clear(); + sessionStorage.clear(); + const { setUser } = useState(); setUser({ @@ -68,25 +84,33 @@ export function useSession() { stopRenewer(); } - async function login(token, tokenMultimedia, keepLogin) { - setToken({ token, tokenMultimedia, keepLogin }); + async function login(data) { + setSession(data); await useRole().fetch(); await useUserConfig().fetch(); await useTokenConfig().fetch(); - - await checkValidity(); + startInterval(); } function isLoggedIn() { const localToken = localStorage.getItem(TOKEN); const sessionToken = sessionStorage.getItem(TOKEN); - + startInterval(); return !!(localToken || sessionToken); } + function startInterval() { + stopRenewer(); + const renewPeriod = +sessionStorage.getItem('renewPeriod'); + if (!renewPeriod) return; + intervalId = setInterval(() => checkValidity(), renewPeriod * 1000); + // JUST FOR TEST + // checkValidity(); + } + function stopRenewer() { - clearInterval(this.intervalId); + clearInterval(intervalId); } async function renewToken() { @@ -98,24 +122,32 @@ export function useSession() { const tokenMultimedia = await axios.post('VnUsers/renewToken', { headers: { Authorization: _tokenMultimedia }, }); - setToken({ token, tokenMultimedia }); + setToken({ token: token.data.id, tokenMultimedia: tokenMultimedia.data.id }); } async function checkValidity() { const { getTokenConfig } = useState(); - const tokenConfig = getTokenConfig(); - const created = +localStorage.getItem('created'); - const ttl = localStorage.getItem('ttl'); + const tokenConfig = getTokenConfig() ?? sessionStorage.getItem('tokenConfig'); + const storage = keepLogin() ? localStorage : sessionStorage; + + const created = +storage.getItem('created'); + const ttl = +storage.getItem('ttl'); if (isCheckingToken || !created) return; isCheckingToken = true; const renewPeriodInSeconds = Math.min(ttl, tokenConfig.value.renewPeriod) * 1000; - const maxDate = created + renewPeriodInSeconds; - const now = new Date(); - if (now.getTime() <= maxDate) return (isCheckingToken = false); + const maxDate = created + renewPeriodInSeconds; + const now = new Date().getTime(); + + //COMMENT THIS IF JUST FOR TEST + if (isNaN(renewPeriodInSeconds) || now <= maxDate) { + console.error('Token not renewed'); + return (isCheckingToken = false); + } + console.error('Token renewed'); await renewToken(); isCheckingToken = false; diff --git a/src/composables/useTokenConfig.js b/src/composables/useTokenConfig.js index afe7a5272..600122199 100644 --- a/src/composables/useTokenConfig.js +++ b/src/composables/useTokenConfig.js @@ -8,15 +8,13 @@ export function useTokenConfig() { async function fetch() { try { - let tokenConfigState = state.getTokenConfig(); - if (tokenConfigState) return tokenConfigState; - - const tokenConfig = await axios.get('AccessTokenConfigs/findOne', { + const { data } = await axios.get('AccessTokenConfigs/findOne', { filter: { fields: ['renewInterval', 'renewPeriod'] }, }); - if (!tokenConfig) return; - state.setTokenConfig(tokenConfig); - return tokenConfig; + if (!data) return; + state.setTokenConfig(data); + sessionStorage.setItem('renewPeriod', data.renewPeriod); + return data; } catch (error) { notify('errors.tokenConfig', 'negative'); console.error('Error fetching token config:', error); diff --git a/src/pages/Login/LoginMain.vue b/src/pages/Login/LoginMain.vue index fcde51edf..5a3490f50 100644 --- a/src/pages/Login/LoginMain.vue +++ b/src/pages/Login/LoginMain.vue @@ -38,7 +38,13 @@ async function onSubmit() { if (!multimediaToken) return; - await session.login(data.token, multimediaToken.id, keepLogin.value); + const login = { + ...data, + created: Date.now(), + tokenMultimedia: multimediaToken.id, + keepLogin: keepLogin.value, + }; + await session.login(login); quasar.notify({ message: t('login.loginSuccess'), diff --git a/src/router/index.js b/src/router/index.js index 3e442f0e6..7a0aedcae 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -12,6 +12,7 @@ import { useSession } from 'src/composables/useSession'; import { useRole } from 'src/composables/useRole'; import { useUserConfig } from 'src/composables/useUserConfig'; import { toLowerCamel } from 'src/filters'; +import { useTokenConfig } from 'src/composables/useTokenConfig'; const state = useState(); const session = useSession(); @@ -55,6 +56,7 @@ export default route(function (/* { store, ssrContext } */) { if (stateRoles.length === 0) { await useRole().fetch(); await useUserConfig().fetch(); + await useTokenConfig().fetch(); } const matches = to.matched; const hasRequiredRoles = matches.every((route) => { From 6018d51642165a13e97ab9bb480d8686bcb62872 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 23 Apr 2024 07:56:07 +0200 Subject: [PATCH 4/7] perf: #7190 remove comments --- src/composables/useSession.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/composables/useSession.js b/src/composables/useSession.js index f308bf3e2..bd3532816 100644 --- a/src/composables/useSession.js +++ b/src/composables/useSession.js @@ -90,6 +90,7 @@ export function useSession() { await useRole().fetch(); await useUserConfig().fetch(); await useTokenConfig().fetch(); + startInterval(); } @@ -105,8 +106,6 @@ export function useSession() { const renewPeriod = +sessionStorage.getItem('renewPeriod'); if (!renewPeriod) return; intervalId = setInterval(() => checkValidity(), renewPeriod * 1000); - // JUST FOR TEST - // checkValidity(); } function stopRenewer() { @@ -144,10 +143,8 @@ export function useSession() { //COMMENT THIS IF JUST FOR TEST if (isNaN(renewPeriodInSeconds) || now <= maxDate) { - console.error('Token not renewed'); return (isCheckingToken = false); } - console.error('Token renewed'); await renewToken(); isCheckingToken = false; From 04fe9330d45e97a4ec470abaf088245657ee1840 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 23 Apr 2024 08:03:09 +0200 Subject: [PATCH 5/7] fix: #7190 tests --- .../__tests__/composables/useSession.spec.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/test/vitest/__tests__/composables/useSession.spec.js b/test/vitest/__tests__/composables/useSession.spec.js index f9f3dcb80..8feb5e571 100644 --- a/test/vitest/__tests__/composables/useSession.spec.js +++ b/test/vitest/__tests__/composables/useSession.spec.js @@ -96,7 +96,11 @@ describe('session', () => { const expectedTokenMultimedia = 'mySessionTokenMultimedia'; const keepLogin = false; - await session.login(expectedToken,expectedTokenMultimedia, keepLogin); + await session.login({ + token: expectedToken, + tokenMultimedia: expectedTokenMultimedia, + keepLogin, + }); const roles = state.getRoles(); const localToken = localStorage.getItem('token'); @@ -106,7 +110,7 @@ describe('session', () => { expect(localToken).toBeNull(); expect(sessionToken).toEqual(expectedToken); - await session.destroy(); // this clears token and user for any other test + await session.destroy(); // this clears token and user for any other test }); it('should fetch the user roles and then set token in the localStorage', async () => { @@ -119,7 +123,11 @@ describe('session', () => { const expectedTokenMultimedia = 'myLocalTokenMultimedia'; const keepLogin = true; - await session.login(expectedToken, expectedTokenMultimedia, keepLogin); + await session.login({ + token: expectedToken, + tokenMultimedia: expectedTokenMultimedia, + keepLogin, + }); const roles = state.getRoles(); const localToken = localStorage.getItem('token'); From a555d11663650f94f64a0619fbdb458f3a2b480e Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Wed, 24 Apr 2024 12:15:12 +0200 Subject: [PATCH 6/7] fix: #7190 test --- src/composables/useSession.js | 3 + src/composables/useTokenConfig.js | 1 + .../__tests__/composables/useSession.spec.js | 197 ++++++++++++------ .../composables/useTokenConfig.spec.js | 31 +++ 4 files changed, 167 insertions(+), 65 deletions(-) create mode 100644 test/vitest/__tests__/composables/useTokenConfig.spec.js diff --git a/src/composables/useSession.js b/src/composables/useSession.js index bd3532816..3b38cdad6 100644 --- a/src/composables/useSession.js +++ b/src/composables/useSession.js @@ -157,5 +157,8 @@ export function useSession() { destroy, login, isLoggedIn, + checkValidity, + setSession, + renewToken, }; } diff --git a/src/composables/useTokenConfig.js b/src/composables/useTokenConfig.js index 600122199..5cf1b34ee 100644 --- a/src/composables/useTokenConfig.js +++ b/src/composables/useTokenConfig.js @@ -23,5 +23,6 @@ export function useTokenConfig() { return { fetch, + state, }; } diff --git a/test/vitest/__tests__/composables/useSession.spec.js b/test/vitest/__tests__/composables/useSession.spec.js index 8feb5e571..2292859a9 100644 --- a/test/vitest/__tests__/composables/useSession.spec.js +++ b/test/vitest/__tests__/composables/useSession.spec.js @@ -1,5 +1,5 @@ -import { vi, describe, expect, it } from 'vitest'; -import { axios } from 'app/test/vitest/helper'; +import { vi, describe, expect, it, beforeAll, beforeEach } from 'vitest'; +import { axios, flushPromises } from 'app/test/vitest/helper'; import { useSession } from 'composables/useSession'; import { useState } from 'composables/useState'; @@ -63,81 +63,148 @@ describe('session', () => { }); }); - describe('login', () => { - const expectedUser = { - id: 999, - name: `T'Challa`, - nickname: 'Black Panther', - lang: 'en', - userConfig: { - darkMode: false, - }, - }; - const rolesData = [ - { - role: { - name: 'salesPerson', + describe( + 'login', + () => { + const expectedUser = { + id: 999, + name: `T'Challa`, + nickname: 'Black Panther', + lang: 'en', + userConfig: { + darkMode: false, }, - }, - { - role: { - name: 'admin', + }; + const rolesData = [ + { + role: { + name: 'salesPerson', + }, }, - }, - ]; + { + role: { + name: 'admin', + }, + }, + ]; - it('should fetch the user roles and then set token in the sessionStorage', async () => { - const expectedRoles = ['salesPerson', 'admin']; - vi.spyOn(axios, 'get').mockResolvedValue({ - data: { roles: rolesData, user: expectedUser }, + it('should fetch the user roles and then set token in the sessionStorage', async () => { + const expectedRoles = ['salesPerson', 'admin']; + vi.spyOn(axios, 'get').mockResolvedValue({ + data: { roles: rolesData, user: expectedUser }, + }); + + const expectedToken = 'mySessionToken'; + const expectedTokenMultimedia = 'mySessionTokenMultimedia'; + const keepLogin = false; + + await session.login({ + token: expectedToken, + tokenMultimedia: expectedTokenMultimedia, + keepLogin, + }); + + const roles = state.getRoles(); + const localToken = localStorage.getItem('token'); + const sessionToken = sessionStorage.getItem('token'); + + expect(roles.value).toEqual(expectedRoles); + expect(localToken).toBeNull(); + expect(sessionToken).toEqual(expectedToken); + + await session.destroy(); // this clears token and user for any other test }); - const expectedToken = 'mySessionToken'; - const expectedTokenMultimedia = 'mySessionTokenMultimedia'; - const keepLogin = false; + it('should fetch the user roles and then set token in the localStorage', async () => { + const expectedRoles = ['salesPerson', 'admin']; + vi.spyOn(axios, 'get').mockResolvedValue({ + data: { roles: rolesData, user: expectedUser }, + }); - await session.login({ - token: expectedToken, - tokenMultimedia: expectedTokenMultimedia, - keepLogin, + const expectedToken = 'myLocalToken'; + const expectedTokenMultimedia = 'myLocalTokenMultimedia'; + const keepLogin = true; + + await session.login({ + token: expectedToken, + tokenMultimedia: expectedTokenMultimedia, + keepLogin, + }); + + const roles = state.getRoles(); + const localToken = localStorage.getItem('token'); + const sessionToken = sessionStorage.getItem('token'); + + expect(roles.value).toEqual(expectedRoles); + expect(localToken).toEqual(expectedToken); + expect(sessionToken).toBeNull(); + + await session.destroy(); // this clears token and user for any other test }); + }, + {} + ); - const roles = state.getRoles(); - const localToken = localStorage.getItem('token'); - const sessionToken = sessionStorage.getItem('token'); - - expect(roles.value).toEqual(expectedRoles); - expect(localToken).toBeNull(); - expect(sessionToken).toEqual(expectedToken); - - await session.destroy(); // this clears token and user for any other test + describe('RenewToken', () => { + const expectedToken = 'myToken'; + const expectedTokenMultimedia = 'myTokenMultimedia'; + const currentDate = new Date(); + beforeAll(() => { + const tokenConfig = { + id: 1, + renewPeriod: 21600, + courtesyTime: 60, + renewInterval: 300, + }; + state.setTokenConfig(tokenConfig); + sessionStorage.setItem('renewPeriod', 1); }); - - it('should fetch the user roles and then set token in the localStorage', async () => { - const expectedRoles = ['salesPerson', 'admin']; - vi.spyOn(axios, 'get').mockResolvedValue({ - data: { roles: rolesData, user: expectedUser }, - }); - - const expectedToken = 'myLocalToken'; - const expectedTokenMultimedia = 'myLocalTokenMultimedia'; - const keepLogin = true; - - await session.login({ + it('NOT Should renewToken', async () => { + const data = { token: expectedToken, tokenMultimedia: expectedTokenMultimedia, - keepLogin, - }); + keepLogin: false, + ttl: 1, + created: Date.now(), + }; + session.setSession(data); + expect(sessionStorage.getItem('keepLogin')).toBeFalsy(); + expect(sessionStorage.getItem('created')).toBeDefined(); + expect(sessionStorage.getItem('ttl')).toEqual(1); + await session.checkValidity(); + expect(sessionStorage.getItem('token')).toEqual(expectedToken); + expect(sessionStorage.getItem('tokenMultimedia')).toEqual( + expectedTokenMultimedia + ); + }); + it('Should renewToken', async () => { + currentDate.setMinutes(currentDate.getMinutes() - 100); + const data = { + token: expectedToken, + tokenMultimedia: expectedTokenMultimedia, + keepLogin: false, + ttl: 1, + created: currentDate, + }; + session.setSession(data); - const roles = state.getRoles(); - const localToken = localStorage.getItem('token'); - const sessionToken = sessionStorage.getItem('token'); - - expect(roles.value).toEqual(expectedRoles); - expect(localToken).toEqual(expectedToken); - expect(sessionToken).toBeNull(); - - await session.destroy(); // this clears token and user for any other test + vi.spyOn(axios, 'post') + .mockResolvedValueOnce({ + data: { id: '' }, + }) + .mockResolvedValueOnce({ + data: { + id: '', + }, + }); + expect(sessionStorage.getItem('keepLogin')).toBeFalsy(); + expect(sessionStorage.getItem('created')).toBeDefined(); + expect(sessionStorage.getItem('ttl')).toEqual(1); + await session.checkValidity(); + expect(sessionStorage.getItem('token')).not.toEqual(expectedToken); + expect(sessionStorage.getItem('tokenMultimedia')).not.toEqual( + expectedTokenMultimedia + ); }); }); }); diff --git a/test/vitest/__tests__/composables/useTokenConfig.spec.js b/test/vitest/__tests__/composables/useTokenConfig.spec.js new file mode 100644 index 000000000..a25a4abb1 --- /dev/null +++ b/test/vitest/__tests__/composables/useTokenConfig.spec.js @@ -0,0 +1,31 @@ +import { vi, describe, expect, it } from 'vitest'; +import { axios, flushPromises } from 'app/test/vitest/helper'; +import { useTokenConfig } from 'composables/useTokenConfig'; +const tokenConfig = useTokenConfig(); + +describe('useTokenConfig', () => { + describe('fetch', () => { + it('should call setTokenConfig of the state with the expected data', async () => { + const data = { + id: 1, + renewPeriod: 21600, + courtesyTime: 60, + renewInterval: 300, + }; + vi.spyOn(axios, 'get').mockResolvedValueOnce({ + data, + }); + + vi.spyOn(tokenConfig.state, 'setTokenConfig'); + + tokenConfig.fetch(); + + await flushPromises(); + + expect(tokenConfig.state.setTokenConfig).toHaveBeenCalledWith(data); + + const renewPeriod = sessionStorage.getItem('renewPeriod'); + expect(renewPeriod).toEqual(data.renewPeriod); + }); + }); +}); From 6c69c44dfdbf93c4b1527599136f9e5a2a4ebf25 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Mon, 29 Apr 2024 08:33:38 +0000 Subject: [PATCH 7/7] remove comment --- src/composables/useSession.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/composables/useSession.js b/src/composables/useSession.js index 3b38cdad6..56bce0279 100644 --- a/src/composables/useSession.js +++ b/src/composables/useSession.js @@ -141,7 +141,6 @@ export function useSession() { const maxDate = created + renewPeriodInSeconds; const now = new Date().getTime(); - //COMMENT THIS IF JUST FOR TEST if (isNaN(renewPeriodInSeconds) || now <= maxDate) { return (isCheckingToken = false); }