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 => {
|
module.exports = async token => {
|
||||||
const accessTokenConfig = await tokenConfig();
|
const accessTokenConfig = await tokenConfig();
|
||||||
|
let now = Date.now();
|
||||||
const now = Date.vnNew();
|
if (Date?.vnNow !== undefined)
|
||||||
|
now = Date.vnNow();
|
||||||
const differenceMilliseconds = now - token.created;
|
const differenceMilliseconds = now - token.created;
|
||||||
const differenceSeconds = Math.floor(differenceMilliseconds / 1000);
|
const differenceSeconds = Math.floor(differenceMilliseconds / 1000);
|
||||||
return differenceSeconds > accessTokenConfig.renewPeriod - accessTokenConfig.courtesyTime;
|
return differenceSeconds > accessTokenConfig.renewPeriod - accessTokenConfig.courtesyTime;
|
||||||
|
|
|
@ -1,22 +1,49 @@
|
||||||
|
const {models} = require('vn-loopback/server/server');
|
||||||
describe('Renew Token', () => {
|
describe('Renew Token', () => {
|
||||||
it('Token is valid', async() => {
|
const startingTime = Date.now();
|
||||||
let login = await VnUser.signIn(unauthCtx, 'salesAssistant', 'nightmare');
|
let ctx = null;
|
||||||
let accessToken = await AccessToken.findById(login.token);
|
beforeAll(async() => {
|
||||||
let ctx = {req: {accessToken: accessToken}};
|
const unAuthCtx = {
|
||||||
|
req: {
|
||||||
expect(login.token).toBeDefined();
|
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;
|
let error;
|
||||||
try {
|
try {
|
||||||
await models.VnUser.validateCode('developer', '123456');
|
await models.VnUser.renewToken(ctx);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error = e;
|
error = e;
|
||||||
}
|
}
|
||||||
|
|
||||||
expect(error).toBeDefined();
|
expect(error).toBeDefined();
|
||||||
expect(error.statusCode).toBe(400);
|
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', () => {
|
const {models} = require('vn-loopback/server/server');
|
||||||
it('Token is not expired', async() => {
|
|
||||||
|
|
||||||
|
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() => {
|
it('Token is expired', async() => {
|
||||||
|
const isValid = await models.VnUser.validateToken(ctx.req.accessToken);
|
||||||
|
|
||||||
|
expect(isValid).toBeFalse();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
module.exports = () => {
|
module.exports = () => {
|
||||||
Date.vnUTC = () => {
|
Date.vnUTC = (env = process.env.NODE_ENV) => {
|
||||||
const env = process.env.NODE_ENV;
|
|
||||||
if (!env || env === 'development')
|
if (!env || env === 'development')
|
||||||
return new Date(Date.UTC(2001, 0, 1, 11));
|
return new Date(Date.UTC(2001, 0, 1, 11));
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue