Compare commits

...

3 Commits

Author SHA1 Message Date
Javier Segarra 37d9f5c902 Merge branch 'dev' into 6264_log_renewToken
gitea/salix/pipeline/pr-dev There was a failure building this commit Details
2024-06-20 13:17:38 +00:00
Javier Segarra b383c604cc test: add CALL util.debugAdd
gitea/salix/pipeline/pr-master This commit looks good Details
gitea/salix/pipeline/pr-dev There was a failure building this commit Details
2024-06-20 11:50:10 +00:00
Javier Segarra eec160497e feat: add CALL util.debugAdd
gitea/salix/pipeline/pr-master There was a failure building this commit Details
2024-06-20 11:49:25 +00:00
2 changed files with 57 additions and 26 deletions

View File

@ -16,8 +16,10 @@ module.exports = Self => {
accessScopes: ['DEFAULT', 'read:multimedia']});
Self.renewToken = async function(ctx) {
const {accessToken: token} = ctx.req;
let accessToken;
let token;
try {
token = ctx.req.accessToken;
const {courtesyTime} = await models.AccessTokenConfig.findOne({
fields: ['courtesyTime']
});
@ -30,9 +32,12 @@ module.exports = Self => {
try {
const exists = await models.AccessToken.findById(token.id);
exists && await Self.logout(token.id);
} catch (err) {
} catch (error) {
// eslint-disable-next-line no-console
console.error(err);
console.error(error);
const body = JSON.stringify({err: error, token});
await handleError(body);
throw new Error(error);
}
}, courtesyTime * 1000);
@ -44,8 +49,17 @@ module.exports = Self => {
createTokenOptions = {scopes: [scopes[0]]};
// Create new accessToken
const user = await Self.findById(token.userId);
const accessToken = await user.accessTokens.create(createTokenOptions);
accessToken = await user.accessTokens.create(createTokenOptions);
return {id: accessToken.id, ttl: accessToken.ttl};
} catch (error) {
const body = JSON.stringify({error: error.message, token, accessToken});
await handleError(body);
throw new Error(error);
}
};
};
async function handleError(body, tag = 'renewToken') {
await models.Application.rawSql('CALL util.debugAdd(?,?);', [tag, body]);
}

View File

@ -61,4 +61,21 @@ describe('Renew Token', () => {
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);
});
});