Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
heitortanoue committed Oct 9, 2023
1 parent 3e9a862 commit 56c9ca6
Showing 1 changed file with 281 additions and 1 deletion.
282 changes: 281 additions & 1 deletion apps/meteor/tests/end-to-end/api/24-methods.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { expect } from 'chai';
import { after, before, beforeEach, describe, it } from 'mocha';

import { getCredentials, request, methodCall, api, credentials } from '../../data/api-data.js';
import { api, credentials, getCredentials, methodCall, request } from '../../data/api-data.js';
import { CI_MAX_ROOMS_PER_GUEST as maxRoomsPerGuest } from '../../data/constants';
import { updatePermission, updateSetting } from '../../data/permissions.helper';
import { createRoom } from '../../data/rooms.helper';
import { password } from '../../data/user';
import { createUser, deleteUser, login } from '../../data/users.helper.js';

describe('Meteor.methods', function () {
Expand Down Expand Up @@ -123,6 +124,285 @@ describe('Meteor.methods', function () {
});
});

describe('[@getReadReceipts]', () => {
let firstMessage = false;
let firstThreadMessage = false;

let channelName = false;
let user, userCredentials, room;

const roomName = `methods-test-channel-${ Date.now() }`;
before(async () => {
user = await createUser();
userCredentials = await login(user.username, password);
room = (await createRoom({ type: 'p', name: roomName, members: [user.username] })).body.group;
});

before(async () => {
await updateSetting('Message_Read_Receipt_Enabled', true);
await updateSetting('Message_Read_Receipt_Store_Users', true);
});

after(async () => {
deleteUser(user);
});

before('send sample message', (done) => {
request
.post(api('chat.sendMessage'))
.set(credentials)
.send({
message: {
text: 'Sample message',
rid: room._id,
},
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
firstMessage = res.body.message;
})
.end(done);
});

before('send sample message into thread', (done) => {
request
.post(api('chat.sendMessage'))
.set(credentials)
.send({
message: {
text: 'Second Sample message',
rid: room._id,
tmid: firstMessage._id,
},
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
firstThreadMessage = res.body.message;
})
.end(done);
});

it('should fail if not logged in', (done) => {
request
.post(methodCall('getReadReceipts'))
.send({
message: JSON.stringify({
method: 'getReadReceipts',
params: [],
id: 'id',
msg: 'method',
}),
})
.expect('Content-Type', 'application/json')
.expect(401)
.expect((res) => {
expect(res.body).to.have.property('status', 'error');
expect(res.body).to.have.property('message');
})
.end(done);
});

it("should return the sender's read receipt for a message sent in the main room", (done) => {
request
.post(methodCall('getReadReceipts'))
.set(credentials)
.send({
message: JSON.stringify({
method: 'getReadReceipts',
params: [{ messageId: firstMessage._id }],
id: 'id',
msg: 'method',
}),
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.a.property('success', true);
expect(res.body).to.have.a.property('message').that.is.a('string');

const data = JSON.parse(res.body.message);
expect(data).to.have.a.property('result').that.is.an('array');
expect(data.result.length).to.equal(1);
expect(data.result[0]).to.have.property('userId', credentials['X-User-Id']);
})
.end(done);
});

it("should return the sender's read receipt for a message sent in a thread", (done) => {
request
.post(methodCall('getReadReceipts'))
.set(credentials)
.send({
message: JSON.stringify({
method: 'getReadReceipts',
params: [{ messageId: firstThreadMessage._id }],
id: 'id',
msg: 'method',
}),
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.a.property('success', true);
expect(res.body).to.have.a.property('message').that.is.a('string');

const data = JSON.parse(res.body.message);
expect(data).to.have.a.property('result').that.is.an('array');
expect(data.result.length).to.equal(1);
expect(data.result[0]).to.have.property('userId', credentials['X-User-Id']);
})
.end(done);
});

it("should read all main room's messages with the invited user", (done) => {
request
.post(api('subscriptions.read'))
.set(credentials)
.send({
rid: room._id,
readThreads: true,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.a.property('success', true);
// expect(res.body).to.have.a.property('message').that.is.a('string');
})
.end(done);
});

it("should return the invited user's read receipt for a message sent in the main room", (done) => {
request
.post(methodCall('getReadReceipts'))
.set(credentials)
.send({
message: JSON.stringify({
method: 'getReadReceipts',
params: [{ messageId: firstMessage._id }],
id: 'id',
msg: 'method',
}),
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.a.property('success', true);
expect(res.body).to.have.a.property('message').that.is.a('string');

const data = JSON.parse(res.body.message);
console.log(data)
expect(data).to.have.a.property('result').that.is.an('array');
expect(data.result.length).to.equal(2);

const receiptsUserIds = [data.result[0].userId, data.result[1].userId];
expect(receiptsUserIds).to.have.members([credentials['X-User-Id'], user._id]);
})
.end(done);
});

it("should return only the sender's read receipt for a message sent in a thread after the main room is read", (done) => {
request
.post(methodCall('getReadReceipts'))
.set(credentials)
.send({
message: JSON.stringify({
method: 'getReadReceipts',
params: [{ messageId: firstThreadMessage._id }],
id: 'id',
msg: 'method',
}),
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.a.property('success', true);
expect(res.body).to.have.a.property('message').that.is.a('string');

const data = JSON.parse(res.body.message);
expect(data).to.have.a.property('result').that.is.an('array');
expect(data.result.length).to.equal(1);
expect(data.result[0]).to.have.property('userId', credentials['X-User-Id']);
})
.end(done);
});

it('should read thread messages with the invited user', (done) => {
request
.post(methodCall('getThreadMessages'))
.set(userCredentials)
.send({
message: JSON.stringify({
id: 'id',
msg: 'method',
method: 'getThreadMessages',
params: [
{
tmid: firstMessage._id,
},
],
}),
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.a.property('success', true);
expect(res.body).to.have.a.property('message').that.is.a('string');

const data = JSON.parse(res.body.message);
expect(data).to.have.a.property('result').that.is.an('array');
expect(data.result.length).to.equal(2);
})
.end(done);
});

it("should return the invited user's read receipt for a message sent in a thread after it is read", (done) => {
request
.post(methodCall('getReadReceipts'))
.set(credentials)
.send({
message: JSON.stringify({
method: 'getReadReceipts',
params: [{ messageId: firstThreadMessage._id }],
id: 'id',
msg: 'method',
}),
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.a.property('success', true);
expect(res.body).to.have.a.property('message').that.is.a('string');

const data = JSON.parse(res.body.message);
expect(data).to.have.a.property('result').that.is.an('array');
expect(data.result.length).to.equal(2);

const receiptsUserIds = [data.result[0].userId, data.result[1].userId];
expect(receiptsUserIds).to.have.members([credentials['X-User-Id'], user._id]);
})
.end(done);
});

it('should mark the thread as read', (done) => {
request
.post(methodCall('readThreads'))
.set(userCredentials)
.send(firstThreadMessage._id)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
console.log(res.body)
expect(res.body).to.have.a.property('success', true);
expect(res.body).to.have.a.property('message').that.is.a('string');
})
.end(done);
});
});

describe('[@getMessages]', () => {
let rid = false;
let firstMessage = false;
Expand Down

0 comments on commit 56c9ca6

Please sign in to comment.