Move tests to reflect package structure

This commit is contained in:
Nick O'Leary
2018-08-19 11:28:03 +01:00
parent 974ba40f28
commit 998bf92ad4
118 changed files with 39 additions and 26 deletions

View File

@@ -0,0 +1,269 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* 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 when = require("when");
var should = require("should");
var paff = require('path');
var storage = require("../../../../red/runtime/storage/index");
describe("red/storage/index", function() {
it('rejects the promise when settings suggest loading a bad module', function(done) {
var wrongModule = {
settings:{
storageModule : "thisaintloading"
}
};
storage.init(wrongModule).then( function() {
var one = 1;
var zero = 0;
try {
zero.should.equal(one, "The initialization promise should never get resolved");
} catch(err) {
done(err);
}
}).catch(function(e) {
done(); //successfully rejected promise
});
});
it('non-string storage module', function(done) {
var initSetsMeToTrue = false;
var moduleWithBooleanSettingInit = {
init : function() {
initSetsMeToTrue = true;
}
};
var setsBooleanModule = {
settings: {
storageModule : moduleWithBooleanSettingInit
}
};
storage.init(setsBooleanModule);
initSetsMeToTrue.should.be.true();
done();
});
it('respects storage interface', function(done) {
var calledFlagGetFlows = false;
var calledFlagGetCredentials = false;
var calledFlagGetAllFlows = false;
var calledInit = false;
var calledFlagGetSettings = false;
var calledFlagGetSessions = false;
var interfaceCheckerModule = {
init : function (settings) {
settings.should.be.an.Object();
calledInit = true;
},
getFlows : function() {
calledFlagGetFlows = true;
return when.resolve([]);
},
saveFlows : function (flows) {
flows.should.be.an.Array();
flows.should.have.lengthOf(0);
return when.resolve("");
},
getCredentials : function() {
calledFlagGetCredentials = true;
return when.resolve({});
},
saveCredentials : function(credentials) {
credentials.should.be.true();
},
getSettings : function() {
calledFlagGetSettings = true;
},
saveSettings : function(settings) {
settings.should.be.true();
},
getSessions : function() {
calledFlagGetSessions = true;
},
saveSessions : function(sessions) {
sessions.should.be.true();
},
getAllFlows : function() {
calledFlagGetAllFlows = true;
},
getFlow : function(fn) {
fn.should.equal("name");
},
saveFlow : function(fn, data) {
fn.should.equal("name");
data.should.be.true();
},
getLibraryEntry : function(type, path) {
type.should.be.true();
path.should.equal("name");
},
saveLibraryEntry : function(type, path, meta, body) {
type.should.be.true();
path.should.equal("name");
meta.should.be.true();
body.should.be.true();
}
};
var moduleToLoad = {
settings: {
storageModule : interfaceCheckerModule
}
};
var promises = [];
storage.init(moduleToLoad);
promises.push(storage.getFlows());
promises.push(storage.saveFlows({flows:[],credentials:{}}));
storage.getSettings();
storage.saveSettings(true);
storage.getSessions();
storage.saveSessions(true);
storage.getAllFlows();
storage.getFlow("name");
storage.saveFlow("name", true);
storage.getLibraryEntry(true, "name");
storage.saveLibraryEntry(true, "name", true, true);
when.settle(promises).then(function() {
try {
calledInit.should.be.true();
calledFlagGetFlows.should.be.true();
calledFlagGetCredentials.should.be.true();
calledFlagGetAllFlows.should.be.true();
done();
} catch(err) {
done(err);
}
});
});
describe('respects deprecated flow library functions', function() {
var savePath;
var saveContent;
var saveMeta;
var saveType;
var interfaceCheckerModule = {
init : function (settings) {
settings.should.be.an.Object();
},
getLibraryEntry : function(type, path) {
if (type === "flows") {
if (path === "/" || path === "\\") {
return when.resolve(["a",{fn:"test.json"}]);
} else if (path == "/a" || path == "\\a") {
return when.resolve([{fn:"test2.json"}]);
} else if (path == paff.join("","a","test2.json")) {
return when.resolve("test content");
}
}
},
saveLibraryEntry : function(type, path, meta, body) {
saveType = type;
savePath = path;
saveContent = body;
saveMeta = meta;
return when.resolve();
}
};
var moduleToLoad = {
settings: {
storageModule : interfaceCheckerModule
}
};
before(function() {
storage.init(moduleToLoad);
});
it('getAllFlows',function(done) {
storage.getAllFlows().then(function (res) {
try {
res.should.eql({ d: { a: { f: ['test2'] } }, f: [ 'test' ] });
done();
} catch(err) {
done(err);
}
});
});
it('getFlow',function(done) {
storage.getFlow(paff.join("a","test2.json")).then(function(res) {
try {
res.should.eql("test content");
done();
} catch(err) {
done(err);
}
});
});
it ('saveFlow', function (done) {
storage.saveFlow(paff.join("a","test2.json"),"new content").then(function(res) {
try {
savePath.should.eql(paff.join("a","test2.json"));
saveContent.should.eql("new content");
saveMeta.should.eql({});
saveType.should.eql("flows");
done();
} catch(err) {
done(err);
}
});
});
});
describe('handles missing settings/sessions interface', function() {
before(function() {
var interfaceCheckerModule = {
init : function () {}
};
storage.init({settings:{storageModule: interfaceCheckerModule}});
});
it('defaults missing getSettings',function(done) {
storage.getSettings().then(function(settings) {
should.not.exist(settings);
done();
});
});
it('defaults missing saveSettings',function(done) {
storage.saveSettings({}).then(function() {
done();
});
});
it('defaults missing getSessions',function(done) {
storage.getSessions().then(function(settings) {
should.not.exist(settings);
done();
});
});
it('defaults missing saveSessions',function(done) {
storage.saveSessions({}).then(function() {
done();
});
});
});
});

View File

@@ -0,0 +1,476 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* 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 fs = require('fs-extra');
var path = require('path');
var sinon = require('sinon');
var localfilesystem = require("../../../../../red/runtime/storage/localfilesystem");
var log = require("../../../../../red/util/log");
describe('storage/localfilesystem', function() {
var mockRuntime = {
log:{
_:function() { return "placeholder message"},
info: function() { },
warn: function() { },
trace: function() {}
}
};
var userDir = path.join(__dirname,".testUserHome");
var testFlow = [{"type":"tab","id":"d8be2a6d.2741d8","label":"Sheet 1"}];
beforeEach(function(done) {
fs.remove(userDir,function(err) {
fs.mkdir(userDir,done);
});
});
afterEach(function(done) {
fs.remove(userDir,done);
});
it('should initialise the user directory',function(done) {
localfilesystem.init({userDir:userDir}, mockRuntime).then(function() {
fs.existsSync(path.join(userDir,"lib")).should.be.true();
fs.existsSync(path.join(userDir,"lib",'flows')).should.be.true();
done();
}).catch(function(err) {
done(err);
});
});
it('should set userDir to NRH if .config.json presents',function(done) {
var oldNRH = process.env.NODE_RED_HOME;
process.env.NODE_RED_HOME = path.join(userDir,"NRH");
fs.mkdirSync(process.env.NODE_RED_HOME);
fs.writeFileSync(path.join(process.env.NODE_RED_HOME,".config.json"),"{}","utf8");
var settings = {};
localfilesystem.init(settings, mockRuntime).then(function() {
try {
fs.existsSync(path.join(process.env.NODE_RED_HOME,"lib")).should.be.true();
fs.existsSync(path.join(process.env.NODE_RED_HOME,"lib",'flows')).should.be.true();
settings.userDir.should.equal(process.env.NODE_RED_HOME);
done();
} catch(err) {
done(err);
} finally {
process.env.NODE_RED_HOME = oldNRH;
}
}).catch(function(err) {
done(err);
});
});
it('should set userDir to HOMEPATH/.node-red if .config.json presents',function(done) {
var oldNRH = process.env.NODE_RED_HOME;
process.env.NODE_RED_HOME = path.join(userDir,"NRH");
var oldHOMEPATH = process.env.HOMEPATH;
process.env.HOMEPATH = path.join(userDir,"HOMEPATH");
fs.mkdirSync(process.env.HOMEPATH);
fs.mkdirSync(path.join(process.env.HOMEPATH,".node-red"));
fs.writeFileSync(path.join(process.env.HOMEPATH,".node-red",".config.json"),"{}","utf8");
var settings = {};
localfilesystem.init(settings, mockRuntime).then(function() {
try {
fs.existsSync(path.join(process.env.HOMEPATH,".node-red","lib")).should.be.true();
fs.existsSync(path.join(process.env.HOMEPATH,".node-red","lib",'flows')).should.be.true();
settings.userDir.should.equal(path.join(process.env.HOMEPATH,".node-red"));
done();
} catch(err) {
done(err);
} finally {
process.env.NODE_RED_HOME = oldNRH;
process.env.NODE_HOMEPATH = oldHOMEPATH;
}
}).catch(function(err) {
done(err);
});
});
it('should set userDir to HOME/.node-red',function(done) {
var oldNRH = process.env.NODE_RED_HOME;
process.env.NODE_RED_HOME = path.join(userDir,"NRH");
var oldHOME = process.env.HOME;
process.env.HOME = path.join(userDir,"HOME");
var oldHOMEPATH = process.env.HOMEPATH;
process.env.HOMEPATH = path.join(userDir,"HOMEPATH");
fs.mkdirSync(process.env.HOME);
var settings = {};
localfilesystem.init(settings, mockRuntime).then(function() {
try {
fs.existsSync(path.join(process.env.HOME,".node-red","lib")).should.be.true();
fs.existsSync(path.join(process.env.HOME,".node-red","lib",'flows')).should.be.true();
settings.userDir.should.equal(path.join(process.env.HOME,".node-red"));
done();
} catch(err) {
done(err);
} finally {
process.env.NODE_RED_HOME = oldNRH;
process.env.HOME = oldHOME;
process.env.HOMEPATH = oldHOMEPATH;
}
}).catch(function(err) {
done(err);
});
});
it('should set userDir to USERPROFILE/.node-red',function(done) {
var oldNRH = process.env.NODE_RED_HOME;
process.env.NODE_RED_HOME = path.join(userDir,"NRH");
var oldHOME = process.env.HOME;
process.env.HOME = "";
var oldHOMEPATH = process.env.HOMEPATH;
process.env.HOMEPATH = path.join(userDir,"HOMEPATH");
var oldUSERPROFILE = process.env.USERPROFILE;
process.env.USERPROFILE = path.join(userDir,"USERPROFILE");
fs.mkdirSync(process.env.USERPROFILE);
var settings = {};
localfilesystem.init(settings, mockRuntime).then(function() {
try {
fs.existsSync(path.join(process.env.USERPROFILE,".node-red","lib")).should.be.true();
fs.existsSync(path.join(process.env.USERPROFILE,".node-red","lib",'flows')).should.be.true();
settings.userDir.should.equal(path.join(process.env.USERPROFILE,".node-red"));
done();
} catch(err) {
done(err);
} finally {
process.env.NODE_RED_HOME = oldNRH;
process.env.HOME = oldHOME;
process.env.HOMEPATH = oldHOMEPATH;
process.env.USERPROFILE = oldUSERPROFILE;
}
}).catch(function(err) {
done(err);
});
});
it('should handle missing flow file',function(done) {
localfilesystem.init({userDir:userDir}, mockRuntime).then(function() {
var flowFile = 'flows_'+require('os').hostname()+'.json';
var flowFilePath = path.join(userDir,flowFile);
fs.existsSync(flowFilePath).should.be.false();
localfilesystem.getFlows().then(function(flows) {
flows.should.eql([]);
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should handle empty flow file, no backup',function(done) {
localfilesystem.init({userDir:userDir}, mockRuntime).then(function() {
var flowFile = 'flows_'+require('os').hostname()+'.json';
var flowFilePath = path.join(userDir,flowFile);
var flowFileBackupPath = path.join(userDir,"."+flowFile+".backup");
fs.closeSync(fs.openSync(flowFilePath, 'w'));
fs.existsSync(flowFilePath).should.be.true();
localfilesystem.getFlows().then(function(flows) {
flows.should.eql([]);
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should handle empty flow file, restores backup',function(done) {
localfilesystem.init({userDir:userDir}, mockRuntime).then(function() {
var flowFile = 'flows_'+require('os').hostname()+'.json';
var flowFilePath = path.join(userDir,flowFile);
var flowFileBackupPath = path.join(userDir,"."+flowFile+".backup");
fs.closeSync(fs.openSync(flowFilePath, 'w'));
fs.existsSync(flowFilePath).should.be.true();
fs.existsSync(flowFileBackupPath).should.be.false();
fs.writeFileSync(flowFileBackupPath,JSON.stringify(testFlow));
fs.existsSync(flowFileBackupPath).should.be.true();
setTimeout(function() {
localfilesystem.getFlows().then(function(flows) {
flows.should.eql(testFlow);
done();
}).catch(function(err) {
done(err);
});
},50);
}).catch(function(err) {
done(err);
});
});
it('should save flows to the default file',function(done) {
localfilesystem.init({userDir:userDir}, mockRuntime).then(function() {
var flowFile = 'flows_'+require('os').hostname()+'.json';
var flowFilePath = path.join(userDir,flowFile);
var flowFileBackupPath = path.join(userDir,"."+flowFile+".backup");
fs.existsSync(flowFilePath).should.be.false();
fs.existsSync(flowFileBackupPath).should.be.false();
localfilesystem.saveFlows(testFlow).then(function() {
fs.existsSync(flowFilePath).should.be.true();
fs.existsSync(flowFileBackupPath).should.be.false();
localfilesystem.getFlows().then(function(flows) {
flows.should.eql(testFlow);
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should save flows to the specified file',function(done) {
var defaultFlowFile = 'flows_'+require('os').hostname()+'.json';
var defaultFlowFilePath = path.join(userDir,defaultFlowFile);
var flowFile = 'test.json';
var flowFilePath = path.join(userDir,flowFile);
localfilesystem.init({userDir:userDir, flowFile:flowFilePath}, mockRuntime).then(function() {
fs.existsSync(defaultFlowFilePath).should.be.false();
fs.existsSync(flowFilePath).should.be.false();
localfilesystem.saveFlows(testFlow).then(function() {
fs.existsSync(defaultFlowFilePath).should.be.false();
fs.existsSync(flowFilePath).should.be.true();
localfilesystem.getFlows().then(function(flows) {
flows.should.eql(testFlow);
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should format the flows file when flowFilePretty specified',function(done) {
var flowFile = 'test.json';
var flowFilePath = path.join(userDir,flowFile);
localfilesystem.init({userDir:userDir, flowFile:flowFilePath,flowFilePretty:true}, mockRuntime).then(function() {
localfilesystem.saveFlows(testFlow).then(function() {
var content = fs.readFileSync(flowFilePath,"utf8");
content.split("\n").length.should.be.above(1);
localfilesystem.getFlows().then(function(flows) {
flows.should.eql(testFlow);
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should fsync the flows file',function(done) {
var flowFile = 'test.json';
var flowFilePath = path.join(userDir,flowFile);
localfilesystem.init({editorTheme:{projects:{enabled:false}},userDir:userDir, flowFile:flowFilePath}, mockRuntime).then(function() {
sinon.spy(fs,"fsync");
localfilesystem.saveFlows(testFlow).then(function() {
fs.fsync.callCount.should.be.greaterThan(0);
fs.fsync.restore();
done();
}).catch(function(err) {
fs.fsync.restore();
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should log fsync errors and continue',function(done) {
var flowFile = 'test.json';
var flowFilePath = path.join(userDir,flowFile);
localfilesystem.init({userDir:userDir, flowFile:flowFilePath}, mockRuntime).then(function() {
sinon.stub(fs,"fsync", function(fd, cb) {
cb(new Error());
});
sinon.spy(log,"warn");
localfilesystem.saveFlows(testFlow).then(function() {
fs.fsync.callCount.should.be.greaterThan(0);
log.warn.restore();
fs.fsync.callCount.should.be.greaterThan(0);
fs.fsync.restore();
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should backup the flows file', function(done) {
var defaultFlowFile = 'flows_'+require('os').hostname()+'.json';
var defaultFlowFilePath = path.join(userDir,defaultFlowFile);
var flowFile = 'test.json';
var flowFilePath = path.join(userDir,flowFile);
var flowFileBackupPath = path.join(userDir,"."+flowFile+".backup");
localfilesystem.init({userDir:userDir, flowFile:flowFilePath}, mockRuntime).then(function() {
fs.existsSync(defaultFlowFilePath).should.be.false();
fs.existsSync(flowFilePath).should.be.false();
fs.existsSync(flowFileBackupPath).should.be.false();
localfilesystem.saveFlows(testFlow).then(function() {
fs.existsSync(flowFileBackupPath).should.be.false();
fs.existsSync(defaultFlowFilePath).should.be.false();
fs.existsSync(flowFilePath).should.be.true();
var content = fs.readFileSync(flowFilePath,'utf8');
var testFlow2 = [{"type":"tab","id":"bc5672ad.2741d8","label":"Sheet 2"}];
localfilesystem.saveFlows(testFlow2).then(function() {
fs.existsSync(flowFileBackupPath).should.be.true();
fs.existsSync(defaultFlowFilePath).should.be.false();
fs.existsSync(flowFilePath).should.be.true();
var backupContent = fs.readFileSync(flowFileBackupPath,'utf8');
content.should.equal(backupContent);
var content2 = fs.readFileSync(flowFilePath,'utf8');
content2.should.not.equal(backupContent);
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should handle missing credentials', function(done) {
var flowFile = 'test.json';
var flowFilePath = path.join(userDir,flowFile);
var credFile = path.join(userDir,"test_cred.json");
localfilesystem.init({userDir:userDir, flowFile:flowFilePath}, mockRuntime).then(function() {
fs.existsSync(credFile).should.be.false();
localfilesystem.getCredentials().then(function(creds) {
creds.should.eql({});
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should handle credentials', function(done) {
var flowFile = 'test.json';
var flowFilePath = path.join(userDir,flowFile);
var credFile = path.join(userDir,"test_cred.json");
localfilesystem.init({userDir:userDir, flowFile:flowFilePath}, mockRuntime).then(function() {
fs.existsSync(credFile).should.be.false();
var credentials = {"abc":{"type":"creds"}};
localfilesystem.saveCredentials(credentials).then(function() {
fs.existsSync(credFile).should.be.true();
localfilesystem.getCredentials().then(function(creds) {
creds.should.eql(credentials);
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should backup existing credentials', function(done) {
var flowFile = 'test.json';
var flowFilePath = path.join(userDir,flowFile);
var credFile = path.join(userDir,"test_cred.json");
var credFileBackup = path.join(userDir,".test_cred.json.backup");
localfilesystem.init({userDir:userDir, flowFile:flowFilePath}, mockRuntime).then(function() {
fs.writeFileSync(credFile,"{}","utf8");
fs.existsSync(credFile).should.be.true();
fs.existsSync(credFileBackup).should.be.false();
var credentials = {"abc":{"type":"creds"}};
localfilesystem.saveCredentials(credentials).then(function() {
fs.existsSync(credFile).should.be.true();
fs.existsSync(credFileBackup).should.be.true();
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should format the creds file when flowFilePretty specified',function(done) {
var flowFile = 'test.json';
var flowFilePath = path.join(userDir,flowFile);
var credFile = path.join(userDir,"test_cred.json");
localfilesystem.init({userDir:userDir, flowFile:flowFilePath, flowFilePretty:true}, mockRuntime).then(function() {
fs.existsSync(credFile).should.be.false();
var credentials = {"abc":{"type":"creds"}};
localfilesystem.saveCredentials(credentials).then(function() {
fs.existsSync(credFile).should.be.true();
var content = fs.readFileSync(credFile,"utf8");
content.split("\n").length.should.be.above(1);
localfilesystem.getCredentials().then(function(creds) {
creds.should.eql(credentials);
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
});

View File

@@ -0,0 +1,205 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* 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 fs = require('fs-extra');
var path = require('path');
var localfilesystemLibrary = require("../../../../../red/runtime/storage/localfilesystem/library");
describe('storage/localfilesystem/library', function() {
var userDir = path.join(__dirname,".testUserHome");
beforeEach(function(done) {
fs.remove(userDir,function(err) {
fs.mkdir(userDir,done);
});
});
afterEach(function(done) {
fs.remove(userDir,done);
});
it('should return an empty list of library objects',function(done) {
localfilesystemLibrary.init({userDir:userDir}).then(function() {
localfilesystemLibrary.getLibraryEntry('object','').then(function(flows) {
flows.should.eql([]);
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should return an empty list of library objects (path=/)',function(done) {
localfilesystemLibrary.init({userDir:userDir}).then(function() {
localfilesystemLibrary.getLibraryEntry('object','/').then(function(flows) {
flows.should.eql([]);
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should return an error for a non-existent library object',function(done) {
localfilesystemLibrary.init({userDir:userDir}).then(function() {
localfilesystemLibrary.getLibraryEntry('object','A/B').then(function(flows) {
should.fail(null,null,"non-existent flow");
}).catch(function(err) {
should.exist(err);
done();
});
}).catch(function(err) {
done(err);
});
});
function createObjectLibrary(type) {
type = type ||"object";
var objLib = path.join(userDir,"lib",type);
try {
fs.mkdirSync(objLib);
} catch(err) {
}
fs.mkdirSync(path.join(objLib,"A"));
fs.mkdirSync(path.join(objLib,"B"));
fs.mkdirSync(path.join(objLib,"B","C"));
if (type === "functions" || type === "object") {
fs.writeFileSync(path.join(objLib,"file1.js"),"// abc: def\n// not a metaline \n\n Hi",'utf8');
fs.writeFileSync(path.join(objLib,"B","file2.js"),"// ghi: jkl\n// not a metaline \n\n Hi",'utf8');
}
if (type === "flows" || type === "object") {
fs.writeFileSync(path.join(objLib,"B","flow.json"),"Hi",'utf8');
}
}
it('should return a directory listing of library objects',function(done) {
localfilesystemLibrary.init({userDir:userDir}).then(function() {
createObjectLibrary();
localfilesystemLibrary.getLibraryEntry('object','').then(function(flows) {
flows.should.eql([ 'A', 'B', { abc: 'def', fn: 'file1.js' } ]);
localfilesystemLibrary.getLibraryEntry('object','B').then(function(flows) {
flows.should.eql([ 'C', { ghi: 'jkl', fn: 'file2.js' }, { fn: 'flow.json' } ]);
localfilesystemLibrary.getLibraryEntry('object','B/C').then(function(flows) {
flows.should.eql([]);
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should load a flow library object with .json unspecified', function(done) {
localfilesystemLibrary.init({userDir:userDir}).then(function() {
createObjectLibrary("flows");
localfilesystemLibrary.getLibraryEntry('flows','B/flow').then(function(flows) {
flows.should.eql("Hi");
done();
}).catch(function(err) {
done(err);
});
});
});
it('should return a library object',function(done) {
localfilesystemLibrary.init({userDir:userDir}).then(function() {
createObjectLibrary();
localfilesystemLibrary.getLibraryEntry('object','B/file2.js').then(function(body) {
body.should.eql("// not a metaline \n\n Hi");
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should return a newly saved library function',function(done) {
localfilesystemLibrary.init({userDir:userDir}).then(function() {
createObjectLibrary("functions");
localfilesystemLibrary.getLibraryEntry('functions','B').then(function(flows) {
flows.should.eql([ 'C', { ghi: 'jkl', fn: 'file2.js' } ]);
var ft = path.join("B","D","file3.js");
localfilesystemLibrary.saveLibraryEntry('functions',ft,{mno:'pqr'},"// another non meta line\n\n Hi There").then(function() {
setTimeout(function() {
localfilesystemLibrary.getLibraryEntry('functions',path.join("B","D")).then(function(flows) {
flows.should.eql([ { mno: 'pqr', fn: 'file3.js' } ]);
localfilesystemLibrary.getLibraryEntry('functions',ft).then(function(body) {
body.should.eql("// another non meta line\n\n Hi There");
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
})
}, 50);
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should return a newly saved library flow',function(done) {
localfilesystemLibrary.init({userDir:userDir}).then(function() {
createObjectLibrary("flows");
localfilesystemLibrary.getLibraryEntry('flows','B').then(function(flows) {
flows.should.eql([ 'C', {fn:'flow.json'} ]);
var ft = path.join("B","D","file3");
localfilesystemLibrary.saveLibraryEntry('flows',ft,{mno:'pqr'},"Hi").then(function() {
setTimeout(function() {
localfilesystemLibrary.getLibraryEntry('flows',path.join("B","D")).then(function(flows) {
flows.should.eql([ { mno: 'pqr', fn: 'file3.json' } ]);
localfilesystemLibrary.getLibraryEntry('flows',ft+".json").then(function(body) {
body.should.eql("Hi");
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
})
}, 50);
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
});

View File

@@ -0,0 +1,19 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* 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.
**/
describe("storage/localfilesystem/projects/Project", function() {
it.skip("NEEDS TESTS WRITING",function() {});
})

View File

@@ -0,0 +1,63 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* 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 defaultFileSet = require("../../../../../../red/runtime/storage/localfilesystem/projects/defaultFileSet");
describe('storage/localfilesystem/projects/defaultFileSet', function() {
var runtime = {
i18n: {
"_": function(name) {
return name;
}
}
};
it('generates package.json for a project', function() {
var generated = defaultFileSet["package.json"]({
name: "A TEST NAME",
summary: "A TEST SUMMARY",
files: {
flow: "MY FLOW FILE",
credentials: "MY CREDENTIALS FILE"
}
}, runtime);
var parsed = JSON.parse(generated);
parsed.should.have.property('name',"A TEST NAME");
parsed.should.have.property('description',"A TEST SUMMARY");
parsed.should.have.property('node-red');
parsed['node-red'].should.have.property('settings');
parsed['node-red'].settings.should.have.property('flowFile',"MY FLOW FILE");
parsed['node-red'].settings.should.have.property('credentialsFile',"MY CREDENTIALS FILE");
});
it('generates README.md for a project', function() {
var generated = defaultFileSet["README.md"]({
name: "A TEST NAME",
summary: "A TEST SUMMARY"
}, runtime);
generated.should.match(/A TEST NAME/);
generated.should.match(/A TEST SUMMARY/);
});
it('generates .gitignore for a project', function() {
var generated = defaultFileSet[".gitignore"]({
name: "A TEST NAME",
summary: "A TEST SUMMARY"
}, runtime);
generated.length.should.be.greaterThan(0);
});
});

View File

@@ -0,0 +1,83 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* 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 authCache = require("../../../../../../../red/runtime/storage/localfilesystem/projects/git/authCache")
describe("localfilesystem/projects/git/authCache", function() {
beforeEach(function() {
authCache.init();
});
afterEach(function() {
authCache.init();
});
it('sets/clears auth details for a given project/remote/user', function() {
should.not.exist(authCache.get("project","remote1","user1"));
should.not.exist(authCache.get("project","remote1","user2"));
authCache.set("project","remote1","user1",{foo1:"bar1"});
authCache.set("project","remote1","user2",{foo2:"bar2"});
var result = authCache.get("project","remote1","user1");
result.should.have.property("foo1","bar1");
result = authCache.get("project","remote1","user2");
result.should.have.property("foo2","bar2");
authCache.clear("project","remote1","user1");
should.not.exist(authCache.get("project","remote1","user1"));
should.exist(authCache.get("project","remote1","user2"));
});
it('clears auth details for all users on a given project/remote', function() {
authCache.set("project","remote1","user1",{foo1:"bar1"});
authCache.set("project","remote1","user2",{foo2:"bar2"});
authCache.set("project","remote2","user1",{foo3:"bar3"});
should.exist(authCache.get("project","remote1","user1"));
should.exist(authCache.get("project","remote1","user2"));
should.exist(authCache.get("project","remote2","user1"));
authCache.clear("project","remote1");
should.not.exist(authCache.get("project","remote1","user1"));
should.not.exist(authCache.get("project","remote1","user2"));
should.exist(authCache.get("project","remote2","user1"));
});
it('clears auth details for all remotes/users on a given project', function() {
authCache.set("project1","remote1","user1",{foo1:"bar1"});
authCache.set("project1","remote1","user2",{foo2:"bar2"});
authCache.set("project2","remote2","user1",{foo3:"bar3"});
should.exist(authCache.get("project1","remote1","user1"));
should.exist(authCache.get("project1","remote1","user2"));
should.exist(authCache.get("project2","remote2","user1"));
authCache.clear("project2");
should.exist(authCache.get("project1","remote1","user1"));
should.exist(authCache.get("project1","remote1","user2"));
should.not.exist(authCache.get("project2","remote2","user1"));
});
});

View File

@@ -0,0 +1,81 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* 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 net = require("net");
var path = require("path");
var os = require("os");
var should = require("should");
var sinon = require("sinon");
var child_process = require("child_process");
var fs = require("fs-extra");
var authServer = require("../../../../../../../red/runtime/storage/localfilesystem/projects/git/authServer");
var sendPrompt = function(localPath, prompt) {
return new Promise(function(resolve,reject) {
var response;
var socket = net.connect(localPath, function() {
socket.on('data', function(data) { response = data; socket.end() });
socket.on('end', function() {
resolve(response);
});
socket.on('error',reject);
socket.write(prompt+"\n", 'utf8');
});
socket.setEncoding('utf8');
});
}
describe("localfilesystem/projects/git/authServer", function() {
it("listens for user/pass prompts and returns provided auth", function(done) {
authServer.ResponseServer({username: "TEST_USER", password: "TEST_PASS"}).then(function(rs) {
sendPrompt(rs.path,"Username").then(function(response) {
response.should.eql("TEST_USER");
return sendPrompt(rs.path,"Password");
}).then(function(response) {
response.should.eql("TEST_PASS");
}).then(() => {
rs.close();
done();
}).catch(function(err) {
rs.close();
done(err);
})
})
});
it("listens for ssh prompts and returns provided auth", function(done) {
authServer.ResponseSSHServer({passphrase: "TEST_PASSPHRASE"}).then(function(rs) {
sendPrompt(rs.path,"The").then(function(response) {
// TODO:
response.should.eql("yes");
return sendPrompt(rs.path,"Enter");
}).then(function(response) {
response.should.eql("TEST_PASSPHRASE");
}).then(() => {
rs.close();
done();
}).catch(function(err) {
rs.close();
done(err);
})
})
})
});

View File

@@ -0,0 +1,81 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* 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 net = require("net");
var path = require("path");
var os = require("os");
var should = require("should");
var sinon = require("sinon");
var child_process = require("child_process");
var fs = require("fs-extra");
var authWriter = "../../../../../../../red/runtime/storage/localfilesystem/projects/git/authWriter";
function getListenPath() {
var seed = (0x100000+Math.random()*0x999999).toString(16);
var fn = 'node-red-git-askpass-'+seed+'-sock';
var listenPath;
if (process.platform === 'win32') {
listenPath = '\\\\.\\pipe\\'+fn;
} else {
listenPath = path.join(process.env['XDG_RUNTIME_DIR'] || os.tmpdir(), fn);
}
// console.log(listenPath);
return listenPath;
}
describe("localfilesystem/projects/git/authWriter", function() {
it("connects to port and sends passphrase", function(done) {
var receivedData = "";
var server = net.createServer(function(connection) {
connection.setEncoding('utf8');
connection.on('data', function(data) {
receivedData += data;
var m = data.indexOf("\n");
if (m !== -1) {
connection.end();
}
});
});
var listenPath = getListenPath();
server.listen(listenPath, function(ready) {
child_process.exec('"'+process.execPath+'" "'+authWriter+'" "'+listenPath+'" TEST_PHRASE_FOO',{cwd:__dirname}, (error,stdout,stderr) => {
server.close();
try {
should.not.exist(error);
receivedData.should.eql("TEST_PHRASE_FOO\n");
done();
} catch(err) {
done(err);
}
});
});
server.on('close', function() {
// console.log("Closing response server");
fs.removeSync(listenPath);
});
server.on('error',function(err) {
console.log("ResponseServer unexpectedError:",err.toString());
server.close();
done(err);
});
})
});

View File

@@ -0,0 +1,19 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* 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.
**/
describe("storage/localfilesystem/projects/git/index", function() {
it.skip("NEEDS TESTS WRITING",function() {});
})

View File

@@ -0,0 +1,19 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* 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.
**/
describe("storage/localfilesystem/projects/index", function() {
it.skip("NEEDS TESTS WRITING",function() {});
})

View File

@@ -0,0 +1,432 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* 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 fs = require('fs-extra');
var path = require('path');
var sshkeys = require("../../../../../../../red/runtime/storage/localfilesystem/projects/ssh");
describe("storage/localfilesystem/projects/ssh", function() {
var userDir = path.join(__dirname,".testSSHKeyUserHome");
var mockSettings = {
userDir: userDir
};
var mockRuntime = {
log:{
_:function() { return "placeholder message"},
info: function() { },
log: function() { },
trace: function() { }
}
};
var oldHOME;
beforeEach(function(done) {
oldHOME = process.env.HOME;
process.env.HOME = "/tmp/doesnt/exist";
fs.remove(userDir,function(err) {
fs.mkdir(userDir,done);
});
});
afterEach(function(done) {
process.env.HOME = oldHOME;
fs.remove(userDir,done);
});
it('should create sshkey directory when sshkey initializes', function(done) {
var sshkeyDirPath = path.join(userDir, 'projects', '.sshkeys');
sshkeys.init(mockSettings, mockRuntime).then(function() {
var ret = fs.existsSync(sshkeyDirPath);
ret.should.be.true();
done();
}).catch(function(err) {
done(err);
});
});
it('should get sshkey empty list if there is no sshkey file', function(done) {
var username = 'test';
sshkeys.init(mockSettings, mockRuntime).then(function() {
sshkeys.listSSHKeys(username).then(function(retObj) {
retObj.should.be.instanceOf(Array).and.have.lengthOf(0);
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should get sshkey list', function(done) {
var sshkeyDirPath = path.join(userDir, 'projects', '.sshkeys');
var username = 'test';
var filenameList = ['test-key01', 'test-key02'];
sshkeys.init(mockSettings, mockRuntime).then(function() {
for(var filename of filenameList) {
fs.writeFileSync(path.join(sshkeyDirPath,username+"_"+filename),"","utf8");
fs.writeFileSync(path.join(sshkeyDirPath,username+"_"+filename+".pub"),"","utf8");
}
sshkeys.listSSHKeys(username).then(function(retObj) {
retObj.should.be.instanceOf(Array).and.have.lengthOf(filenameList.length);
for(var filename of filenameList) {
retObj.should.containEql({ name: filename });
}
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should not get sshkey file if there is only private key', function(done) {
var sshkeyDirPath = path.join(userDir, 'projects', '.sshkeys');
var username = 'test';
var filenameList = ['test-key01', 'test-key02'];
var onlyPrivateKeyFilenameList = ['test-key03', 'test-key04'];
sshkeys.init(mockSettings, mockRuntime).then(function() {
for(var filename of filenameList) {
fs.writeFileSync(path.join(sshkeyDirPath,username+"_"+filename),"","utf8");
fs.writeFileSync(path.join(sshkeyDirPath,username+"_"+filename+".pub"),"","utf8");
}
for(var filename of onlyPrivateKeyFilenameList) {
fs.writeFileSync(path.join(sshkeyDirPath,username+"_"+filename),"","utf8");
}
sshkeys.listSSHKeys(username).then(function(retObj) {
retObj.should.be.instanceOf(Array).and.have.lengthOf(filenameList.length);
for(var filename of filenameList) {
retObj.should.containEql({ name: filename });
}
for(var filename of onlyPrivateKeyFilenameList) {
retObj.should.not.containEql({ name: filename });
}
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should not get sshkey file if there is only public key', function(done) {
var sshkeyDirPath = path.join(userDir, 'projects', '.sshkeys');
var username = 'test';
var filenameList = ['test-key01', 'test-key02'];
var directoryList = ['test-key03', '.test-key04'];
sshkeys.init(mockSettings, mockRuntime).then(function() {
for(var filename of filenameList) {
fs.writeFileSync(path.join(sshkeyDirPath,username+"_"+filename),"","utf8");
fs.writeFileSync(path.join(sshkeyDirPath,username+"_"+filename+".pub"),"","utf8");
}
for(var filename of directoryList) {
fs.ensureDirSync(path.join(sshkeyDirPath,filename));
}
sshkeys.listSSHKeys(username).then(function(retObj) {
retObj.should.be.instanceOf(Array).and.have.lengthOf(filenameList.length);
for(var filename of filenameList) {
retObj.should.containEql({ name: filename });
}
for(var directoryname of directoryList) {
retObj.should.not.containEql({ name: directoryname });
}
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should get sshkey list that does not have directory', function(done) {
var sshkeyDirPath = path.join(userDir, 'projects', '.sshkeys');
var username = 'test';
var otherUsername = 'other';
var filenameList = ['test-key01', 'test-key02'];
var otherUserFilenameList = ['test-key03', 'test-key04'];
sshkeys.init(mockSettings, mockRuntime).then(function() {
for(var filename of filenameList) {
fs.writeFileSync(path.join(sshkeyDirPath,username+"_"+filename),"","utf8");
fs.writeFileSync(path.join(sshkeyDirPath,username+"_"+filename+".pub"),"","utf8");
}
for(var filename of otherUserFilenameList) {
fs.writeFileSync(path.join(sshkeyDirPath,otherUsername+"_"+filename),"","utf8");
fs.writeFileSync(path.join(sshkeyDirPath,otherUsername+"_"+filename+".pub"),"","utf8");
}
sshkeys.listSSHKeys(username).then(function(retObj) {
retObj.should.be.instanceOf(Array).and.have.lengthOf(filenameList.length);
for(var filename of filenameList) {
retObj.should.containEql({ name: filename });
}
for(var filename of otherUserFilenameList) {
retObj.should.not.containEql({ name: filename });
}
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should get sshkey list that have keys of specified user', function(done) {
var sshkeyDirPath = path.join(userDir, 'projects', '.sshkeys');
var username = 'test';
var otherUsername = 'other';
var filenameList = ['test-key01', 'test-key02'];
var otherUserFilenameList = ['test-key03', 'test-key04'];
sshkeys.init(mockSettings, mockRuntime).then(function() {
for(var filename of filenameList) {
fs.writeFileSync(path.join(sshkeyDirPath,username+"_"+filename),"","utf8");
fs.writeFileSync(path.join(sshkeyDirPath,username+"_"+filename+".pub"),"","utf8");
}
for(var filename of otherUserFilenameList) {
fs.writeFileSync(path.join(sshkeyDirPath,otherUsername+"_"+filename),"","utf8");
fs.writeFileSync(path.join(sshkeyDirPath,otherUsername+"_"+filename+".pub"),"","utf8");
}
sshkeys.listSSHKeys(username).then(function(retObj) {
retObj.should.be.instanceOf(Array).and.have.lengthOf(filenameList.length);
for(var filename of filenameList) {
retObj.should.containEql({ name: filename });
}
for(var filename of otherUserFilenameList) {
retObj.should.not.containEql({ name: filename });
}
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should generate sshkey file with empty data', function(done) {
this.timeout(10000);
var sshkeyDirPath = path.join(userDir, 'projects', '.sshkeys');
var username = 'test';
var options = {
name: 'test-key01'
};
sshkeys.init(mockSettings, mockRuntime).then(function() {
sshkeys.generateSSHKey(username, options).then(function(retObj) {
retObj.should.be.equal(options.name);
fs.existsSync(path.join(sshkeyDirPath,username+'_'+options.name)).should.be.true();
fs.existsSync(path.join(sshkeyDirPath,username+'_'+options.name+'.pub')).should.be.true();
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should generate sshkey file with only comment data', function(done) {
this.timeout(10000);
var sshkeyDirPath = path.join(userDir, 'projects', '.sshkeys');
var username = 'test';
var options = {
comment: 'test@test.com',
name: 'test-key01'
};
sshkeys.init(mockSettings, mockRuntime).then(function() {
sshkeys.generateSSHKey(username, options).then(function(retObj) {
retObj.should.be.equal(options.name);
fs.existsSync(path.join(sshkeyDirPath,username+'_'+options.name)).should.be.true();
fs.existsSync(path.join(sshkeyDirPath,username+'_'+options.name+'.pub')).should.be.true();
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should generate sshkey file with password data', function(done) {
this.timeout(10000);
var sshkeyDirPath = path.join(userDir, 'projects', '.sshkeys');
var username = 'test';
var options = {
comment: 'test@test.com',
name: 'test-key01',
password: 'testtest'
};
sshkeys.init(mockSettings, mockRuntime).then(function() {
sshkeys.generateSSHKey(username, options).then(function(retObj) {
retObj.should.be.equal(options.name);
fs.existsSync(path.join(sshkeyDirPath,username+'_'+options.name)).should.be.true();
fs.existsSync(path.join(sshkeyDirPath,username+'_'+options.name+'.pub')).should.be.true();
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should generate sshkey file with size data', function(done) {
this.timeout(20000);
var sshkeyDirPath = path.join(userDir, 'projects', '.sshkeys');
var username = 'test';
var options = {
comment: 'test@test.com',
name: 'test-key01',
size: 4096
};
sshkeys.init(mockSettings, mockRuntime).then(function() {
sshkeys.generateSSHKey(username, options).then(function(retObj) {
retObj.should.be.equal(options.name);
fs.existsSync(path.join(sshkeyDirPath,username+'_'+options.name)).should.be.true();
fs.existsSync(path.join(sshkeyDirPath,username+'_'+options.name+'.pub')).should.be.true();
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should generate sshkey file with password & size data', function(done) {
this.timeout(20000);
var sshkeyDirPath = path.join(userDir, 'projects', '.sshkeys');
var username = 'test';
var options = {
comment: 'test@test.com',
name: 'test-key01',
password: 'testtest',
size: 4096
};
sshkeys.init(mockSettings, mockRuntime).then(function() {
sshkeys.generateSSHKey(username, options).then(function(retObj) {
retObj.should.be.equal(options.name);
fs.existsSync(path.join(sshkeyDirPath,username+'_'+options.name)).should.be.true();
fs.existsSync(path.join(sshkeyDirPath,username+'_'+options.name+'.pub')).should.be.true();
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should not generate sshkey file with illegal size data', function(done) {
this.timeout(10000);
var sshkeyDirPath = path.join(userDir, 'projects', '.sshkeys');
var username = 'test';
var options = {
comment: 'test@test.com',
name: 'test-key01',
size: 1023
};
sshkeys.init(mockSettings, mockRuntime).then(function() {
sshkeys.generateSSHKey(username, options).then(function(retObj) {
done(new Error('Does NOT throw error!'));
}).catch(function(err) {
try {
err.should.have.property('code', 'key_length_too_short');
done();
}
catch (error) {
done(error);
}
});
}).catch(function(err) {
done(err);
});
});
it('should not generate sshkey file with illegal password', function(done) {
this.timeout(10000);
var sshkeyDirPath = path.join(userDir, 'projects', '.sshkeys');
var username = 'test';
var options = {
comment: 'test@test.com',
name: 'test-key01',
password: 'aa'
};
sshkeys.init(mockSettings, mockRuntime).then(function() {
sshkeys.generateSSHKey(username, options).then(function(retObj) {
done(new Error('Does NOT throw error!'));
}).catch(function(err) {
try {
err.should.have.property('code', 'key_passphrase_too_short');
done();
}
catch (error) {
done(error);
}
});
}).catch(function(err) {
done(err);
});
});
it('should get sshkey file content', function(done) {
var sshkeyDirPath = path.join(userDir, 'projects', '.sshkeys');
var username = 'test';
var filename = 'test-key01';
var fileContent = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQD3a+sgtgzSbbliWxmOq5p6+H/mE+0gjWfLWrkIVmHENd1mifV4uCmIHAR2NfuadUYMQ3+bQ90kpmmEKTMYPsyentsKpHQZxTzG7wOCAIpJnbPTHDMxEJhVTaAwEjbVyMSIzTTPfnhoavWIBu0+uMgKDDlBm+RjlgkFlyhXyCN6UwFrIUUMH6Gw+eQHLiooKIl8ce7uDxIlt+9b7hFCU+sQ3kvuse239DZluu6+8buMWqJvrEHgzS9adRFKku8nSPAEPYn85vDi7OgVAcLQufknNgs47KHBAx9h04LeSrFJ/P5J1b//ItRpMOIme+O9d1BR46puzhvUaCHLdvO9czj+OmW+dIm+QIk6lZIOOMnppG72kZxtLfeKT16ur+2FbwAdL9ItBp4BI/YTlBPoa5mLMxpuWfmX1qHntvtGc9wEwS1P7YFfmF3XiK5apxalzrn0Qlr5UmDNbVIqJb1OlbC0w03Z0oktti1xT+R2DGOLWM4lBbpXDHV1BhQ7oYOvbUD8Cnof55lTP0WHHsOHlQc/BGDti1XA9aBX/OzVyzBUYEf0pkimsD0RYo6aqt7QwehJYdlz9x1NBguBffT0s4NhNb9IWr+ASnFPvNl2sw4XH/8U0J0q8ZkMpKkbLM1Zdp1Fv00GF0f5UNRokai6uM3w/ccantJ3WvZ6GtctqytWrw== \n";
sshkeys.init(mockSettings, mockRuntime).then(function() {
fs.writeFileSync(path.join(sshkeyDirPath,username+"_"+filename),"","utf8");
fs.writeFileSync(path.join(sshkeyDirPath,username+"_"+filename+".pub"),fileContent,"utf8");
sshkeys.getSSHKey(username, filename).then(function(retObj) {
retObj.should.be.equal(fileContent);
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
it('should delete sshkey files', function(done) {
var sshkeyDirPath = path.join(userDir, 'projects', '.sshkeys');
var username = 'test';
var filename = 'test-key01';
var fileContent = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQD3a+sgtgzSbbliWxmOq5p6+H/mE+0gjWfLWrkIVmHENd1mifV4uCmIHAR2NfuadUYMQ3+bQ90kpmmEKTMYPsyentsKpHQZxTzG7wOCAIpJnbPTHDMxEJhVTaAwEjbVyMSIzTTPfnhoavWIBu0+uMgKDDlBm+RjlgkFlyhXyCN6UwFrIUUMH6Gw+eQHLiooKIl8ce7uDxIlt+9b7hFCU+sQ3kvuse239DZluu6+8buMWqJvrEHgzS9adRFKku8nSPAEPYn85vDi7OgVAcLQufknNgs47KHBAx9h04LeSrFJ/P5J1b//ItRpMOIme+O9d1BR46puzhvUaCHLdvO9czj+OmW+dIm+QIk6lZIOOMnppG72kZxtLfeKT16ur+2FbwAdL9ItBp4BI/YTlBPoa5mLMxpuWfmX1qHntvtGc9wEwS1P7YFfmF3XiK5apxalzrn0Qlr5UmDNbVIqJb1OlbC0w03Z0oktti1xT+R2DGOLWM4lBbpXDHV1BhQ7oYOvbUD8Cnof55lTP0WHHsOHlQc/BGDti1XA9aBX/OzVyzBUYEf0pkimsD0RYo6aqt7QwehJYdlz9x1NBguBffT0s4NhNb9IWr+ASnFPvNl2sw4XH/8U0J0q8ZkMpKkbLM1Zdp1Fv00GF0f5UNRokai6uM3w/ccantJ3WvZ6GtctqytWrw== \n";
sshkeys.init(mockSettings, mockRuntime).then(function() {
fs.writeFileSync(path.join(sshkeyDirPath,username+"_"+filename),"","utf8");
fs.writeFileSync(path.join(sshkeyDirPath,username+"_"+filename+".pub"),fileContent,"utf8");
sshkeys.deleteSSHKey(username, filename).then(function() {
fs.existsSync(path.join(sshkeyDirPath,username+'_'+filename)).should.be.false();
fs.existsSync(path.join(sshkeyDirPath,username+'_'+filename+'.pub')).should.be.false();
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
});

View File

@@ -0,0 +1,109 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* 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 child_process = require('child_process');
var EventEmitter = require("events");
var keygen = require("../../../../../../../red/runtime/storage/localfilesystem/projects/ssh/keygen")
describe("localfilesystem/projects/ssh/keygen", function() {
afterEach(function() {
if (child_process.spawn.restore) {
child_process.spawn.restore();
}
})
it("invokes sshkeygen", function(done) {
var command;
var args;
var opts;
sinon.stub(child_process,"spawn", function(_command,_args,_opts) {
_command = command;
_args = args;
_opts = opts;
var e = new EventEmitter();
e.stdout = new EventEmitter();
e.stderr = new EventEmitter();
setTimeout(function() {
e.stdout.emit("data","result");
e.emit("close",0);
},50)
return e;
});
keygen.generateKey({
size: 1024,
location: 'location',
comment: 'comment',
password: 'password'
}).then(function(output) {
output.should.equal("result");
done();
}).catch(function(err) {
done(err);
})
})
it("reports passphrase too short", function(done) {
var command;
var args;
var opts;
try {
keygen.generateKey({
size: 1024,
location: 'location',
comment: 'comment',
password: '123'
}).then(function(output) {
done(new Error("Error not thrown"));
}).catch(function(err) {
done(new Error("Error not thrown"));
})
} catch(err) {
err.should.have.property("code","key_passphrase_too_short");
done();
}
});
it("reports key length too short", function(done) {
var command;
var args;
var opts;
try {
keygen.generateKey({
size: 123,
location: 'location',
comment: 'comment',
password: 'password'
}).then(function(output) {
done(new Error("Error not thrown"));
}).catch(function(err) {
done(new Error("Error not thrown"));
})
} catch(err) {
err.should.have.property("code","key_length_too_short");
done();
}
});
});

View File

@@ -0,0 +1,79 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* 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 fs = require('fs-extra');
var path = require('path');
var localfilesystemSessions = require("../../../../../red/runtime/storage/localfilesystem/sessions");
describe('storage/localfilesystem/sessions', function() {
var userDir = path.join(__dirname,".testUserHome");
beforeEach(function(done) {
fs.remove(userDir,function(err) {
fs.mkdir(userDir,done);
});
});
afterEach(function(done) {
fs.remove(userDir,done);
});
it('should handle non-existent sessions', function(done) {
var sessionsFile = path.join(userDir,".sessions.json");
localfilesystemSessions.init({userDir:userDir});
fs.existsSync(sessionsFile).should.be.false();
localfilesystemSessions.getSessions().then(function(sessions) {
sessions.should.eql({});
done();
}).catch(function(err) {
done(err);
});
});
it('should handle corrupt sessions', function(done) {
var sessionsFile = path.join(userDir,".sessions.json");
fs.writeFileSync(sessionsFile,"[This is not json","utf8");
localfilesystemSessions.init({userDir:userDir});
fs.existsSync(sessionsFile).should.be.true();
localfilesystemSessions.getSessions().then(function(sessions) {
sessions.should.eql({});
done();
}).catch(function(err) {
done(err);
});
});
it('should handle sessions', function(done) {
var sessionsFile = path.join(userDir,".sessions.json");
localfilesystemSessions.init({userDir:userDir});
fs.existsSync(sessionsFile).should.be.false();
var sessions = {"abc":{"type":"creds"}};
localfilesystemSessions.saveSessions(sessions).then(function() {
fs.existsSync(sessionsFile).should.be.true();
localfilesystemSessions.getSessions().then(function(_sessions) {
_sessions.should.eql(sessions);
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
});

View File

@@ -0,0 +1,80 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* 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 fs = require('fs-extra');
var path = require('path');
var localfilesystemSettings = require("../../../../../red/runtime/storage/localfilesystem/settings");
describe('storage/localfilesystem/settings', function() {
var userDir = path.join(__dirname,".testUserHome");
beforeEach(function(done) {
fs.remove(userDir,function(err) {
fs.mkdir(userDir,done);
});
});
afterEach(function(done) {
fs.remove(userDir,done);
});
it('should handle non-existent settings', function(done) {
var settingsFile = path.join(userDir,".settings.json");
localfilesystemSettings.init({userDir:userDir});
fs.existsSync(settingsFile).should.be.false();
localfilesystemSettings.getSettings().then(function(settings) {
settings.should.eql({});
done();
}).catch(function(err) {
done(err);
});
});
it('should handle corrupt settings', function(done) {
var settingsFile = path.join(userDir,".config.json");
fs.writeFileSync(settingsFile,"[This is not json","utf8");
localfilesystemSettings.init({userDir:userDir});
fs.existsSync(settingsFile).should.be.true();
localfilesystemSettings.getSettings().then(function(settings) {
settings.should.eql({});
done();
}).catch(function(err) {
done(err);
});
});
it('should handle settings', function(done) {
var settingsFile = path.join(userDir,".config.json");
localfilesystemSettings.init({userDir:userDir});
fs.existsSync(settingsFile).should.be.false();
var settings = {"abc":{"type":"creds"}};
localfilesystemSettings.saveSettings(settings).then(function() {
fs.existsSync(settingsFile).should.be.true();
localfilesystemSettings.getSettings().then(function(_settings) {
_settings.should.eql(settings);
done();
}).catch(function(err) {
done(err);
});
}).catch(function(err) {
done(err);
});
});
});

View File

@@ -0,0 +1,31 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* 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 util = require("../../../../../red/runtime/storage/localfilesystem/util");
describe('storage/localfilesystem/util', function() {
describe('parseJSON', function() {
it('returns parsed JSON', function() {
var result = util.parseJSON('{"a":123}');
result.should.eql({a:123});
})
it('ignores BOM character', function() {
var result = util.parseJSON('\uFEFF{"a":123}');
result.should.eql({a:123});
})
})
});