1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Installing a module returns module info

Removing a module checks module exists and checks type is not in use
This commit is contained in:
Anna Thomas 2014-11-21 15:15:24 +00:00
parent 4c9d53388c
commit dd5821ee1b
4 changed files with 145 additions and 64 deletions

View File

@ -34,6 +34,7 @@ module.exports = {
res.send(redNodes.getNodeConfigs());
}
},
post: function(req,res) {
if (!settings.available()) {
res.send(400,new Error("Settings unavailable").toString());
@ -55,7 +56,7 @@ module.exports = {
return;
}
promise.then(function(info) {
res.json(info);
res.json(redNodes.getModuleInfo(node.module));
}).otherwise(function(err) {
if (err.code === 404) {
res.send(404);

View File

@ -55,7 +55,7 @@ function checkTypeInUse(id) {
var nodeInfo = registry.getNodeInfo(id);
if (!nodeInfo) {
throw new Error("Unrecognised id: "+id);
}
} else {
var inUse = {};
flows.each(function(n) {
inUse[n.type] = (inUse[n.type]||0)+1;
@ -71,6 +71,7 @@ function checkTypeInUse(id) {
throw new Error("Type in use: "+msg);
}
}
}
function removeNode(id) {
checkTypeInUse(id);
@ -79,12 +80,15 @@ function removeNode(id) {
function removeModule(module) {
var info = registry.getNodeModuleInfo(module);
if (!info) {
throw new Error("Unrecognised module: "+module);
} else {
for (var i=0;i<info.nodes.length;i++) {
checkTypeInUse(info.nodes[i]);
checkTypeInUse(module+"/"+info.nodes[i]);
}
return registry.removeModule(module);
}
}
function disableNode(id) {
checkTypeInUse(id);

View File

@ -203,13 +203,21 @@ describe("nodes api", function() {
});
describe('by module', function() {
it('installs the module and returns node info', function(done) {
it('installs the module and returns module info', function(done) {
var settingsAvailable = sinon.stub(settings,'available', function() {
return true;
});
var getNodeModuleInfo = sinon.stub(redNodes,'getNodeModuleInfo',function(id) {
return null;
});
var getModuleInfo = sinon.stub(redNodes,'getModuleInfo',function(module) {
if (module === "foo") {
return {
name:"foo",
nodes:[{id:123}]
};
}
});
var installModule = sinon.stub(server,'installModule', function() {
return when.resolve({id:"123"});
});
@ -221,11 +229,14 @@ describe("nodes api", function() {
.end(function(err,res) {
settingsAvailable.restore();
getNodeModuleInfo.restore();
getModuleInfo.restore();
installModule.restore();
if (err) {
throw err;
}
res.body.should.have.property("id","123");
res.body.should.have.property("name","foo");
res.body.should.have.property("nodes");
res.body.nodes[0].should.have.property("id","123");
done();
});
});

View File

@ -135,7 +135,7 @@ describe("red/nodes/index", function() {
});
describe('allows nodes to be added/remove/enabled/disabled from the registry', function() {
describe('allows nodes to be added/removed/enabled/disabled from the registry', function() {
var registry = require("../../../red/nodes/registry");
var randomNodeInfo = {id:"5678",types:["random"]};
@ -248,8 +248,73 @@ describe("red/nodes/index", function() {
done(err);
});
});
});
describe('allows modules to be removed from the registry', function() {
var registry = require("../../../red/nodes/registry");
var randomNodeInfo = {id:"5678",types:["random"]};
var randomModuleInfo = {
name:"random",
nodes: [randomNodeInfo]
};
before(function() {
sinon.stub(registry,"getNodeInfo",function(id) {
if (id == "node-red/foo") {
return {id:"1234",types:["test"]};
} else if (id == "doesnotexist") {
return null;
} else {
return randomNodeInfo;
}
});
sinon.stub(registry,"getNodeModuleInfo",function(module) {
if (module == "node-red") {
return {name:"node-red",nodes:["foo"]};
} else if (module == "doesnotexist") {
return null;
} else {
return randomModuleInfo.nodes;
}
});
sinon.stub(registry,"removeModule",function(id) {
return randomModuleInfo;
});
});
after(function() {
registry.getNodeInfo.restore();
registry.getNodeModuleInfo.restore();
registry.removeModule.restore();
});
it(': prevents removing a module that is in use',function(done) {
index.init(settings, storage);
index.registerType('test', TestNode);
index.loadFlows().then(function() {
/*jshint immed: false */
(function() {
index.removeModule("node-red");
}).should.throw();
done();
}).otherwise(function(err) {
done(err);
});
});
it(': prevents removing a module that is unknown',function(done) {
index.init(settings, storage);
index.registerType('test', TestNode);
index.loadFlows().then(function() {
/*jshint immed: false */
(function() {
index.removeModule("doesnotexist");
}).should.throw();
done();
}).otherwise(function(err) {
done(err);
});
});
});
});