1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00
node-red/test/red/api/admin/flow_spec.js

248 lines
8.0 KiB
JavaScript
Raw Normal View History

2015-12-09 22:59:42 +01:00
/**
* Copyright JS Foundation and other contributors, http://js.foundation
2015-12-09 22:59:42 +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');
var bodyParser = require('body-parser');
var sinon = require('sinon');
var when = require('when');
var flow = require("../../../../red/api/admin/flow");
2015-12-09 22:59:42 +01:00
2017-09-20 11:30:07 +02:00
describe("api/admin/flow", function() {
2015-12-09 22:59:42 +01:00
var app;
2016-03-18 22:01:21 +01:00
before(function() {
app = express();
app.use(bodyParser.json());
app.get("/flow/:id",flow.get);
app.post("/flow",flow.post);
app.put("/flow/:id",flow.put);
app.delete("/flow/:id",flow.delete);
});
2015-12-09 22:59:42 +01:00
2016-03-18 22:01:21 +01:00
describe("get", function() {
before(function() {
2018-04-24 16:01:49 +02:00
var opts;
2016-03-18 22:01:21 +01:00
flow.init({
2018-04-24 16:01:49 +02:00
flows: {
getFlow: function(_opts) {
opts = _opts;
if (opts.id === '123') {
return Promise.resolve({id:'123'});
2016-03-18 22:01:21 +01:00
} else {
2018-04-24 16:01:49 +02:00
var err = new Error("message");
err.code = "not_found";
err.status = 404;
var p = Promise.reject(err);
p.catch(()=>{});
return p;
2016-03-18 22:01:21 +01:00
}
}
2018-04-24 16:01:49 +02:00
}
2016-03-18 22:01:21 +01:00
});
})
it('gets a known flow', function(done) {
request(app)
.get('/flow/123')
.set('Accept', 'application/json')
.expect(200)
.end(function(err,res) {
if (err) {
return done(err);
}
res.body.should.has.a.property('id','123');
done();
});
})
it('404s an unknown flow', function(done) {
request(app)
.get('/flow/456')
.set('Accept', 'application/json')
.expect(404)
.end(done);
})
});
describe("add", function() {
2018-04-24 16:01:49 +02:00
var opts;
2016-03-18 22:01:21 +01:00
before(function() {
flow.init({
2018-04-24 16:01:49 +02:00
flows: {
addFlow: function(_opts) {
opts = _opts;
if (opts.flow.id === "123") {
return Promise.resolve('123')
2016-03-18 22:01:21 +01:00
} else {
2018-04-24 16:01:49 +02:00
var err = new Error("random error");
err.code = "random_error";
err.status = 400;
var p = Promise.reject(err);
p.catch(()=>{});
return p;
2016-03-18 22:01:21 +01:00
}
}
2018-04-24 16:01:49 +02:00
}
2016-03-18 22:01:21 +01:00
});
})
it('adds a new flow', function(done) {
request(app)
.post('/flow')
.set('Accept', 'application/json')
.send({id:'123'})
.expect(200)
.end(function(err,res) {
if (err) {
return done(err);
}
res.body.should.has.a.property('id','123');
done();
});
})
it('400 an invalid flow', function(done) {
request(app)
.post('/flow')
.set('Accept', 'application/json')
.send({id:'error'})
.expect(400)
.end(function(err,res) {
if (err) {
return done(err);
}
2018-04-24 16:01:49 +02:00
res.body.should.has.a.property('code','random_error');
res.body.should.has.a.property('message','random error');
2016-03-18 22:01:21 +01:00
done();
});
})
})
describe("update", function() {
2018-04-24 16:01:49 +02:00
var opts;
2016-03-18 22:01:21 +01:00
before(function() {
2018-04-24 16:01:49 +02:00
flow.init({
flows: {
updateFlow: function(_opts) {
opts = _opts;
if (opts.id === "123") {
return Promise.resolve('123')
} else {
var err = new Error("random error");
err.code = "random_error";
err.status = 400;
var p = Promise.reject(err);
p.catch(()=>{});
return p;
}
2016-03-18 22:01:21 +01:00
}
}
});
})
it('updates an existing flow', function(done) {
request(app)
.put('/flow/123')
.set('Accept', 'application/json')
.send({id:'123'})
.expect(200)
.end(function(err,res) {
if (err) {
return done(err);
}
res.body.should.has.a.property('id','123');
2018-04-24 16:01:49 +02:00
opts.should.have.property('id','123');
opts.should.have.property('flow',{id:'123'})
2016-03-18 22:01:21 +01:00
done();
});
})
2018-04-24 16:01:49 +02:00
it('400 an invalid flow', function(done) {
2016-03-18 22:01:21 +01:00
request(app)
2018-04-24 16:01:49 +02:00
.put('/flow/456')
2016-03-18 22:01:21 +01:00
.set('Accept', 'application/json')
2018-04-24 16:01:49 +02:00
.send({id:'456'})
2016-03-18 22:01:21 +01:00
.expect(400)
.end(function(err,res) {
if (err) {
return done(err);
}
2018-04-24 16:01:49 +02:00
res.body.should.has.a.property('code','random_error');
res.body.should.has.a.property('message','random error');
2016-03-18 22:01:21 +01:00
done();
});
})
})
describe("delete", function() {
2018-04-24 16:01:49 +02:00
var opts;
2016-03-18 22:01:21 +01:00
before(function() {
2018-04-24 16:01:49 +02:00
flow.init({
flows: {
deleteFlow: function(_opts) {
opts = _opts;
if (opts.id === "123") {
return Promise.resolve()
} else {
var err = new Error("random error");
err.code = "random_error";
err.status = 400;
var p = Promise.reject(err);
p.catch(()=>{});
return p;
}
2016-03-18 22:01:21 +01:00
}
}
});
})
2018-04-24 16:01:49 +02:00
it('deletes an existing flow', function(done) {
2016-03-18 22:01:21 +01:00
request(app)
2018-04-24 16:01:49 +02:00
.del('/flow/123')
.set('Accept', 'application/json')
2016-03-18 22:01:21 +01:00
.expect(204)
.end(function(err,res) {
if (err) {
return done(err);
}
2018-04-24 16:01:49 +02:00
opts.should.have.property('id','123');
2016-03-18 22:01:21 +01:00
done();
});
})
2018-04-24 16:01:49 +02:00
it('400 an invalid flow', function(done) {
2016-03-18 22:01:21 +01:00
request(app)
2018-04-24 16:01:49 +02:00
.del('/flow/456')
.set('Accept', 'application/json')
2016-03-18 22:01:21 +01:00
.expect(400)
.end(function(err,res) {
if (err) {
return done(err);
}
2018-04-24 16:01:49 +02:00
res.body.should.has.a.property('code','random_error');
res.body.should.has.a.property('message','random error');
2016-03-18 22:01:21 +01:00
done();
});
})
})
2015-12-09 22:59:42 +01:00
});