From 27bc3397217c4b68430bab56660add720c95c8a4 Mon Sep 17 00:00:00 2001 From: Carlos Jimenez <=> Date: Mon, 13 Aug 2018 16:54:34 +0200 Subject: [PATCH] #522 updateBillingData.js Backend unit tests --- .../client/specs/updateBillingData.spec.js | 59 +++++++++++++++++++ .../client/specs/updateFiscalData.spec.js | 2 +- .../common/methods/client/updateBasicData.js | 11 +++- 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 services/loopback/common/methods/client/specs/updateBillingData.spec.js diff --git a/services/loopback/common/methods/client/specs/updateBillingData.spec.js b/services/loopback/common/methods/client/specs/updateBillingData.spec.js new file mode 100644 index 000000000..ad1abf4da --- /dev/null +++ b/services/loopback/common/methods/client/specs/updateBillingData.spec.js @@ -0,0 +1,59 @@ +const app = require(`${servicesDir}/client/server/server`); + +describe('Client updateBillingData', () => { + afterAll(async() => { + let ctxOfAdmin = {req: {accessToken: {userId: 5}}}; + let validparams = {iban: null}; + let idWithDataChecked = 101; + + await app.models.Client.updateBillingData(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.updateBillingData(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.updateBillingData(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 billing data and return the count if changes made', async() => { + let ctxOfAdmin = {req: {accessToken: {userId: 5}}}; + let validparams = {iban: 12345}; + let idWithDataChecked = 101; + + let client = await app.models.Client.findById(idWithDataChecked); + + expect(client.iban).toBeFalsy(); + + let result = await app.models.Client.updateBillingData(ctxOfAdmin, validparams, idWithDataChecked); + + expect(result).toEqual({count: 1}); + + let updatedClient = await app.models.Client.findById(idWithDataChecked); + + expect(updatedClient.iban).toEqual('12345'); + }); +}); diff --git a/services/loopback/common/methods/client/specs/updateFiscalData.spec.js b/services/loopback/common/methods/client/specs/updateFiscalData.spec.js index b4e65f1fd..32aedd375 100644 --- a/services/loopback/common/methods/client/specs/updateFiscalData.spec.js +++ b/services/loopback/common/methods/client/specs/updateFiscalData.spec.js @@ -39,7 +39,7 @@ describe('Client updateFiscalData', () => { 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() => { + it('should update the client fiscal data and return the count if changes made', async() => { let ctxOfAdmin = {req: {accessToken: {userId: 5}}}; let validparams = {postcode: 12345}; let idWithDataChecked = 101; diff --git a/services/loopback/common/methods/client/updateBasicData.js b/services/loopback/common/methods/client/updateBasicData.js index c9c9d38c8..d2618aa8e 100644 --- a/services/loopback/common/methods/client/updateBasicData.js +++ b/services/loopback/common/methods/client/updateBasicData.js @@ -29,7 +29,16 @@ module.exports = Self => { }); Self.updateBasicData = async(params, id) => { - let validUpdateParams = ['id', 'contact', 'name', 'email', 'phone', 'mobile', 'salesPersonFk', 'contactChannelFk']; + let validUpdateParams = [ + 'contact', + 'name', + 'email', + 'phone', + 'mobile', + 'salesPersonFk', + 'contactChannelFk' + ]; + for (const key in params) { if (validUpdateParams.indexOf(key) === -1) throw new UserError(`You don't have enough privileges to do that`);