const {models} = require('vn-loopback/server/server'); 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(); }); it('should renew token', async() => { const {courtesyTime} = await models.AccessTokenConfig.findOne({ fields: ['courtesyTime'] }); const mockDate = new Date(startingTime + 26600000); jasmine.clock().mockDate(mockDate); const {id} = await models.VnUser.renewToken(ctx); expect(id).not.toEqual(ctx.req.accessToken.id); await models.VnUser.logout(ctx.req.accessToken.id); jasmine.clock().tick((courtesyTime + 10) * 1000); let tokenNotExists; try { tokenNotExists = await models.AccessToken.findById(ctx.req.accessToken.id); } catch (e) { error = e; } expect(tokenNotExists).toBeNull(); }); it('NOT should renew', async() => { let error; let response; try { response = await models.VnUser.renewToken(ctx); } catch (e) { error = e; } expect(error).toBeUndefined(); expect(response.id).toEqual(ctx.req.accessToken.id); }); 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); }); });