Skip to content

Commit

Permalink
Chore: Fix Omnichannel E2E tests not running (#26092)
Browse files Browse the repository at this point in the history
  • Loading branch information
murtaza98 authored and weslley543 committed Jul 19, 2022
1 parent fcd75da commit fb8f342
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 441 deletions.
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
3 changes: 0 additions & 3 deletions apps/meteor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@
"deploy": "npm run build && pm2 startOrRestart pm2.json",
"coverage": "nyc -r html mocha --config ./.mocharc.js",
"test:e2e": "playwright test",
<<<<<<< HEAD
"test:e2e:nyc": "nyc report --reporter=text-summary --reporter=lcov",
=======
>>>>>>> Chore: Tests refactor pageobjects (#26245)
"testapi": "mocha --config ./.mocharc.api.js",
"testunit": "npm run .testunit:definition && npm run .testunit:client && npm run .testunit:server",
".testunit:server": "mocha --config ./.mocharc.js",
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 });
}

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

0 comments on commit fb8f342

Please sign in to comment.