refs #7190 feat: renewToken for multimedia

This commit is contained in:
Javier Segarra 2024-04-15 08:47:27 +02:00
parent 0e2e68488d
commit 253465cf13
2 changed files with 47 additions and 4 deletions

View File

@ -40,9 +40,15 @@ module.exports = Self => {
} }
}, courtesyTime * 1000); }, courtesyTime * 1000);
// Get scopes
let createTokenOptions = {};
const {scopes} = token;
if (scopes)
createTokenOptions = {scopes: [scopes[0]]};
// Create new accessToken // Create new accessToken
const user = await Self.findById(token.userId); 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}; return {id: accessToken.id, ttl: accessToken.ttl};
}; };

View File

@ -1,6 +1,9 @@
const {models} = require('vn-loopback/server/server'); const {models} = require('vn-loopback/server/server');
const TOKEN_MULTIMEDIA = 'read:multimedia';
describe('Share Token', () => { describe('Share Token', () => {
let ctx = null; let ctx = null;
const startingTime = Date.now();
let multimediaToken = null;
beforeAll(async() => { beforeAll(async() => {
const unAuthCtx = { const unAuthCtx = {
req: { req: {
@ -17,11 +20,45 @@ describe('Share Token', () => {
ctx = {req: {accessToken: accessToken}}; ctx = {req: {accessToken: accessToken}};
}); });
it('should renew token', async() => { beforeEach(async() => {
const multimediaToken = await models.VnUser.shareToken(ctx); 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(Object.keys(multimediaToken).length).toEqual(1);
expect(multimediaToken.multimediaToken.userId).toEqual(ctx.req.accessToken.userId); 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);
}); });
}); });