|
|
|
|
@ -4,6 +4,7 @@ import { expect } from 'chai'; |
|
|
|
|
import { after, before, describe, it } from 'mocha'; |
|
|
|
|
|
|
|
|
|
import { getCredentials, api, request, credentials, apiUsername, apiEmail, methodCall } from '../../data/api-data'; |
|
|
|
|
import { pinMessage, sendMessage, starMessage } from '../../data/chat.helper'; |
|
|
|
|
import { updateSetting, updatePermission } from '../../data/permissions.helper'; |
|
|
|
|
import { deleteRoom } from '../../data/rooms.helper'; |
|
|
|
|
import { testFileUploads } from '../../data/uploads.helper'; |
|
|
|
|
@ -369,12 +370,60 @@ describe('[Direct Messages]', () => { |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
describe('/im.messages', () => { |
|
|
|
|
let testUser: IUser; |
|
|
|
|
let testUserDMRoom: IRoom; |
|
|
|
|
let testUserCredentials: Credentials; |
|
|
|
|
|
|
|
|
|
before(async () => { |
|
|
|
|
testUser = await createUser({ joinDefaultChannels: false, roles: ['admin'] }); |
|
|
|
|
|
|
|
|
|
testUserCredentials = await login(testUser.username, password); |
|
|
|
|
await setUserStatus(testUserCredentials); |
|
|
|
|
|
|
|
|
|
testUserDMRoom = ( |
|
|
|
|
await request |
|
|
|
|
.post(api('im.create')) |
|
|
|
|
.set(testUserCredentials) |
|
|
|
|
.send({ username: `${testUser.username}` }) |
|
|
|
|
).body.room; |
|
|
|
|
|
|
|
|
|
const messages = [ |
|
|
|
|
{ |
|
|
|
|
rid: testUserDMRoom._id, |
|
|
|
|
msg: `@${adminUsername} youre being mentioned`, |
|
|
|
|
mentions: [{ username: adminUsername, _id: adminUsername, name: adminUsername }], |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
rid: testUserDMRoom._id, |
|
|
|
|
msg: `@${testUser.username} youre being mentioned`, |
|
|
|
|
mentions: [{ username: testUser.username, _id: testUser._id, name: testUser.name }], |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
rid: testUserDMRoom._id, |
|
|
|
|
msg: `A simple message`, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
rid: testUserDMRoom._id, |
|
|
|
|
msg: `A pinned simple message`, |
|
|
|
|
}, |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
const [, , starredMessage, pinnedMessage] = await Promise.all( |
|
|
|
|
messages.map((message) => sendMessage({ message, requestCredentials: testUserCredentials })), |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
await Promise.all([ |
|
|
|
|
starMessage({ messageId: starredMessage.body.message._id, requestCredentials: testUserCredentials }), |
|
|
|
|
pinMessage({ messageId: pinnedMessage.body.message._id, requestCredentials: testUserCredentials }), |
|
|
|
|
]); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should return all DM messages that were sent to yourself using your username', (done) => { |
|
|
|
|
void request |
|
|
|
|
.get(api('im.messages')) |
|
|
|
|
.set(credentials) |
|
|
|
|
.set(testUserCredentials) |
|
|
|
|
.query({ |
|
|
|
|
username: adminUsername, |
|
|
|
|
username: testUser.username, |
|
|
|
|
}) |
|
|
|
|
.expect('Content-Type', 'application/json') |
|
|
|
|
.expect(200) |
|
|
|
|
@ -384,6 +433,100 @@ describe('[Direct Messages]', () => { |
|
|
|
|
}) |
|
|
|
|
.end(done); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should return an error when trying to access a DM that does not belong to the current user', async () => { |
|
|
|
|
await request |
|
|
|
|
.get(api('im.messages')) |
|
|
|
|
.set(credentials) |
|
|
|
|
.query({ roomId: testUserDMRoom._id }) |
|
|
|
|
.expect('Content-Type', 'application/json') |
|
|
|
|
.expect(403) |
|
|
|
|
.expect((res) => { |
|
|
|
|
expect(res.body).to.have.property('success', false); |
|
|
|
|
expect(res.body).to.have.property('error', 'unauthorized'); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should return messages that mention a single user', async () => { |
|
|
|
|
await request |
|
|
|
|
.get(api('im.messages')) |
|
|
|
|
.set(testUserCredentials) |
|
|
|
|
.query({ |
|
|
|
|
roomId: testUserDMRoom._id, |
|
|
|
|
mentionIds: adminUsername, |
|
|
|
|
}) |
|
|
|
|
.expect('Content-Type', 'application/json') |
|
|
|
|
.expect(200) |
|
|
|
|
.expect((res) => { |
|
|
|
|
expect(res.body).to.have.property('success', true); |
|
|
|
|
expect(res.body.messages).to.have.lengthOf(1); |
|
|
|
|
expect(res.body.messages[0]).to.have.nested.property('mentions').that.is.an('array').and.to.have.lengthOf(1); |
|
|
|
|
expect(res.body.messages[0].mentions[0]).to.have.property('_id', adminUsername); |
|
|
|
|
expect(res.body).to.have.property('count', 1); |
|
|
|
|
expect(res.body).to.have.property('total', 1); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should return messages that mention multiple users', async () => { |
|
|
|
|
await request |
|
|
|
|
.get(api('im.messages')) |
|
|
|
|
.set(testUserCredentials) |
|
|
|
|
.query({ |
|
|
|
|
roomId: testUserDMRoom._id, |
|
|
|
|
mentionIds: `${adminUsername},${testUser._id}`, |
|
|
|
|
}) |
|
|
|
|
.expect('Content-Type', 'application/json') |
|
|
|
|
.expect(200) |
|
|
|
|
.expect((res) => { |
|
|
|
|
expect(res.body).to.have.property('success', true); |
|
|
|
|
expect(res.body.messages).to.have.lengthOf(2); |
|
|
|
|
expect(res.body).to.have.property('count', 2); |
|
|
|
|
expect(res.body).to.have.property('total', 2); |
|
|
|
|
|
|
|
|
|
const mentionIds = res.body.messages.map((message: any) => message.mentions[0]._id); |
|
|
|
|
expect(mentionIds).to.include.members([adminUsername, testUser._id]); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should return messages that are starred by a specific user', async () => { |
|
|
|
|
await request |
|
|
|
|
.get(api('im.messages')) |
|
|
|
|
.set(testUserCredentials) |
|
|
|
|
.query({ |
|
|
|
|
roomId: testUserDMRoom._id, |
|
|
|
|
starredIds: testUser._id, |
|
|
|
|
}) |
|
|
|
|
.expect('Content-Type', 'application/json') |
|
|
|
|
.expect(200) |
|
|
|
|
.expect((res) => { |
|
|
|
|
expect(res.body).to.have.property('success', true); |
|
|
|
|
expect(res.body.messages).to.have.lengthOf(1); |
|
|
|
|
expect(res.body.messages[0]).to.have.nested.property('starred').that.is.an('array').and.to.have.lengthOf(1); |
|
|
|
|
expect(res.body).to.have.property('count', 1); |
|
|
|
|
expect(res.body).to.have.property('total', 1); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should return messages that are pinned', async () => { |
|
|
|
|
await request |
|
|
|
|
.get(api('im.messages')) |
|
|
|
|
.set(testUserCredentials) |
|
|
|
|
.query({ |
|
|
|
|
roomId: testUserDMRoom._id, |
|
|
|
|
pinned: true, |
|
|
|
|
}) |
|
|
|
|
.expect('Content-Type', 'application/json') |
|
|
|
|
.expect(200) |
|
|
|
|
.expect((res) => { |
|
|
|
|
expect(res.body).to.have.property('success', true); |
|
|
|
|
expect(res.body.messages).to.have.lengthOf(1); |
|
|
|
|
expect(res.body.messages[0]).to.have.nested.property('pinned').that.is.an('boolean').and.to.be.true; |
|
|
|
|
expect(res.body.messages[0]).to.have.nested.property('pinnedBy').that.is.an('object'); |
|
|
|
|
expect(res.body.messages[0].pinnedBy).to.have.property('_id', testUser._id); |
|
|
|
|
expect(res.body).to.have.property('count', 1); |
|
|
|
|
expect(res.body).to.have.property('total', 1); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
describe('/im.messages.others', () => { |
|
|
|
|
|