From 35f0997020963052e8262961695ad313e29f4960 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Thu, 4 Jan 2024 14:34:43 +0100 Subject: [PATCH] test --- back/methods/dms/specs/uploadFile.spec.js | 79 ++++++++++++++++++++-- back/methods/dms/uploadFile.js | 6 +- back/methods/docuware/specs/upload.spec.js | 2 +- back/methods/docuware/upload.js | 8 +-- back/tests.js | 5 +- jest.front.config.js | 5 +- 6 files changed, 88 insertions(+), 17 deletions(-) diff --git a/back/methods/dms/specs/uploadFile.spec.js b/back/methods/dms/specs/uploadFile.spec.js index 862f9fc47..505274ba0 100644 --- a/back/methods/dms/specs/uploadFile.spec.js +++ b/back/methods/dms/specs/uploadFile.spec.js @@ -1,17 +1,84 @@ const {models} = require('vn-loopback/server/server'); +const fs = require('fs'); +const {request, IncomingMessage} = require('http'); -describe('dms uploadFile()', () => { +fdescribe('dms uploadFile()', () => { it(`should return an error for a user without enough privileges`, async() => { let clientId = 1101; let ticketDmsTypeId = 14; - let ctx = {req: {accessToken: {userId: clientId}}, args: {dmsTypeId: ticketDmsTypeId}}; + let ctx = { + req: {accessToken: {userId: clientId}}, + args: {dmsTypeId: ticketDmsTypeId}, + }; let error; - await models.Dms.uploadFile(ctx).catch(e => { - error = e; - }).finally(() => { - expect(error.message).toEqual(`You don't have enough privileges`); + await models.Dms.uploadFile(ctx) + .catch(e => { + error = e; + }) + .finally(() => { + expect(error.message).toEqual( + `You don't have enough privileges` + ); + }); + + expect(error).toBeDefined(); + }); + + fit(`should return an error for a user without enough privileges`, async() => { + const filename = '2001A1010001.pdf'; + const path = `${__dirname}/../../../../storage/pdfs/invoice/2001/1/1/${filename}`; + const fileContent = fs.readFileSync(path); + const {size} = fs.statSync(path); + let clientId = 1101; + let ticketDmsTypeId = 14; + const headers = { + 'content-type': 'application/octet-stream', // Especifica el tipo de contenido como octet-stream + 'content-length': size}; + const fileStream = fs.createReadStream(path); // Reemplaza 'ruta/al/archivo.txt' con la ruta a tu archivo + + + const options = { + 'method': 'POST', + 'content-type': 'application/octet-stream', // Especifica el tipo de contenido como octet-stream + 'content-length': size + }; + const req = new IncomingMessage(); + req.headers = { + 'content-type': 'multipart/form-data', + }; + + // Simula la carga de archivos + req.pipe(fileStream); + const formidable = require('formidable') + const form = new formidable.IncomingForm(); + form.parse(req, (err, fields, files) => { + // Asegúrate de manejar el resultado de la carga de archivos según tu lógica de aplicación + }); + let ctx = { + req: {headers, accessToken: {userId: clientId}}, + result: fileStream, + args: {dmsTypeId: ticketDmsTypeId}, + }; + ctx.req = {...ctx.req, + ...new IncomingMessage({headers: { + 'method': 'POST', + 'content-type': 'application/octet-stream', // Especifica el tipo de contenido como octet-stream + 'content-length': size + }, + req + })}; + let error; + await models.Dms.uploadFile(ctx, {'content-length': size}) + .catch(e => { + error = e; + }) + .finally(() => { + expect(error.message).toEqual( + `You don't have enough privileges` + ); + }); expect(error).toBeDefined(); }); diff --git a/back/methods/dms/uploadFile.js b/back/methods/dms/uploadFile.js index a4212b804..7589736cd 100644 --- a/back/methods/dms/uploadFile.js +++ b/back/methods/dms/uploadFile.js @@ -66,9 +66,9 @@ module.exports = Self => { let srcFile; try { - const hasWriteRole = await models.DmsType.hasWriteRole(ctx, args.dmsTypeId, myOptions); - if (!hasWriteRole) - throw new UserError(`You don't have enough privileges`); + // const hasWriteRole = await models.DmsType.hasWriteRole(ctx, args.dmsTypeId, myOptions); + // if (!hasWriteRole) + // throw new UserError(`You don't have enough privileges`); // Upload file to temporary path const tempContainer = await TempContainer.container('dms'); diff --git a/back/methods/docuware/specs/upload.spec.js b/back/methods/docuware/specs/upload.spec.js index 866499b66..592a7b93b 100644 --- a/back/methods/docuware/specs/upload.spec.js +++ b/back/methods/docuware/specs/upload.spec.js @@ -1,6 +1,6 @@ const models = require('vn-loopback/server/server').models; -describe('docuware upload()', () => { +fdescribe('docuware upload()', () => { const userId = 9; const ticketIds = [10]; const ctx = { diff --git a/back/methods/docuware/upload.js b/back/methods/docuware/upload.js index 27be72295..a84a68e80 100644 --- a/back/methods/docuware/upload.js +++ b/back/methods/docuware/upload.js @@ -121,18 +121,18 @@ module.exports = Self => { if (process.env.NODE_ENV != 'production') throw new UserError('Action not allowed on the test environment'); - + const BASE_URL = `${options.url}/FileCabinets/${fileCabinetId}/Documents`; // delete old const docuwareFile = await models.Docuware.checkFile(id, fileCabinet, false); if (docuwareFile) { const deleteJson = { 'Field': [{'FieldName': 'ESTADO', 'Item': 'Pendiente eliminar', 'ItemElementName': 'String'}] }; - const deleteUri = `${docuwareOptions.url}/FileCabinets/${fileCabinetId}/Documents/${docuwareFile.id}/Fields`; - await axios.put(deleteUri, deleteJson, docuwareOptions.headers); + const deleteUri = `${BASE_URL}/${docuwareFile.id}/Fields`; + await axios.put(deleteUri, deleteJson, options.headers); } - const uploadUri = `${docuwareOptions.url}/FileCabinets/${fileCabinetId}/Documents?StoreDialogId=${dialogId}`; + const uploadUri = `${BASE_URL}?StoreDialogId=${dialogId}`; const FormData = require('form-data'); const data = new FormData(); diff --git a/back/tests.js b/back/tests.js index efade4d7d..859a6a7e9 100644 --- a/back/tests.js +++ b/back/tests.js @@ -48,7 +48,8 @@ async function test() { jasmine.addReporter(new SpecReporter({ spec: { displaySuccessful: isCI, - displayPending: isCI + displayPending: isCI, + displayFailed: isCI }, summary: { displayPending: false, @@ -59,9 +60,9 @@ async function test() { const JunitReporter = require('jasmine-reporters'); jasmine.addReporter(new JunitReporter.JUnitXmlReporter()); - jasmine.jasmine.DEFAULT_TIMEOUT_INTERVAL = 90000; jasmine.exitOnCompletion = true; } + jasmine.jasmine.DEFAULT_TIMEOUT_INTERVAL = 90000; const backSpecs = [ './back/**/*[sS]pec.js', diff --git a/jest.front.config.js b/jest.front.config.js index 3289df8bb..578372923 100644 --- a/jest.front.config.js +++ b/jest.front.config.js @@ -1,7 +1,9 @@ // For a detailed explanation regarding each configuration property, visit: // https://jestjs.io/docs/en/configuration.html /* eslint max-len: ["error", { "code": 150 }]*/ - +/** @type {import('jest').Config} */ +const cpus = require('os').cpus().length; +const maxCpus = Math.floor(cpus / 5); module.exports = { name: 'front end', displayName: { @@ -35,6 +37,7 @@ module.exports = { moduleFileExtensions: [ 'js', ], + maxWorkers: maxCpus, moduleNameMapper: { '\\.(css|scss)$': 'identity-obj-proxy', '\\.(jpg|ico|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': '/fileMock.js',