Node.js based forum software built for the modern web
test: fix tests, dont hang if payload is string
Barış Soner Uşaklı6b09b7c7d45b
diff --git a/src/controllers/accounts/chats.js b/src/controllers/accounts/chats.jsindex 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.jsindex 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.jsindex 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 onloaddiff --git a/test/messaging.js b/test/messaging.jsindex 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 () => {