2014-07-17 09:08:24 +02:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2014-07-17 09:08:24 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
2013-11-10 23:19:01 +01:00
|
|
|
var should = require("should");
|
|
|
|
var fs = require('fs-extra');
|
|
|
|
var path = require('path');
|
2017-11-17 18:29:33 +01:00
|
|
|
var sinon = require('sinon');
|
2013-11-10 23:19:01 +01:00
|
|
|
|
2017-08-23 18:31:33 +02:00
|
|
|
var localfilesystem = require("../../../../../red/runtime/storage/localfilesystem");
|
2018-01-16 12:21:54 +01:00
|
|
|
var log = require("../../../../../red/runtime/log");
|
2013-11-10 23:19:01 +01:00
|
|
|
|
2017-08-23 18:31:33 +02:00
|
|
|
describe('storage/localfilesystem', function() {
|
2017-09-20 11:30:07 +02:00
|
|
|
var mockRuntime = {
|
|
|
|
log:{
|
|
|
|
_:function() { return "placeholder message"},
|
2018-01-16 17:18:18 +01:00
|
|
|
info: function() { },
|
|
|
|
warn: function() { },
|
|
|
|
trace: function() {}
|
2017-09-20 11:30:07 +02:00
|
|
|
}
|
|
|
|
};
|
2013-11-10 23:19:01 +01:00
|
|
|
var userDir = path.join(__dirname,".testUserHome");
|
|
|
|
var testFlow = [{"type":"tab","id":"d8be2a6d.2741d8","label":"Sheet 1"}];
|
2014-03-31 15:37:38 +02:00
|
|
|
beforeEach(function(done) {
|
|
|
|
fs.remove(userDir,function(err) {
|
|
|
|
fs.mkdir(userDir,done);
|
|
|
|
});
|
2013-11-10 23:19:01 +01:00
|
|
|
});
|
2014-03-31 15:37:38 +02:00
|
|
|
afterEach(function(done) {
|
|
|
|
fs.remove(userDir,done);
|
2013-11-10 23:19:01 +01:00
|
|
|
});
|
|
|
|
|
2014-03-31 15:37:38 +02:00
|
|
|
it('should initialise the user directory',function(done) {
|
2017-09-20 11:30:07 +02:00
|
|
|
localfilesystem.init({userDir:userDir}, mockRuntime).then(function() {
|
2016-10-10 14:27:43 +02:00
|
|
|
fs.existsSync(path.join(userDir,"lib")).should.be.true();
|
|
|
|
fs.existsSync(path.join(userDir,"lib",'flows')).should.be.true();
|
2014-03-31 15:37:38 +02:00
|
|
|
done();
|
2014-05-07 01:33:50 +02:00
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
2013-11-12 18:13:06 +01:00
|
|
|
});
|
2013-11-10 23:19:01 +01:00
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
|
|
|
|
2017-07-03 16:22:49 +02:00
|
|
|
it('should set userDir to NRH if .config.json presents',function(done) {
|
2015-03-22 18:40:12 +01:00
|
|
|
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 = {};
|
2017-09-20 11:30:07 +02:00
|
|
|
localfilesystem.init(settings, mockRuntime).then(function() {
|
2015-03-22 18:40:12 +01:00
|
|
|
try {
|
2016-10-10 14:27:43 +02:00
|
|
|
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();
|
2015-03-22 18:40:12 +01:00
|
|
|
settings.userDir.should.equal(process.env.NODE_RED_HOME);
|
|
|
|
done();
|
|
|
|
} catch(err) {
|
|
|
|
done(err);
|
|
|
|
} finally {
|
|
|
|
process.env.NODE_RED_HOME = oldNRH;
|
|
|
|
}
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2017-07-03 16:22:49 +02:00
|
|
|
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 = {};
|
2017-09-20 11:30:07 +02:00
|
|
|
localfilesystem.init(settings, mockRuntime).then(function() {
|
2017-07-03 16:22:49 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-03-22 18:40:12 +01:00
|
|
|
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");
|
2017-07-03 16:22:49 +02:00
|
|
|
var oldHOMEPATH = process.env.HOMEPATH;
|
|
|
|
process.env.HOMEPATH = path.join(userDir,"HOMEPATH");
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2015-03-22 18:40:12 +01:00
|
|
|
fs.mkdirSync(process.env.HOME);
|
|
|
|
var settings = {};
|
2017-09-20 11:30:07 +02:00
|
|
|
localfilesystem.init(settings, mockRuntime).then(function() {
|
2015-03-22 18:40:12 +01:00
|
|
|
try {
|
2016-10-10 14:27:43 +02:00
|
|
|
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();
|
2015-03-22 18:40:12 +01:00
|
|
|
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;
|
2017-07-03 16:22:49 +02:00
|
|
|
process.env.HOMEPATH = oldHOMEPATH;
|
|
|
|
}
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set userDir to USERPROFILE/.node-red',function(done) {
|
|
|
|
var oldNRH = process.env.NODE_RED_HOME;
|
2017-07-21 12:12:04 +02:00
|
|
|
process.env.NODE_RED_HOME = path.join(userDir,"NRH");
|
2017-07-03 16:22:49 +02:00
|
|
|
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 = {};
|
2017-09-20 11:30:07 +02:00
|
|
|
localfilesystem.init(settings, mockRuntime).then(function() {
|
2017-07-03 16:22:49 +02:00
|
|
|
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;
|
2015-03-22 18:40:12 +01:00
|
|
|
}
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2013-11-10 23:19:01 +01:00
|
|
|
it('should handle missing flow file',function(done) {
|
2017-09-20 11:30:07 +02:00
|
|
|
localfilesystem.init({userDir:userDir}, mockRuntime).then(function() {
|
2013-11-12 18:13:06 +01:00
|
|
|
var flowFile = 'flows_'+require('os').hostname()+'.json';
|
|
|
|
var flowFilePath = path.join(userDir,flowFile);
|
2016-10-10 14:27:43 +02:00
|
|
|
fs.existsSync(flowFilePath).should.be.false();
|
2013-11-12 18:13:06 +01:00
|
|
|
localfilesystem.getFlows().then(function(flows) {
|
|
|
|
flows.should.eql([]);
|
|
|
|
done();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
2014-05-07 01:33:50 +02:00
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
2013-11-10 23:19:01 +01:00
|
|
|
});
|
|
|
|
});
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2016-03-11 23:58:11 +01:00
|
|
|
it('should handle empty flow file, no backup',function(done) {
|
2017-09-20 11:30:07 +02:00
|
|
|
localfilesystem.init({userDir:userDir}, mockRuntime).then(function() {
|
2016-03-11 23:58:11 +01:00
|
|
|
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'));
|
2016-10-10 14:27:43 +02:00
|
|
|
fs.existsSync(flowFilePath).should.be.true();
|
2016-03-11 23:58:11 +01:00
|
|
|
localfilesystem.getFlows().then(function(flows) {
|
|
|
|
flows.should.eql([]);
|
|
|
|
done();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle empty flow file, restores backup',function(done) {
|
2017-09-20 11:30:07 +02:00
|
|
|
localfilesystem.init({userDir:userDir}, mockRuntime).then(function() {
|
2016-03-11 23:58:11 +01:00
|
|
|
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'));
|
2016-10-10 14:27:43 +02:00
|
|
|
fs.existsSync(flowFilePath).should.be.true();
|
|
|
|
fs.existsSync(flowFileBackupPath).should.be.false();
|
2016-03-11 23:58:11 +01:00
|
|
|
fs.writeFileSync(flowFileBackupPath,JSON.stringify(testFlow));
|
2016-10-10 14:27:43 +02:00
|
|
|
fs.existsSync(flowFileBackupPath).should.be.true();
|
2017-04-12 21:54:31 +02:00
|
|
|
setTimeout(function() {
|
|
|
|
localfilesystem.getFlows().then(function(flows) {
|
|
|
|
flows.should.eql(testFlow);
|
|
|
|
done();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
},50);
|
2016-03-11 23:58:11 +01:00
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-11-10 23:19:01 +01:00
|
|
|
it('should save flows to the default file',function(done) {
|
2017-09-20 11:30:07 +02:00
|
|
|
localfilesystem.init({userDir:userDir}, mockRuntime).then(function() {
|
2013-11-12 18:13:06 +01:00
|
|
|
var flowFile = 'flows_'+require('os').hostname()+'.json';
|
|
|
|
var flowFilePath = path.join(userDir,flowFile);
|
2015-02-04 14:45:45 +01:00
|
|
|
var flowFileBackupPath = path.join(userDir,"."+flowFile+".backup");
|
2016-10-10 14:27:43 +02:00
|
|
|
fs.existsSync(flowFilePath).should.be.false();
|
|
|
|
fs.existsSync(flowFileBackupPath).should.be.false();
|
2013-11-12 18:13:06 +01:00
|
|
|
localfilesystem.saveFlows(testFlow).then(function() {
|
2016-10-10 14:27:43 +02:00
|
|
|
fs.existsSync(flowFilePath).should.be.true();
|
|
|
|
fs.existsSync(flowFileBackupPath).should.be.false();
|
2013-11-12 18:13:06 +01:00
|
|
|
localfilesystem.getFlows().then(function(flows) {
|
|
|
|
flows.should.eql(testFlow);
|
|
|
|
done();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
2013-11-10 23:19:01 +01:00
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
2014-05-07 01:33:50 +02:00
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
2013-11-10 23:19:01 +01:00
|
|
|
});
|
|
|
|
});
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2013-11-10 23:19:01 +01:00
|
|
|
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);
|
|
|
|
|
2017-09-20 11:30:07 +02:00
|
|
|
localfilesystem.init({userDir:userDir, flowFile:flowFilePath}, mockRuntime).then(function() {
|
2016-10-10 14:27:43 +02:00
|
|
|
fs.existsSync(defaultFlowFilePath).should.be.false();
|
|
|
|
fs.existsSync(flowFilePath).should.be.false();
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2013-11-12 18:13:06 +01:00
|
|
|
localfilesystem.saveFlows(testFlow).then(function() {
|
2016-10-10 14:27:43 +02:00
|
|
|
fs.existsSync(defaultFlowFilePath).should.be.false();
|
|
|
|
fs.existsSync(flowFilePath).should.be.true();
|
2013-11-12 18:13:06 +01:00
|
|
|
localfilesystem.getFlows().then(function(flows) {
|
|
|
|
flows.should.eql(testFlow);
|
|
|
|
done();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
2013-11-10 23:19:01 +01:00
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
2014-05-07 01:33:50 +02:00
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
2013-11-10 23:19:01 +01:00
|
|
|
});
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2015-03-22 18:40:12 +01:00
|
|
|
it('should format the flows file when flowFilePretty specified',function(done) {
|
|
|
|
var flowFile = 'test.json';
|
|
|
|
var flowFilePath = path.join(userDir,flowFile);
|
2017-09-20 11:30:07 +02:00
|
|
|
localfilesystem.init({userDir:userDir, flowFile:flowFilePath,flowFilePretty:true}, mockRuntime).then(function() {
|
2015-03-22 18:40:12 +01:00
|
|
|
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();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2017-11-17 18:29:33 +01:00
|
|
|
it('should fsync the flows file',function(done) {
|
|
|
|
var flowFile = 'test.json';
|
|
|
|
var flowFilePath = path.join(userDir,flowFile);
|
2018-01-19 00:13:55 +01:00
|
|
|
localfilesystem.init({editorTheme:{projects:{enabled:false}},userDir:userDir, flowFile:flowFilePath}, mockRuntime).then(function() {
|
2017-11-17 18:29:33 +01:00
|
|
|
sinon.spy(fs,"fsync");
|
|
|
|
localfilesystem.saveFlows(testFlow).then(function() {
|
|
|
|
fs.fsync.callCount.should.eql(1);
|
|
|
|
fs.fsync.restore();
|
|
|
|
done();
|
|
|
|
}).otherwise(function(err) {
|
2018-01-19 00:13:55 +01:00
|
|
|
fs.fsync.restore();
|
2017-11-17 18:29:33 +01:00
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should log fsync errors and continue',function(done) {
|
|
|
|
var flowFile = 'test.json';
|
|
|
|
var flowFilePath = path.join(userDir,flowFile);
|
2018-01-16 17:18:18 +01:00
|
|
|
localfilesystem.init({userDir:userDir, flowFile:flowFilePath}, mockRuntime).then(function() {
|
2017-11-17 18:29:33 +01:00
|
|
|
sinon.stub(fs,"fsync", function(fd, cb) {
|
|
|
|
cb(new Error());
|
|
|
|
});
|
|
|
|
sinon.spy(log,"warn");
|
|
|
|
localfilesystem.saveFlows(testFlow).then(function() {
|
|
|
|
log.warn.callCount.should.eql(1);
|
2018-01-16 12:21:54 +01:00
|
|
|
log.warn.restore();
|
2017-11-17 18:29:33 +01:00
|
|
|
fs.fsync.callCount.should.eql(1);
|
|
|
|
fs.fsync.restore();
|
|
|
|
done();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-05-07 01:33:50 +02:00
|
|
|
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);
|
2015-02-04 14:45:45 +01:00
|
|
|
var flowFileBackupPath = path.join(userDir,"."+flowFile+".backup");
|
2014-05-07 01:33:50 +02:00
|
|
|
|
2017-09-20 11:30:07 +02:00
|
|
|
localfilesystem.init({userDir:userDir, flowFile:flowFilePath}, mockRuntime).then(function() {
|
2016-10-10 14:27:43 +02:00
|
|
|
fs.existsSync(defaultFlowFilePath).should.be.false();
|
|
|
|
fs.existsSync(flowFilePath).should.be.false();
|
|
|
|
fs.existsSync(flowFileBackupPath).should.be.false();
|
2014-05-07 01:33:50 +02:00
|
|
|
|
|
|
|
localfilesystem.saveFlows(testFlow).then(function() {
|
2016-10-10 14:27:43 +02:00
|
|
|
fs.existsSync(flowFileBackupPath).should.be.false();
|
|
|
|
fs.existsSync(defaultFlowFilePath).should.be.false();
|
|
|
|
fs.existsSync(flowFilePath).should.be.true();
|
2014-05-07 01:33:50 +02:00
|
|
|
var content = fs.readFileSync(flowFilePath,'utf8');
|
|
|
|
var testFlow2 = [{"type":"tab","id":"bc5672ad.2741d8","label":"Sheet 2"}];
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2014-05-07 01:33:50 +02:00
|
|
|
localfilesystem.saveFlows(testFlow2).then(function() {
|
2016-10-10 14:27:43 +02:00
|
|
|
fs.existsSync(flowFileBackupPath).should.be.true();
|
|
|
|
fs.existsSync(defaultFlowFilePath).should.be.false();
|
|
|
|
fs.existsSync(flowFilePath).should.be.true();
|
2014-05-07 01:33:50 +02:00
|
|
|
var backupContent = fs.readFileSync(flowFileBackupPath,'utf8');
|
|
|
|
content.should.equal(backupContent);
|
|
|
|
var content2 = fs.readFileSync(flowFilePath,'utf8');
|
|
|
|
content2.should.not.equal(backupContent);
|
|
|
|
done();
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2014-05-07 01:33:50 +02:00
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2014-05-07 01:33:50 +02:00
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
|
|
|
|
2014-05-07 01:33:50 +02:00
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2013-11-10 23:19:01 +01:00
|
|
|
it('should handle missing credentials', function(done) {
|
2014-05-02 15:35:51 +02:00
|
|
|
var flowFile = 'test.json';
|
|
|
|
var flowFilePath = path.join(userDir,flowFile);
|
|
|
|
var credFile = path.join(userDir,"test_cred.json");
|
2017-09-20 11:30:07 +02:00
|
|
|
localfilesystem.init({userDir:userDir, flowFile:flowFilePath}, mockRuntime).then(function() {
|
2016-10-10 14:27:43 +02:00
|
|
|
fs.existsSync(credFile).should.be.false();
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2013-11-12 18:13:06 +01:00
|
|
|
localfilesystem.getCredentials().then(function(creds) {
|
|
|
|
creds.should.eql({});
|
|
|
|
done();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
2014-05-07 01:33:50 +02:00
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
2013-11-10 23:19:01 +01:00
|
|
|
});
|
|
|
|
});
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2013-11-10 23:19:01 +01:00
|
|
|
it('should handle credentials', function(done) {
|
2014-05-03 23:38:33 +02:00
|
|
|
var flowFile = 'test.json';
|
|
|
|
var flowFilePath = path.join(userDir,flowFile);
|
2014-05-02 15:35:51 +02:00
|
|
|
var credFile = path.join(userDir,"test_cred.json");
|
2013-11-10 23:19:01 +01:00
|
|
|
|
2017-09-20 11:30:07 +02:00
|
|
|
localfilesystem.init({userDir:userDir, flowFile:flowFilePath}, mockRuntime).then(function() {
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2016-10-10 14:27:43 +02:00
|
|
|
fs.existsSync(credFile).should.be.false();
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2013-11-12 18:13:06 +01:00
|
|
|
var credentials = {"abc":{"type":"creds"}};
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2013-11-12 18:13:06 +01:00
|
|
|
localfilesystem.saveCredentials(credentials).then(function() {
|
2016-10-10 14:27:43 +02:00
|
|
|
fs.existsSync(credFile).should.be.true();
|
2013-11-12 18:13:06 +01:00
|
|
|
localfilesystem.getCredentials().then(function(creds) {
|
|
|
|
creds.should.eql(credentials);
|
|
|
|
done();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
2013-11-10 23:19:01 +01:00
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
2014-05-07 01:33:50 +02:00
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
2013-11-10 23:19:01 +01:00
|
|
|
});
|
|
|
|
});
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2015-03-22 18:40:12 +01:00
|
|
|
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");
|
|
|
|
|
2017-09-20 11:30:07 +02:00
|
|
|
localfilesystem.init({userDir:userDir, flowFile:flowFilePath}, mockRuntime).then(function() {
|
2015-03-22 18:40:12 +01:00
|
|
|
|
|
|
|
fs.writeFileSync(credFile,"{}","utf8");
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2016-10-10 14:27:43 +02:00
|
|
|
fs.existsSync(credFile).should.be.true();
|
|
|
|
fs.existsSync(credFileBackup).should.be.false();
|
2015-03-22 18:40:12 +01:00
|
|
|
|
|
|
|
var credentials = {"abc":{"type":"creds"}};
|
|
|
|
|
|
|
|
localfilesystem.saveCredentials(credentials).then(function() {
|
2016-10-10 14:27:43 +02:00
|
|
|
fs.existsSync(credFile).should.be.true();
|
|
|
|
fs.existsSync(credFileBackup).should.be.true();
|
2015-03-22 18:40:12 +01:00
|
|
|
done();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2015-03-22 18:40:12 +01:00
|
|
|
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");
|
|
|
|
|
2017-09-20 11:30:07 +02:00
|
|
|
localfilesystem.init({userDir:userDir, flowFile:flowFilePath, flowFilePretty:true}, mockRuntime).then(function() {
|
2015-03-22 18:40:12 +01:00
|
|
|
|
2016-10-10 14:27:43 +02:00
|
|
|
fs.existsSync(credFile).should.be.false();
|
2015-03-22 18:40:12 +01:00
|
|
|
|
|
|
|
var credentials = {"abc":{"type":"creds"}};
|
|
|
|
|
|
|
|
localfilesystem.saveCredentials(credentials).then(function() {
|
2016-10-10 14:27:43 +02:00
|
|
|
fs.existsSync(credFile).should.be.true();
|
2015-03-22 18:40:12 +01:00
|
|
|
var content = fs.readFileSync(credFile,"utf8");
|
|
|
|
content.split("\n").length.should.be.above(1);
|
|
|
|
localfilesystem.getCredentials().then(function(creds) {
|
|
|
|
creds.should.eql(credentials);
|
|
|
|
done();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
2013-11-10 23:19:01 +01:00
|
|
|
});
|