From 6b09b7c7d45b74ef9cabd6cab6241bc5bbf2785c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Sat, 19 Aug 2023 16:40:43 -0400 Subject: [PATCH] test: fix tests, dont hang if payload is string closes #11933 --- src/controllers/accounts/chats.js | 2 +- src/controllers/helpers.js | 9 +++++---- src/messaging/rooms.js | 10 +++++----- test/messaging.js | 6 +++--- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/controllers/accounts/chats.js b/src/controllers/accounts/chats.js index d8555967df..5bee1f65a6 100644 --- a/src/controllers/accounts/chats.js +++ b/src/controllers/accounts/chats.js @@ -20,7 +20,7 @@ chatsController.get = async function (req, res, next) { } const canChat = await privileges.global.can('chat', req.uid); if (!canChat) { - return helpers.notAllowed(req, res, '[[error:no-privileges]]'); + return helpers.notAllowed(req, res); } const payload = { diff --git a/src/controllers/helpers.js b/src/controllers/helpers.js index 0f6b5b4a34..fb228cbfb7 100644 --- a/src/controllers/helpers.js +++ b/src/controllers/helpers.js @@ -475,8 +475,8 @@ helpers.formatApiResponse = async (statusCode, res, payload) => { status: { code, message }, response: payload || {}, }); - } else if (payload instanceof Error) { - const { message } = payload; + } else if (payload instanceof Error || typeof payload === 'string') { + const message = payload instanceof Error ? payload.message : payload; const response = {}; // Update status code based on some common error codes @@ -512,9 +512,10 @@ helpers.formatApiResponse = async (statusCode, res, payload) => { process.stdout.write(payload.stack); } res.status(statusCode).json(returnPayload); - } else if (!payload) { + } else { // Non-2xx statusCode, generate predefined error - const returnPayload = await helpers.generateError(statusCode, null, res); + const message = payload ? String(payload) : null; + const returnPayload = await helpers.generateError(statusCode, message, res); res.status(statusCode).json(returnPayload); } }; diff --git a/src/messaging/rooms.js b/src/messaging/rooms.js index 202b8238df..03586ef6da 100644 --- a/src/messaging/rooms.js +++ b/src/messaging/rooms.js @@ -447,17 +447,17 @@ module.exports = function (Messaging) { user.isAdministrator(uid), user.isGlobalModerator(uid), ]); - - if (!canChat) { - throw new Error('[[error:no-privileges]]'); + if (!room) { + return null; } - if (!room || + + if (!canChat || (!room.public && !inRoom) || (room.public && ( Array.isArray(room.groups) && room.groups.length && !isAdmin && !(await groups.isMemberOfAny(uid, room.groups))) ) ) { - return null; + throw new Error('[[error:no-privileges]]'); } // add user to public room onload diff --git a/test/messaging.js b/test/messaging.js index 1501f28e17..2e5af6b60c 100644 --- a/test/messaging.js +++ b/test/messaging.js @@ -697,7 +697,7 @@ describe('Messaging Library', () => { assert.equal(response.statusCode, 404); }); - it('should 500 for guest with no privilege error', async () => { + it('should 401 for guest with not-authorised status code', async () => { meta.config.disableChat = 0; const response = await request(`${nconf.get('url')}/api/user/baz/chats`, { resolveWithFullResponse: true, @@ -706,8 +706,8 @@ describe('Messaging Library', () => { }); const { body } = response; - assert.equal(response.statusCode, 500); - assert.equal(body.error, '[[error:no-privileges]]'); + assert.equal(response.statusCode, 401); + assert.equal(body.status.code, 'not-authorised'); }); it('should 404 for non-existent user', async () => {