Skip to content

Commit

Permalink
test: Omnichannel Users & Departments (#30820)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman authored Nov 1, 2023
1 parent 542a6fe commit ea8f99c
Show file tree
Hide file tree
Showing 5 changed files with 540 additions and 51 deletions.
4 changes: 2 additions & 2 deletions apps/meteor/tests/data/livechat/units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ export const createMonitor = async (username: string): Promise<{ _id: string; us
});
};

export const createUnit = async (monitorId: string, username: string, departmentIds: string[]): Promise<IOmnichannelBusinessUnit> => {
export const createUnit = async (monitorId: string, username: string, departmentIds: string[], name?: string): Promise<IOmnichannelBusinessUnit> => {
return new Promise((resolve, reject) => {
request
.post(methodCall(`livechat:saveUnit`))
.set(credentials)
.send({
message: JSON.stringify({
method: 'livechat:saveUnit',
params: [null, { name: faker.person.firstName(), visibility: faker.helpers.arrayElement(['public', 'private']) }, [{ monitorId, username }], departmentIds.map((departmentId) => ({ departmentId }))],
params: [null, { name: name || faker.person.firstName(), visibility: faker.helpers.arrayElement(['public', 'private']) }, [{ monitorId, username }], departmentIds.map((departmentId) => ({ departmentId }))],
id: '101',
msg: 'method',
}),
Expand Down
21 changes: 21 additions & 0 deletions apps/meteor/tests/end-to-end/api/livechat/01-agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,27 @@ describe('LIVECHAT - Agents', function () {
// cleanup
await deleteUser(user);
});

it('should properly create a manager', async () => {
const user: IUser = await createUser();
await request
.post(api('livechat/users/manager'))
.set(credentials)
.send({
username: user.username,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res: Response) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('user');
expect(res.body.user).to.have.property('_id');
expect(res.body.user).to.have.property('username');
});

// cleanup
await deleteUser(user);
});
});

describe('GET livechat/users/:type/:_id', () => {
Expand Down
232 changes: 229 additions & 3 deletions apps/meteor/tests/end-to-end/api/livechat/10-departments.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { faker } from '@faker-js/faker';
import type { ILivechatDepartment } from '@rocket.chat/core-typings';
import { expect } from 'chai';
import { before, describe, it } from 'mocha';
import { before, describe, it, after } from 'mocha';
import type { Response } from 'supertest';

import { getCredentials, api, request, credentials } from '../../../data/api-data';
Expand All @@ -14,10 +15,71 @@ import {
getLivechatRoomInfo,
} from '../../../data/livechat/rooms';
import { createMonitor, createUnit } from '../../../data/livechat/units';
import { updatePermission, updateSetting } from '../../../data/permissions.helper';
import { restorePermissionToRoles, updatePermission, updateSetting } from '../../../data/permissions.helper';
import { createUser, deleteUser } from '../../../data/users.helper';
import { IS_EE } from '../../../e2e/config/constants';

(IS_EE ? describe.skip : describe)('LIVECHAT - Departments[CE]', () => {
before((done) => getCredentials(done));

before(async () => {
await updateSetting('Livechat_enabled', true);
await restorePermissionToRoles('view-livechat-manager');
await createAgent();
await makeAgentAvailable();
await updateSetting('Omnichannel_enable_department_removal', true);
});

// Remove departments that may have been created before
before(async () => {
const { body } = await request.get(api('livechat/department')).set(credentials).expect('Content-Type', 'application/json').expect(200);

for await (const department of body.departments) {
await deleteDepartment(department._id);
}
});

let departmentId: string;

after(async () => {
await deleteDepartment(departmentId);
});

it('should create a new department', async () => {
const { body } = await request
.post(api('livechat/department'))
.set(credentials)
.send({ department: { name: 'Test', enabled: true, showOnOfflineForm: true, showOnRegistration: true, email: 'bla@bla' } })
.expect('Content-Type', 'application/json')
.expect(200);
expect(body).to.have.property('success', true);
expect(body).to.have.property('department');
expect(body.department).to.have.property('_id');
expect(body.department).to.have.property('name', 'Test');
expect(body.department).to.have.property('enabled', true);
expect(body.department).to.have.property('showOnOfflineForm', true);
expect(body.department).to.have.property('showOnRegistration', true);
departmentId = body.department._id;
});

it('should not create a 2nd department', () => {
return request
.post(api('livechat/department'))
.set(credentials)
.send({ department: { name: 'Test', enabled: true, showOnOfflineForm: true, showOnRegistration: true, email: 'bla@bla' } })
.expect('Content-Type', 'application/json')
.expect(400);
});

it('should return a list of 1 department', async () => {
const { body } = await request.get(api('livechat/department')).set(credentials).expect('Content-Type', 'application/json').expect(200);
expect(body).to.have.property('success', true);
expect(body).to.have.property('departments');
expect(body.departments).to.be.an('array');
expect(body.departments).to.have.lengthOf(1);
});
});

(IS_EE ? describe : describe.skip)('LIVECHAT - Departments', () => {
before((done) => getCredentials(done));

Expand Down Expand Up @@ -51,6 +113,72 @@ import { IS_EE } from '../../../e2e/config/constants';
expect(res.body.departments).to.have.length.of.at.least(0);
});
});

it('should reject invalid pagination params', async () => {
await request
.get(api('livechat/department'))
.set(credentials)
.query({ count: 'invalid' })
.expect('Content-Type', 'application/json')
.expect(400);
});

it('should return a list of paginated departments', async () => {
await request
.get(api('livechat/department'))
.set(credentials)
.query({ count: 1, offset: 0 })
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res: Response) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('departments');
expect(res.body.departments).to.be.an('array');
expect(res.body.departments).to.have.lengthOf(1);
});
});

it('should sort list alphabetically following mongodb default sort (no collation)', async () => {
const department1 = await createDepartment(undefined, 'A test');
const department2 = await createDepartment(undefined, 'a test');
await request
.get(api('livechat/department'))
.set(credentials)
.query({ count: 2, offset: 0, text: 'test' })
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res: Response) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('departments');
expect(res.body.departments).to.be.an('array');
expect(res.body.departments).to.have.lengthOf(2);
expect(res.body.departments[0]).to.have.property('_id', department1._id);
expect(res.body.departments[1]).to.have.property('_id', department2._id);
});
await deleteDepartment(department1._id);
await deleteDepartment(department2._id);
});

it('should return a list of departments matching name', async () => {
const department1 = await createDepartment(undefined, 'A test 123');
const department2 = await createDepartment(undefined, 'a test 456');
await request
.get(api('livechat/department'))
.set(credentials)
.query({ count: 2, offset: 0, text: 'A test 123' })
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res: Response) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('departments');
expect(res.body.departments).to.be.an('array');
expect(res.body.departments).to.have.lengthOf(1);
expect(res.body.departments[0]).to.have.property('_id', department1._id);
expect(res.body.departments.find((dept: ILivechatDepartment) => dept._id === department2._id)).to.be.undefined;
});
await deleteDepartment(department1._id);
await deleteDepartment(department2._id);
});
});

describe('POST livechat/departments', () => {
Expand All @@ -76,8 +204,62 @@ import { IS_EE } from '../../../e2e/config/constants';
.expect(400);
});

it('should return an error if requestTagsBeforeClosing is true but no tags are provided', async () => {
await request
.post(api('livechat/department'))
.set(credentials)
.send({
department: {
name: 'Test',
enabled: true,
showOnOfflineForm: true,
showOnRegistration: true,
email: 'bla@bla',
requestTagBeforeClosingChat: true,
},
})
.expect('Content-Type', 'application/json')
.expect(400);
});

it('should return an error if requestTagsBeforeClosing is true but tags are not an array', async () => {
await request
.post(api('livechat/department'))
.set(credentials)
.send({
department: {
name: 'Test',
enabled: true,
showOnOfflineForm: true,
showOnRegistration: true,
email: 'bla@bla',
requestTagBeforeClosingChat: true,
chatClosingTags: 'not an array',
},
})
.expect('Content-Type', 'application/json')
.expect(400);
});

it('should return an error if fallbackForwardDepartment is present but is not a department id', async () => {
await request
.post(api('livechat/department'))
.set(credentials)
.send({
department: {
name: 'Test',
enabled: true,
showOnOfflineForm: true,
showOnRegistration: true,
email: 'bla@bla',
fallbackForwardDepartment: 'not a department id',
},
})
.expect('Content-Type', 'application/json')
.expect(400);
});

it('should create a new department', async () => {
await updatePermission('manage-livechat-departments', ['admin']);
const { body } = await request
.post(api('livechat/department'))
.set(credentials)
Expand All @@ -93,6 +275,28 @@ import { IS_EE } from '../../../e2e/config/constants';
expect(body.department).to.have.property('showOnRegistration', true);
await deleteDepartment(body.department._id);
});

it('should create a new disabled department', async () => {
const { body } = await request
.post(api('livechat/department'))
.set(credentials)
.send({
department: {
name: faker.hacker.adjective(),
enabled: false,
showOnOfflineForm: true,
showOnRegistration: true,
email: faker.internet.email(),
},
})
.expect('Content-Type', 'application/json')
.expect(200);
expect(body).to.have.property('success', true);
expect(body).to.have.property('department');
expect(body.department).to.have.property('_id');
expect(body.department).to.have.property('enabled', false);
await deleteDepartment(body.department._id);
});
});

describe('GET livechat/department/:_id', () => {
Expand Down Expand Up @@ -139,6 +343,28 @@ import { IS_EE } from '../../../e2e/config/constants';
});
});

describe('PUT livechat/departments/:_id', () => {
it('should return an error if fallbackForwardDepartment points to same department', async () => {
const department = await createDepartment();
await request
.put(api(`livechat/department/${department._id}`))
.set(credentials)
.send({
department: {
name: faker.hacker.adjective(),
enabled: true,
showOnOfflineForm: true,
showOnRegistration: true,
email: faker.internet.email(),
fallbackForwardDepartment: department._id,
},
})
.expect('Content-Type', 'application/json')
.expect(400);
await deleteDepartment(department._id);
});
});

describe('DELETE livechat/department/:_id', () => {
it('should return unauthorized error when the user does not have the necessary permission', async () => {
await updatePermission('manage-livechat-departments', []);
Expand Down
Loading

0 comments on commit ea8f99c

Please sign in to comment.