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 sinon = require("sinon");
19 var when = require("when");
20 var util = require("util");
22 var index = require("../../../red/nodes/index");
23 var credentials = require("../../../red/nodes/credentials");
25 describe('Credentials', function() {
27 afterEach(function() {
28 index.clearRegistry();
31 it('loads from storage',function(done) {
34 getCredentials: function() {
35 return when.promise(function(resolve,reject) {
36 resolve({"a":{"b":1,"c":2}});
41 credentials.init(storage);
43 credentials.load().then(function() {
45 credentials.get("a").should.have.property('b',1);
46 credentials.get("a").should.have.property('c',2);
53 it('saves to storage', function(done) {
55 getCredentials: function() {
56 return when.promise(function(resolve,reject) {
57 resolve({"a":{"b":1,"c":2}});
60 saveCredentials: function(creds) {
64 sinon.spy(storage,"saveCredentials");
65 credentials.init(storage);
66 credentials.load().then(function() {
67 should.not.exist(credentials.get("b"))
68 credentials.add('b',{"d":3});
69 storage.saveCredentials.callCount.should.be.exactly(1);
70 credentials.get("b").should.have.property('d',3);
71 storage.saveCredentials.restore();
76 it('deletes from storage', function(done) {
78 getCredentials: function() {
79 return when.promise(function(resolve,reject) {
80 resolve({"a":{"b":1,"c":2}});
83 saveCredentials: function(creds) {
87 sinon.spy(storage,"saveCredentials");
88 credentials.init(storage);
89 credentials.load().then(function() {
90 should.exist(credentials.get("a"))
91 credentials.delete('a');
92 storage.saveCredentials.callCount.should.be.exactly(1);
93 should.not.exist(credentials.get("a"));
94 storage.saveCredentials.restore();
100 it('clean up from storage', function(done) {
102 getCredentials: function() {
103 return when.promise(function(resolve,reject) {
104 resolve({"a":{"b":1,"c":2}});
107 saveCredentials: function(creds) {
111 sinon.spy(storage,"saveCredentials");
112 credentials.init(storage);
113 credentials.load().then(function() {
114 should.exist(credentials.get("a"));
115 credentials.clean(function() {
118 storage.saveCredentials.callCount.should.be.exactly(1);
119 should.not.exist(credentials.get("a"));
120 storage.saveCredentials.restore();
125 it('handle error loading from storage', function(done) {
127 getCredentials: function() {
128 return when.promise(function(resolve,reject) {
129 reject("test forcing failure");
132 saveCredentials: function(creds) {
136 var logmsg = 'no errors yet';
137 sinon.stub(util, 'log', function(msg) {
141 credentials.init(storage);
142 credentials.load().then(function() {
143 should.equal('[red] Error loading credentials : test forcing failure', logmsg);
146 }).otherwise(function(err){
152 it('credential type is not registered when extract', function(done) {
153 var testFlows = [{"type":"test","id":"tab1","label":"Sheet 1"}];
155 getFlows: function() {
156 var defer = when.defer();
157 defer.resolve(testFlows);
158 return defer.promise;
160 getCredentials: function() {
161 return when.promise(function(resolve,reject) {
162 resolve({"tab1":{"b":1,"c":2}});
165 saveFlows: function(conf) {
166 var defer = when.defer();
168 should.deepEqual(testFlows, conf);
169 return defer.promise;
171 saveCredentials: function(creds) {
174 getSettings: function() {
177 saveSettings: function(s) {
181 function TestNode(n) {
182 index.createNode(this, n);
186 this.name = 'barney';
189 this.on("log", function() {
193 var logmsg = 'nothing logged yet';
194 sinon.stub(util, 'log', function(msg) {
198 available: function() { return false;}
200 index.init(settings, storage);
201 index.registerType('test', TestNode);
202 index.loadFlows().then(function() {
203 var testnode = new TestNode({id:'tab1',type:'test',name:'barney'});
204 credentials.extract(testnode);
205 should.equal(logmsg, 'Credential Type test is not registered.');
208 }).otherwise(function(err){
214 describe('extract and store credential updates in the provided node', function() {
215 var path = require('path');
216 var fs = require('fs-extra');
217 var http = require('http');
218 var express = require('express');
219 var server = require("../../../red/server");
220 var localfilesystem = require("../../../red/storage/localfilesystem");
222 var RED = require("../../../red/red.js");
224 var userDir = path.join(__dirname,".testUserHome");
225 before(function(done) {
226 fs.remove(userDir,function(err) {
227 fs.mkdir(userDir,function() {
228 sinon.stub(index, 'load', function() {
229 return when.promise(function(resolve,reject){
233 sinon.stub(localfilesystem, 'getCredentials', function() {
234 return when.promise(function(resolve,reject) {
235 resolve({"tab1":{"foo": 2, "pswd":'sticks'}});
238 RED.init(http.createServer(function(req,res){app(req,res)}),
240 server.start().then(function () {
247 after(function(done) {
248 fs.remove(userDir,done);
250 index.load.restore();
251 localfilesystem.getCredentials.restore();
254 function TestNode(n) {
255 index.createNode(this, n);
257 this.on("log", function() {
262 it(': credential updated with good value', function(done) {
263 index.registerType('test', TestNode, {
268 index.loadFlows().then(function() {
269 var testnode = new TestNode({id:'tab1',type:'test',name:'barney'});
270 credentials.extract(testnode);
271 should.exist(credentials.get('tab1'));
272 credentials.get('tab1').should.have.property('foo',2);
274 // set credentials to be an updated value and checking this is extracted properly
275 testnode.credentials = {"foo": 3};
276 credentials.extract(testnode);
277 should.exist(credentials.get('tab1'));
278 credentials.get('tab1').should.not.have.property('foo',2);
279 credentials.get('tab1').should.have.property('foo',3);
281 }).otherwise(function(err){
286 it(': credential updated with empty value', function(done) {
287 index.registerType('test', TestNode, {
292 index.loadFlows().then(function() {
293 var testnode = new TestNode({id:'tab1',type:'test',name:'barney'});
294 // setting value of "foo" credential to be empty removes foo as a property
295 testnode.credentials = {"foo": ''};
296 credentials.extract(testnode);
297 should.exist(credentials.get('tab1'));
298 credentials.get('tab1').should.not.have.property('foo',2);
299 credentials.get('tab1').should.not.have.property('foo');
301 }).otherwise(function(err){
306 it(': undefined credential updated', function(done) {
307 index.registerType('test', TestNode, {
312 index.loadFlows().then(function() {
313 var testnode = new TestNode({id:'tab1',type:'test',name:'barney'});
314 // setting value of an undefined credential should not change anything
315 testnode.credentials = {"bar": 4};
316 credentials.extract(testnode);
317 should.exist(credentials.get('tab1'));
318 credentials.get('tab1').should.have.property('foo',2);
319 credentials.get('tab1').should.not.have.property('bar');
321 }).otherwise(function(err){
326 it(': password credential updated', function(done) {
327 index.registerType('password', TestNode, {
329 pswd: {type:"password"}
332 index.loadFlows().then(function() {
333 var testnode = new TestNode({id:'tab1',type:'password',name:'barney'});
334 // setting value of password credential should update password
335 testnode.credentials = {"pswd": 'fiddle'};
336 credentials.extract(testnode);
337 should.exist(credentials.get('tab1'));
338 credentials.get('tab1').should.have.property('pswd','fiddle');
339 credentials.get('tab1').should.not.have.property('pswd','sticks');
341 }).otherwise(function(err){
346 it(': password credential not updated', function(done) {
347 index.registerType('password', TestNode, {
349 pswd: {type:"password"}
352 index.loadFlows().then(function() {
353 var testnode = new TestNode({id:'tab1',type:'password',name:'barney'});
354 // setting value of password credential should update password
355 testnode.credentials = {"pswd": '__PWRD__'};
356 credentials.extract(testnode);
357 should.exist(credentials.get('tab1'));
358 credentials.get('tab1').should.have.property('pswd','sticks');
359 credentials.get('tab1').should.not.have.property('pswd','__PWRD__');
361 }).otherwise(function(err){
368 describe('registerEndpoint', function() {
369 var path = require('path');
370 var fs = require('fs-extra');
371 var http = require('http');
372 var express = require('express');
373 var request = require('supertest');
375 var server = require("../../../red/server");
376 var localfilesystem = require("../../../red/storage/localfilesystem");
378 var RED = require("../../../red/red.js");
380 var userDir = path.join(__dirname,".testUserHome");
381 before(function(done) {
382 fs.remove(userDir,function(err) {
383 fs.mkdir(userDir,function() {
384 sinon.stub(index, 'load', function() {
385 return when.promise(function(resolve,reject){
389 sinon.stub(localfilesystem, 'getCredentials', function() {
390 return when.promise(function(resolve,reject) {
391 resolve({"tab1":{"foo": 2, "pswd":'sticks'}});
394 RED.init(http.createServer(function(req,res){app(req,res)}),
396 server.start().then(function () {
403 after(function(done) {
404 fs.remove(userDir,done);
406 index.load.restore();
407 localfilesystem.getCredentials.restore();
410 function TestNode(n) {
411 index.createNode(this, n);
413 this.on("log", function() {
418 it(': valid credential type', function(done) {
419 index.registerType('test', TestNode, {
424 index.loadFlows().then(function() {
425 var testnode = new TestNode({id:'tab1',type:'foo',name:'barney'});
426 request(RED.httpAdmin).get('/credentials/test/tab1').expect(200).end(function(err,res) {
430 res.body.should.have.property('foo', 2);
433 }).otherwise(function(err){
438 it(': password credential type', function(done) {
439 index.registerType('password', TestNode, {
441 pswd: {type:"password"}
444 index.loadFlows().then(function() {
445 var testnode = new TestNode({id:'tab1',type:'pswd',name:'barney'});
446 request(RED.httpAdmin).get('/credentials/password/tab1').expect(200).end(function(err,res) {
450 res.body.should.have.property('has_pswd', true);
451 res.body.should.not.have.property('pswd');
454 }).otherwise(function(err){
459 it(': returns 404 for undefined credential type', function(done) {
460 index.registerType('test', TestNode, {
465 index.loadFlows().then(function() {
466 var testnode = new TestNode({id:'tab1',type:'foo',name:'barney'});
467 request(RED.httpAdmin).get('/credentials/unknownType/tab1').expect(404).end(done);
468 }).otherwise(function(err){
473 it(': undefined nodeID', function(done) {
474 index.registerType('test', TestNode, {
479 index.loadFlows().then(function() {
480 var testnode = new TestNode({id:'tab1',type:'foo',name:'barney'});
481 request(RED.httpAdmin).get('/credentials/test/unknownNode').expect(200).end(function(err,res) {
486 res.body.should.not.have.property('foo');
489 }).otherwise(function(err){