mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Fix for when after from function or change is an array
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
module.exports = (org) => ({
|
||||
module.exports = (org, workerId = 'worker-id') => ({
|
||||
logger: {
|
||||
metadata: {
|
||||
organization: org
|
||||
@@ -13,6 +13,7 @@ module.exports = (org) => ({
|
||||
},
|
||||
},
|
||||
event: {
|
||||
workers:[{id: `${workerId}:::something:::else`}],
|
||||
event: {
|
||||
organization: org,
|
||||
token: {
|
||||
|
@@ -5,13 +5,17 @@ const assert = require('assert');
|
||||
|
||||
describe.only('Unit: PayloadValidator', () => {
|
||||
it('Should not log when no changes', () => {
|
||||
const beforeEvent = orgEvent('before');
|
||||
const payloadValidator = new PayloadValidator(beforeEvent);
|
||||
const nodeId = 'abc-id'
|
||||
const workerId = 'worker-id'
|
||||
const beforeEvent = orgEvent('before', workerId);
|
||||
const payloadValidator = new PayloadValidator(beforeEvent, `before-${workerId}-${nodeId}`);
|
||||
payloadValidator.verify(beforeEvent);
|
||||
});
|
||||
|
||||
it('Should warn when org is overwritten', () => {
|
||||
const beforeEvent = orgEvent('before');
|
||||
const nodeId = 'abc-id'
|
||||
const workerId = 'worker-id'
|
||||
const beforeEvent = orgEvent('before', workerId);
|
||||
errorLogStub = sinon.stub();
|
||||
appLogStub = sinon.stub();
|
||||
beforeEvent.logger.error = errorLogStub;
|
||||
@@ -20,8 +24,8 @@ describe.only('Unit: PayloadValidator', () => {
|
||||
organization: appLogStub
|
||||
}
|
||||
};
|
||||
|
||||
const payloadValidator = new PayloadValidator(beforeEvent);
|
||||
|
||||
const payloadValidator = new PayloadValidator(beforeEvent, `before-${workerId}-${nodeId}`);
|
||||
|
||||
const modifiedEvent = orgEvent('after');
|
||||
|
||||
@@ -30,14 +34,24 @@ describe.only('Unit: PayloadValidator', () => {
|
||||
assert(appLogStub.callCount === 4)
|
||||
const [[log1], [log2], [log3], [log4]] = appLogStub.args
|
||||
assert(log1.details.message.includes('logger.metadata.organization'))
|
||||
assert.strictEqual(log1.details.nodeId, nodeId)
|
||||
assert.strictEqual(log1.details.workerId, workerId)
|
||||
assert(log2.details.message.includes('payload.system.organization'))
|
||||
assert.strictEqual(log2.details.nodeId, nodeId)
|
||||
assert.strictEqual(log2.details.workerId, workerId)
|
||||
assert(log3.details.message.includes('event.event.organization'))
|
||||
assert.strictEqual(log3.details.nodeId, nodeId)
|
||||
assert.strictEqual(log3.details.workerId, workerId)
|
||||
assert(log4.details.message.includes('event.event.token.contents.organization'))
|
||||
assert.strictEqual(log4.details.nodeId, nodeId)
|
||||
assert.strictEqual(log4.details.workerId, workerId)
|
||||
});
|
||||
|
||||
|
||||
it('Should warn when org is deleted', () => {
|
||||
const beforeEvent = orgEvent('before');
|
||||
const nodeId = 'abc-id'
|
||||
const workerId = 'worker-id'
|
||||
const beforeEvent = orgEvent('before', workerId);
|
||||
errorLogStub = sinon.stub();
|
||||
appLogStub = sinon.stub();
|
||||
beforeEvent.logger.error = errorLogStub;
|
||||
@@ -47,7 +61,8 @@ describe.only('Unit: PayloadValidator', () => {
|
||||
}
|
||||
};
|
||||
|
||||
const payloadValidator = new PayloadValidator(beforeEvent);
|
||||
const payloadValidator = new PayloadValidator(beforeEvent, `before-${workerId}-${nodeId}`);
|
||||
|
||||
|
||||
delete beforeEvent.logger.metadata.organization;
|
||||
delete beforeEvent.payload.system.organization;
|
||||
@@ -58,11 +73,70 @@ describe.only('Unit: PayloadValidator', () => {
|
||||
assert(appLogStub.callCount === 4)
|
||||
const [[log1], [log2], [log3], [log4]] = appLogStub.args
|
||||
assert(log1.details.message.includes('logger.metadata.organization'))
|
||||
assert.strictEqual(log1.details.nodeId, nodeId)
|
||||
assert.strictEqual(log1.details.workerId, workerId)
|
||||
assert(log2.details.message.includes('payload.system.organization'))
|
||||
assert.strictEqual(log2.details.nodeId, nodeId)
|
||||
assert.strictEqual(log2.details.workerId, workerId)
|
||||
assert(log3.details.message.includes('event.event.organization'))
|
||||
assert.strictEqual(log3.details.nodeId, nodeId)
|
||||
assert.strictEqual(log3.details.workerId, workerId)
|
||||
assert(log4.details.message.includes('event.event.token.contents.organization'))
|
||||
assert.strictEqual(log4.details.nodeId, nodeId)
|
||||
assert.strictEqual(log4.details.workerId, workerId)
|
||||
});
|
||||
it('Should handle when after is an array', () =>{
|
||||
const nodeId = 'abc-id'
|
||||
const workerId = 'worker-id'
|
||||
const beforeEvent = orgEvent('before', workerId);
|
||||
errorLogStub = sinon.stub();
|
||||
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.logger.metadata.organization = 'after'
|
||||
payloadValidator.verify([modifiedEvent]);
|
||||
assert(errorLogStub.callCount === 1)
|
||||
assert(appLogStub.callCount === 1)
|
||||
const [[log1]] = appLogStub.args
|
||||
assert(log1.details.message.includes('logger.metadata.organization'))
|
||||
assert.strictEqual(log1.details.nodeId, nodeId)
|
||||
assert.strictEqual(log1.details.workerId, workerId)
|
||||
})
|
||||
|
||||
it('Should handle when after is an array in something other than first position', () =>{
|
||||
const nodeId = 'abc-id'
|
||||
const workerId = 'worker-id'
|
||||
const beforeEvent = orgEvent('before', workerId);
|
||||
errorLogStub = sinon.stub();
|
||||
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.logger.metadata.organization = 'after'
|
||||
payloadValidator.verify([null, null, modifiedEvent, null]);
|
||||
assert(errorLogStub.callCount === 1)
|
||||
assert(appLogStub.callCount === 1)
|
||||
const [[log1]] = appLogStub.args
|
||||
assert(log1.details.message.includes('logger.metadata.organization'))
|
||||
assert.strictEqual(log1.details.nodeId, nodeId)
|
||||
assert.strictEqual(log1.details.workerId, workerId)
|
||||
})
|
||||
|
||||
it('Should not die when error', () => {
|
||||
const beforeEvent = orgEvent('before');
|
||||
const payloadValidator = new PayloadValidator(beforeEvent);
|
||||
@@ -80,4 +154,6 @@ describe.only('Unit: PayloadValidator', () => {
|
||||
const payloadValidator = new PayloadValidator({});
|
||||
payloadValidator.verify({});
|
||||
});
|
||||
|
||||
|
||||
});
|
Reference in New Issue
Block a user