2 * Copyright 2014 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 var should = require("should");
18 var fs = require('fs-extra');
19 var path = require('path');
20 var when = require("when");
21 var sinon = require('sinon');
23 var index = require("../../../red/nodes/index");
25 describe("red/nodes/index", function() {
27 afterEach(function() {
28 index.clearRegistry();
31 var testFlows = [{"type":"test","id":"tab1","label":"Sheet 1"}];
33 getFlows: function() {
34 return when(testFlows);
36 getCredentials: function() {
37 return when({"tab1":{"b":1,"c":2}});
39 saveFlows: function(conf) {
40 should.deepEqual(testFlows, conf);
43 saveCredentials: function(creds) {
49 available: function() { return false }
52 function TestNode(n) {
53 index.createNode(this, n);
55 this.on("log", function() {
60 it('nodes are initialised with credentials',function(done) {
62 index.init(settings, storage);
63 index.registerType('test', TestNode);
64 index.loadFlows().then(function() {
65 var testnode = new TestNode({id:'tab1',type:'test',name:'barney'});
66 testnode.credentials.should.have.property('b',1);
67 testnode.credentials.should.have.property('c',2);
69 }).otherwise(function(err) {
75 it('flows should be initialised',function(done) {
76 index.init(settings, storage);
77 index.loadFlows().then(function() {
78 should.deepEqual(testFlows, index.getFlows());
80 }).otherwise(function(err) {
86 describe("registerType should register credentials definition", function() {
87 var http = require('http');
88 var express = require('express');
90 var server = require("../../../red/server");
91 var credentials = require("../../../red/nodes/credentials");
92 var localfilesystem = require("../../../red/storage/localfilesystem");
93 var RED = require("../../../red/red.js");
95 var userDir = path.join(__dirname,".testUserHome");
96 before(function(done) {
97 fs.remove(userDir,function(err) {
98 fs.mkdir(userDir,function() {
99 sinon.stub(index, 'load', function() {
100 return when.promise(function(resolve,reject){
104 sinon.stub(localfilesystem, 'getCredentials', function() {
105 return when.promise(function(resolve,reject) {
106 resolve({"tab1":{"b":1,"c":2}});
109 RED.init(http.createServer(function(req,res){app(req,res)}),
111 server.start().then(function () {
118 after(function(done) {
119 fs.remove(userDir,done);
121 index.load.restore();
122 localfilesystem.getCredentials.restore();
125 it(': definition defined',function(done) {
126 index.registerType('test', TestNode, {
131 var testnode = new TestNode({id:'tab1',type:'test',name:'barney'});
132 credentials.getDefinition("test").should.have.property('foo');
138 describe('allows nodes to be added/remove/enabled/disabled from the registry', function() {
139 var registry = require("../../../red/nodes/registry");
140 var randomNodeInfo = {id:"5678",types:["random"]};
143 sinon.stub(registry,"getNodeInfo",function(id) {
145 return {id:"1234",types:["test"]};
146 } else if (id == "doesnotexist") {
149 return randomNodeInfo;
152 sinon.stub(registry,"removeNode",function(id) {
153 return randomNodeInfo;
155 sinon.stub(registry,"disableNode",function(id) {
156 return randomNodeInfo;
160 registry.getNodeInfo.restore();
161 registry.removeNode.restore();
162 registry.disableNode.restore();
165 it(': allows an unused node type to be removed',function(done) {
166 index.init(settings, storage);
167 index.registerType('test', TestNode);
168 index.loadFlows().then(function() {
169 var info = index.removeNode("5678");
170 registry.removeNode.calledOnce.should.be.true;
171 registry.removeNode.calledWith("5678").should.be.true;
172 info.should.eql(randomNodeInfo);
174 }).otherwise(function(err) {
179 it(': allows an unused node type to be disabled',function(done) {
180 index.init(settings, storage);
181 index.registerType('test', TestNode);
182 index.loadFlows().then(function() {
183 var info = index.disableNode("5678");
184 registry.disableNode.calledOnce.should.be.true;
185 registry.disableNode.calledWith("5678").should.be.true;
186 info.should.eql(randomNodeInfo);
188 }).otherwise(function(err) {
193 it(': prevents removing a node type that is in use',function(done) {
194 index.init(settings, storage);
195 index.registerType('test', TestNode);
196 index.loadFlows().then(function() {
197 /*jshint immed: false */
199 index.removeNode("test");
203 }).otherwise(function(err) {
208 it(': prevents disabling a node type that is in use',function(done) {
209 index.init(settings, storage);
210 index.registerType('test', TestNode);
211 index.loadFlows().then(function() {
212 /*jshint immed: false */
214 index.disabledNode("test");
218 }).otherwise(function(err) {
223 it(': prevents removing a node type that is unknown',function(done) {
224 index.init(settings, storage);
225 index.registerType('test', TestNode);
226 index.loadFlows().then(function() {
227 /*jshint immed: false */
229 index.removeNode("doesnotexist");
233 }).otherwise(function(err) {
237 it(': prevents disabling a node type that is unknown',function(done) {
238 index.init(settings, storage);
239 index.registerType('test', TestNode);
240 index.loadFlows().then(function() {
241 /*jshint immed: false */
243 index.disableNode("doesnotexist");
247 }).otherwise(function(err) {