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);
if (!canChat) {
return helpers.notAllowed(req, res, '[[error:no-privileges]]');
return helpers.notAllowed(req, res);
}
const payload = {

@ -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);
}
};

@ -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

@ -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 () => {

Loading…
Cancel
Save