refs #6264 test: validate-token and renew-token
This commit is contained in:
parent
72a0932e35
commit
81be3b18f7
|
@ -2,8 +2,9 @@ const tokenConfig = require('./token-config');
|
|||
|
||||
module.exports = async token => {
|
||||
const accessTokenConfig = await tokenConfig();
|
||||
|
||||
const now = Date.vnNew();
|
||||
let now = Date.now();
|
||||
if (Date?.vnNow !== undefined)
|
||||
now = Date.vnNow();
|
||||
const differenceMilliseconds = now - token.created;
|
||||
const differenceSeconds = Math.floor(differenceMilliseconds / 1000);
|
||||
return differenceSeconds > accessTokenConfig.renewPeriod - accessTokenConfig.courtesyTime;
|
||||
|
|
|
@ -1,22 +1,49 @@
|
|||
const {models} = require('vn-loopback/server/server');
|
||||
describe('Renew Token', () => {
|
||||
it('Token is valid', async() => {
|
||||
let login = await VnUser.signIn(unauthCtx, 'salesAssistant', 'nightmare');
|
||||
let accessToken = await AccessToken.findById(login.token);
|
||||
let ctx = {req: {accessToken: accessToken}};
|
||||
|
||||
expect(login.token).toBeDefined();
|
||||
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}};
|
||||
});
|
||||
|
||||
it('Token is is invalid', async() => {
|
||||
beforeEach(() => {
|
||||
jasmine.clock().install();
|
||||
jasmine.clock().mockDate(new Date(startingTime));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jasmine.clock().uninstall();
|
||||
});
|
||||
|
||||
it('should renew process', async() => {
|
||||
jasmine.clock().mockDate(new Date(startingTime + 21600000));
|
||||
const {id} = await models.VnUser.renewToken(ctx);
|
||||
|
||||
expect(id).not.toEqual(ctx.req.accessToken.id);
|
||||
});
|
||||
|
||||
it('NOT should renew', async() => {
|
||||
let error;
|
||||
try {
|
||||
await models.VnUser.validateCode('developer', '123456');
|
||||
await models.VnUser.renewToken(ctx);
|
||||
} catch (e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
expect(error).toBeDefined();
|
||||
expect(error.statusCode).toBe(400);
|
||||
expect(error.message).toEqual('Invalid or expired verification code');
|
||||
expect(error.message).toEqual('The renew period has not been exceeded');
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,9 +1,43 @@
|
|||
describe('Validate Token', () => {
|
||||
it('Token is not expired', async() => {
|
||||
const {models} = require('vn-loopback/server/server');
|
||||
|
||||
describe('Validate 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();
|
||||
});
|
||||
|
||||
it('Token is not expired', async() => {
|
||||
jasmine.clock().mockDate(new Date(startingTime + 21600000));
|
||||
const isValid = await models.VnUser.validateToken(ctx.req.accessToken);
|
||||
|
||||
expect(isValid).toBeTrue();
|
||||
});
|
||||
|
||||
it('Token is expired', async() => {
|
||||
const isValid = await models.VnUser.validateToken(ctx.req.accessToken);
|
||||
|
||||
expect(isValid).toBeFalse();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
module.exports = () => {
|
||||
Date.vnUTC = () => {
|
||||
const env = process.env.NODE_ENV;
|
||||
Date.vnUTC = (env = process.env.NODE_ENV) => {
|
||||
if (!env || env === 'development')
|
||||
return new Date(Date.UTC(2001, 0, 1, 11));
|
||||
|
||||
|
|
Loading…
Reference in New Issue