#523 updateFiscalData.js Backend unit tests plus refactor

This commit is contained in:
Carlos Jimenez 2018-08-13 14:10:03 +02:00
parent bd2befe3ef
commit 9268fdb2da
2 changed files with 52 additions and 2 deletions

View File

@ -0,0 +1,51 @@
const app = require(`${servicesDir}/client/server/server`);
describe('Client updateFiscalData', () => {
afterAll(async() => {
let ctxOfAdmin = {req: {accessToken: {userId: 5}}};
let validparams = {postcode: 46460};
let idWithDataChecked = 101;
await app.models.Client.updateFiscalData(ctxOfAdmin, validparams, idWithDataChecked);
});
it('should return an error if the user is not administrative and the isTaxDataChecked value is true', async() => {
let error;
let ctxOfNoAdmin = {req: {accessToken: {userId: 1}}};
let params = [];
let idWithDataChecked = 101;
await app.models.Client.updateFiscalData(ctxOfNoAdmin, params, idWithDataChecked)
.catch(e => {
error = e;
});
expect(error.toString()).toContain(`You don't have enough privileges to do that`);
});
it('should return an error if the user is administrative and the isTaxDataChecked value is true BUT the params aint valid', async() => {
let error;
let ctxOfAdmin = {req: {accessToken: {userId: 5}}};
let invalidparams = {invalid: 'param for update'};
let idWithDataChecked = 101;
await app.models.Client.updateFiscalData(ctxOfAdmin, invalidparams, idWithDataChecked)
.catch(e => {
error = e;
});
expect(error.toString()).toContain(`You don't have enough privileges to do that`);
});
it('should update the client discal data and return the count if changes made', async() => {
let ctxOfAdmin = {req: {accessToken: {userId: 5}}};
let validparams = {postcode: 12345};
let idWithDataChecked = 101;
let result = await app.models.Client.updateFiscalData(ctxOfAdmin, validparams, idWithDataChecked);
expect(result).toEqual({count: 1});
});
});

View File

@ -31,13 +31,12 @@ module.exports = Self => {
Self.updateFiscalData = async(ctx, params, id) => {
let userId = ctx.req.accessToken.userId;
let isAdministrative = await Self.app.models.Account.hasRole(userId, 'administrative');
let [taxData] = await Self.app.models.Client.find({where: {id: id}, fields: ['isTaxDataChecked']});
if (!isAdministrative && taxData.isTaxDataChecked)
throw new UserError(`You don't have enough privileges to do that`);
let validUpdateParams = [
'id',
'socialName',
'fi',
'street',