test: fix tests, dont hang if payload is string

closes #11933
isekai-main
Barış Soner Uşaklı 1 year ago
parent 82d95ba812
commit 6b09b7c7d4

@ -20,7 +20,7 @@ chatsController.get = async function (req, res, next) {
} }
const canChat = await privileges.global.can('chat', req.uid); const canChat = await privileges.global.can('chat', req.uid);
if (!canChat) { if (!canChat) {
return helpers.notAllowed(req, res, '[[error:no-privileges]]'); return helpers.notAllowed(req, res);
} }
const payload = { const payload = {

@ -475,8 +475,8 @@ helpers.formatApiResponse = async (statusCode, res, payload) => {
status: { code, message }, status: { code, message },
response: payload || {}, response: payload || {},
}); });
} else if (payload instanceof Error) { } else if (payload instanceof Error || typeof payload === 'string') {
const { message } = payload; const message = payload instanceof Error ? payload.message : payload;
const response = {}; const response = {};
// Update status code based on some common error codes // Update status code based on some common error codes
@ -512,9 +512,10 @@ helpers.formatApiResponse = async (statusCode, res, payload) => {
process.stdout.write(payload.stack); process.stdout.write(payload.stack);
} }
res.status(statusCode).json(returnPayload); res.status(statusCode).json(returnPayload);
} else if (!payload) { } else {
// Non-2xx statusCode, generate predefined error // 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); res.status(statusCode).json(returnPayload);
} }
}; };

@ -447,17 +447,17 @@ module.exports = function (Messaging) {
user.isAdministrator(uid), user.isAdministrator(uid),
user.isGlobalModerator(uid), user.isGlobalModerator(uid),
]); ]);
if (!room) {
if (!canChat) { return null;
throw new Error('[[error:no-privileges]]');
} }
if (!room ||
if (!canChat ||
(!room.public && !inRoom) || (!room.public && !inRoom) ||
(room.public && ( (room.public && (
Array.isArray(room.groups) && room.groups.length && !isAdmin && !(await groups.isMemberOfAny(uid, room.groups))) 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 // add user to public room onload

@ -697,7 +697,7 @@ describe('Messaging Library', () => {
assert.equal(response.statusCode, 404); 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; meta.config.disableChat = 0;
const response = await request(`${nconf.get('url')}/api/user/baz/chats`, { const response = await request(`${nconf.get('url')}/api/user/baz/chats`, {
resolveWithFullResponse: true, resolveWithFullResponse: true,
@ -706,8 +706,8 @@ describe('Messaging Library', () => {
}); });
const { body } = response; const { body } = response;
assert.equal(response.statusCode, 500); assert.equal(response.statusCode, 401);
assert.equal(body.error, '[[error:no-privileges]]'); assert.equal(body.status.code, 'not-authorised');
}); });
it('should 404 for non-existent user', async () => { it('should 404 for non-existent user', async () => {

Loading…
Cancel
Save