Updated unit tests
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2022-02-23 12:07:05 +01:00
parent 0d8dffac30
commit 9d298c2a5e
5 changed files with 58 additions and 34 deletions

View File

@ -35,18 +35,16 @@ module.exports = Self => {
}; };
async function sendMessage(sender, channel, message) { async function sendMessage(sender, channel, message) {
/* if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
return new Promise(resolve => { return new Promise(resolve => {
return resolve({ return resolve({
body: JSON.stringify( statusCode: 200,
{statusCode: 200, message: 'Fake notification sent'} message: 'Fake notification sent'
)
}); });
}); });
} }
*/
const login = await Self.getServiceAuth();
const login = await Self.getServiceAuth();
const avatar = `${login.host}/avatar/${sender.name}`; const avatar = `${login.host}/avatar/${sender.name}`;
const options = { const options = {

View File

@ -44,7 +44,7 @@ module.exports = Self => {
if (!recipient) if (!recipient)
throw new Error(`Could not send message "${message}" to worker id ${recipientId} from user ${userId}`); throw new Error(`Could not send message "${message}" to worker id ${recipientId} from user ${userId}`);
const {data} = await getUserStatus(recipient.name); const {data} = await Self.getUserStatus(recipient.name);
if (data) { if (data) {
if (data.status === 'offline') { if (data.status === 'offline') {
// Send message to department room // Send message to department room
@ -69,7 +69,17 @@ module.exports = Self => {
* @param {string} username - The recipient user name * @param {string} username - The recipient user name
* @return {Promise} - The request promise * @return {Promise} - The request promise
*/ */
async function getUserStatus(username) { Self.getUserStatus = async function getUserStatus(username) {
if (process.env.NODE_ENV !== 'production') {
return new Promise(resolve => {
return resolve({
data: {
status: 'online'
}
});
});
}
const login = await Self.getServiceAuth(); const login = await Self.getServiceAuth();
const options = { const options = {
@ -81,5 +91,5 @@ module.exports = Self => {
}; };
return axios.get(`${login.api}/users.getStatus`, options); return axios.get(`${login.api}/users.getStatus`, options);
} };
}; };

View File

@ -1,46 +1,62 @@
const app = require('vn-loopback/server/server'); const models = require('vn-loopback/server/server').models;
describe('Chat sendCheckingPresence()', () => { describe('Chat sendCheckingPresence()', () => {
const today = new Date(); const today = new Date();
today.setHours(6, 0); today.setHours(6, 0);
const ctx = {req: {accessToken: {userId: 1}}}; const ctx = {req: {accessToken: {userId: 1}}};
const chatModel = app.models.Chat; const chatModel = models.Chat;
const departmentId = 23; const departmentId = 23;
const workerId = 1107; const workerId = 1107;
it(`should call send() method with the worker name if he's currently working then return a response`, async() => { it(`should call to send() method with "@HankPym" as recipient argument`, async() => {
spyOn(chatModel, 'send').and.callThrough(); spyOn(chatModel, 'send').and.callThrough();
spyOn(chatModel, 'getUserStatus').and.returnValue(
const timeEntry = await app.models.WorkerTimeControl.create({ new Promise(resolve => {
userFk: workerId, return resolve({
timed: today, data: {
manual: false, status: 'online'
direction: 'in' }
}); });
})
);
const response = await chatModel.sendCheckingPresence(ctx, workerId, 'I changed something'); const response = await chatModel.sendCheckingPresence(ctx, workerId, 'I changed something');
expect(response.statusCode).toEqual(200); expect(response.statusCode).toEqual(200);
expect(response.message).toEqual('Fake notification sent'); expect(response.message).toEqual('Fake notification sent');
expect(chatModel.send).toHaveBeenCalledWith(ctx, '@HankPym', 'I changed something'); expect(chatModel.send).toHaveBeenCalledWith(ctx, '@HankPym', 'I changed something');
// restores
await app.models.WorkerTimeControl.destroyById(timeEntry.id);
}); });
it(`should call to send() method with the worker department channel if he's not currently working then return a response`, async() => { it(`should call to send() method with "#cooler" as recipient argument`, async() => {
spyOn(chatModel, 'send').and.callThrough(); spyOn(chatModel, 'send').and.callThrough();
spyOn(chatModel, 'getUserStatus').and.returnValue(
new Promise(resolve => {
return resolve({
data: {
status: 'offline'
}
});
})
);
const department = await app.models.Department.findById(departmentId); const tx = await models.Claim.beginTransaction({});
await department.updateAttribute('chatName', 'cooler');
const response = await chatModel.sendCheckingPresence(ctx, workerId, 'I changed something'); try {
const options = {transaction: tx};
expect(response.statusCode).toEqual(200); const department = await models.Department.findById(departmentId, null, options);
expect(response.message).toEqual('Fake notification sent'); await department.updateAttribute('chatName', 'cooler');
expect(chatModel.send).toHaveBeenCalledWith(ctx, '#cooler', '@HankPym ➔ I changed something');
// restores const response = await chatModel.sendCheckingPresence(ctx, workerId, 'I changed something');
await department.updateAttribute('chatName', null);
expect(response.statusCode).toEqual(200);
expect(response.message).toEqual('Fake notification sent');
expect(chatModel.send).toHaveBeenCalledWith(ctx, '#cooler', '@HankPym ➔ I changed something');
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
}); });
}); });

View File

@ -57,7 +57,7 @@ describe('Claim createFromSales()', () => {
const todayMinusEightDays = new Date(); const todayMinusEightDays = new Date();
todayMinusEightDays.setDate(todayMinusEightDays.getDate() - 8); todayMinusEightDays.setDate(todayMinusEightDays.getDate() - 8);
const ticket = await models.Ticket.findById(ticketId, options); const ticket = await models.Ticket.findById(ticketId, null, options);
await ticket.updateAttribute('landed', todayMinusEightDays, options); await ticket.updateAttribute('landed', todayMinusEightDays, options);
const claim = await models.Claim.createFromSales(ctx, ticketId, newSale, options); const claim = await models.Claim.createFromSales(ctx, ticketId, newSale, options);
@ -88,7 +88,7 @@ describe('Claim createFromSales()', () => {
const todayMinusEightDays = new Date(); const todayMinusEightDays = new Date();
todayMinusEightDays.setDate(todayMinusEightDays.getDate() - 8); todayMinusEightDays.setDate(todayMinusEightDays.getDate() - 8);
const ticket = await models.Ticket.findById(ticketId, options); const ticket = await models.Ticket.findById(ticketId, null, options);
await ticket.updateAttribute('landed', todayMinusEightDays, options); await ticket.updateAttribute('landed', todayMinusEightDays, options);
await models.Claim.createFromSales(ctx, ticketId, newSale, options); await models.Claim.createFromSales(ctx, ticketId, newSale, options);

View File

@ -35,7 +35,7 @@ describe('Client updateFiscalData', () => {
try { try {
const options = {transaction: tx}; const options = {transaction: tx};
const client = await models.Client.findById(clientId, options); const client = await models.Client.findById(clientId, null, options);
await client.updateAttribute('isTaxDataChecked', false, options); await client.updateAttribute('isTaxDataChecked', false, options);
const ctx = {req: {accessToken: {userId: salesAssistantId}}}; const ctx = {req: {accessToken: {userId: salesAssistantId}}};