node-red
This commit is contained in:
@@ -0,0 +1,518 @@
|
||||
/**
|
||||
* 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 NR_TEST_UTILS = require("nr-test-utils");
|
||||
var process = require("process");
|
||||
|
||||
var localfilesystem = NR_TEST_UTILS.require("@node-red/runtime/lib/storage/localfilesystem");
|
||||
var log = NR_TEST_UTILS.require("@node-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,getUserSettings: () => {{}}}, 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 = {getUserSettings: () => {{}}};
|
||||
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 = {getUserSettings: () => {{}}};
|
||||
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 = {getUserSettings: () => {{}}};
|
||||
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 = {getUserSettings: () => {{}}};
|
||||
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,getUserSettings: () => {{}}}, 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,getUserSettings: () => {{}}}, 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,getUserSettings: () => {{}}}, 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,getUserSettings: () => {{}}}, 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,getUserSettings: () => {{}}}, 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,getUserSettings: () => {{}}}, 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,getUserSettings: () => {{}}}, 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,getUserSettings: () => {{}}}, mockRuntime).then(function() {
|
||||
sinon.stub(fs,"fsync").callsFake(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,getUserSettings: () => {{}}}, 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,getUserSettings: () => {{}}}, 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,getUserSettings: () => {{}}}, 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,getUserSettings: () => {{}}}, 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,getUserSettings: () => {{}}}, 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);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle flow file in random unc path and non-existent subfolder',function(done) {
|
||||
// only test on win32
|
||||
if (process.platform !== 'win32') {
|
||||
console.log('skipped test as not win32');
|
||||
done();
|
||||
return;
|
||||
}
|
||||
|
||||
// get a real windows path
|
||||
var flowFile = path.win32.resolve(userDir+'/some/random/path');
|
||||
var rootdir = path.win32.resolve(userDir+'/some');
|
||||
// make it into a local UNC path
|
||||
flowFile = flowFile.replace('C:\\', '\\\\localhost\\c$\\');
|
||||
localfilesystem.init({userDir:userDir, flowFile:flowFile, getUserSettings: () => {{}}}, mockRuntime).then(function() {
|
||||
fs.existsSync(flowFile).should.be.false();
|
||||
localfilesystem.saveFlows(testFlow).then(function() {
|
||||
fs.existsSync(flowFile).should.be.true();
|
||||
localfilesystem.getFlows().then(function(flows) {
|
||||
flows.should.eql(testFlow);
|
||||
// cleanup
|
||||
fs.removeSync(rootdir);
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
// cleanup
|
||||
fs.removeSync(rootdir);
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
// cleanup
|
||||
fs.removeSync(rootdir);
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
// cleanup
|
||||
fs.removeSync(rootdir);
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
@@ -0,0 +1,244 @@
|
||||
/**
|
||||
* 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 NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var localfilesystemLibrary = NR_TEST_UTILS.require("@node-red/runtime/lib/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"));
|
||||
fs.mkdirSync(path.join(objLib, "D"));
|
||||
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');
|
||||
fs.writeFileSync(path.join(objLib, "D", "file3.js"), "// mno: 日本語テスト\n\nこんにちわ", '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', 'D', { 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([]);
|
||||
localfilesystemLibrary.getLibraryEntry('object', 'D').then(function (flows) {
|
||||
flows.should.eql([{ mno: '日本語テスト', fn: 'file3.js' }]);
|
||||
done();
|
||||
}).catch(function (err) {
|
||||
done(err);
|
||||
});
|
||||
}).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);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return a newly saved library flow (multi-byte character)',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","file4");
|
||||
localfilesystemLibrary.saveLibraryEntry('flows',ft,{mno:'pqr'},"こんにちわこんにちわこんにちわ").then(function() {
|
||||
setTimeout(function() {
|
||||
localfilesystemLibrary.getLibraryEntry('flows',path.join("B","D")).then(function(flows) {
|
||||
flows.should.eql([ { mno: 'pqr', fn: 'file4.json' } ]);
|
||||
localfilesystemLibrary.getLibraryEntry('flows',ft+".json").then(function(body) {
|
||||
body.should.eql("こんにちわこんにちわこんにちわ");
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* 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 NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
describe("storage/localfilesystem/projects/Project", function() {
|
||||
it.skip("NEEDS TESTS WRITING",function() {});
|
||||
})
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* 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 NR_TEST_UTILS = require("nr-test-utils");
|
||||
var defaultFileSet = NR_TEST_UTILS.require("@node-red/runtime/lib/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);
|
||||
});
|
||||
});
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* 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 NR_TEST_UTILS = require("nr-test-utils");
|
||||
var authCache = NR_TEST_UTILS.require("@node-red/runtime/lib/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"));
|
||||
});
|
||||
|
||||
});
|
||||
+83
@@ -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 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 NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var authServer = NR_TEST_UTILS.require("@node-red/runtime/lib/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);
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
});
|
||||
+83
@@ -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 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 NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var authWriter = NR_TEST_UTILS.resolve("@node-red/runtime/lib/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);
|
||||
});
|
||||
|
||||
|
||||
})
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* 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 NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
describe("storage/localfilesystem/projects/git/index", function() {
|
||||
it.skip("NEEDS TESTS WRITING",function() {});
|
||||
})
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* 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 NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
describe("storage/localfilesystem/projects/index", function() {
|
||||
it.skip("NEEDS TESTS WRITING",function() {});
|
||||
})
|
||||
@@ -0,0 +1,433 @@
|
||||
/**
|
||||
* 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 NR_TEST_UTILS = require("nr-test-utils");
|
||||
var sshkeys = NR_TEST_UTILS.require("@node-red/runtime/lib/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);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* 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 NR_TEST_UTILS = require("nr-test-utils");
|
||||
var keygen = NR_TEST_UTILS.require("@node-red/runtime/lib/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").callsFake(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();
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
@@ -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 NR_TEST_UTILS = require("nr-test-utils");
|
||||
var localfilesystemSessions = NR_TEST_UTILS.require("@node-red/runtime/lib/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);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,134 @@
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
|
||||
const should = require("should");
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var localfilesystemSettings = NR_TEST_UTILS.require("@node-red/runtime/lib/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,".config.json");
|
||||
localfilesystemSettings.init({userDir:userDir}).then(function() {
|
||||
fs.existsSync(settingsFile).should.be.false();
|
||||
return localfilesystemSettings.getSettings();
|
||||
}).then(function(settings) {
|
||||
settings.should.eql({});
|
||||
done();
|
||||
}).catch(err => { done(err)});
|
||||
});
|
||||
|
||||
it('should migrate single config.json to multiple files', function(done) {
|
||||
var settingsFile = path.join(userDir,".config.json");
|
||||
fs.writeFileSync(settingsFile,JSON.stringify({
|
||||
nodes:{a:1},
|
||||
_credentialSecret: "foo",
|
||||
users:{b:2},
|
||||
projects: {c:3}
|
||||
}),"utf8");
|
||||
|
||||
async function checkFile(sectionName, expectedContents) {
|
||||
const file = path.join(userDir,".config."+sectionName+".json");
|
||||
fs.existsSync(file).should.be.true();
|
||||
var contents = await fs.readFile(file,'utf8');
|
||||
var data = JSON.parse(contents);
|
||||
data.should.eql(expectedContents)
|
||||
}
|
||||
|
||||
localfilesystemSettings.init({userDir:userDir}).then(async function() {
|
||||
// (For now) leave the old settings file in place
|
||||
fs.existsSync(settingsFile).should.be.true();
|
||||
await checkFile("nodes",{a:1})
|
||||
await checkFile("users",{b:2})
|
||||
await checkFile("projects",{c:3})
|
||||
await checkFile("runtime",{_credentialSecret:"foo"})
|
||||
done();
|
||||
}).catch(err => { done(err)});
|
||||
});
|
||||
|
||||
it('should load separate settings file', async function() {
|
||||
await fs.writeFile( path.join(userDir,".config.nodes.json"),JSON.stringify({a:1}),"utf8");
|
||||
await fs.writeFile( path.join(userDir,".config.users.json"),JSON.stringify({b:2}),"utf8");
|
||||
await fs.writeFile( path.join(userDir,".config.projects.json"),JSON.stringify({c:3}),"utf8");
|
||||
await fs.writeFile( path.join(userDir,".config.runtime.json"),JSON.stringify({_credentialSecret:"foo"}),"utf8");
|
||||
|
||||
return localfilesystemSettings.init({userDir:userDir})
|
||||
.then(localfilesystemSettings.getSettings)
|
||||
.then(settings => {
|
||||
settings.should.eql({
|
||||
nodes:{a:1},
|
||||
_credentialSecret: "foo",
|
||||
users:{b:2},
|
||||
projects: {c:3}
|
||||
})
|
||||
})
|
||||
});
|
||||
|
||||
it('should write only the files that need writing', async function() {
|
||||
await fs.writeFile( path.join(userDir,".config.nodes.json"),JSON.stringify({a:1}),"utf8");
|
||||
await fs.writeFile( path.join(userDir,".config.users.json"),JSON.stringify({b:2}),"utf8");
|
||||
await fs.writeFile( path.join(userDir,".config.projects.json"),JSON.stringify({c:3}),"utf8");
|
||||
await fs.writeFile( path.join(userDir,".config.runtime.json"),JSON.stringify({_credentialSecret:"foo"}),"utf8");
|
||||
|
||||
const fsStatNodes = await fs.stat(path.join(userDir,".config.nodes.json"))
|
||||
const fsStatUsers = await fs.stat(path.join(userDir,".config.users.json"))
|
||||
const fsStatProjects = await fs.stat(path.join(userDir,".config.projects.json"))
|
||||
const fsStatRuntime = await fs.stat(path.join(userDir,".config.runtime.json"))
|
||||
|
||||
return localfilesystemSettings.init({userDir:userDir}).then(function() {
|
||||
return new Promise(res => {
|
||||
setTimeout(function() {
|
||||
res();
|
||||
},10)
|
||||
});
|
||||
}).then(() => {
|
||||
return localfilesystemSettings.saveSettings({
|
||||
nodes:{d:4},
|
||||
_credentialSecret: "bar",
|
||||
users:{b:2},
|
||||
projects: {c:3}
|
||||
})
|
||||
}).then(async function() {
|
||||
|
||||
const newFsStatNodes = await fs.stat(path.join(userDir,".config.nodes.json"))
|
||||
const newFsStatUsers = await fs.stat(path.join(userDir,".config.users.json"))
|
||||
const newFsStatProjects = await fs.stat(path.join(userDir,".config.projects.json"))
|
||||
const newFsStatRuntime = await fs.stat(path.join(userDir,".config.runtime.json"))
|
||||
|
||||
// Not changed
|
||||
newFsStatUsers.mtimeMs.should.eql(fsStatUsers.mtimeMs);
|
||||
newFsStatProjects.mtimeMs.should.eql(fsStatProjects.mtimeMs);
|
||||
|
||||
// Changed
|
||||
newFsStatNodes.mtimeMs.should.not.eql(fsStatNodes.mtimeMs);
|
||||
newFsStatRuntime.mtimeMs.should.not.eql(fsStatRuntime.mtimeMs);
|
||||
|
||||
})
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
|
||||
const should = require("should");
|
||||
const NR_TEST_UTILS = require("nr-test-utils");
|
||||
const util = NR_TEST_UTILS.require("@node-red/runtime/lib/storage/localfilesystem/util");
|
||||
const { mkdtemp, readFile } = require('fs/promises');
|
||||
const { join } = require('path');
|
||||
const { tmpdir } = require('os');
|
||||
|
||||
describe('storage/localfilesystem/util', function() {
|
||||
describe('writeFile', function () {
|
||||
it('manages concurrent calls to modify the same file', async function () {
|
||||
const testDirectory = await mkdtemp(join(tmpdir(), 'nr-test-'));
|
||||
const testFile = join(testDirectory, 'foo.txt')
|
||||
const testBackupFile = testFile + '.$$$'
|
||||
|
||||
let counter = 0
|
||||
const promises = [
|
||||
util.writeFile(testFile, `update-${counter++}`, testBackupFile ),
|
||||
util.writeFile(testFile, `update-${counter++}`, testBackupFile ),
|
||||
util.writeFile(testFile, `update-${counter++}`, testBackupFile )
|
||||
]
|
||||
|
||||
await Promise.all(promises)
|
||||
|
||||
const result = await readFile(testFile, { encoding: 'utf-8' })
|
||||
result.should.equal('update-2')
|
||||
})
|
||||
})
|
||||
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});
|
||||
})
|
||||
})
|
||||
});
|
||||
Reference in New Issue
Block a user