diff --git a/back/methods/vn-user/renew-token.js b/back/methods/vn-user/renew-token.js index d00085d8a..2fd1f43c0 100644 --- a/back/methods/vn-user/renew-token.js +++ b/back/methods/vn-user/renew-token.js @@ -33,16 +33,23 @@ module.exports = Self => { // Schedule to remove current token setTimeout(async() => { try { - await Self.logout(token.id); + const exists = await models.AccessToken.findById(token.id); + exists && await Self.logout(token.id); } catch (err) { // eslint-disable-next-line no-console console.error(err); } }, courtesyTime * 1000); + // Get scopes + + let createTokenOptions = {}; + const {scopes} = token; + if (scopes) + createTokenOptions = {scopes: [scopes[0]]}; // Create new accessToken const user = await Self.findById(token.userId); - const accessToken = await user.createAccessToken(); + const accessToken = await user.accessTokens.create(createTokenOptions); return {id: accessToken.id, ttl: accessToken.ttl}; }; diff --git a/back/methods/vn-user/specs/renew-token.spec.js b/back/methods/vn-user/specs/renew-token.spec.js index 8d9bbf11c..741388bf9 100644 --- a/back/methods/vn-user/specs/renew-token.spec.js +++ b/back/methods/vn-user/specs/renew-token.spec.js @@ -33,6 +33,17 @@ describe('Renew Token', () => { 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(70 * 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() => { diff --git a/back/methods/vn-user/specs/share-token.spec.js b/back/methods/vn-user/specs/share-token.spec.js index aaa83817c..e072a4fa8 100644 --- a/back/methods/vn-user/specs/share-token.spec.js +++ b/back/methods/vn-user/specs/share-token.spec.js @@ -1,6 +1,9 @@ const {models} = require('vn-loopback/server/server'); +const TOKEN_MULTIMEDIA = 'read:multimedia'; describe('Share Token', () => { let ctx = null; + const startingTime = Date.now(); + let multimediaToken = null; beforeAll(async() => { const unAuthCtx = { req: { @@ -17,11 +20,45 @@ describe('Share Token', () => { ctx = {req: {accessToken: accessToken}}; }); - it('should renew token', async() => { - const multimediaToken = await models.VnUser.shareToken(ctx); + beforeEach(async() => { + multimediaToken = await models.VnUser.shareToken(ctx); + jasmine.clock().install(); + jasmine.clock().mockDate(new Date(startingTime)); + }); + afterEach(() => { + jasmine.clock().uninstall(); + }); + + it('should generate token', async() => { expect(Object.keys(multimediaToken).length).toEqual(1); expect(multimediaToken.multimediaToken.userId).toEqual(ctx.req.accessToken.userId); - expect(multimediaToken.multimediaToken.scopes[0]).toEqual('read:multimedia'); + expect(multimediaToken.multimediaToken.scopes[0]).toEqual(TOKEN_MULTIMEDIA); + }); + + 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('should renew token', async() => { + const mockDate = new Date(startingTime + 26600000); + jasmine.clock().mockDate(mockDate); + + const newShareToken = await models.VnUser.renewToken({req: {accessToken: multimediaToken.multimediaToken}}); + const {id} = newShareToken; + + expect(id).not.toEqual(ctx.req.accessToken.id); + const newMultimediaToken = await models.AccessToken.findById(id); + + expect(newMultimediaToken.scopes[0]).toEqual(TOKEN_MULTIMEDIA); }); });