feat: #6184 Added tests and process.env.IS_SPEC_RUNNING

This commit is contained in:
Guillermo Bonet 2024-01-23 11:05:39 +01:00
parent 6701f70a33
commit 12bfb8ca77
9 changed files with 61 additions and 65 deletions

View File

@ -74,10 +74,11 @@ async function test() {
spec_files: backSpecs,
helpers: [],
});
process.env.IS_SPEC_RUNNING = true;
await jasmine.execute();
if (app) await app.disconnect();
if (container) await container.rm();
// eslint-disable-next-line no-console
console.log('App disconnected & container removed');
}

View File

@ -2384,7 +2384,7 @@ INSERT INTO `vn`.`dmsType`(`id`, `name`, `path`, `readRoleFk`, `writeRoleFk`, `c
(14, 'Ticket', 'tickets', 1, 1, 'ticket'),
(15, 'Presupuestos', 'Presupuestos', NULL, NULL, 'budgets'),
(16, 'Logistica', 'logistica', NULL, NULL, 'logistics'),
(17, 'cmr', 'cmr', NULL, NULL, 'cmr'),
(17, 'cmr', 'cmr', 1, 1, 'cmr'),
(18, 'dua', 'dua', NULL, NULL, 'dua'),
(19, 'inmovilizado', 'inmovilizado', NULL, NULL, 'fixedAssets'),
(20, 'Reclamación', 'reclamacion', 1, 1, 'claim');
@ -3043,3 +3043,12 @@ INSERT INTO `vn`.`clientSms` (`id`, `clientFk`, `smsFk`, `ticketFk`)
(4, 1103, 4, 32),
(13, 1101, 1, NULL),
(14, 1101, 4, 27);
INSERT INTO `vn`.`cmr` (id,truckPlate,observations,senderInstruccions,paymentInstruccions,specialAgreements,companyFk,addressToFk,addressFromFk,supplierFk,packagesList,merchandiseDetail,state)
VALUES (1,'123456A','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet',442,1,2,1,'Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet'),
(2,'123456N','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet',69,3,4,2,'Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet'),
(3,'123456B','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet',567,5,6,69,'Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet');
UPDATE `vn`.`ticket` SET `cmrFk`= 1 WHERE `id`= 1;
UPDATE `vn`.`ticket` SET `cmrFk`= 2 WHERE `id`= 2;
UPDATE `vn`.`ticket` SET `cmrFk`= 3 WHERE `id`= 3;

View File

@ -43,8 +43,10 @@ module.exports = Self => {
ids = ids.split(',');
for (const id of ids) {
const data = await models.Route.generateCmrPdf(ctx, id, options);
zip.file(`${id}.pdf`, data, {binary: true});
ctx.args = ctx.args || {};
ctx.args.id = Number(id);
const data = await models.Route.cmr(ctx, myOptions);
zip.file(`${id}.pdf`, data[0], {binary: true});
}
const zipStream = zip.generateNodeStream({streamFiles: true});

View File

@ -1,58 +0,0 @@
const axios = require('axios');
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('generateCmrPdf', {
description: 'Generate a pdf of a cmr',
accessType: 'READ',
accepts: [
{
arg: 'id',
type: 'string',
description: 'The cmr',
}
],
returns: [
{
arg: 'body',
type: 'file',
root: true
}, {
arg: 'Content-Type',
type: 'string',
http: {target: 'header'}
}, {
arg: 'Content-Disposition',
type: 'string',
http: {target: 'header'}
}
],
http: {
path: '/generateCmrPdf',
verb: 'GET'
}
});
Self.generateCmrPdf = async function(ctx, id, options) {
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const baseUrl = (await Self.app.models.Url.getUrl()).replace('#!', 'api');
const response = await axios.get(
`${baseUrl}Routes/${id}/cmr`, {
...myOptions,
headers: {
Authorization: ctx.req.accessToken.id
},
responseType: 'arraybuffer',
});
if (response.headers['content-type'] !== 'application/pdf')
throw new UserError(`The response is not a PDF`);
return response.data;
};
};

View File

@ -0,0 +1,18 @@
const models = require('vn-loopback/server/server').models;
describe('route downloadCmrsZip()', () => {
it('should create a zip file with the given cmr ids', async() => {
const ctx = {
req: {
getLocale: () => {
return 'en';
},
accessToken: {userId: 9}
}
};
let cmrs = '1,2';
let result = await models.Route.downloadCmrsZip(ctx, cmrs);
expect(result.length).toBeGreaterThanOrEqual(1);
});
});

View File

@ -0,0 +1,24 @@
const models = require('vn-loopback/server/server').models;
fdescribe('Ticket saveCmr()', () => {
let ctx = {req: {
accessToken: {userId: 9}
}};
it(`should throw error if the cmr can't save`, async() => {
const tx = await models.Route.beginTransaction({});
let error;
try {
const options = {transaction: tx};
const ticket = [1];
await models.Route.saveCmr(ctx, ticket, options);
await tx.rollback();
} catch (e) {
error = e;
await tx.rollback();
}
expect(error).toBeDefined();
});
});

View File

@ -17,7 +17,6 @@ module.exports = Self => {
require('../methods/route/cmr')(Self);
require('../methods/route/getExternalCmrs')(Self);
require('../methods/route/downloadCmrsZip')(Self);
require('../methods/route/generateCmrPdf')(Self);
require('../methods/route/getExpeditionSummary')(Self);
require('../methods/route/getByWorker')(Self);

View File

@ -54,7 +54,8 @@ module.exports = Self => {
}, myOptions);
if (!hasDmsCmr?.dms()) {
const response = await models.Route.generateCmrPdf(ctx, ticket.cmrFk.toString(), myOptions);
ctx.args.id = ticket.cmrFk;
const response = await models.Route.cmr(ctx, myOptions);
const pdfStream = Readable.from(Buffer.from(response));
const data = {
workerFk: ctx.req.accessToken.userId,

View File

@ -35,7 +35,7 @@ module.exports = {
logger.error(`[Print] => ${err.message}`);
});
cluster.on('queue', () => logger.info('Printing task initialized by pool'));
cluster.on('queue', () => !process.env.IS_SPEC_RUNNING && logger.info('Printing task initialized by pool'));
});
}
};