2014-05-03 23:26:35 +02:00
|
|
|
/**
|
2015-11-04 12:13:02 +01:00
|
|
|
* Copyright 2014, 2015 IBM Corp.
|
2014-05-03 23:26:35 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
var should = require("should");
|
|
|
|
var sinon = require("sinon");
|
|
|
|
var when = require("when");
|
2014-07-30 11:56:42 +02:00
|
|
|
var util = require("util");
|
2014-05-03 23:26:35 +02:00
|
|
|
|
2015-03-13 22:26:50 +01:00
|
|
|
var express = require("express");
|
|
|
|
var request = require("supertest");
|
|
|
|
|
2015-11-12 08:56:23 +01:00
|
|
|
var index = require("../../../../red/runtime/nodes/index");
|
|
|
|
var credentials = require("../../../../red/runtime/nodes/credentials");
|
|
|
|
var log = require("../../../../red/runtime/log");
|
|
|
|
var auth = require("../../../../red/api/auth");
|
2015-03-13 22:26:50 +01:00
|
|
|
|
2014-05-03 23:26:35 +02:00
|
|
|
|
|
|
|
describe('Credentials', function() {
|
2015-11-04 12:13:02 +01:00
|
|
|
|
2014-08-01 23:05:49 +02:00
|
|
|
afterEach(function() {
|
|
|
|
index.clearRegistry();
|
|
|
|
});
|
2015-11-04 12:13:02 +01:00
|
|
|
|
2014-05-03 23:26:35 +02:00
|
|
|
it('loads from storage',function(done) {
|
2015-11-04 12:13:02 +01:00
|
|
|
|
2014-05-03 23:26:35 +02:00
|
|
|
var storage = {
|
|
|
|
getCredentials: function() {
|
|
|
|
return when.promise(function(resolve,reject) {
|
|
|
|
resolve({"a":{"b":1,"c":2}});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2015-11-04 12:13:02 +01:00
|
|
|
|
2014-05-03 23:26:35 +02:00
|
|
|
credentials.init(storage);
|
2015-11-04 12:13:02 +01:00
|
|
|
|
2014-05-03 23:26:35 +02:00
|
|
|
credentials.load().then(function() {
|
2015-11-04 12:13:02 +01:00
|
|
|
|
2014-05-03 23:26:35 +02:00
|
|
|
credentials.get("a").should.have.property('b',1);
|
|
|
|
credentials.get("a").should.have.property('c',2);
|
2015-11-04 12:13:02 +01:00
|
|
|
|
2014-05-03 23:26:35 +02:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-11-04 12:13:02 +01:00
|
|
|
|
2014-05-03 23:26:35 +02:00
|
|
|
it('saves to storage', function(done) {
|
2015-03-13 22:26:50 +01:00
|
|
|
var storage = {
|
|
|
|
saveCredentials: function(creds) {
|
|
|
|
return when.resolve("saveCalled");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
credentials.init(storage);
|
|
|
|
credentials.save().then(function(res) {
|
|
|
|
res.should.equal("saveCalled");
|
|
|
|
done();
|
|
|
|
});
|
2015-11-04 12:13:02 +01:00
|
|
|
});
|
|
|
|
|
2015-03-13 22:26:50 +01:00
|
|
|
it('saves to storage when new cred added', function(done) {
|
2014-05-03 23:26:35 +02:00
|
|
|
var storage = {
|
|
|
|
getCredentials: function() {
|
|
|
|
return when.promise(function(resolve,reject) {
|
|
|
|
resolve({"a":{"b":1,"c":2}});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
saveCredentials: function(creds) {
|
|
|
|
return when(true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
sinon.spy(storage,"saveCredentials");
|
|
|
|
credentials.init(storage);
|
|
|
|
credentials.load().then(function() {
|
|
|
|
should.not.exist(credentials.get("b"))
|
|
|
|
credentials.add('b',{"d":3});
|
|
|
|
storage.saveCredentials.callCount.should.be.exactly(1);
|
|
|
|
credentials.get("b").should.have.property('d',3);
|
2014-08-01 22:56:27 +02:00
|
|
|
storage.saveCredentials.restore();
|
2014-05-03 23:26:35 +02:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-11-04 12:13:02 +01:00
|
|
|
|
2014-05-03 23:26:35 +02:00
|
|
|
it('deletes from storage', function(done) {
|
|
|
|
var storage = {
|
|
|
|
getCredentials: function() {
|
|
|
|
return when.promise(function(resolve,reject) {
|
|
|
|
resolve({"a":{"b":1,"c":2}});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
saveCredentials: function(creds) {
|
|
|
|
return when(true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
sinon.spy(storage,"saveCredentials");
|
|
|
|
credentials.init(storage);
|
|
|
|
credentials.load().then(function() {
|
|
|
|
should.exist(credentials.get("a"))
|
|
|
|
credentials.delete('a');
|
|
|
|
storage.saveCredentials.callCount.should.be.exactly(1);
|
|
|
|
should.not.exist(credentials.get("a"));
|
2014-08-01 22:56:27 +02:00
|
|
|
storage.saveCredentials.restore();
|
2014-05-03 23:26:35 +02:00
|
|
|
done();
|
|
|
|
});
|
2015-11-04 12:13:02 +01:00
|
|
|
|
2014-05-03 23:26:35 +02:00
|
|
|
});
|
2015-11-04 12:13:02 +01:00
|
|
|
|
2014-07-16 10:25:08 +02:00
|
|
|
it('clean up from storage', function(done) {
|
|
|
|
var storage = {
|
|
|
|
getCredentials: function() {
|
|
|
|
return when.promise(function(resolve,reject) {
|
|
|
|
resolve({"a":{"b":1,"c":2}});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
saveCredentials: function(creds) {
|
|
|
|
return when(true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
sinon.spy(storage,"saveCredentials");
|
|
|
|
credentials.init(storage);
|
|
|
|
credentials.load().then(function() {
|
|
|
|
should.exist(credentials.get("a"));
|
2015-01-10 23:09:37 +01:00
|
|
|
credentials.clean([]);
|
2014-07-16 10:25:08 +02:00
|
|
|
storage.saveCredentials.callCount.should.be.exactly(1);
|
|
|
|
should.not.exist(credentials.get("a"));
|
2014-08-01 22:56:27 +02:00
|
|
|
storage.saveCredentials.restore();
|
2014-07-16 10:25:08 +02:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-11-04 12:13:02 +01:00
|
|
|
|
2014-07-28 15:29:35 +02:00
|
|
|
it('handle error loading from storage', function(done) {
|
|
|
|
var storage = {
|
|
|
|
getCredentials: function() {
|
|
|
|
return when.promise(function(resolve,reject) {
|
|
|
|
reject("test forcing failure");
|
|
|
|
});
|
|
|
|
},
|
|
|
|
saveCredentials: function(creds) {
|
|
|
|
return when(true);
|
|
|
|
}
|
|
|
|
};
|
2015-02-03 23:02:26 +01:00
|
|
|
var logmsg = 'nothing logged yet';
|
|
|
|
sinon.stub(log, 'warn', function(msg) {
|
2014-07-28 15:29:35 +02:00
|
|
|
logmsg = msg;
|
|
|
|
});
|
2015-11-04 12:13:02 +01:00
|
|
|
|
2014-07-28 15:29:35 +02:00
|
|
|
credentials.init(storage);
|
|
|
|
credentials.load().then(function() {
|
2015-05-08 15:21:01 +02:00
|
|
|
log.warn.calledOnce.should.be.true;
|
2015-02-03 23:02:26 +01:00
|
|
|
log.warn.restore();
|
2014-07-28 15:29:35 +02:00
|
|
|
done();
|
|
|
|
}).otherwise(function(err){
|
2015-02-03 23:02:26 +01:00
|
|
|
log.warn.restore();
|
2014-07-28 15:29:35 +02:00
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
2015-11-04 12:13:02 +01:00
|
|
|
|
2014-07-30 11:56:42 +02:00
|
|
|
it('credential type is not registered when extract', function(done) {
|
|
|
|
var testFlows = [{"type":"test","id":"tab1","label":"Sheet 1"}];
|
|
|
|
var storage = {
|
|
|
|
getFlows: function() {
|
|
|
|
var defer = when.defer();
|
|
|
|
defer.resolve(testFlows);
|
|
|
|
return defer.promise;
|
|
|
|
},
|
|
|
|
getCredentials: function() {
|
|
|
|
return when.promise(function(resolve,reject) {
|
|
|
|
resolve({"tab1":{"b":1,"c":2}});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
saveFlows: function(conf) {
|
|
|
|
var defer = when.defer();
|
|
|
|
defer.resolve();
|
|
|
|
should.deepEqual(testFlows, conf);
|
|
|
|
return defer.promise;
|
|
|
|
},
|
|
|
|
saveCredentials: function(creds) {
|
|
|
|
return when(true);
|
2014-08-28 01:35:07 +02:00
|
|
|
},
|
|
|
|
getSettings: function() {
|
|
|
|
return when({});
|
|
|
|
},
|
|
|
|
saveSettings: function(s) {
|
|
|
|
return when();
|
2014-07-30 11:56:42 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
function TestNode(n) {
|
|
|
|
index.createNode(this, n);
|
2015-11-04 12:13:02 +01:00
|
|
|
|
2014-07-30 11:56:42 +02:00
|
|
|
this.id = 'tab1';
|
|
|
|
this.type = 'test';
|
|
|
|
this.name = 'barney';
|
|
|
|
var node = this;
|
|
|
|
|
|
|
|
this.on("log", function() {
|
|
|
|
// do nothing
|
|
|
|
});
|
|
|
|
}
|
|
|
|
var logmsg = 'nothing logged yet';
|
2015-02-03 23:02:26 +01:00
|
|
|
sinon.stub(log, 'warn', function(msg) {
|
2014-07-30 11:56:42 +02:00
|
|
|
logmsg = msg;
|
|
|
|
});
|
2014-08-28 01:35:07 +02:00
|
|
|
var settings = {
|
|
|
|
available: function() { return false;}
|
|
|
|
}
|
2015-11-17 22:12:43 +01:00
|
|
|
index.init({settings:settings, storage:storage});
|
2015-11-04 12:13:02 +01:00
|
|
|
index.registerType('test', TestNode);
|
2014-07-30 11:56:42 +02:00
|
|
|
index.loadFlows().then(function() {
|
2015-11-04 12:13:02 +01:00
|
|
|
var testnode = new TestNode({id:'tab1',type:'test',name:'barney'});
|
2014-07-30 11:56:42 +02:00
|
|
|
credentials.extract(testnode);
|
2015-05-08 15:21:01 +02:00
|
|
|
log.warn.calledOnce.should.be.true;
|
2015-02-03 23:02:26 +01:00
|
|
|
log.warn.restore();
|
2014-07-30 11:56:42 +02:00
|
|
|
done();
|
|
|
|
}).otherwise(function(err){
|
2015-02-03 23:02:26 +01:00
|
|
|
log.warn.restore();
|
2014-07-30 11:56:42 +02:00
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
2015-11-04 12:13:02 +01:00
|
|
|
|
2015-03-13 22:26:50 +01:00
|
|
|
it('extract and store credential updates in the provided node', function(done) {
|
|
|
|
credentials.init({saveCredentials:function(){}},express());
|
|
|
|
credentials.register("test",{
|
|
|
|
user1:{type:"text"},
|
|
|
|
password1:{type:"password"},
|
|
|
|
user2:{type:"text"},
|
|
|
|
password2:{type:"password"},
|
|
|
|
user3:{type:"text"},
|
|
|
|
password3:{type:"password"}
|
2015-11-04 12:13:02 +01:00
|
|
|
|
2015-03-13 22:26:50 +01:00
|
|
|
});
|
|
|
|
credentials.add("node",{user1:"abc",password1:"123",user2:"def",password2:"456",user3:"ghi",password3:"789"});
|
|
|
|
var node = {id:"node",type:"test",credentials:{
|
|
|
|
// user1 unchanged
|
|
|
|
password1:"__PWRD__",
|
|
|
|
user2: "",
|
|
|
|
password2:" ",
|
|
|
|
user3:"newUser",
|
|
|
|
password3:"newPassword"
|
|
|
|
}};
|
|
|
|
credentials.extract(node);
|
2015-11-04 12:13:02 +01:00
|
|
|
|
2015-03-13 22:26:50 +01:00
|
|
|
node.should.not.have.a.property("credentials");
|
2015-11-04 12:13:02 +01:00
|
|
|
|
2015-03-13 22:26:50 +01:00
|
|
|
var newCreds = credentials.get("node");
|
|
|
|
newCreds.should.have.a.property("user1","abc");
|
|
|
|
newCreds.should.have.a.property("password1","123");
|
|
|
|
newCreds.should.not.have.a.property("user2");
|
|
|
|
newCreds.should.not.have.a.property("password2");
|
|
|
|
newCreds.should.have.a.property("user3","newUser");
|
|
|
|
newCreds.should.have.a.property("password3","newPassword");
|
2015-11-04 12:13:02 +01:00
|
|
|
|
2015-03-13 22:26:50 +01:00
|
|
|
done();
|
|
|
|
});
|
2015-11-04 12:13:02 +01:00
|
|
|
})
|