[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / test / red / nodes / index_spec.js
1 /**
2  * Copyright 2014 IBM Corp.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  **/
16
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');
22
23 var index = require("../../../red/nodes/index");
24
25 describe("red/nodes/index", function() {
26         
27     afterEach(function() {
28         index.clearRegistry();
29     });
30
31     var testFlows = [{"type":"test","id":"tab1","label":"Sheet 1"}];
32     var storage = {
33             getFlows: function() {
34                 return when(testFlows);
35             },
36             getCredentials: function() {
37                 return when({"tab1":{"b":1,"c":2}});
38             },
39             saveFlows: function(conf) {
40                 should.deepEqual(testFlows, conf);
41                 return when();
42             },
43             saveCredentials: function(creds) {
44                 return when(true);
45             }
46     };
47     
48     var settings = {
49         available: function() { return false }
50     };
51
52     function TestNode(n) {
53         index.createNode(this, n);
54         var node = this;
55         this.on("log", function() {
56             // do nothing
57         });
58     }
59     
60    it('nodes are initialised with credentials',function(done) {      
61
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);
68             done();
69         }).otherwise(function(err) {
70             done(err);
71         });
72
73     });
74    
75    it('flows should be initialised',function(done) {      
76         index.init(settings, storage);
77         index.loadFlows().then(function() {
78             should.deepEqual(testFlows, index.getFlows());
79             done();
80         }).otherwise(function(err) {
81             done(err);
82         });
83
84     });
85    
86    describe("registerType should register credentials definition", function() {
87        var http = require('http');
88        var express = require('express');
89        var app = 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");
94        
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){
101                            resolve([]);
102                        });
103                    });
104                    sinon.stub(localfilesystem, 'getCredentials', function() {
105                         return when.promise(function(resolve,reject) {
106                                resolve({"tab1":{"b":1,"c":2}});
107                         });
108                    }) ;
109                    RED.init(http.createServer(function(req,res){app(req,res)}),
110                             {userDir: userDir});
111                    server.start().then(function () {
112                        done(); 
113                     });
114                });
115            });
116        });
117
118        after(function(done) {
119            fs.remove(userDir,done);
120            server.stop();
121            index.load.restore();
122            localfilesystem.getCredentials.restore();
123        });
124        
125        it(': definition defined',function(done) {      
126            index.registerType('test', TestNode, {
127                credentials: {
128                    foo: {type:"test"}
129                }   
130            }); 
131            var testnode = new TestNode({id:'tab1',type:'test',name:'barney'});    
132            credentials.getDefinition("test").should.have.property('foo');
133            done();
134        });
135
136    });
137    
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"]};
141        
142        before(function() {
143            sinon.stub(registry,"getNodeInfo",function(id) {
144                if (id == "test") {
145                    return {id:"1234",types:["test"]};
146                } else if (id == "doesnotexist") {
147                    return null;
148                } else {
149                    return randomNodeInfo;
150                }
151            });
152            sinon.stub(registry,"removeNode",function(id) {
153                return randomNodeInfo;
154            });
155            sinon.stub(registry,"disableNode",function(id) {
156                return randomNodeInfo;
157            });
158        });
159        after(function() {
160            registry.getNodeInfo.restore();
161            registry.removeNode.restore();
162            registry.disableNode.restore();
163        });
164
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);
173                 done();
174             }).otherwise(function(err) {
175                 done(err);
176             });
177        });
178        
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);
187                 done();
188             }).otherwise(function(err) {
189                 done(err);
190             });
191        });
192
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 */
198                 (function() {
199                     index.removeNode("test");
200                 }).should.throw();    
201                 
202                 done();
203             }).otherwise(function(err) {
204                 done(err);
205             });
206        });
207        
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 */
213                 (function() {
214                     index.disabledNode("test");
215                 }).should.throw();    
216                 
217                 done();
218             }).otherwise(function(err) {
219                 done(err);
220             });
221        });
222        
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 */
228                 (function() {
229                     index.removeNode("doesnotexist");
230                 }).should.throw();    
231                 
232                 done();
233             }).otherwise(function(err) {
234                 done(err);
235             });
236         });
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 */
242                 (function() {
243                     index.disableNode("doesnotexist");
244                 }).should.throw();    
245                 
246                 done();
247             }).otherwise(function(err) {
248                 done(err);
249             });
250         });
251
252     });
253    
254    
255 });