test: migrate socket modules tests to v3 api

This commit is contained in:
Julian Lam
2023-12-05 11:47:55 -05:00
parent 565ca3cc3b
commit 445b70deda
3 changed files with 88 additions and 78 deletions

View File

@@ -36,7 +36,11 @@ async function rateLimitExceeded(caller, field) {
return false; return false;
} }
chatsAPI.list = async (caller, { uid, start, stop, page, perPage }) => { chatsAPI.list = async (caller, { uid = caller.uid, start, stop, page, perPage } = {}) => {
if (!start && !stop && !page) {
throw new Error('[[error:invalid-data]]');
}
if (!start && !stop && page) { if (!start && !stop && page) {
winston.warn('[api/chats] Sending `page` and `perPage` to .list() is deprecated in favour of `start` and `stop`. The deprecated parameters will be removed in v4.'); winston.warn('[api/chats] Sending `page` and `perPage` to .list() is deprecated in favour of `start` and `stop`. The deprecated parameters will be removed in v4.');
start = Math.max(0, page - 1) * perPage; start = Math.max(0, page - 1) * perPage;
@@ -315,7 +319,11 @@ chatsAPI.toggleOwner = async (caller, { roomId, uid, state }) => {
return await messaging.toggleOwner(uid, roomId, state); return await messaging.toggleOwner(uid, roomId, state);
}; };
chatsAPI.listMessages = async (caller, { uid, roomId, start, direction = null }) => { chatsAPI.listMessages = async (caller, { uid = caller.uid, roomId, start = 0, direction = null } = {}) => {
if (!roomId) {
throw new Error('[[error:invalid-data]]');
}
const count = 50; const count = 50;
let stop = start + count - 1; let stop = start + count - 1;
if (direction === 1 || direction === -1) { if (direction === 1 || direction === -1) {
@@ -353,12 +361,20 @@ chatsAPI.getPinnedMessages = async (caller, { start, roomId }) => {
return { messages }; return { messages };
}; };
chatsAPI.getMessage = async (caller, { mid, roomId }) => { chatsAPI.getMessage = async (caller, { mid, roomId } = {}) => {
if (!mid || !roomId) {
throw new Error('[[error:invalid-data]]');
}
const messages = await messaging.getMessagesData([mid], caller.uid, roomId, false); const messages = await messaging.getMessagesData([mid], caller.uid, roomId, false);
return messages.pop(); return messages.pop();
}; };
chatsAPI.getRawMessage = async (caller, { mid, roomId }) => { chatsAPI.getRawMessage = async (caller, { mid, roomId } = {}) => {
if (!mid || !roomId) {
throw new Error('[[error:invalid-data]]');
}
const [isAdmin, canViewMessage, inRoom] = await Promise.all([ const [isAdmin, canViewMessage, inRoom] = await Promise.all([
user.isAdministrator(caller.uid), user.isAdministrator(caller.uid),
messaging.canViewMessage(mid, roomId, caller.uid), messaging.canViewMessage(mid, roomId, caller.uid),

View File

@@ -147,7 +147,11 @@ usersAPI.getStatus = async (caller, { uid }) => {
return { status }; return { status };
}; };
usersAPI.getPrivateRoomId = async (caller, { uid }) => { usersAPI.getPrivateRoomId = async (caller, { uid } = {}) => {
if (!uid) {
throw new Error('[[error:invalid-data]]');
}
let roomId = await messaging.hasPrivateChat(caller.uid, uid); let roomId = await messaging.hasPrivateChat(caller.uid, uid);
roomId = parseInt(roomId, 10); roomId = parseInt(roomId, 10);

View File

@@ -374,8 +374,9 @@ describe('Messaging Library', () => {
assert.equal(messageData.content, 'first chat message'); assert.equal(messageData.content, 'first chat message');
assert(messageData.fromUser); assert(messageData.fromUser);
assert(messageData.roomId, roomId); assert(messageData.roomId, roomId);
const raw = const { content: raw } = await api.chats.getRawMessage(
await util.promisify(socketModules.chats.getRaw)({ uid: mocks.users.foo.uid }, { mid: messageData.messageId }); { uid: mocks.users.foo.uid }, { mid: messageData.messageId, roomId }
);
assert.equal(raw, 'first chat message'); assert.equal(raw, 'first chat message');
}); });
@@ -390,33 +391,38 @@ describe('Messaging Library', () => {
meta.config.chatMessageDelay = oldValue; meta.config.chatMessageDelay = oldValue;
}); });
it('should return invalid-data error', (done) => { it('should return invalid-data error', async () => {
socketModules.chats.getRaw({ uid: mocks.users.foo.uid }, null, (err) => { await assert.rejects(
assert.equal(err.message, '[[error:invalid-data]]'); api.chats.getRawMessage({ uid: mocks.users.foo.uid }, undefined),
socketModules.chats.getRaw({ uid: mocks.users.foo.uid }, {}, (err) => { { message: '[[error:invalid-data]]' }
assert.equal(err.message, '[[error:invalid-data]]'); );
done();
});
}); await assert.rejects(
api.chats.getRawMessage({ uid: mocks.users.foo.uid }, {}),
{ message: '[[error:invalid-data]]' }
);
}); });
it('should return not allowed error if mid is not in room', async () => { it('should return not allowed error if user is not in room', async () => {
const uids = await User.create({ username: 'dummy' }); const uids = await User.create({ username: 'dummy' });
let { body } = await callv3API('post', '/chats', { uids: [uids] }, 'baz'); let { body } = await callv3API('post', '/chats', { uids: [uids] }, 'baz');
const myRoomId = body.response.roomId; const myRoomId = body.response.roomId;
assert(myRoomId); assert(myRoomId);
try { try {
await socketModules.chats.getRaw({ uid: mocks.users.baz.uid }, { mid: 200 }); await api.chats.getRawMessage({ uid: mocks.users.baz.uid }, { mid: 200 });
} catch (err) { } catch (err) {
assert(err); assert(err);
assert.equal(err.message, '[[error:not-allowed]]'); assert.equal(err.message, '[[error:invalid-data]]');
} }
({ body } = await callv3API('post', `/chats/${myRoomId}`, { roomId: myRoomId, message: 'admin will see this' }, 'baz')); ({ body } = await callv3API('post', `/chats/${myRoomId}`, { roomId: myRoomId, message: 'admin will see this' }, 'baz'));
const message = body.response; const message = body.response;
const raw = await socketModules.chats.getRaw({ uid: mocks.users.foo.uid }, { mid: message.messageId }); const { content } = await api.chats.getRawMessage(
assert.equal(raw, 'admin will see this'); { uid: mocks.users.foo.uid }, { mid: message.messageId, roomId: myRoomId }
);
assert.equal(content, 'admin will see this');
}); });
@@ -476,13 +482,6 @@ describe('Messaging Library', () => {
await api.chats.mark({ uid: mocks.users.foo.uid }, { state: 0, roomId: roomId }); await api.chats.mark({ uid: mocks.users.foo.uid }, { state: 0, roomId: roomId });
}); });
it('should mark all rooms read', (done) => {
socketModules.chats.markAllRead({ uid: mocks.users.foo.uid }, {}, (err) => {
assert.ifError(err);
done();
});
});
it('should fail to rename room with invalid data', async () => { it('should fail to rename room with invalid data', async () => {
const { body } = await callv3API('put', `/chats/${roomId}`, { name: null }, 'foo'); const { body } = await callv3API('put', `/chats/${roomId}`, { name: null }, 'foo');
assert.strictEqual(body.status.message, await translator.translate('[[error:invalid-data]]')); assert.strictEqual(body.status.message, await translator.translate('[[error:invalid-data]]'));
@@ -517,68 +516,59 @@ describe('Messaging Library', () => {
assert.strictEqual(body.response.roomName, 'new room name'); assert.strictEqual(body.response.roomName, 'new room name');
}); });
it('should return true if user is dnd', (done) => { it('should return true if user is dnd', async () => {
db.setObjectField(`user:${mocks.users.herp.uid}`, 'status', 'dnd', (err) => { await db.setObjectField(`user:${mocks.users.herp.uid}`, 'status', 'dnd');
assert.ifError(err); const { status } = await api.users.getStatus({ uid: mocks.users.foo.uid }, { uid: mocks.users.herp.uid });
socketModules.chats.isDnD({ uid: mocks.users.foo.uid }, mocks.users.herp.uid, (err, isDnD) => { assert.strictEqual(status, 'dnd');
assert.ifError(err);
assert(isDnD);
done();
});
});
}); });
it('should fail to load recent chats with invalid data', (done) => { it('should fail to load recent chats with invalid data', async () => {
socketModules.chats.getRecentChats({ uid: mocks.users.foo.uid }, null, (err) => { await assert.rejects(
assert.equal(err.message, '[[error:invalid-data]]'); api.chats.list({ uid: mocks.users.foo.uid }, undefined),
socketModules.chats.getRecentChats({ uid: mocks.users.foo.uid }, { after: null }, (err) => { { message: '[[error:invalid-data]]' }
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.getRecentChats({ uid: mocks.users.foo.uid }, { after: 0, uid: null }, (err) => {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
});
it('should load recent chats of user', (done) => {
socketModules.chats.getRecentChats(
{ uid: mocks.users.foo.uid },
{ after: 0, uid: mocks.users.foo.uid },
(err, data) => {
assert.ifError(err);
assert(Array.isArray(data.rooms));
done();
}
); );
await assert.rejects(
api.chats.list({ uid: mocks.users.foo.uid }, { start: null }),
{ message: '[[error:invalid-data]]' }
);
await assert.rejects(
api.chats.list({ uid: mocks.users.foo.uid }, { start: 0, uid: null }),
{ message: '[[error:invalid-data]]' }
);
});
it('should load recent chats of user', async () => {
const { rooms } = await api.chats.list(
{ uid: mocks.users.foo.uid }, { start: 0, stop: 9, uid: mocks.users.foo.uid }
);
assert(Array.isArray(rooms));
}); });
it('should escape teaser', async () => { it('should escape teaser', async () => {
await callv3API('post', `/chats/${roomId}`, { roomId: roomId, message: '<svg/onload=alert(document.location);' }, 'foo'); await callv3API('post', `/chats/${roomId}`, { roomId: roomId, message: '<svg/onload=alert(document.location);' }, 'foo');
const data = await util.promisify(socketModules.chats.getRecentChats)( const { rooms } = await api.chats.list(
{ uid: mocks.users.foo.uid }, { uid: mocks.users.foo.uid }, { start: 0, stop: 9, uid: mocks.users.foo.uid }
{ after: 0, uid: mocks.users.foo.uid } );
assert.equal(rooms[0].teaser.content, '&lt;svg&#x2F;onload=alert(document.location);');
});
it('should fail to check if user has private chat with invalid data', async () => {
await assert.rejects(
api.users.getPrivateRoomId({ uid: null }, undefined),
{ message: '[[error:invalid-data]]' }
); );
assert.equal(data.rooms[0].teaser.content, '&lt;svg&#x2F;onload=alert(document.location);'); await assert.rejects(
api.users.getPrivateRoomId({ uid: mocks.users.foo.uid }, undefined),
{ message: '[[error:invalid-data]]' }
);
}); });
it('should fail to check if user has private chat with invalid data', (done) => { it('should check if user has private chat with another uid', async () => {
socketModules.chats.hasPrivateChat({ uid: null }, null, (err) => { const { roomId } = await api.users.getPrivateRoomId({ uid: mocks.users.foo.uid }, { uid: mocks.users.herp.uid });
assert.equal(err.message, '[[error:invalid-data]]'); assert(roomId);
socketModules.chats.hasPrivateChat({ uid: mocks.users.foo.uid }, null, (err) => {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
it('should check if user has private chat with another uid', (done) => {
socketModules.chats.hasPrivateChat({ uid: mocks.users.foo.uid }, mocks.users.herp.uid, (err, roomId) => {
assert.ifError(err);
assert(roomId);
done();
});
}); });
}); });