diff --git a/.gitignore b/.gitignore index 5415d9f4f..0b459fb49 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ node_modules public locales/zz-ZZ nodes/core/locales/zz-ZZ +.nyc_output* \ No newline at end of file diff --git a/nodes/PayloadValidator.js b/nodes/PayloadValidator.js index 4c0b6134e..05d15c4f6 100644 --- a/nodes/PayloadValidator.js +++ b/nodes/PayloadValidator.js @@ -9,44 +9,61 @@ const variablesToCheck = [ module.exports = class PayloadValidator { constructor(_before) { - const before = clone(_before); - const { - logger, - payload: { - system: { - bot, conversationId, organization, region + try { + const before = clone(_before); + const { + logger, + payload: { + system: { + bot, + conversationId, + organization, + region + } } - } - } = before; - this.before = before; - this.logger = logger; - this.bot = bot; - this.conversationId = conversationId; - this.organization = organization; - this.region = region; + } = before; + this.before = before; + this.logger = logger; + this.bot = bot; + this.conversationId = conversationId; + this.organization = organization; + this.region = region; + this.isValidBefore = true; + } catch (e) { + console.log('Error while instantiating class with invalid object'); + console.log(e); + this.isValidBefore = false; + } + } + + getValue(object, location) { + return location.split('.').reduce((p, c) => (p && p[c]) || null, object); } verify(after) { - try { - variablesToCheck.forEach((location) => { - const getValue = (object) => location.split('.').reduce((p, c) => (p && p[c]) || null, object); - if (getValue(this.before) !== getValue(after)) { - const details = { - message: `msg.${location} changed from "${getValue(this.before)}" to "${getValue(after)}" for bot "${this.bot}"` - }; - this.logger.error(details.message); - this.logger.app.platform.organization({ - srn: `srn:botnet:${this.region}:${this.organization}:bot:${this.bot}`, - action: 'exception', - actionType: 'invalid-payload-modification', - details, - conversationId: this.conversationId - }); - } - }); - } catch (e) { - console.log('Error while trying to verify variable changes'); - console.log(e); + if (this.isValidBefore) { + try { + variablesToCheck.forEach((location) => { + if (this.getValue(this.before, location) !== this.getValue(after, location)) { + const details = { + message: `msg.${location} changed from "${this.getValue(this.before, location)}" to "${this.getValue(after, location)}" for bot "${this.bot}"` + }; + this.logger.error(details.message); + this.logger.app.platform.organization({ + srn: `srn:botnet:${this.region}:${this.organization}:bot:${this.bot}`, + action: 'exception', + actionType: 'invalid-payload-modification', + details, + conversationId: this.conversationId + }); + } + }); + } catch (e) { + console.log('Error while trying to verify variable changes'); + console.log(e); + } + } else { + console.log('Error while trying to verify variable changes, wasn\'t initted with correct object'); } } }; diff --git a/test/unit/test-payloadValidator.js b/test/unit/test-payloadValidator.js index c903f956e..e15fbc81b 100644 --- a/test/unit/test-payloadValidator.js +++ b/test/unit/test-payloadValidator.js @@ -71,4 +71,13 @@ describe.only('Unit: PayloadValidator', () => { payloadValidator.verify(modifiedEvent); }); + + it('Should not die with initiating the class with bad object', () => { + const payloadValidator = new PayloadValidator({}); + }); + + it('Should not die with initiating the class with bad object and then calling verify', () => { + const payloadValidator = new PayloadValidator({}); + payloadValidator.verify({}); + }); }); \ No newline at end of file