From 12bfb8ca7743f753fd455abf68bff4406988e08b Mon Sep 17 00:00:00 2001 From: guillermo Date: Tue, 23 Jan 2024 11:05:39 +0100 Subject: [PATCH] feat: #6184 Added tests and process.env.IS_SPEC_RUNNING --- back/tests.js | 3 +- db/dump/fixtures.sql | 11 +++- .../back/methods/route/downloadCmrsZip.js | 6 +- .../back/methods/route/generateCmrPdf.js | 58 ------------------- .../route/specs/downloadCmrsZip.spec.js | 18 ++++++ .../back/methods/route/specs/saveCmr.spec.js | 24 ++++++++ modules/route/back/models/route.js | 1 - modules/ticket/back/methods/ticket/saveCmr.js | 3 +- print/core/cluster.js | 2 +- 9 files changed, 61 insertions(+), 65 deletions(-) delete mode 100644 modules/route/back/methods/route/generateCmrPdf.js create mode 100644 modules/route/back/methods/route/specs/downloadCmrsZip.spec.js create mode 100644 modules/route/back/methods/route/specs/saveCmr.spec.js diff --git a/back/tests.js b/back/tests.js index 2678f6744..19738acfb 100644 --- a/back/tests.js +++ b/back/tests.js @@ -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'); } diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index b243692bb..ba5e0d887 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -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; diff --git a/modules/route/back/methods/route/downloadCmrsZip.js b/modules/route/back/methods/route/downloadCmrsZip.js index 21d10d6e9..ccbb57d87 100644 --- a/modules/route/back/methods/route/downloadCmrsZip.js +++ b/modules/route/back/methods/route/downloadCmrsZip.js @@ -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}); diff --git a/modules/route/back/methods/route/generateCmrPdf.js b/modules/route/back/methods/route/generateCmrPdf.js deleted file mode 100644 index 56b8f0f2f..000000000 --- a/modules/route/back/methods/route/generateCmrPdf.js +++ /dev/null @@ -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; - }; -}; diff --git a/modules/route/back/methods/route/specs/downloadCmrsZip.spec.js b/modules/route/back/methods/route/specs/downloadCmrsZip.spec.js new file mode 100644 index 000000000..ba5da9bde --- /dev/null +++ b/modules/route/back/methods/route/specs/downloadCmrsZip.spec.js @@ -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); + }); +}); diff --git a/modules/route/back/methods/route/specs/saveCmr.spec.js b/modules/route/back/methods/route/specs/saveCmr.spec.js new file mode 100644 index 000000000..2f99723fe --- /dev/null +++ b/modules/route/back/methods/route/specs/saveCmr.spec.js @@ -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(); + }); +}); diff --git a/modules/route/back/models/route.js b/modules/route/back/models/route.js index 9777aa8d8..9b5f3564f 100644 --- a/modules/route/back/models/route.js +++ b/modules/route/back/models/route.js @@ -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); diff --git a/modules/ticket/back/methods/ticket/saveCmr.js b/modules/ticket/back/methods/ticket/saveCmr.js index 15f9b3fad..9de84a1fa 100644 --- a/modules/ticket/back/methods/ticket/saveCmr.js +++ b/modules/ticket/back/methods/ticket/saveCmr.js @@ -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, diff --git a/print/core/cluster.js b/print/core/cluster.js index a75c4cf24..869907a7d 100644 --- a/print/core/cluster.js +++ b/print/core/cluster.js @@ -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')); }); } };