2014-11-04 18:05:29 +01:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2014-11-04 18:05:29 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
var should = require("should");
|
|
|
|
var request = require('supertest');
|
|
|
|
var express = require('express');
|
2015-07-15 23:43:24 +02:00
|
|
|
var bodyParser = require('body-parser');
|
2014-11-04 18:05:29 +01:00
|
|
|
var sinon = require('sinon');
|
|
|
|
var when = require('when');
|
|
|
|
|
2017-08-22 23:26:29 +02:00
|
|
|
var flows = require("../../../../red/api/admin/flows");
|
2014-11-04 18:05:29 +01:00
|
|
|
|
2017-09-20 11:30:07 +02:00
|
|
|
describe("api/admin/flows", function() {
|
2015-07-15 23:43:24 +02:00
|
|
|
|
2014-11-04 18:05:29 +01:00
|
|
|
var app;
|
|
|
|
|
|
|
|
before(function() {
|
|
|
|
app = express();
|
2015-07-15 23:43:24 +02:00
|
|
|
app.use(bodyParser.json());
|
2014-11-04 18:05:29 +01:00
|
|
|
app.get("/flows",flows.get);
|
|
|
|
app.post("/flows",flows.post);
|
|
|
|
});
|
2015-07-15 23:43:24 +02:00
|
|
|
|
2016-10-09 23:02:24 +02:00
|
|
|
it('returns flow - v1', function(done) {
|
2015-11-11 23:11:02 +01:00
|
|
|
flows.init({
|
2016-05-17 23:09:57 +02:00
|
|
|
settings: {},
|
2015-11-11 23:11:02 +01:00
|
|
|
log:{warn:function(){},_:function(){},audit:function(){}},
|
2015-11-24 23:38:42 +01:00
|
|
|
nodes:{
|
2016-10-09 23:02:24 +02:00
|
|
|
getFlows: function() { return {rev:"123",flows:[1,2,3]}; }
|
2015-11-11 23:11:02 +01:00
|
|
|
}
|
2014-11-04 18:05:29 +01:00
|
|
|
});
|
|
|
|
request(app)
|
|
|
|
.get('/flows')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err,res) {
|
|
|
|
if (err) {
|
2015-11-20 23:59:08 +01:00
|
|
|
return done(err);
|
2014-11-04 18:05:29 +01:00
|
|
|
}
|
2016-10-09 23:02:24 +02:00
|
|
|
try {
|
|
|
|
res.body.should.have.lengthOf(3);
|
|
|
|
done();
|
|
|
|
} catch(e) {
|
|
|
|
return done(e);
|
|
|
|
}
|
2014-11-04 18:05:29 +01:00
|
|
|
});
|
|
|
|
});
|
2016-10-09 23:02:24 +02:00
|
|
|
it('returns flow - v2', function(done) {
|
|
|
|
flows.init({
|
|
|
|
settings: {},
|
|
|
|
log:{warn:function(){},_:function(){},audit:function(){}},
|
|
|
|
nodes:{
|
|
|
|
getFlows: function() { return {rev:"123",flows:[1,2,3]}; }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
request(app)
|
|
|
|
.get('/flows')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Node-RED-API-Version','v2')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err,res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
res.body.should.have.a.property('rev','123');
|
|
|
|
res.body.should.have.a.property('flows');
|
|
|
|
res.body.flows.should.have.lengthOf(3);
|
|
|
|
done();
|
|
|
|
} catch(e) {
|
|
|
|
return done(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('returns flow - bad version', function(done) {
|
|
|
|
request(app)
|
|
|
|
.get('/flows')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Node-RED-API-Version','xxx')
|
|
|
|
.expect(400)
|
|
|
|
.end(function(err,res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
try {
|
2016-10-12 11:33:51 +02:00
|
|
|
res.body.should.have.a.property('code','invalid_api_version');
|
2016-10-09 23:02:24 +02:00
|
|
|
done();
|
|
|
|
} catch(e) {
|
|
|
|
return done(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('sets flows - default - v1', function(done) {
|
2015-11-20 23:59:08 +01:00
|
|
|
var setFlows = sinon.spy(function() { return when.resolve();});
|
2015-11-11 23:11:02 +01:00
|
|
|
flows.init({
|
|
|
|
log:{warn:function(){},_:function(){},audit:function(){}},
|
2015-11-24 23:38:42 +01:00
|
|
|
nodes:{
|
2015-11-20 23:59:08 +01:00
|
|
|
setFlows: setFlows
|
2015-11-11 23:11:02 +01:00
|
|
|
}
|
2014-11-04 18:05:29 +01:00
|
|
|
});
|
|
|
|
request(app)
|
|
|
|
.post('/flows')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect(204)
|
|
|
|
.end(function(err,res) {
|
|
|
|
if (err) {
|
2015-11-20 23:59:08 +01:00
|
|
|
return done(err);
|
2014-11-04 18:05:29 +01:00
|
|
|
}
|
2016-10-10 14:27:43 +02:00
|
|
|
setFlows.calledOnce.should.be.true();
|
2015-11-20 23:59:08 +01:00
|
|
|
setFlows.lastCall.args[1].should.eql('full');
|
2014-11-04 18:05:29 +01:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2016-10-09 23:02:24 +02:00
|
|
|
it('sets flows - non-default - v1', function(done) {
|
2015-11-20 23:59:08 +01:00
|
|
|
var setFlows = sinon.spy(function() { return when.resolve();});
|
|
|
|
flows.init({
|
|
|
|
log:{warn:function(){},_:function(){},audit:function(){}},
|
2015-11-24 23:38:42 +01:00
|
|
|
nodes:{
|
2015-11-20 23:59:08 +01:00
|
|
|
setFlows: setFlows
|
|
|
|
}
|
|
|
|
});
|
|
|
|
request(app)
|
|
|
|
.post('/flows')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Node-RED-Deployment-Type','nodes')
|
|
|
|
.expect(204)
|
|
|
|
.end(function(err,res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2016-10-10 14:27:43 +02:00
|
|
|
setFlows.calledOnce.should.be.true();
|
2015-11-20 23:59:08 +01:00
|
|
|
setFlows.lastCall.args[1].should.eql('nodes');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-10-09 23:02:24 +02:00
|
|
|
it('set flows - rejects mismatched revision - v2', function(done) {
|
|
|
|
var setFlows = sinon.spy(function() { return when.resolve();});
|
|
|
|
var getFlows = sinon.spy(function() { return {rev:123,flows:[1,2,3]}});
|
|
|
|
flows.init({
|
|
|
|
log:{warn:function(){},_:function(){},audit:function(){}},
|
|
|
|
nodes:{
|
|
|
|
setFlows: setFlows,
|
|
|
|
getFlows: getFlows
|
|
|
|
}
|
|
|
|
});
|
|
|
|
request(app)
|
|
|
|
.post('/flows')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Node-RED-API-Version','v2')
|
|
|
|
.send({rev:456,flows:[4,5,6]})
|
|
|
|
.expect(409)
|
|
|
|
.end(function(err,res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2016-10-12 11:33:51 +02:00
|
|
|
res.body.should.have.property("code","version_mismatch");
|
2016-10-09 23:02:24 +02:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('set flows - rev provided - v2', function(done) {
|
|
|
|
var setFlows = sinon.spy(function() { return when.resolve(456);});
|
|
|
|
var getFlows = sinon.spy(function() { return {rev:123,flows:[1,2,3]}});
|
|
|
|
flows.init({
|
|
|
|
log:{warn:function(){},_:function(){},audit:function(){}},
|
|
|
|
nodes:{
|
|
|
|
setFlows: setFlows,
|
|
|
|
getFlows: getFlows
|
|
|
|
}
|
|
|
|
});
|
|
|
|
request(app)
|
|
|
|
.post('/flows')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Node-RED-API-Version','v2')
|
|
|
|
.send({rev:123,flows:[4,5,6]})
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err,res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
res.body.should.have.property("rev",456);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('set flows - no rev provided - v2', function(done) {
|
|
|
|
var setFlows = sinon.spy(function() { return when.resolve(456);});
|
|
|
|
var getFlows = sinon.spy(function() { return {rev:123,flows:[1,2,3]}});
|
|
|
|
flows.init({
|
|
|
|
log:{warn:function(){},_:function(){},audit:function(){}},
|
|
|
|
nodes:{
|
|
|
|
setFlows: setFlows,
|
|
|
|
getFlows: getFlows
|
|
|
|
}
|
|
|
|
});
|
|
|
|
request(app)
|
|
|
|
.post('/flows')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Node-RED-API-Version','v2')
|
|
|
|
.send({flows:[4,5,6]})
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err,res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
res.body.should.have.property("rev",456);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('sets flow - bad version', function(done) {
|
|
|
|
request(app)
|
|
|
|
.post('/flows')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Node-RED-API-Version','xxx')
|
|
|
|
.expect(400)
|
|
|
|
.end(function(err,res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
try {
|
2016-10-12 11:33:51 +02:00
|
|
|
res.body.should.have.a.property('code','invalid_api_version');
|
2016-10-09 23:02:24 +02:00
|
|
|
done();
|
|
|
|
} catch(e) {
|
|
|
|
return done(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2015-11-20 23:59:08 +01:00
|
|
|
it('reloads flows', function(done) {
|
|
|
|
var loadFlows = sinon.spy(function() { return when.resolve(); });
|
|
|
|
flows.init({
|
|
|
|
log:{warn:function(){},_:function(){},audit:function(){}},
|
2015-11-24 23:38:42 +01:00
|
|
|
nodes:{
|
2015-11-20 23:59:08 +01:00
|
|
|
loadFlows: loadFlows
|
|
|
|
}
|
|
|
|
});
|
|
|
|
request(app)
|
|
|
|
.post('/flows')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Node-RED-Deployment-Type','reload')
|
|
|
|
.expect(204)
|
|
|
|
.end(function(err,res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2016-10-10 14:27:43 +02:00
|
|
|
loadFlows.called.should.be.true();
|
2015-11-20 23:59:08 +01:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-04 18:05:29 +01:00
|
|
|
it('returns error when set fails', function(done) {
|
2015-11-11 23:11:02 +01:00
|
|
|
flows.init({
|
|
|
|
log:{warn:function(){},_:function(){},audit:function(){}},
|
2015-11-24 23:38:42 +01:00
|
|
|
nodes:{
|
2015-11-11 23:11:02 +01:00
|
|
|
setFlows: function() { return when.reject(new Error("expected error")); }
|
|
|
|
}
|
2014-11-04 18:05:29 +01:00
|
|
|
});
|
|
|
|
request(app)
|
|
|
|
.post('/flows')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect(500)
|
|
|
|
.end(function(err,res) {
|
|
|
|
if (err) {
|
2015-11-20 23:59:08 +01:00
|
|
|
return done(err);
|
2014-11-04 18:05:29 +01:00
|
|
|
}
|
2015-03-30 15:16:04 +02:00
|
|
|
res.body.should.have.property("message","expected error");
|
2014-11-04 18:05:29 +01:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-07-15 23:43:24 +02:00
|
|
|
|
2014-11-04 18:05:29 +01:00
|
|
|
});
|