diff --git a/packages/rocketchat-api/server/v1/chat.js b/packages/rocketchat-api/server/v1/chat.js index 1d3e8621cad52..b25c5777c2397 100644 --- a/packages/rocketchat-api/server/v1/chat.js +++ b/packages/rocketchat-api/server/v1/chat.js @@ -18,7 +18,7 @@ RocketChat.API.v1.addRoute('chat.delete', { authRequired: true }, { return RocketChat.API.v1.failure('The room id provided does not match where the message is from.'); } - if (this.bodyParams.asUser && msg.u._id !== this.userId && !RocketChat.authz.hasPermission(Meteor.userId(), 'force-delete-message', msg.rid)) { + if (this.bodyParams.asUser && msg.u._id !== this.userId && !RocketChat.authz.hasPermission(this.userId, 'force-delete-message', msg.rid)) { return RocketChat.API.v1.failure('Unauthorized. You must have the permission "force-delete-message" to delete other\'s message as them.'); } diff --git a/tests/end-to-end/api/05-chat.js b/tests/end-to-end/api/05-chat.js index 783d9bc687d93..ce06a76e6764d 100644 --- a/tests/end-to-end/api/05-chat.js +++ b/tests/end-to-end/api/05-chat.js @@ -9,6 +9,7 @@ import { credentials, message, } from '../../data/api-data.js'; +import { password } from '../../data/user'; describe('[Chat]', function() { this.retries(0); @@ -346,6 +347,107 @@ describe('[Chat]', function() { }); }); + describe('[/chat.delete]', () => { + let msgId; + let user; + let userCredentials; + before((done) => { + const username = `user.test.${ Date.now() }`; + const email = `${ username }@rocket.chat`; + request.post(api('users.create')) + .set(credentials) + .send({ email, name: username, username, password }) + .end((err, res) => { + user = res.body.user; + done(); + }); + }); + before((done) => { + request.post(api('login')) + .send({ + user: user.username, + password, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + userCredentials = {}; + userCredentials['X-Auth-Token'] = res.body.data.authToken; + userCredentials['X-User-Id'] = res.body.data.userId; + }) + .end(done); + }); + after((done) => { + request.post(api('users.delete')).set(credentials).send({ + userId: user._id, + }).end(done); + user = undefined; + }); + beforeEach((done) => { + request.post(api('chat.sendMessage')) + .set(credentials) + .send({ + message: { + rid: 'GENERAL', + msg: 'Sample message', + }, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + msgId = res.body.message._id; + }) + .end(done); + }); + it('should delete a message successfully', (done) => { + request.post(api('chat.delete')) + .set(credentials) + .send({ + roomId: 'GENERAL', + msgId, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + }) + .end(done); + }); + it('sending message as another user...', (done) => { + request.post(api('chat.sendMessage')) + .set(userCredentials) + .send({ + message: { + rid: 'GENERAL', + msg: 'Sample message', + }, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + msgId = res.body.message._id; + }) + .end(done); + }); + it('should delete a message successfully when the user deletes a message send by another user', (done) => { + request.post(api('chat.delete')) + .set(credentials) + .send({ + roomId: 'GENERAL', + msgId, + asUser: true, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + }) + .end(done); + }); + }); + describe('/chat.search', () => { it('should return a list of messages when execute successfully', (done) => { request.get(api('chat.search'))