salix/back/methods/vn-user/specs/renew-token.spec.js

82 lines
2.3 KiB
JavaScript
Raw Normal View History

const {models} = require('vn-loopback/server/server');
2023-11-10 12:13:17 +00:00
describe('Renew Token', () => {
const startingTime = Date.now();
let ctx = null;
beforeAll(async() => {
const unAuthCtx = {
req: {
headers: {},
connection: {
remoteAddress: '127.0.0.1'
},
getLocale: () => 'en'
},
args: {}
};
let login = await models.VnUser.signIn(unAuthCtx, 'salesAssistant', 'nightmare');
let accessToken = await models.AccessToken.findById(login.token);
ctx = {req: {accessToken: accessToken}};
});
beforeEach(() => {
jasmine.clock().install();
jasmine.clock().mockDate(new Date(startingTime));
});
afterEach(() => {
jasmine.clock().uninstall();
});
2023-12-21 09:10:18 +00:00
it('should renew token', async() => {
2024-04-19 08:50:14 +00:00
const {courtesyTime} = await models.AccessTokenConfig.findOne({
fields: ['courtesyTime']
});
2023-12-21 09:10:18 +00:00
const mockDate = new Date(startingTime + 26600000);
jasmine.clock().mockDate(mockDate);
const {id} = await models.VnUser.renewToken(ctx);
2023-11-10 12:13:17 +00:00
expect(id).not.toEqual(ctx.req.accessToken.id);
2024-04-15 06:47:49 +00:00
await models.VnUser.logout(ctx.req.accessToken.id);
2024-04-19 08:50:14 +00:00
jasmine.clock().tick((courtesyTime + 10) * 1000);
2024-04-15 06:47:49 +00:00
let tokenNotExists;
try {
tokenNotExists = await models.AccessToken.findById(ctx.req.accessToken.id);
} catch (e) {
error = e;
}
expect(tokenNotExists).toBeNull();
2023-11-10 12:13:17 +00:00
});
it('NOT should renew', async() => {
2023-11-10 12:13:17 +00:00
let error;
let response;
2023-11-10 12:13:17 +00:00
try {
response = await models.VnUser.renewToken(ctx);
2023-11-10 12:13:17 +00:00
} catch (e) {
error = e;
}
expect(error).toBeUndefined();
expect(response.id).toEqual(ctx.req.accessToken.id);
2023-11-10 12:13:17 +00:00
});
2024-06-20 11:50:10 +00:00
it('throw error', async() => {
let error;
try {
await models.VnUser.renewToken({req: {token: null}});
} catch (e) {
error = e;
}
expect(error).toBeDefined();
const query = 'SELECT * FROM util.debug';
const debugLog = await models.Application.rawSql(query, null);
expect(debugLog.length).toEqual(1);
});
2023-11-10 12:13:17 +00:00
});