Skip to content
Snippets Groups Projects
Unverified Commit 2dfdbb31 authored by Murtaza Patrawala's avatar Murtaza Patrawala Committed by GitHub
Browse files

chore: improve tests for livechat/config api (#28658)

parent 713ffb3f
No related branches found
No related tags found
No related merge requests found
...@@ -10,6 +10,7 @@ import type { ...@@ -10,6 +10,7 @@ import type {
import { api, credentials, methodCall, request } from '../api-data'; import { api, credentials, methodCall, request } from '../api-data';
import { updatePermission } from '../permissions.helper'; import { updatePermission } from '../permissions.helper';
import { adminUsername } from '../user'; import { adminUsername } from '../user';
import { getRandomVisitorToken } from './users';
import type { DummyResponse } from './utils'; import type { DummyResponse } from './utils';
export const createLivechatRoom = async (visitorToken: string, extraRoomParams?: Record<string, string>): Promise<IOmnichannelRoom> => { export const createLivechatRoom = async (visitorToken: string, extraRoomParams?: Record<string, string>): Promise<IOmnichannelRoom> => {
...@@ -31,7 +32,7 @@ export const createLivechatRoom = async (visitorToken: string, extraRoomParams?: ...@@ -31,7 +32,7 @@ export const createLivechatRoom = async (visitorToken: string, extraRoomParams?:
export const createVisitor = (department?: string): Promise<ILivechatVisitor> => export const createVisitor = (department?: string): Promise<ILivechatVisitor> =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
const token = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); const token = getRandomVisitorToken();
const email = `${token}@${token}.com`; const email = `${token}@${token}.com`;
const phone = `${Math.floor(Math.random() * 10000000000)}`; const phone = `${Math.floor(Math.random() * 10000000000)}`;
request.get(api(`livechat/visitor/${token}`)).end((err: Error, res: DummyResponse<ILivechatVisitor>) => { request.get(api(`livechat/visitor/${token}`)).end((err: Error, res: DummyResponse<ILivechatVisitor>) => {
......
import faker from "@faker-js/faker";
import type { IUser } from "@rocket.chat/core-typings";
import { password } from "../user";
import { createUser, login } from "../users.helper";
import { createAgent, makeAgentAvailable } from "./rooms";
export const createBotAgent = async (): Promise<{
credentials: { 'X-Auth-Token': string; 'X-User-Id': string; };
user: IUser;
}> => {
const agent: IUser = await createUser({
roles: ['bot']
});
const createdUserCredentials = await login(agent.username, password);
await createAgent(agent.username);
await makeAgentAvailable(createdUserCredentials);
return {
credentials: createdUserCredentials,
user: agent,
};
}
export const getRandomVisitorToken = (): string => faker.random.alphaNumeric(17);
...@@ -4,8 +4,11 @@ import { expect } from 'chai'; ...@@ -4,8 +4,11 @@ import { expect } from 'chai';
import { getCredentials, api, request, credentials } from '../../../data/api-data'; import { getCredentials, api, request, credentials } from '../../../data/api-data';
import { createCustomField, deleteCustomField } from '../../../data/livechat/custom-fields'; import { createCustomField, deleteCustomField } from '../../../data/livechat/custom-fields';
import { createVisitor, createLivechatRoom } from '../../../data/livechat/rooms'; import { addOrRemoveAgentFromDepartment, createDepartmentWithAnOnlineAgent } from '../../../data/livechat/department';
import { createVisitor, createLivechatRoom, makeAgentUnavailable } from '../../../data/livechat/rooms';
import { createBotAgent, getRandomVisitorToken } from '../../../data/livechat/users';
import { updateSetting } from '../../../data/permissions.helper'; import { updateSetting } from '../../../data/permissions.helper';
import { IS_EE } from '../../../e2e/config/constants';
describe('LIVECHAT - Utils', function () { describe('LIVECHAT - Utils', function () {
this.retries(0); this.retries(0);
...@@ -102,6 +105,66 @@ describe('LIVECHAT - Utils', function () { ...@@ -102,6 +105,66 @@ describe('LIVECHAT - Utils', function () {
await deleteCustomField(customFieldName); await deleteCustomField(customFieldName);
}); });
(IS_EE ? it : it.skip)('should return online as true if there is at least one agent online', async () => {
const { department } = await createDepartmentWithAnOnlineAgent();
const { body } = await request.get(api(`livechat/config?department=${department._id}`)).set(credentials);
expect(body).to.have.property('config');
expect(body.config).to.have.property('online', true);
});
(IS_EE ? it : it.skip)('should return online as false if there is no agent online', async () => {
const { department, agent } = await createDepartmentWithAnOnlineAgent();
await makeAgentUnavailable(agent.credentials);
const { body } = await request.get(api(`livechat/config?department=${department._id}`)).set(credentials);
expect(body).to.have.property('config');
expect(body.config).to.have.property('online', false);
});
(IS_EE ? it : it.skip)('should return online as true if bot is online and there is no agent online', async () => {
await updateSetting('Livechat_assign_new_conversation_to_bot', true);
const { department, agent } = await createDepartmentWithAnOnlineAgent();
await makeAgentUnavailable(agent.credentials);
const botUser = await createBotAgent();
await addOrRemoveAgentFromDepartment(department._id, { agentId: botUser.user._id, username: botUser.user.username as string }, true);
const { body } = await request.get(api(`livechat/config?department=${department._id}`)).set(credentials);
expect(body).to.have.property('config');
await updateSetting('Livechat_assign_new_conversation_to_bot', false);
await makeAgentUnavailable(botUser.credentials);
});
it('should return a guest if there exists a guest with the same token', async () => {
const guest = await createVisitor();
const { body } = await request.get(api(`livechat/config?token=${guest.token}`)).set(credentials);
expect(body).to.have.property('config');
expect(body.config).to.have.property('guest');
expect(body.config.guest).to.have.property('name', guest.name);
});
it('should not return a guest if there exists a guest with the same token but the guest is not online', async () => {
const token = getRandomVisitorToken();
const { body } = await request.get(api(`livechat/config?token=${token}`)).set(credentials);
expect(body).to.have.property('config');
expect(body.config).to.not.have.property('guest');
});
it('should return no online room if visitor is not chatting with an agent', async () => {
const visitor = await createVisitor();
const { body } = await request.get(api(`livechat/config?token=${visitor.token}`)).set(credentials);
expect(body).to.have.property('config');
expect(body.config).to.not.have.property('room');
});
it('should return online room if visitor is already chatting with an agent', async () => {
const newVisitor = await createVisitor();
const newRoom = await createLivechatRoom(newVisitor.token);
const { body } = await request.get(api(`livechat/config?token=${newVisitor.token}`)).set(credentials);
expect(body).to.have.property('config');
expect(body.config).to.have.property('room');
expect(body.config.room).to.have.property('_id', newRoom._id);
});
}); });
describe('livechat/page.visited', () => { describe('livechat/page.visited', () => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment