|
|
|
|
@ -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'; |
|
|
|
|
@ -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)); |
|
|
|
|
|
|
|
|
|
@ -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', () => { |
|
|
|
|
@ -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) |
|
|
|
|
@ -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', () => { |
|
|
|
|
@ -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', []); |
|
|
|
|
|