diff --git a/server/methods/createDirectMessage.js b/server/methods/createDirectMessage.js index 0a729ce7238..6c237605696 100644 --- a/server/methods/createDirectMessage.js +++ b/server/methods/createDirectMessage.js @@ -18,7 +18,7 @@ export function createDirectMessage(usernames, userId, excludeSelf = false) { }); } - const me = Users.findOneById(userId, { fields: { username: 1 } }); + const me = Users.findOneById(userId, { fields: { username: 1, name: 1 } }); if (!me?.username) { throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'createDirectMessage', diff --git a/tests/data/users.helper.js b/tests/data/users.helper.js index 44ee4f73c74..71eeb9c0c4f 100644 --- a/tests/data/users.helper.js +++ b/tests/data/users.helper.js @@ -1,12 +1,12 @@ import { api, credentials, request } from './api-data'; import { password } from './user'; -export const createUser = () => new Promise((resolve) => { +export const createUser = (userData = {}) => new Promise((resolve) => { const username = `user.test.${ Date.now() }`; const email = `${ username }@rocket.chat`; request.post(api('users.create')) .set(credentials) - .send({ email, name: username, username, password }) + .send({ email, name: username, username, password, ...userData }) .end((err, res) => resolve(res.body.user)); }); diff --git a/tests/end-to-end/api/04-direct-message.js b/tests/end-to-end/api/04-direct-message.js index 33dce8b18c5..6b6aa91590c 100644 --- a/tests/end-to-end/api/04-direct-message.js +++ b/tests/end-to-end/api/04-direct-message.js @@ -339,6 +339,7 @@ describe('[Direct Messages]', function() { const email = `fname_${ apiEmail }`; let userId; let directMessageId; + let user; before((done) => { request.post(api('users.create')) @@ -354,6 +355,7 @@ describe('[Direct Messages]', function() { verified: true, }) .expect((res) => { + user = res.body.user; userId = res.body.user._id; }) .end(done); @@ -377,6 +379,8 @@ describe('[Direct Messages]', function() { .end(done); }); + after(async () => deleteUser(user)); + it('should have fname property', (done) => { request.get(api('subscriptions.getOne')) .set(credentials) @@ -481,34 +485,55 @@ describe('[Direct Messages]', function() { }); describe('/im.create', () => { + let user; + let userCredentials; + let otherUser; - let roomId; + let otherUserCredentials; + + let thirdUser; + let thirdUserCredentials; + + let roomIds = {}; + + // Names have to be in alfabetical order so we can test the room's fullname + const userFullName = 'User A'; + const otherUserFullName = 'User B'; + const thirdUserFullName = 'User C'; before(async () => { - otherUser = await createUser(); + user = await createUser({ name: userFullName }); + otherUser = await createUser({ name: otherUserFullName }); + thirdUser = await createUser({ name: thirdUserFullName }); + + userCredentials = await login(user.username, password); + otherUserCredentials = await login(otherUser.username, password); + thirdUserCredentials = await login(thirdUser.username, password); }); after(async () => { - if (roomId) { - await deleteRoom({ type: 'd', roomId }); - } + await Promise.all(Object.values(roomIds).map((roomId) => deleteRoom({ type: 'd', roomId }))); + await deleteUser(user); await deleteUser(otherUser); + await deleteUser(thirdUser); + user = undefined; otherUser = undefined; + thirdUser = undefined; }); it('creates a DM between two other parties (including self)', (done) => { request.post(api('im.create')) - .set(credentials) + .set(userCredentials) .send({ - usernames: ['rocket.cat', otherUser.username].join(','), + usernames: [otherUser.username, thirdUser.username].join(','), }) .expect(200) .expect('Content-Type', 'application/json') .expect((res) => { expect(res.body).to.have.property('success', true); expect(res.body).to.have.property('room').and.to.be.an('object'); - expect(res.body.room).to.have.property('usernames').and.to.have.members([adminUsername, 'rocket.cat', otherUser.username]); - roomId = res.body.room._id; + expect(res.body.room).to.have.property('usernames').and.to.have.members([thirdUser.username, user.username, otherUser.username]); + roomIds = { ...roomIds, multipleDm: res.body.room._id }; }) .end(done); }); @@ -517,7 +542,7 @@ describe('[Direct Messages]', function() { request.post(api('im.create')) .set(credentials) .send({ - usernames: ['rocket.cat', otherUser.username].join(','), + usernames: [user.username, otherUser.username].join(','), excludeSelf: true, }) .expect(200) @@ -525,27 +550,58 @@ describe('[Direct Messages]', function() { .expect((res) => { expect(res.body).to.have.property('success', true); expect(res.body).to.have.property('room').and.to.be.an('object'); - expect(res.body.room).to.have.property('usernames').and.to.have.members(['rocket.cat', otherUser.username]); - roomId = res.body.room._id; + expect(res.body.room).to.have.property('usernames').and.to.have.members([user.username, otherUser.username]); + roomIds = { ...roomIds, dm: res.body.room._id }; }) .end(done); }); it('should create a self-DM', (done) => { request.post(api('im.create')) - .set(credentials) + .set(userCredentials) .send({ - username: adminUsername, + username: user.username, }) .expect(200) .expect('Content-Type', 'application/json') .expect((res) => { expect(res.body).to.have.property('success', true); expect(res.body).to.have.property('room').and.to.be.an('object'); - expect(res.body.room).to.have.property('usernames').and.to.have.members([adminUsername]); + expect(res.body.room).to.have.property('usernames').and.to.have.members([user.username]); + roomIds = { ...roomIds, self: res.body.room._id }; }) .end(done); }); + + async function testRoomFNameForUser(testCredentials, roomId, fullName) { + return request.get(api('subscriptions.getOne')) + .set(testCredentials) + .query({ roomId }) + .expect(200) + .expect('Content-Type', 'application/json') + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('subscription').and.to.be.an('object'); + expect(res.body.subscription).to.have.property('fname', fullName); + }); + } + + describe('Rooms fullName', () => { + it('should be own user\'s name for self DM', async () => { + await testRoomFNameForUser(userCredentials, roomIds.self, userFullName); + }); + + it('should be other user\'s name concatenated for multiple users\'s DM for every user', async () => { + await testRoomFNameForUser(userCredentials, roomIds.multipleDm, [otherUserFullName, thirdUserFullName].join(', ')); + await testRoomFNameForUser(otherUserCredentials, roomIds.multipleDm, [userFullName, thirdUserFullName].join(', ')); + await testRoomFNameForUser(thirdUserCredentials, roomIds.multipleDm, [userFullName, otherUserFullName].join(', ')); + }); + + it('should be other user\'s name for DM for both users', async () => { + await testRoomFNameForUser(userCredentials, roomIds.dm, otherUserFullName); + await testRoomFNameForUser(otherUserCredentials, roomIds.dm, userFullName); + }); + }); }); describe('/im.delete', () => {