Merge branch 'dev' into 4033-route
gitea/salix/pipeline/head Something is wrong with the build of this commit Details

This commit is contained in:
Joan Sanchez 2022-10-06 08:23:19 +00:00
commit 8f3e09b88f
14 changed files with 123 additions and 1731 deletions

View File

@ -41,11 +41,14 @@ async function test() {
} }
})); }));
jasmine.exitOnCompletion = false;
if (isCI) { if (isCI) {
const JunitReporter = require('jasmine-reporters'); const JunitReporter = require('jasmine-reporters');
jasmine.addReporter(new JunitReporter.JUnitXmlReporter()); jasmine.addReporter(new JunitReporter.JUnitXmlReporter());
jasmine.jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000; jasmine.jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
jasmine.exitOnCompletion = true;
} }
const backSpecs = [ const backSpecs = [
@ -60,11 +63,10 @@ async function test() {
helpers: [], helpers: [],
}); });
jasmine.exitOnCompletion = false;
await jasmine.execute(); await jasmine.execute();
if (app) await app.disconnect(); if (app) await app.disconnect();
if (container) await container.rm(); if (container) await container.rm();
console.log('app disconnected & container removed'); console.log('App disconnected & container removed');
} }
test(); test();

View File

@ -19,8 +19,9 @@ module.exports = class Docker {
* to avoid a bug with OverlayFS driver on MacOS. * to avoid a bug with OverlayFS driver on MacOS.
* *
* @param {Boolean} ci continuous integration environment argument * @param {Boolean} ci continuous integration environment argument
* @param {String} networkName Name of the container network
*/ */
async run(ci) { async run(ci, networkName = 'jenkins') {
let d = new Date(); let d = new Date();
let pad = v => v < 10 ? '0' + v : v; let pad = v => v < 10 ? '0' + v : v;
let stamp = `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`; let stamp = `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
@ -42,8 +43,16 @@ module.exports = class Docker {
let runChown = process.platform != 'linux'; let runChown = process.platform != 'linux';
let network = '';
if (ci) network = `--network="${networkName}"`;
log('Starting container...'); log('Starting container...');
const container = await this.execP(`docker run --env RUN_CHOWN=${runChown} -d ${dockerArgs} salix-db`); const container = await this.execP(`
docker run \
${network} \
--env RUN_CHOWN=${runChown} \
-d ${dockerArgs} salix-db
`);
this.id = container.stdout.trim(); this.id = container.stdout.trim();
try { try {
@ -51,10 +60,11 @@ module.exports = class Docker {
let inspect = await this.execP(`docker inspect -f "{{json .NetworkSettings}}" ${this.id}`); let inspect = await this.execP(`docker inspect -f "{{json .NetworkSettings}}" ${this.id}`);
let netSettings = JSON.parse(inspect.stdout); let netSettings = JSON.parse(inspect.stdout);
if (ci) if (ci) {
this.dbConf.host = netSettings.Gateway; this.dbConf.host = netSettings.Networks[networkName].IPAddress;
this.dbConf.port = 3306;
this.dbConf.port = netSettings.Ports['3306/tcp'][0]['HostPort']; } else
this.dbConf.port = netSettings.Ports['3306/tcp'][0]['HostPort'];
} }
await this.wait(); await this.wait();

View File

@ -8,7 +8,7 @@ describe('Client Edit web access path', () => {
beforeAll(async() => { beforeAll(async() => {
browser = await getBrowser(); browser = await getBrowser();
page = browser.page; page = browser.page;
await page.loginAndModule('employee', 'client'); await page.loginAndModule('salesPerson', 'client');
await page.accessToSearchResult('max'); await page.accessToSearchResult('max');
await page.accessToSection('client.card.webAccess'); await page.accessToSection('client.card.webAccess');
}); });

View File

@ -131,5 +131,6 @@
"Fichadas impares": "Odd signs", "Fichadas impares": "Odd signs",
"Descanso diario 9h.": "Daily rest 9h.", "Descanso diario 9h.": "Daily rest 9h.",
"Descanso semanal 36h. / 72h.": "Weekly rest 36h. / 72h.", "Descanso semanal 36h. / 72h.": "Weekly rest 36h. / 72h.",
"Password does not meet requirements": "Password does not meet requirements" "Password does not meet requirements": "Password does not meet requirements",
"Not enough privileges to edit a client": "Not enough privileges to edit a client"
} }

View File

@ -232,5 +232,8 @@
"Fichadas impares": "Fichadas impares", "Fichadas impares": "Fichadas impares",
"Descanso diario 12h.": "Descanso diario 12h.", "Descanso diario 12h.": "Descanso diario 12h.",
"Descanso semanal 36h. / 72h.": "Descanso semanal 36h. / 72h.", "Descanso semanal 36h. / 72h.": "Descanso semanal 36h. / 72h.",
"Dirección incorrecta": "Dirección incorrecta" "Dirección incorrecta": "Dirección incorrecta",
} "Modifiable user details only by an administrator": "Detalles de usuario modificables solo por un administrador",
"Modifiable password only via recovery or by an administrator": "Contraseña modificable solo a través de la recuperación o por un administrador",
"Not enough privileges to edit a client": "No tienes suficientes privilegios para editar un cliente"
}

View File

@ -1,5 +1,6 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
Self.remoteMethod('setPassword', { Self.remoteMethodCtx('setPassword', {
description: 'Sets the password of a non-worker client', description: 'Sets the password of a non-worker client',
accepts: [ accepts: [
{ {
@ -20,13 +21,21 @@ module.exports = Self => {
} }
}); });
Self.setPassword = async function(id, newPassword) { Self.setPassword = async function(ctx, id, newPassword) {
const models = Self.app.models; const models = Self.app.models;
const userId = ctx.req.accessToken.userId;
const isWorker = await models.Worker.findById(id); const isSalesPerson = await models.Account.hasRole(userId, 'salesPerson');
if (isWorker)
throw new Error(`Can't change the password of another worker`);
await models.Account.setPassword(id, newPassword); if (!isSalesPerson)
throw new UserError(`Not enough privileges to edit a client`);
const isClient = await models.Client.findById(id, null);
const isUserAccount = await models.UserAccount.findById(id, null);
if (isClient && !isUserAccount)
await models.Account.setPassword(id, newPassword);
else
throw new UserError(`Modifiable password only via recovery or by an administrator`);
}; };
}; };

View File

@ -1,23 +1,43 @@
const models = require('vn-loopback/server/server').models; const models = require('vn-loopback/server/server').models;
describe('Client setPassword', () => { describe('Client setPassword', () => {
it('should throw an error the setPassword target is not just a client but a worker', async() => { const salesPersonId = 19;
let error; const ctx = {
req: {accessToken: {userId: salesPersonId}}
};
it(`should throw an error if you don't have enough permissions`, async() => {
let error;
const employeeId = 1;
const ctx = {
req: {accessToken: {userId: employeeId}}
};
try { try {
await models.Client.setPassword(1106, 'newPass?'); await models.Client.setPassword(ctx, 1, 't0pl3v3l.p455w0rd!');
} catch (e) { } catch (e) {
error = e; error = e;
} }
expect(error.message).toEqual(`Can't change the password of another worker`); expect(error.message).toEqual(`Not enough privileges to edit a client`);
});
it('should throw an error the setPassword target is not just a client but a worker', async() => {
let error;
try {
await models.Client.setPassword(ctx, 1, 't0pl3v3l.p455w0rd!');
} catch (e) {
error = e;
}
expect(error.message).toEqual(`Modifiable password only via recovery or by an administrator`);
}); });
it('should change the password of the client', async() => { it('should change the password of the client', async() => {
let error; let error;
try { try {
await models.Client.setPassword(1101, 't0pl3v3l.p455w0rd!'); await models.Client.setPassword(ctx, 1101, 't0pl3v3l.p455w0rd!');
} catch (e) { } catch (e) {
error = e; error = e;
} }

View File

@ -10,8 +10,9 @@ describe('Client updateUser', () => {
} }
} }
}; };
const salesPersonId = 19;
const ctx = { const ctx = {
req: {accessToken: {userId: employeeId}}, req: {accessToken: {userId: salesPersonId}},
args: {name: 'test', active: true} args: {name: 'test', active: true}
}; };
@ -21,8 +22,13 @@ describe('Client updateUser', () => {
}); });
}); });
it('should throw an error the target user is not just a client but a worker', async() => { it(`should throw an error if you don't have enough permissions`, async() => {
let error; let error;
const employeeId = 1;
const ctx = {
req: {accessToken: {userId: employeeId}},
args: {name: 'test', active: true}
};
try { try {
const clientID = 1106; const clientID = 1106;
await models.Client.updateUser(ctx, clientID); await models.Client.updateUser(ctx, clientID);
@ -30,7 +36,19 @@ describe('Client updateUser', () => {
error = e; error = e;
} }
expect(error.message).toEqual(`Can't update the user details of another worker`); expect(error.message).toEqual(`Not enough privileges to edit a client`);
});
it('should throw an error the target user is not just a client but a worker', async() => {
let error;
try {
const clientID = 1;
await models.Client.updateUser(ctx, clientID);
} catch (e) {
error = e;
}
expect(error.message).toEqual(`Modifiable user details only by an administrator`);
}); });
it('should update the user data', async() => { it('should update the user data', async() => {

View File

@ -1,3 +1,4 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('updateUser', { Self.remoteMethodCtx('updateUser', {
description: 'Updates the user information', description: 'Updates the user information',
@ -5,8 +6,7 @@ module.exports = Self => {
{ {
arg: 'id', arg: 'id',
type: 'number', type: 'number',
description: 'The user id', description: 'The user id'
http: {source: 'path'}
}, },
{ {
arg: 'name', arg: 'name',
@ -15,7 +15,7 @@ module.exports = Self => {
}, },
{ {
arg: 'email', arg: 'email',
type: 'string', type: 'any',
description: 'the user email' description: 'the user email'
}, },
{ {
@ -32,6 +32,7 @@ module.exports = Self => {
Self.updateUser = async function(ctx, id, options) { Self.updateUser = async function(ctx, id, options) {
const models = Self.app.models; const models = Self.app.models;
const userId = ctx.req.accessToken.userId;
let tx; let tx;
const myOptions = {}; const myOptions = {};
@ -44,13 +45,19 @@ module.exports = Self => {
} }
try { try {
const isWorker = await models.Worker.findById(id, null, myOptions); const isSalesPerson = await models.Account.hasRole(userId, 'salesPerson', myOptions);
if (isWorker)
throw new Error(`Can't update the user details of another worker`);
const user = await models.Account.findById(id, null, myOptions); if (!isSalesPerson)
throw new UserError(`Not enough privileges to edit a client`);
await user.updateAttributes(ctx.args, myOptions); const isClient = await models.Client.findById(id, null, myOptions);
const isUserAccount = await models.UserAccount.findById(id, null, myOptions);
if (isClient && !isUserAccount) {
const user = await models.Account.findById(id, null, myOptions);
await user.updateAttributes(ctx.args, myOptions);
} else
throw new UserError(`Modifiable user details only by an administrator`);
if (tx) await tx.commit(); if (tx) await tx.commit();
} catch (e) { } catch (e) {

View File

@ -1,5 +1,5 @@
const UserError = require('vn-loopback/util/user-error'); const UserError = require('vn-loopback/util/user-error');
const {Report, storage} = require('vn-print'); const print = require('vn-print');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('createPdf', { Self.remoteMethodCtx('createPdf', {
@ -52,7 +52,7 @@ module.exports = Self => {
hasPdf: true hasPdf: true
}, myOptions); }, myOptions);
const invoiceReport = new Report('invoice', { const invoiceReport = new print.Report('invoice', {
reference: invoiceOut.ref, reference: invoiceOut.ref,
recipientId: invoiceOut.clientFk recipientId: invoiceOut.clientFk
}); });
@ -66,7 +66,7 @@ module.exports = Self => {
const fileName = `${year}${invoiceOut.ref}.pdf`; const fileName = `${year}${invoiceOut.ref}.pdf`;
// Store invoice // Store invoice
storage.write(stream, { print.storage.write(stream, {
type: 'invoice', type: 'invoice',
path: `${year}/${month}/${day}`, path: `${year}/${month}/${day}`,
fileName: fileName fileName: fileName

View File

@ -62,8 +62,14 @@ module.exports = Self => {
name: fileName name: fileName
}; };
await fs.access(file.path); try {
let stream = fs.createReadStream(file.path); await fs.access(file.path);
} catch (error) {
await Self.createPdf(ctx, id);
}
const stream = fs.createReadStream(file.path);
return [stream, file.contentType, `filename="${file.name}"`]; return [stream, file.contentType, `filename="${file.name}"`];
} catch (error) { } catch (error) {
if (error.code === 'ENOENT') if (error.code === 'ENOENT')

View File

@ -1,7 +1,6 @@
const models = require('vn-loopback/server/server').models; const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context'); const LoopBackContext = require('loopback-context');
const fs = require('fs-extra'); const print = require('vn-print');
const axios = require('axios');
describe('InvoiceOut createPdf()', () => { describe('InvoiceOut createPdf()', () => {
const userId = 1; const userId = 1;
@ -16,22 +15,15 @@ describe('InvoiceOut createPdf()', () => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx active: activeCtx
}); });
const response = {
data: { spyOn(print, 'Report').and.returnValue({
pipe: () => {}, toPdfStream: () => {
on: () => {}, return '';
} }
};
spyOn(axios, 'get').and.returnValue(new Promise(resolve => resolve(response)));
spyOn(models.InvoiceContainer, 'container').and.returnValue({
client: {root: '/path'}
});
spyOn(fs, 'mkdir').and.returnValue(true);
spyOn(fs, 'createWriteStream').and.returnValue({
on: (event, cb) => cb(),
end: () => {}
}); });
spyOn(print.storage, 'write').and.returnValue(true);
const tx = await models.InvoiceOut.beginTransaction({}); const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx}; const options = {transaction: tx};

1676
print/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -20,7 +20,7 @@
"juice": "^5.2.0", "juice": "^5.2.0",
"mysql2": "^1.7.0", "mysql2": "^1.7.0",
"nodemailer": "^4.7.0", "nodemailer": "^4.7.0",
"puppeteer": "^2.0.0", "puppeteer": "^18.0.5",
"qrcode": "^1.4.2", "qrcode": "^1.4.2",
"strftime": "^0.10.0", "strftime": "^0.10.0",
"vue": "^2.6.10", "vue": "^2.6.10",