mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Updated logic to handle when parts of the msg object are changed to strings
This commit is contained in:
parent
d2a045a237
commit
10771a2019
@ -65,6 +65,21 @@ module.exports = class PayloadValidator {
|
|||||||
return _.set(object, location, value);
|
return _.set(object, location, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* lodash set will always pass but might not actually set the value
|
||||||
|
* So here we test that it was actually set and use that to confirm we can set
|
||||||
|
*
|
||||||
|
* @param {*} object
|
||||||
|
* @param {*} location
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
canSet(object, location) {
|
||||||
|
const test = 'test';
|
||||||
|
let clonedObject = clone(object);
|
||||||
|
clonedObject = this.setValue(clonedObject, location, test);
|
||||||
|
return test === this.getValue(clonedObject, location);
|
||||||
|
}
|
||||||
|
|
||||||
check(_after) {
|
check(_after) {
|
||||||
let after = _after;
|
let after = _after;
|
||||||
try {
|
try {
|
||||||
@ -85,6 +100,10 @@ module.exports = class PayloadValidator {
|
|||||||
details,
|
details,
|
||||||
conversationId: this.conversationId
|
conversationId: this.conversationId
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!this.canSet(after, location)) {
|
||||||
|
throw new Error(`Cant set value as ${location} is no longer present in after`);
|
||||||
|
}
|
||||||
after = this.setValue(after, location, beforeValue);
|
after = this.setValue(after, location, beforeValue);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -26,9 +26,7 @@ describe('Unit: PayloadValidator', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const payloadValidator = new PayloadValidator(beforeEvent, `before-${workerId}-${nodeId}`);
|
const payloadValidator = new PayloadValidator(beforeEvent, `before-${workerId}-${nodeId}`);
|
||||||
|
|
||||||
const modifiedEvent = orgEvent('after');
|
const modifiedEvent = orgEvent('after');
|
||||||
|
|
||||||
const result = payloadValidator.verify(modifiedEvent);
|
const result = payloadValidator.verify(modifiedEvent);
|
||||||
|
|
||||||
assert(errorLogStub.callCount === 4);
|
assert(errorLogStub.callCount === 4);
|
||||||
@ -54,6 +52,38 @@ describe('Unit: PayloadValidator', () => {
|
|||||||
assert.deepStrictEqual(result, beforeEvent);
|
assert.deepStrictEqual(result, beforeEvent);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Should warn when org is overwritten and fail to fix it due to overwriting payload', () => {
|
||||||
|
const nodeId = 'abc-id';
|
||||||
|
const workerId = 'worker-id';
|
||||||
|
const beforeEvent = orgEvent('before', workerId);
|
||||||
|
const errorLogStub = sinon.stub();
|
||||||
|
const appLogStub = sinon.stub();
|
||||||
|
beforeEvent.logger.error = errorLogStub;
|
||||||
|
beforeEvent.logger.app = {
|
||||||
|
platform: {
|
||||||
|
organization: appLogStub
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const payloadValidator = new PayloadValidator(beforeEvent, `before-${workerId}-${nodeId}`);
|
||||||
|
const modifiedEvent = orgEvent('before');
|
||||||
|
modifiedEvent.payload = 'some text';
|
||||||
|
const result = payloadValidator.verify(modifiedEvent);
|
||||||
|
console.log(errorLogStub.callCount);
|
||||||
|
assert(errorLogStub.callCount === 1);
|
||||||
|
assert(appLogStub.callCount === 1);
|
||||||
|
const [[log]] = appLogStub.args;
|
||||||
|
assert(log.details.message.includes('msg.payload.system.organization'));
|
||||||
|
assert.strictEqual(log.details.nodeId, nodeId);
|
||||||
|
assert.strictEqual(log.details.workerId, workerId);
|
||||||
|
// deleting due to sinon funness
|
||||||
|
delete result.logger.error;
|
||||||
|
delete result.logger.app;
|
||||||
|
delete modifiedEvent.logger.error;
|
||||||
|
delete modifiedEvent.logger.app;
|
||||||
|
assert.deepStrictEqual(result, modifiedEvent);
|
||||||
|
});
|
||||||
|
|
||||||
it('Should warn when org is deleted and fix it', () => {
|
it('Should warn when org is deleted and fix it', () => {
|
||||||
const nodeId = 'abc-id';
|
const nodeId = 'abc-id';
|
||||||
const workerId = 'worker-id';
|
const workerId = 'worker-id';
|
||||||
@ -187,15 +217,6 @@ describe('Unit: PayloadValidator', () => {
|
|||||||
assert.deepStrictEqual(result, beforeEvent);
|
assert.deepStrictEqual(result, beforeEvent);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Should not die when error', () => {
|
|
||||||
const beforeEvent = orgEvent('before');
|
|
||||||
const payloadValidator = new PayloadValidator(beforeEvent);
|
|
||||||
|
|
||||||
const modifiedEvent = orgEvent('after');
|
|
||||||
|
|
||||||
payloadValidator.verify(modifiedEvent);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('Should not die with initiating the class with bad object', () => {
|
it('Should not die with initiating the class with bad object', () => {
|
||||||
const payloadValidator = new PayloadValidator({});
|
const payloadValidator = new PayloadValidator({});
|
||||||
});
|
});
|
||||||
@ -209,7 +230,7 @@ describe('Unit: PayloadValidator', () => {
|
|||||||
let payloadValidator;
|
let payloadValidator;
|
||||||
before(() => {
|
before(() => {
|
||||||
const event = orgEvent('event');
|
const event = orgEvent('event');
|
||||||
payloadValidator = new PayloadValidator(event);
|
payloadValidator = new PayloadValidator(event, 'before-worker-id-nodeId');
|
||||||
});
|
});
|
||||||
it('Should set a root level variable', () => {
|
it('Should set a root level variable', () => {
|
||||||
const location = 'hello';
|
const location = 'hello';
|
||||||
@ -241,7 +262,7 @@ describe('Unit: PayloadValidator', () => {
|
|||||||
let payloadValidator;
|
let payloadValidator;
|
||||||
before(() => {
|
before(() => {
|
||||||
const event = orgEvent('event');
|
const event = orgEvent('event');
|
||||||
payloadValidator = new PayloadValidator(event);
|
payloadValidator = new PayloadValidator(event, 'before-worker-id-nodeId');
|
||||||
});
|
});
|
||||||
it('Should set a root level variable', () => {
|
it('Should set a root level variable', () => {
|
||||||
const object = {};
|
const object = {};
|
||||||
@ -259,4 +280,26 @@ describe('Unit: PayloadValidator', () => {
|
|||||||
assert.strictEqual(result.should.set.hello, value);
|
assert.strictEqual(result.should.set.hello, value);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('Can set', () => {
|
||||||
|
let payloadValidator;
|
||||||
|
before(() => {
|
||||||
|
const event = orgEvent('event');
|
||||||
|
payloadValidator = new PayloadValidator(event, 'before-worker-id-nodeId');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should get back false for canSet when object is a string', () => {
|
||||||
|
const object = 'some string';
|
||||||
|
const location = 'hello';
|
||||||
|
const result = payloadValidator.canSet(object, location);
|
||||||
|
assert.strictEqual(result, false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should get back true for canSet', () => {
|
||||||
|
const object = {};
|
||||||
|
const location = 'should.set.hello';
|
||||||
|
const result = payloadValidator.canSet(object, location);
|
||||||
|
assert.strictEqual(result, true);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user