Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Fix Omnichannel E2E tests not running #26092

Merged
merged 9 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/meteor/.mocharc.api.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ module.exports = {
file: 'tests/end-to-end/teardown.js',
spec: [
'tests/unit/app/api/server/v1/**/*.spec.ts',
'tests/end-to-end/api/*.js',
'tests/end-to-end/api/*.ts',
'tests/end-to-end/api/**/*.js',
'tests/end-to-end/api/**/*.ts',
'tests/end-to-end/apps/*.js',
'tests/end-to-end/apps/*.ts',
],
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/server/models/raw/LivechatVisitors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export class LivechatVisitorsRaw extends BaseRaw<ILivechatVisitor> implements IL
}

removeById(_id: string): Promise<DeleteResult> {
return this.removeById(_id);
return this.deleteOne({ _id });
Copy link
Member

@sampaiodiego sampaiodiego Jul 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this being used? I mean, glad you fixed since it is an infinite recursive loop, if this is being used somewhere how none found it before? 🙈

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I believe this is getting used on Livechat when visitor requests to delete their data 🙈

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used as murtaza said. But as that feature is not that used 🙈 probably no one found it. (This single line could easily be a regression tho)

}

saveGuestEmailPhoneById(_id: string, emails: string[], phones: string[]): Promise<UpdateResult | Document | void> {
Expand Down
67 changes: 57 additions & 10 deletions apps/meteor/tests/data/livechat/rooms.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { api, credentials, request } from '../api-data';
import { api, credentials, methodCall, request } from '../api-data';
import { adminUsername } from '../user';

export const createLivechatRoom = (visitorToken) =>
Expand All @@ -10,8 +10,11 @@ export const createLivechatRoom = (visitorToken) =>
});

export const createVisitor = () =>
new Promise((resolve) => {
request.get(api('livechat/visitor/iNKE8a6k6cjbqWhWd')).end((err, res) => {
new Promise((resolve, reject) => {
const token = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
const email = `${token}@${token}.com`;
const phone = `${Math.floor(Math.random() * 10000000000)}`;
request.get(api(`livechat/visitor/${token}`)).end((err, res) => {
if (!err && res && res.body && res.body.visitor) {
return resolve(res.body.visitor);
}
Expand All @@ -21,36 +24,80 @@ export const createVisitor = () =>
.send({
visitor: {
name: `Visitor ${Date.now()}`,
email: 'visitor@rocket.chat',
token: 'iNKE8a6k6cjbqWhWd',
phone: '55 51 5555-5555',
email,
token,
phone,
customFields: [{ key: 'address', value: 'Rocket.Chat street', overwrite: true }],
},
})
.end((err, res) => {
if (err) {
return reject(err);
}
resolve(res.body.visitor);
});
});
});

export const createAgent = () =>
new Promise((resolve) => {
new Promise((resolve, reject) => {
request
.post(api('livechat/users/agent'))
.set(credentials)
.send({
username: adminUsername,
})
.end((err, res) => resolve(res.body.user));
.end((err, res) => {
if (err) {
return reject(err);
}
resolve(res.body.user);
});
});

export const createManager = () =>
new Promise((resolve) => {
new Promise((resolve, reject) => {
request
.post(api('livechat/users/manager'))
.set(credentials)
.send({
username: adminUsername,
})
.end((err, res) => resolve(res.body.user));
.end((err, res) => {
if (err) {
return reject(err);
}
resolve(res.body.user);
});
});

export const makeAgentAvailable = () =>
new Promise((resolve, reject) => {
request.post(api('users.setStatus')).set(credentials).send({
message: '',
status: 'online',
}).end((err, res) => {
if (err) {
return reject(err);
}
request
.post(methodCall('livechat/changeLivechatStatus'))
.set(credentials)
.send({
message: JSON.stringify({
method: 'livechat/changeLivechatStatus',
params: ['available'],
id: 'id',
msg: 'method',
}),
})
.end((err, res) => {
if (err) {
return reject(err);
}
resolve(res.body);
});
});

});

111 changes: 57 additions & 54 deletions apps/meteor/tests/end-to-end/api/livechat/00-rooms.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,57 @@
import { expect } from 'chai';
import { IOmnichannelRoom, IVisitor } from '@rocket.chat/core-typings';
import { Response } from 'supertest';

import { getCredentials, api, request, credentials } from '../../../data/api-data.js';
import { createVisitor, createLivechatRoom, createAgent } from '../../../data/livechat/rooms.js';
import { createVisitor, createLivechatRoom, createAgent, makeAgentAvailable } from '../../../data/livechat/rooms.js';
import { updatePermission, updateSetting } from '../../../data/permissions.helper';

describe('LIVECHAT - rooms', function () {
this.retries(0);
let visitor: IVisitor;
let room: IOmnichannelRoom;

before((done) => getCredentials(done));

before((done) => {
updateSetting('Livechat_enabled', true).then(() => {
createAgent()
.then(() => makeAgentAvailable())
.then(() => createVisitor())
.then((visitor) => createLivechatRoom(visitor.token))
.then(() => done());
.then((createdVisitor) => {
visitor = createdVisitor;
return createLivechatRoom(createdVisitor.token);
})
.then((createdRoom) => {
room = createdRoom;
done();
});
});
});

describe('livechat/rooms', () => {
it('should return an "unauthorized error" when the user does not have the necessary permission', (done) => {
updatePermission('view-livechat-manager', []).then(() => {
updatePermission('view-livechat-rooms', []).then(() => {
request
.get(api('livechat/rooms'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(403)
.expect((res) => {
.expect((res: Response) => {
expect(res.body).to.have.property('success', false);
expect(res.body.error).to.be.equal('unauthorized');
})
.end(done);
});
});
it('should return an error when the "agents" query parameter is not valid', (done) => {
updatePermission('view-livechat-manager', ['admin']).then(() => {
updatePermission('view-livechat-rooms', ['admin']).then(() => {
request
.get(api('livechat/rooms?agents=invalid'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
.expect((res: Response) => {
expect(res.body).to.have.property('success', false);
})
.end(done);
Expand All @@ -52,7 +63,7 @@ describe('LIVECHAT - rooms', function () {
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
.expect((res: Response) => {
expect(res.body).to.have.property('success', false);
})
.end(done);
Expand All @@ -63,7 +74,7 @@ describe('LIVECHAT - rooms', function () {
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
.expect((res: Response) => {
expect(res.body).to.have.property('success', false);
})
.end(done);
Expand All @@ -74,7 +85,7 @@ describe('LIVECHAT - rooms', function () {
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
.expect((res: Response) => {
expect(res.body).to.have.property('success', false);
})
.end(done);
Expand All @@ -85,7 +96,7 @@ describe('LIVECHAT - rooms', function () {
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
.expect((res: Response) => {
expect(res.body).to.have.property('success', false);
})
.end(done);
Expand All @@ -96,7 +107,7 @@ describe('LIVECHAT - rooms', function () {
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
.expect((res: Response) => {
expect(res.body).to.have.property('success', false);
})
.end(done);
Expand All @@ -107,7 +118,7 @@ describe('LIVECHAT - rooms', function () {
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
.expect((res: Response) => {
expect(res.body).to.have.property('success', false);
})
.end(done);
Expand All @@ -118,7 +129,7 @@ describe('LIVECHAT - rooms', function () {
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
.expect((res: Response) => {
expect(res.body).to.have.property('success', false);
})
.end(done);
Expand All @@ -129,7 +140,7 @@ describe('LIVECHAT - rooms', function () {
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
.expect((res: Response) => {
expect(res.body).to.have.property('success', true);
expect(res.body.rooms).to.be.an('array');
expect(res.body).to.have.property('offset');
Expand All @@ -148,7 +159,7 @@ describe('LIVECHAT - rooms', function () {
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
.expect((res: Response) => {
expect(res.body).to.have.property('success', true);
expect(res.body.rooms).to.be.an('array');
expect(res.body).to.have.property('offset');
Expand All @@ -162,11 +173,15 @@ describe('LIVECHAT - rooms', function () {
describe('livechat/room.close', () => {
it('should return an "invalid-token" error when the visitor is not found due to an invalid token', (done) => {
request
.get(api('livechat/room.close'))
.post(api('livechat/room.close'))
.set(credentials)
.send({
token: 'invalid-token',
rid: room._id,
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
.expect((res: Response) => {
expect(res.body).to.have.property('success', false);
expect(res.body.error).to.be.equal('invalid-token');
})
Expand All @@ -175,64 +190,52 @@ describe('LIVECHAT - rooms', function () {

it('should return an "invalid-room" error when the room is not found due to invalid token and/or rid', (done) => {
request
.get(api('livechat/room.close'))
.post(api('livechat/room.close'))
.set(credentials)
.send({
token: visitor.token,
rid: 'invalid-rid',
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
.expect((res: Response) => {
expect(res.body).to.have.property('success', false);
expect(res.body.error).to.be.equal('invalid-room');
})
.end(done);
});

it('should return an "room-closed" error when the room is already closed', (done) => {
it('should return both the rid and the comment of the room when the query params is all valid', (done) => {
request
.get(api('livechat/room.close'))
.post(api(`livechat/room.close`))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
expect(res.body.error).to.be.equal('room-closed');
.send({
token: visitor.token,
rid: room._id,
})
.end(done);
});

it('should return an error when the "rid" query parameter is not valid', (done) => {
request
.get(api('livechat/rooms?rid=invalid'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
.expect(200)
.expect((res: Response) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('rid');
expect(res.body).to.have.property('comment');
})
.end(done);
});

it('should return an error when the "token" query parameter is not valid', (done) => {
it('should return an "room-closed" error when the room is already closed', (done) => {
request
.get(api('livechat/rooms?token=invalid'))
.post(api('livechat/room.close'))
.set(credentials)
.send({
token: visitor.token,
rid: room._id,
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
.expect((res: Response) => {
expect(res.body).to.have.property('success', false);
})
.end(done);
});

it('should return both the rid and the comment of the room when the query params is all valid', (done) => {
request
.get(api(`livechat/room.close?rid=123&token=321`))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('rid');
expect(res.body).to.have.property('comment');
expect(res.body.error).to.be.equal('room-closed');
})
.end(done);
});
Expand Down
Loading