forked from verdnatura/salix-front
fix: #7190 test
This commit is contained in:
parent
bf08ca7655
commit
a555d11663
|
@ -157,5 +157,8 @@ export function useSession() {
|
||||||
destroy,
|
destroy,
|
||||||
login,
|
login,
|
||||||
isLoggedIn,
|
isLoggedIn,
|
||||||
|
checkValidity,
|
||||||
|
setSession,
|
||||||
|
renewToken,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,5 +23,6 @@ export function useTokenConfig() {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
fetch,
|
fetch,
|
||||||
|
state,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { vi, describe, expect, it } from 'vitest';
|
import { vi, describe, expect, it, beforeAll, beforeEach } from 'vitest';
|
||||||
import { axios } from 'app/test/vitest/helper';
|
import { axios, flushPromises } from 'app/test/vitest/helper';
|
||||||
import { useSession } from 'composables/useSession';
|
import { useSession } from 'composables/useSession';
|
||||||
import { useState } from 'composables/useState';
|
import { useState } from 'composables/useState';
|
||||||
|
|
||||||
|
@ -63,81 +63,148 @@ describe('session', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('login', () => {
|
describe(
|
||||||
const expectedUser = {
|
'login',
|
||||||
id: 999,
|
() => {
|
||||||
name: `T'Challa`,
|
const expectedUser = {
|
||||||
nickname: 'Black Panther',
|
id: 999,
|
||||||
lang: 'en',
|
name: `T'Challa`,
|
||||||
userConfig: {
|
nickname: 'Black Panther',
|
||||||
darkMode: false,
|
lang: 'en',
|
||||||
},
|
userConfig: {
|
||||||
};
|
darkMode: false,
|
||||||
const rolesData = [
|
|
||||||
{
|
|
||||||
role: {
|
|
||||||
name: 'salesPerson',
|
|
||||||
},
|
},
|
||||||
},
|
};
|
||||||
{
|
const rolesData = [
|
||||||
role: {
|
{
|
||||||
name: 'admin',
|
role: {
|
||||||
|
name: 'salesPerson',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
{
|
||||||
];
|
role: {
|
||||||
|
name: 'admin',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
it('should fetch the user roles and then set token in the sessionStorage', async () => {
|
it('should fetch the user roles and then set token in the sessionStorage', async () => {
|
||||||
const expectedRoles = ['salesPerson', 'admin'];
|
const expectedRoles = ['salesPerson', 'admin'];
|
||||||
vi.spyOn(axios, 'get').mockResolvedValue({
|
vi.spyOn(axios, 'get').mockResolvedValue({
|
||||||
data: { roles: rolesData, user: expectedUser },
|
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';
|
it('should fetch the user roles and then set token in the localStorage', async () => {
|
||||||
const expectedTokenMultimedia = 'mySessionTokenMultimedia';
|
const expectedRoles = ['salesPerson', 'admin'];
|
||||||
const keepLogin = false;
|
vi.spyOn(axios, 'get').mockResolvedValue({
|
||||||
|
data: { roles: rolesData, user: expectedUser },
|
||||||
|
});
|
||||||
|
|
||||||
await session.login({
|
const expectedToken = 'myLocalToken';
|
||||||
token: expectedToken,
|
const expectedTokenMultimedia = 'myLocalTokenMultimedia';
|
||||||
tokenMultimedia: expectedTokenMultimedia,
|
const keepLogin = true;
|
||||||
keepLogin,
|
|
||||||
|
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();
|
describe('RenewToken', () => {
|
||||||
const localToken = localStorage.getItem('token');
|
const expectedToken = 'myToken';
|
||||||
const sessionToken = sessionStorage.getItem('token');
|
const expectedTokenMultimedia = 'myTokenMultimedia';
|
||||||
|
const currentDate = new Date();
|
||||||
expect(roles.value).toEqual(expectedRoles);
|
beforeAll(() => {
|
||||||
expect(localToken).toBeNull();
|
const tokenConfig = {
|
||||||
expect(sessionToken).toEqual(expectedToken);
|
id: 1,
|
||||||
|
renewPeriod: 21600,
|
||||||
await session.destroy(); // this clears token and user for any other test
|
courtesyTime: 60,
|
||||||
|
renewInterval: 300,
|
||||||
|
};
|
||||||
|
state.setTokenConfig(tokenConfig);
|
||||||
|
sessionStorage.setItem('renewPeriod', 1);
|
||||||
});
|
});
|
||||||
|
it('NOT Should renewToken', async () => {
|
||||||
it('should fetch the user roles and then set token in the localStorage', async () => {
|
const data = {
|
||||||
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({
|
|
||||||
token: expectedToken,
|
token: expectedToken,
|
||||||
tokenMultimedia: expectedTokenMultimedia,
|
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();
|
vi.spyOn(axios, 'post')
|
||||||
const localToken = localStorage.getItem('token');
|
.mockResolvedValueOnce({
|
||||||
const sessionToken = sessionStorage.getItem('token');
|
data: { id: '' },
|
||||||
|
})
|
||||||
expect(roles.value).toEqual(expectedRoles);
|
.mockResolvedValueOnce({
|
||||||
expect(localToken).toEqual(expectedToken);
|
data: {
|
||||||
expect(sessionToken).toBeNull();
|
id: '',
|
||||||
|
},
|
||||||
await session.destroy(); // this clears token and user for any other test
|
});
|
||||||
|
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
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue