2021-02-01 08:29:47 +00:00
|
|
|
const app = require('vn-loopback/server/server');
|
|
|
|
|
|
|
|
describe('Chat notifyIssue()', () => {
|
|
|
|
const ctx = {req: {accessToken: {userId: 1}}};
|
|
|
|
ctx.req.__ = value => {
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
const chatModel = app.models.Chat;
|
2021-04-12 05:48:29 +00:00
|
|
|
const osTicketModel = app.models.OsTicket;
|
2021-02-01 08:29:47 +00:00
|
|
|
const departmentId = 31;
|
|
|
|
|
2021-02-02 07:15:20 +00:00
|
|
|
it(`should not call to the send() method and neither return a response`, async() => {
|
|
|
|
spyOn(chatModel, 'send').and.callThrough();
|
2021-04-12 05:48:29 +00:00
|
|
|
spyOn(osTicketModel, 'rawSql').and.returnValue([]);
|
2021-02-02 07:15:20 +00:00
|
|
|
|
|
|
|
const response = await chatModel.notifyIssues(ctx);
|
|
|
|
|
|
|
|
expect(chatModel.send).not.toHaveBeenCalled();
|
|
|
|
expect(response).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`should return a response calling the send() method`, async() => {
|
2021-02-01 08:29:47 +00:00
|
|
|
spyOn(chatModel, 'send').and.callThrough();
|
2021-04-12 05:48:29 +00:00
|
|
|
spyOn(osTicketModel, 'rawSql').and.returnValue([{
|
|
|
|
id: 1,
|
|
|
|
number: '00001',
|
|
|
|
username: 'batman',
|
|
|
|
subject: 'Issue title'}
|
|
|
|
]);
|
2021-04-12 20:25:55 +00:00
|
|
|
const expectedMessage = `@all ➔ There's a new urgent ticket:\r\n[ID: *00001* - Issue title (@batman)](https://cau.verdnatura.es/scp/tickets.php?id=1)`;
|
2021-02-01 08:29:47 +00:00
|
|
|
|
|
|
|
const department = await app.models.Department.findById(departmentId);
|
|
|
|
let orgChatName = department.chatName;
|
|
|
|
await department.updateAttribute('chatName', 'IT');
|
|
|
|
|
|
|
|
const response = await chatModel.notifyIssues(ctx);
|
|
|
|
|
|
|
|
expect(response.statusCode).toEqual(200);
|
|
|
|
expect(response.message).toEqual('Fake notification sent');
|
2021-04-12 05:48:29 +00:00
|
|
|
expect(chatModel.send).toHaveBeenCalledWith(ctx, '#IT', expectedMessage);
|
2021-02-01 08:29:47 +00:00
|
|
|
|
|
|
|
// restores
|
|
|
|
await department.updateAttribute('chatName', orgChatName);
|
|
|
|
});
|
|
|
|
});
|