2023-11-27 08:48:15 +00:00
|
|
|
const {models} = require('vn-loopback/server/server');
|
2023-11-10 12:13:17 +00:00
|
|
|
describe('Renew Token', () => {
|
2023-11-27 08:48:15 +00:00
|
|
|
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() => {
|
|
|
|
const mockDate = new Date(startingTime + 26600000);
|
|
|
|
jasmine.clock().mockDate(mockDate);
|
2023-11-27 08:48:15 +00:00
|
|
|
const {id} = await models.VnUser.renewToken(ctx);
|
2023-11-10 12:13:17 +00:00
|
|
|
|
2023-11-27 08:48:15 +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);
|
|
|
|
jasmine.clock().tick(70 * 1000);
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2023-11-27 08:48:15 +00:00
|
|
|
it('NOT should renew', async() => {
|
2023-11-10 12:13:17 +00:00
|
|
|
let error;
|
2023-12-14 11:25:40 +00:00
|
|
|
let response;
|
2023-11-10 12:13:17 +00:00
|
|
|
try {
|
2023-12-14 11:25:40 +00:00
|
|
|
response = await models.VnUser.renewToken(ctx);
|
2023-11-10 12:13:17 +00:00
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
2023-12-14 11:25:40 +00:00
|
|
|
|
|
|
|
expect(error).toBeUndefined();
|
|
|
|
expect(response.id).toEqual(ctx.req.accessToken.id);
|
2023-11-10 12:13:17 +00:00
|
|
|
});
|
|
|
|
});
|