Updated the logic to be easier to follow

This commit is contained in:
Steve Walsh 2021-07-13 17:06:37 +01:00
parent 9a435217ec
commit 3596c030c8
2 changed files with 37 additions and 24 deletions

View File

@ -65,13 +65,13 @@ module.exports = class PayloadValidator {
return _.set(object, location, value); return _.set(object, location, value);
} }
check(_after) { /**
let after = _after; * Log that we had a invalid payload modification
try { * @param {*} beforeValue
variablesToCheck.forEach((location) => { * @param {*} afterValue
const beforeValue = this.getValue(this.before, location); * @param {*} location
const afterValue = this.getValue(after, location); */
if (beforeValue !== afterValue) { logException(beforeValue, afterValue, location) {
const details = { const details = {
message: `msg.${location} changed from "${beforeValue}" to "${afterValue}" for bot "${this.bot}"`, message: `msg.${location} changed from "${beforeValue}" to "${afterValue}" for bot "${this.bot}"`,
nodeId: this.nodeId, nodeId: this.nodeId,
@ -85,12 +85,27 @@ module.exports = class PayloadValidator {
details, details,
conversationId: this.conversationId conversationId: this.conversationId
}); });
}
/**
* Ensures that the organization in the before matches the org in the after
* If they are different then it attempts to set the organization back to the correct one
* @param {*} after
* @returns
*/
ensureOrganizationNotModified(after) {
try {
variablesToCheck.forEach((location) => {
const beforeValue = this.getValue(this.before, location);
const afterValue = this.getValue(after, location);
if (beforeValue !== afterValue) {
this.logException(beforeValue, afterValue, location);
// attempt to set the value back to its correct one
after = this.setValue(after, location, beforeValue); after = this.setValue(after, location, beforeValue);
if (!_.has(after, location, beforeValue)) { if (!_.has(after, location, beforeValue)) {
throw new Error(`Cant set value as ${location} is no longer present in after`); this.logger.error(`Cant set value as ${location} is no longer assessable in after`);
} }
} }
}); });
@ -101,15 +116,14 @@ module.exports = class PayloadValidator {
return after; return after;
} }
verify(_after) { verify(after) {
const after = _after;
if (this.isValidBefore) { if (this.isValidBefore) {
if (Array.isArray(after)) { if (Array.isArray(after)) {
const afterIndex = after.findIndex((msg) => !!msg); const afterIndex = after.findIndex(msg => !!msg);
after[afterIndex] = this.check(after[afterIndex]); after[afterIndex] = this.ensureOrganizationNotModified(after[afterIndex]);
return after; return after;
} }
return this.check(after); return this.ensureOrganizationNotModified(after);
} }
console.log('Error while trying to verify variable changes, wasn\'t initted with correct object'); console.log('Error while trying to verify variable changes, wasn\'t initted with correct object');
return after; return after;

View File

@ -69,7 +69,6 @@ describe('Unit: PayloadValidator', () => {
const modifiedEvent = orgEvent('before'); const modifiedEvent = orgEvent('before');
modifiedEvent.payload = 'some text'; modifiedEvent.payload = 'some text';
const result = payloadValidator.verify(modifiedEvent); const result = payloadValidator.verify(modifiedEvent);
console.log(errorLogStub.callCount);
assert(errorLogStub.callCount === 1); assert(errorLogStub.callCount === 1);
assert(appLogStub.callCount === 1); assert(appLogStub.callCount === 1);
const [[log]] = appLogStub.args; const [[log]] = appLogStub.args;