From af88653152de51ac674f62835ec0bf2d1c0ae3ea Mon Sep 17 00:00:00 2001 From: Thayanne Luiza Date: Tue, 20 Aug 2019 16:34:27 -0300 Subject: [PATCH] [NEW] REST API Endpoint to get pinned messages from a room (#13864) --- app/api/server/v1/chat.js | 31 + tests/data/chat.helper.js | 12 + tests/end-to-end/api/05-chat.js | 1184 +++++++++++++++++-------------- 3 files changed, 678 insertions(+), 549 deletions(-) diff --git a/app/api/server/v1/chat.js b/app/api/server/v1/chat.js index 733b9614da5..50b7eeb9d9e 100644 --- a/app/api/server/v1/chat.js +++ b/app/api/server/v1/chat.js @@ -392,6 +392,37 @@ API.v1.addRoute('chat.getDeletedMessages', { authRequired: true }, { }, }); +API.v1.addRoute('chat.getPinnedMessages', { authRequired: true }, { + get() { + const { roomId } = this.queryParams; + const { offset, count } = this.getPaginationItems(); + + if (!roomId) { + throw new Meteor.Error('error-roomId-param-not-provided', 'The required "roomId" query param is missing.'); + } + const room = Meteor.call('canAccessRoom', roomId, this.userId); + if (!room) { + throw new Meteor.Error('error-not-allowed', 'Not allowed'); + } + + const cursor = Messages.findPinnedByRoom(room._id, { + skip: offset, + limit: count, + }); + + const total = cursor.count(); + + const messages = cursor.fetch(); + + return API.v1.success({ + messages, + count: messages.length, + offset, + total, + }); + }, +}); + API.v1.addRoute('chat.getThreadsList', { authRequired: true }, { get() { const { rid } = this.queryParams; diff --git a/tests/data/chat.helper.js b/tests/data/chat.helper.js index 06c9a5ae55c..171c8d64051 100644 --- a/tests/data/chat.helper.js +++ b/tests/data/chat.helper.js @@ -17,6 +17,18 @@ export const sendSimpleMessage = ({ roomId, text = 'test message', tmid }) => { .send({ message }); }; +export const pinMessage = ({ msgId }) => { + if (!msgId) { + throw new Error('"msgId" is required in "pinMessage" test helper'); + } + + return request.post(api('chat.pinMessage')) + .set(credentials) + .send({ + messageId: msgId, + }); +}; + export const deleteMessage = ({ roomId, msgId }) => { if (!roomId) { throw new Error('"roomId" is required in "deleteMessage" test helper'); diff --git a/tests/end-to-end/api/05-chat.js b/tests/end-to-end/api/05-chat.js index 8dfd3d035d4..c626cae609e 100644 --- a/tests/end-to-end/api/05-chat.js +++ b/tests/end-to-end/api/05-chat.js @@ -7,7 +7,7 @@ import { } from '../../data/api-data.js'; import { password } from '../../data/user'; import { createRoom } from '../../data/rooms.helper.js'; -import { sendSimpleMessage, deleteMessage } from '../../data/chat.helper.js'; +import { sendSimpleMessage, deleteMessage, pinMessage } from '../../data/chat.helper.js'; import { updatePermission, updateSetting } from '../../data/permissions.helper'; import { createUser, login } from '../../data/users.helper'; @@ -1323,205 +1323,150 @@ describe('[Chat]', function() { }); }); - describe('Threads', () => { - after((done) => { - updateSetting('API_Upper_Count_Limit', 100) - .then(() => updatePermission('view-c-room', ['admin', 'user', 'bot'])) - .then(done); - }); - - describe('[/chat.getThreadsList]', () => { - let testChannel; - let threadMessage; - before((done) => { - createRoom({ type: 'c', name: `channel.test.threads.${ Date.now() }` }) - .end((err, channel) => { - testChannel = channel.body.channel; - sendSimpleMessage({ - roomId: testChannel._id, - text: 'Message to create thread', - }).end((err, message) => { - sendSimpleMessage({ - roomId: testChannel._id, - text: 'Thread Message', - tmid: message.body.message._id, - }).end((err, res) => { - threadMessage = res.body.message; - done(); - }); - }); + describe('[/chat.getPinnedMessages]', () => { + let roomId; + before((done) => { + createRoom({ + type: 'c', + name: `channel.test.${ Date.now() }`, + }).end((err, res) => { + roomId = res.body.channel._id; + sendSimpleMessage({ roomId }) + .end((err, res) => { + const msgId = res.body.message._id; + pinMessage({ msgId }).end(done); }); }); + }); - it('should return an error for chat.getThreadsList when threads are not allowed in this server', (done) => { - updateSetting('Threads_enabled', false).then(() => { - request.get(api('chat.getThreadsList')) - .set(credentials) - .query({ - rid: testChannel._id, - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - expect(res.body).to.have.property('errorType', 'error-not-allowed'); - expect(res.body).to.have.property('error', 'Threads Disabled [error-not-allowed]'); - }) - .end(done); - }); + describe('when execute successfully', () => { + it('should return a list of pinned messages', (done) => { + request.get(api('chat.getPinnedMessages')) + .set(credentials) + .query({ + roomId, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('messages').and.to.be.an('array'); + expect(res.body.messages.length).to.be.equal(1); + }) + .end(done); }); - - it('should return an error when the user is not allowed access the room', (done) => { - createUser().then((createdUser) => { - login(createdUser.username, password).then((userCredentials) => { - updateSetting('Threads_enabled', true).then(() => { - updatePermission('view-c-room', []).then(() => { - request.get(api('chat.getThreadsList')) - .set(userCredentials) - .query({ - rid: testChannel._id, - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - expect(res.body).to.have.property('errorType', 'error-not-allowed'); - expect(res.body).to.have.property('error', 'Not Allowed [error-not-allowed]'); - }) - .end(done); - }); - }); - }); - }); + it('should return a list of pinned messages when the user sets count query parameter', (done) => { + request.get(api('chat.getPinnedMessages')) + .set(credentials) + .query({ + roomId, + count: 1, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('messages').and.to.be.an('array'); + expect(res.body.messages.length).to.be.equal(1); + }) + .end(done); }); + it('should return a list of pinned messages when the user sets count and offset query parameters', (done) => { + request.get(api('chat.getPinnedMessages')) + .set(credentials) + .query({ + roomId, + count: 1, + offset: 0, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('messages').and.to.be.an('array'); + expect(res.body.messages.length).to.be.equal(1); + }) + .end(done); + }); + }); - it('should return the room\'s thread list', (done) => { - updatePermission('view-c-room', ['admin', 'user']).then(() => { - request.get(api('chat.getThreadsList')) - .set(credentials) - .query({ - rid: testChannel._id, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.property('threads').and.to.be.an('array'); - expect(res.body).to.have.property('total'); - expect(res.body).to.have.property('offset'); - expect(res.body).to.have.property('count'); - expect(res.body.threads).to.have.lengthOf(1); - expect(res.body.threads[0]._id).to.be.equal(threadMessage.tmid); - }) - .end(done); - }); + describe('when an error occurs', () => { + it('should return statusCode 400 and an error when "roomId" is not provided', (done) => { + request.get(api('chat.getPinnedMessages')) + .set(credentials) + .query({ + count: 1, + offset: 0, + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body.errorType).to.be.equal('error-roomId-param-not-provided'); + }) + .end(done); }); }); + }); +}); - describe('[/chat.syncThreadsList]', () => { - let testChannel; - let threadMessage; - before((done) => { - createRoom({ type: 'c', name: `.threads.sync.${ Date.now() }` }) - .end((err, channel) => { - testChannel = channel.body.channel; +describe('Threads', () => { + after((done) => { + updateSetting('API_Upper_Count_Limit', 100) + .then(() => updatePermission('view-c-room', ['admin', 'user', 'bot'])) + .then(done); + }); + + describe('[/chat.getThreadsList]', () => { + let testChannel; + let threadMessage; + before((done) => { + createRoom({ type: 'c', name: `channel.test.threads.${ Date.now() }` }) + .end((err, channel) => { + testChannel = channel.body.channel; + sendSimpleMessage({ + roomId: testChannel._id, + text: 'Message to create thread', + }).end((err, message) => { sendSimpleMessage({ roomId: testChannel._id, - text: 'Message to create thread', - }).end((err, message) => { - sendSimpleMessage({ - roomId: testChannel._id, - text: 'Thread Message', - tmid: message.body.message._id, - }).end((err, res) => { - threadMessage = res.body.message; - done(); - }); + text: 'Thread Message', + tmid: message.body.message._id, + }).end((err, res) => { + threadMessage = res.body.message; + done(); }); }); - }); - - it('should return an error for chat.getThreadsList when threads are not allowed in this server', (done) => { - updateSetting('Threads_enabled', false).then(() => { - request.get(api('chat.getThreadsList')) - .set(credentials) - .query({ - rid: testChannel._id, - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - expect(res.body).to.have.property('errorType', 'error-not-allowed'); - expect(res.body).to.have.property('error', 'Threads Disabled [error-not-allowed]'); - }) - .end(done); }); - }); - - it('should return an error when the required param "rid" is missing', (done) => { - updateSetting('Threads_enabled', true).then(() => { - request.get(api('chat.syncThreadsList')) - .set(credentials) - .query({ - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - expect(res.body).to.have.property('errorType', 'error-room-id-param-not-provided'); - expect(res.body).to.have.property('error', 'The required \"rid\" query param is missing. [error-room-id-param-not-provided]'); - }) - .end(done); - }); - }); - - it('should return an error when the required param "updatedSince" is missing', (done) => { - updateSetting('Threads_enabled', true).then(() => { - request.get(api('chat.syncThreadsList')) - .set(credentials) - .query({ - rid: testChannel._id, - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - expect(res.body).to.have.property('errorType', 'error-updatedSince-param-invalid'); - expect(res.body).to.have.property('error', 'The required param \"updatedSince\" is missing. [error-updatedSince-param-invalid]'); - }) - .end(done); - }); - }); + }); - it('should return an error when the param "updatedSince" is an invalid date', (done) => { - updateSetting('Threads_enabled', true).then(() => { - request.get(api('chat.syncThreadsList')) - .set(credentials) - .query({ - rid: testChannel._id, - updatedSince: 'invalid-date', - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - expect(res.body).to.have.property('errorType', 'error-updatedSince-param-invalid'); - expect(res.body).to.have.property('error', 'The \"updatedSince\" query parameter must be a valid date. [error-updatedSince-param-invalid]'); - }) - .end(done); - }); + it('should return an error for chat.getThreadsList when threads are not allowed in this server', (done) => { + updateSetting('Threads_enabled', false).then(() => { + request.get(api('chat.getThreadsList')) + .set(credentials) + .query({ + rid: testChannel._id, + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('errorType', 'error-not-allowed'); + expect(res.body).to.have.property('error', 'Threads Disabled [error-not-allowed]'); + }) + .end(done); }); + }); - it('should return an error when the user is not allowed access the room', (done) => { - createUser().then((createdUser) => { - login(createdUser.username, password).then((userCredentials) => { + it('should return an error when the user is not allowed access the room', (done) => { + createUser().then((createdUser) => { + login(createdUser.username, password).then((userCredentials) => { + updateSetting('Threads_enabled', true).then(() => { updatePermission('view-c-room', []).then(() => { - request.get(api('chat.syncThreadsList')) + request.get(api('chat.getThreadsList')) .set(userCredentials) .query({ rid: testChannel._id, - updatedSince: new Date().toISOString(), }) .expect('Content-Type', 'application/json') .expect(400) @@ -1535,227 +1480,225 @@ describe('[Chat]', function() { }); }); }); + }); - it('should return the room\'s thread synced list', (done) => { - updatePermission('view-c-room', ['admin', 'user']).then(() => { - request.get(api('chat.syncThreadsList')) - .set(credentials) - .query({ - rid: testChannel._id, - updatedSince: new Date('2019-04-01').toISOString(), - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.property('threads').and.to.be.an('object'); - expect(res.body.threads).to.have.property('update').and.to.be.an('array'); - expect(res.body.threads).to.have.property('remove').and.to.be.an('array'); - expect(res.body.threads.update).to.have.lengthOf(1); - expect(res.body.threads.remove).to.have.lengthOf(0); - expect(res.body.threads.update[0]._id).to.be.equal(threadMessage.tmid); - }) - .end(done); - }); + it('should return the room\'s thread list', (done) => { + updatePermission('view-c-room', ['admin', 'user']).then(() => { + request.get(api('chat.getThreadsList')) + .set(credentials) + .query({ + rid: testChannel._id, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('threads').and.to.be.an('array'); + expect(res.body).to.have.property('total'); + expect(res.body).to.have.property('offset'); + expect(res.body).to.have.property('count'); + expect(res.body.threads).to.have.lengthOf(1); + expect(res.body.threads[0]._id).to.be.equal(threadMessage.tmid); + }) + .end(done); }); }); + }); - describe('[/chat.getThreadMessages]', () => { - let testChannel; - let threadMessage; - let createdThreadMessage; - before((done) => { - createRoom({ type: 'c', name: `channel.test.threads.${ Date.now() }` }) - .end((err, res) => { - testChannel = res.body.channel; + describe('[/chat.syncThreadsList]', () => { + let testChannel; + let threadMessage; + before((done) => { + createRoom({ type: 'c', name: `.threads.sync.${ Date.now() }` }) + .end((err, channel) => { + testChannel = channel.body.channel; + sendSimpleMessage({ + roomId: testChannel._id, + text: 'Message to create thread', + }).end((err, message) => { sendSimpleMessage({ roomId: testChannel._id, - text: 'Message to create thread', - }).end((err, message) => { - createdThreadMessage = message.body.message; - sendSimpleMessage({ - roomId: testChannel._id, - text: 'Thread Message', - tmid: createdThreadMessage._id, - }).end((err, res) => { - threadMessage = res.body.message; - done(); - }); + text: 'Thread Message', + tmid: message.body.message._id, + }).end((err, res) => { + threadMessage = res.body.message; + done(); }); }); - }); - - it('should return an error for chat.getThreadMessages when threads are not allowed in this server', (done) => { - updateSetting('Threads_enabled', false).then(() => { - request.get(api('chat.getThreadMessages')) - .set(credentials) - .query({ - tmid: threadMessage.tmid, - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - expect(res.body).to.have.property('errorType', 'error-not-allowed'); - expect(res.body).to.have.property('error', 'Threads Disabled [error-not-allowed]'); - }) - .end(done); }); + }); + + it('should return an error for chat.getThreadsList when threads are not allowed in this server', (done) => { + updateSetting('Threads_enabled', false).then(() => { + request.get(api('chat.getThreadsList')) + .set(credentials) + .query({ + rid: testChannel._id, + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('errorType', 'error-not-allowed'); + expect(res.body).to.have.property('error', 'Threads Disabled [error-not-allowed]'); + }) + .end(done); }); + }); - it('should return an error when the user is not allowed access the room', (done) => { - createUser().then((createdUser) => { - login(createdUser.username, password).then((userCredentials) => { - updateSetting('Threads_enabled', true).then(() => { - updatePermission('view-c-room', []).then(() => { - request.get(api('chat.getThreadMessages')) - .set(userCredentials) - .query({ - tmid: threadMessage.tmid, - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - expect(res.body).to.have.property('errorType', 'error-not-allowed'); - expect(res.body).to.have.property('error', 'Not Allowed [error-not-allowed]'); - }) - .end(done); - }); - }); - }); - }); + it('should return an error when the required param "rid" is missing', (done) => { + updateSetting('Threads_enabled', true).then(() => { + request.get(api('chat.syncThreadsList')) + .set(credentials) + .query({ + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('errorType', 'error-room-id-param-not-provided'); + expect(res.body).to.have.property('error', 'The required \"rid\" query param is missing. [error-room-id-param-not-provided]'); + }) + .end(done); }); + }); - it('should return the thread\'s message list', (done) => { - updatePermission('view-c-room', ['admin', 'user']).then(() => { - request.get(api('chat.getThreadMessages')) - .set(credentials) - .query({ - tmid: threadMessage.tmid, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.property('messages').and.to.be.an('array'); - expect(res.body).to.have.property('total').and.to.be.equal(1); - expect(res.body).to.have.property('offset'); - expect(res.body).to.have.property('count'); - expect(res.body.messages).to.have.lengthOf(1); - expect(res.body.messages[0].tmid).to.be.equal(createdThreadMessage._id); - }) - .end(done); - }); + it('should return an error when the required param "updatedSince" is missing', (done) => { + updateSetting('Threads_enabled', true).then(() => { + request.get(api('chat.syncThreadsList')) + .set(credentials) + .query({ + rid: testChannel._id, + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('errorType', 'error-updatedSince-param-invalid'); + expect(res.body).to.have.property('error', 'The required param \"updatedSince\" is missing. [error-updatedSince-param-invalid]'); + }) + .end(done); }); }); - describe('[/chat.syncThreadMessages]', () => { - let testChannel; - let threadMessage; - let createdThreadMessage; - before((done) => { - createRoom({ type: 'c', name: `message.threads.${ Date.now() }` }) - .end((err, res) => { - testChannel = res.body.channel; - sendSimpleMessage({ - roomId: testChannel._id, - text: 'Message to create thread', - }).end((err, message) => { - createdThreadMessage = message.body.message; - sendSimpleMessage({ - roomId: testChannel._id, - text: 'Thread Message', - tmid: createdThreadMessage._id, - }).end((err, res) => { - threadMessage = res.body.message; - done(); - }); - }); - }); + it('should return an error when the param "updatedSince" is an invalid date', (done) => { + updateSetting('Threads_enabled', true).then(() => { + request.get(api('chat.syncThreadsList')) + .set(credentials) + .query({ + rid: testChannel._id, + updatedSince: 'invalid-date', + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('errorType', 'error-updatedSince-param-invalid'); + expect(res.body).to.have.property('error', 'The \"updatedSince\" query parameter must be a valid date. [error-updatedSince-param-invalid]'); + }) + .end(done); }); + }); - it('should return an error for chat.syncThreadMessages when threads are not allowed in this server', (done) => { - updateSetting('Threads_enabled', false).then(() => { - request.get(api('chat.syncThreadMessages')) - .set(credentials) - .query({ - tmid: threadMessage.tmid, - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - expect(res.body).to.have.property('errorType', 'error-not-allowed'); - expect(res.body).to.have.property('error', 'Threads Disabled [error-not-allowed]'); - }) - .end(done); + it('should return an error when the user is not allowed access the room', (done) => { + createUser().then((createdUser) => { + login(createdUser.username, password).then((userCredentials) => { + updatePermission('view-c-room', []).then(() => { + request.get(api('chat.syncThreadsList')) + .set(userCredentials) + .query({ + rid: testChannel._id, + updatedSince: new Date().toISOString(), + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('errorType', 'error-not-allowed'); + expect(res.body).to.have.property('error', 'Not Allowed [error-not-allowed]'); + }) + .end(done); + }); }); }); + }); - it('should return an error when the required param "tmid" is missing', (done) => { - updateSetting('Threads_enabled', true).then(() => { - request.get(api('chat.syncThreadMessages')) - .set(credentials) - .query({ - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - expect(res.body).to.have.property('errorType', 'error-invalid-params'); - expect(res.body).to.have.property('error', 'The required \"tmid\" query param is missing. [error-invalid-params]'); - }) - .end(done); - }); + it('should return the room\'s thread synced list', (done) => { + updatePermission('view-c-room', ['admin', 'user']).then(() => { + request.get(api('chat.syncThreadsList')) + .set(credentials) + .query({ + rid: testChannel._id, + updatedSince: new Date('2019-04-01').toISOString(), + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('threads').and.to.be.an('object'); + expect(res.body.threads).to.have.property('update').and.to.be.an('array'); + expect(res.body.threads).to.have.property('remove').and.to.be.an('array'); + expect(res.body.threads.update).to.have.lengthOf(1); + expect(res.body.threads.remove).to.have.lengthOf(0); + expect(res.body.threads.update[0]._id).to.be.equal(threadMessage.tmid); + }) + .end(done); }); + }); + }); - it('should return an error when the required param "updatedSince" is missing', (done) => { - updateSetting('Threads_enabled', true).then(() => { - request.get(api('chat.syncThreadMessages')) - .set(credentials) - .query({ - tmid: threadMessage.tmid, - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - expect(res.body).to.have.property('errorType', 'error-updatedSince-param-invalid'); - expect(res.body).to.have.property('error', 'The required param \"updatedSince\" is missing. [error-updatedSince-param-invalid]'); - }) - .end(done); + describe('[/chat.getThreadMessages]', () => { + let testChannel; + let threadMessage; + let createdThreadMessage; + before((done) => { + createRoom({ type: 'c', name: `channel.test.threads.${ Date.now() }` }) + .end((err, res) => { + testChannel = res.body.channel; + sendSimpleMessage({ + roomId: testChannel._id, + text: 'Message to create thread', + }).end((err, message) => { + createdThreadMessage = message.body.message; + sendSimpleMessage({ + roomId: testChannel._id, + text: 'Thread Message', + tmid: createdThreadMessage._id, + }).end((err, res) => { + threadMessage = res.body.message; + done(); + }); + }); }); - }); + }); - it('should return an error when the param "updatedSince" is an invalid date', (done) => { - updateSetting('Threads_enabled', true).then(() => { - request.get(api('chat.syncThreadMessages')) - .set(credentials) - .query({ - tmid: threadMessage.tmid, - updatedSince: 'invalid-date', - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - expect(res.body).to.have.property('errorType', 'error-updatedSince-param-invalid'); - expect(res.body).to.have.property('error', 'The \"updatedSince\" query parameter must be a valid date. [error-updatedSince-param-invalid]'); - }) - .end(done); - }); + it('should return an error for chat.getThreadMessages when threads are not allowed in this server', (done) => { + updateSetting('Threads_enabled', false).then(() => { + request.get(api('chat.getThreadMessages')) + .set(credentials) + .query({ + tmid: threadMessage.tmid, + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('errorType', 'error-not-allowed'); + expect(res.body).to.have.property('error', 'Threads Disabled [error-not-allowed]'); + }) + .end(done); }); + }); - it('should return an error when the user is not allowed access the room', (done) => { - createUser().then((createdUser) => { - login(createdUser.username, password).then((userCredentials) => { + it('should return an error when the user is not allowed access the room', (done) => { + createUser().then((createdUser) => { + login(createdUser.username, password).then((userCredentials) => { + updateSetting('Threads_enabled', true).then(() => { updatePermission('view-c-room', []).then(() => { - request.get(api('chat.syncThreadMessages')) + request.get(api('chat.getThreadMessages')) .set(userCredentials) .query({ tmid: threadMessage.tmid, - updatedSince: new Date().toISOString(), }) .expect('Content-Type', 'application/json') .expect(400) @@ -1769,225 +1712,368 @@ describe('[Chat]', function() { }); }); }); + }); - it('should return the thread\'s message list', (done) => { - updatePermission('view-c-room', ['admin', 'user']).then(() => { - request.get(api('chat.syncThreadMessages')) - .set(credentials) - .query({ - tmid: threadMessage.tmid, - updatedSince: new Date('2019-04-01').toISOString(), - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.property('messages').and.to.be.an('object'); - expect(res.body.messages).to.have.property('update').and.to.be.an('array'); - expect(res.body.messages).to.have.property('remove').and.to.be.an('array'); - expect(res.body.messages.update).to.have.lengthOf(1); - expect(res.body.messages.remove).to.have.lengthOf(0); - expect(res.body.messages.update[0].id).to.be.equal(createdThreadMessage.tmid); - }) - .end(done); - }); + it('should return the thread\'s message list', (done) => { + updatePermission('view-c-room', ['admin', 'user']).then(() => { + request.get(api('chat.getThreadMessages')) + .set(credentials) + .query({ + tmid: threadMessage.tmid, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('messages').and.to.be.an('array'); + expect(res.body).to.have.property('total').and.to.be.equal(1); + expect(res.body).to.have.property('offset'); + expect(res.body).to.have.property('count'); + expect(res.body.messages).to.have.lengthOf(1); + expect(res.body.messages[0].tmid).to.be.equal(createdThreadMessage._id); + }) + .end(done); }); }); + }); - describe('[/chat.followMessage]', () => { - let testChannel; - let threadMessage; - before((done) => { - createRoom({ type: 'c', name: `channel.test.threads.follow.${ Date.now() }` }) - .end((err, res) => { - testChannel = res.body.channel; + describe('[/chat.syncThreadMessages]', () => { + let testChannel; + let threadMessage; + let createdThreadMessage; + before((done) => { + createRoom({ type: 'c', name: `message.threads.${ Date.now() }` }) + .end((err, res) => { + testChannel = res.body.channel; + sendSimpleMessage({ + roomId: testChannel._id, + text: 'Message to create thread', + }).end((err, message) => { + createdThreadMessage = message.body.message; sendSimpleMessage({ roomId: testChannel._id, - text: 'Message to create thread', - }).end((err, message) => { - sendSimpleMessage({ - roomId: testChannel._id, - text: 'Thread Message', - tmid: message.body.message._id, - }).end((err, res) => { - threadMessage = res.body.message; - done(); - }); + text: 'Thread Message', + tmid: createdThreadMessage._id, + }).end((err, res) => { + threadMessage = res.body.message; + done(); }); }); + }); + }); + + it('should return an error for chat.syncThreadMessages when threads are not allowed in this server', (done) => { + updateSetting('Threads_enabled', false).then(() => { + request.get(api('chat.syncThreadMessages')) + .set(credentials) + .query({ + tmid: threadMessage.tmid, + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('errorType', 'error-not-allowed'); + expect(res.body).to.have.property('error', 'Threads Disabled [error-not-allowed]'); + }) + .end(done); }); + }); - it('should return an error for chat.followMessage when threads are not allowed in this server', (done) => { - updateSetting('Threads_enabled', false).then(() => { - request.post(api('chat.followMessage')) - .set(credentials) - .send({ - mid: threadMessage.tmid, - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - expect(res.body).to.have.property('errorType', 'error-not-allowed'); - expect(res.body).to.have.property('error', 'not-allowed [error-not-allowed]'); - }) - .end(done); - }); + it('should return an error when the required param "tmid" is missing', (done) => { + updateSetting('Threads_enabled', true).then(() => { + request.get(api('chat.syncThreadMessages')) + .set(credentials) + .query({ + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('errorType', 'error-invalid-params'); + expect(res.body).to.have.property('error', 'The required \"tmid\" query param is missing. [error-invalid-params]'); + }) + .end(done); }); + }); - it('should return an error when the message does not exist', (done) => { - updateSetting('Threads_enabled', true).then(() => { - request.post(api('chat.followMessage')) - .set(credentials) - .send({ - mid: 'invalid-message-id', - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - expect(res.body).to.have.property('errorType', 'error-invalid-message'); - expect(res.body).to.have.property('error', 'Invalid message [error-invalid-message]'); - }) - .end(done); - }); + it('should return an error when the required param "updatedSince" is missing', (done) => { + updateSetting('Threads_enabled', true).then(() => { + request.get(api('chat.syncThreadMessages')) + .set(credentials) + .query({ + tmid: threadMessage.tmid, + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('errorType', 'error-updatedSince-param-invalid'); + expect(res.body).to.have.property('error', 'The required param \"updatedSince\" is missing. [error-updatedSince-param-invalid]'); + }) + .end(done); + }); + }); + + it('should return an error when the param "updatedSince" is an invalid date', (done) => { + updateSetting('Threads_enabled', true).then(() => { + request.get(api('chat.syncThreadMessages')) + .set(credentials) + .query({ + tmid: threadMessage.tmid, + updatedSince: 'invalid-date', + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('errorType', 'error-updatedSince-param-invalid'); + expect(res.body).to.have.property('error', 'The \"updatedSince\" query parameter must be a valid date. [error-updatedSince-param-invalid]'); + }) + .end(done); }); + }); - it('should return an error when the user is not allowed access the room', (done) => { - createUser().then((createdUser) => { - login(createdUser.username, password).then((userCredentials) => { - updatePermission('view-c-room', []).then(() => { - request.post(api('chat.followMessage')) - .set(userCredentials) - .send({ - mid: threadMessage.tmid, - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - expect(res.body).to.have.property('errorType', 'error-not-allowed'); - expect(res.body).to.have.property('error', 'not-allowed [error-not-allowed]'); - }) - .end(done); - }); + it('should return an error when the user is not allowed access the room', (done) => { + createUser().then((createdUser) => { + login(createdUser.username, password).then((userCredentials) => { + updatePermission('view-c-room', []).then(() => { + request.get(api('chat.syncThreadMessages')) + .set(userCredentials) + .query({ + tmid: threadMessage.tmid, + updatedSince: new Date().toISOString(), + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('errorType', 'error-not-allowed'); + expect(res.body).to.have.property('error', 'Not Allowed [error-not-allowed]'); + }) + .end(done); }); }); }); + }); - it('should return success: true when it execute successfully', (done) => { - updatePermission('view-c-room', ['admin', 'user']).then(() => { - request.post(api('chat.followMessage')) - .set(credentials) - .send({ - mid: threadMessage.tmid, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - }) - .end(done); - }); + it('should return the thread\'s message list', (done) => { + updatePermission('view-c-room', ['admin', 'user']).then(() => { + request.get(api('chat.syncThreadMessages')) + .set(credentials) + .query({ + tmid: threadMessage.tmid, + updatedSince: new Date('2019-04-01').toISOString(), + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('messages').and.to.be.an('object'); + expect(res.body.messages).to.have.property('update').and.to.be.an('array'); + expect(res.body.messages).to.have.property('remove').and.to.be.an('array'); + expect(res.body.messages.update).to.have.lengthOf(1); + expect(res.body.messages.remove).to.have.lengthOf(0); + expect(res.body.messages.update[0].id).to.be.equal(createdThreadMessage.tmid); + }) + .end(done); }); }); + }); - describe('[/chat.unfollowMessage]', () => { - let testChannel; - let threadMessage; - before((done) => { - createRoom({ type: 'c', name: `channel.test.threads.unfollow.${ Date.now() }` }) - .end((err, res) => { - testChannel = res.body.channel; + describe('[/chat.followMessage]', () => { + let testChannel; + let threadMessage; + before((done) => { + createRoom({ type: 'c', name: `channel.test.threads.follow.${ Date.now() }` }) + .end((err, res) => { + testChannel = res.body.channel; + sendSimpleMessage({ + roomId: testChannel._id, + text: 'Message to create thread', + }).end((err, message) => { sendSimpleMessage({ roomId: testChannel._id, - text: 'Message to create thread', - }).end((err, message) => { - sendSimpleMessage({ - roomId: testChannel._id, - text: 'Thread Message', - tmid: message.body.message._id, - }).end((err, res) => { - threadMessage = res.body.message; - done(); - }); + text: 'Thread Message', + tmid: message.body.message._id, + }).end((err, res) => { + threadMessage = res.body.message; + done(); }); }); + }); + }); + + it('should return an error for chat.followMessage when threads are not allowed in this server', (done) => { + updateSetting('Threads_enabled', false).then(() => { + request.post(api('chat.followMessage')) + .set(credentials) + .send({ + mid: threadMessage.tmid, + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('errorType', 'error-not-allowed'); + expect(res.body).to.have.property('error', 'not-allowed [error-not-allowed]'); + }) + .end(done); }); + }); - it('should return an error for chat.unfollowMessage when threads are not allowed in this server', (done) => { - updateSetting('Threads_enabled', false).then(() => { - request.post(api('chat.unfollowMessage')) - .set(credentials) - .send({ - mid: threadMessage.tmid, - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - expect(res.body).to.have.property('errorType', 'error-not-allowed'); - expect(res.body).to.have.property('error', 'not-allowed [error-not-allowed]'); - }) - .end(done); - }); + it('should return an error when the message does not exist', (done) => { + updateSetting('Threads_enabled', true).then(() => { + request.post(api('chat.followMessage')) + .set(credentials) + .send({ + mid: 'invalid-message-id', + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('errorType', 'error-invalid-message'); + expect(res.body).to.have.property('error', 'Invalid message [error-invalid-message]'); + }) + .end(done); }); + }); - it('should return an error when the message does not exist', (done) => { - updateSetting('Threads_enabled', true).then(() => { - request.post(api('chat.unfollowMessage')) - .set(credentials) - .send({ - mid: 'invalid-message-id', - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - expect(res.body).to.have.property('errorType', 'error-invalid-message'); - expect(res.body).to.have.property('error', 'Invalid message [error-invalid-message]'); - }) - .end(done); + it('should return an error when the user is not allowed access the room', (done) => { + createUser().then((createdUser) => { + login(createdUser.username, password).then((userCredentials) => { + updatePermission('view-c-room', []).then(() => { + request.post(api('chat.followMessage')) + .set(userCredentials) + .send({ + mid: threadMessage.tmid, + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('errorType', 'error-not-allowed'); + expect(res.body).to.have.property('error', 'not-allowed [error-not-allowed]'); + }) + .end(done); + }); }); }); + }); - it('should return an error when the user is not allowed access the room', (done) => { - createUser().then((createdUser) => { - login(createdUser.username, password).then((userCredentials) => { - updatePermission('view-c-room', []).then(() => { - request.post(api('chat.unfollowMessage')) - .set(userCredentials) - .send({ - mid: threadMessage.tmid, - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - expect(res.body).to.have.property('errorType', 'error-not-allowed'); - expect(res.body).to.have.property('error', 'not-allowed [error-not-allowed]'); - }) - .end(done); + it('should return success: true when it execute successfully', (done) => { + updatePermission('view-c-room', ['admin', 'user']).then(() => { + request.post(api('chat.followMessage')) + .set(credentials) + .send({ + mid: threadMessage.tmid, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + }) + .end(done); + }); + }); + }); + + describe('[/chat.unfollowMessage]', () => { + let testChannel; + let threadMessage; + before((done) => { + createRoom({ type: 'c', name: `channel.test.threads.unfollow.${ Date.now() }` }) + .end((err, res) => { + testChannel = res.body.channel; + sendSimpleMessage({ + roomId: testChannel._id, + text: 'Message to create thread', + }).end((err, message) => { + sendSimpleMessage({ + roomId: testChannel._id, + text: 'Thread Message', + tmid: message.body.message._id, + }).end((err, res) => { + threadMessage = res.body.message; + done(); }); }); }); + }); + + it('should return an error for chat.unfollowMessage when threads are not allowed in this server', (done) => { + updateSetting('Threads_enabled', false).then(() => { + request.post(api('chat.unfollowMessage')) + .set(credentials) + .send({ + mid: threadMessage.tmid, + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('errorType', 'error-not-allowed'); + expect(res.body).to.have.property('error', 'not-allowed [error-not-allowed]'); + }) + .end(done); + }); + }); + + it('should return an error when the message does not exist', (done) => { + updateSetting('Threads_enabled', true).then(() => { + request.post(api('chat.unfollowMessage')) + .set(credentials) + .send({ + mid: 'invalid-message-id', + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('errorType', 'error-invalid-message'); + expect(res.body).to.have.property('error', 'Invalid message [error-invalid-message]'); + }) + .end(done); }); + }); - it('should return success: true when it execute successfully', (done) => { - updatePermission('view-c-room', ['admin', 'user']).then(() => { - request.post(api('chat.unfollowMessage')) - .set(credentials) - .send({ - mid: threadMessage.tmid, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - }) - .end(done); + it('should return an error when the user is not allowed access the room', (done) => { + createUser().then((createdUser) => { + login(createdUser.username, password).then((userCredentials) => { + updatePermission('view-c-room', []).then(() => { + request.post(api('chat.unfollowMessage')) + .set(userCredentials) + .send({ + mid: threadMessage.tmid, + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('errorType', 'error-not-allowed'); + expect(res.body).to.have.property('error', 'not-allowed [error-not-allowed]'); + }) + .end(done); + }); }); }); }); + + it('should return success: true when it execute successfully', (done) => { + updatePermission('view-c-room', ['admin', 'user']).then(() => { + request.post(api('chat.unfollowMessage')) + .set(credentials) + .send({ + mid: threadMessage.tmid, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + }) + .end(done); + }); + }); }); });