Merge pull request #291 from hindessm/test-library

Add library tests
This commit is contained in:
Nick O'Leary 2014-07-23 23:06:12 +01:00
commit 6bc4b235bb
2 changed files with 176 additions and 5 deletions

View File

@ -49,7 +49,8 @@
"grunt-contrib-jshint": "0.10.0",
"mocha": "1.18.2",
"should": "3.3.1",
"sinon": "1.9.1"
"sinon": "1.9.1",
"supertest": "^0.13.0"
},
"engines": {
"node": ">=0.8"

View File

@ -13,10 +13,180 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var should = require("should");
describe("red/library", function() {
it('can be required without errors', function() {
require("../../red/library");
var should = require("should");
var sinon = require('sinon');
var request = require('supertest');
var http = require('http');
var express = require('express');
var fs = require('fs-extra');
var path = require('path');
var when = require('when');
var app = express();
var RED = require("../../red/red.js");
var server = require("../../red/server.js");
var nodes = require("../../red/nodes");
describe("library", function() {
var userDir = path.join(__dirname,".testUserHome");
before(function(done) {
fs.remove(userDir,function(err) {
fs.mkdir(userDir,function() {
sinon.stub(nodes, 'load', function() {
return when.promise(function(resolve,reject){
resolve([]);
});
});
RED.init(http.createServer(function(req,res){app(req,res)}),
{userDir: userDir});
server.start().then(function () { done(); });
});
});
});
after(function(done) {
fs.remove(userDir,done);
server.stop();
nodes.load.restore();
});
afterEach(function(done) {
fs.remove(userDir,function(err) {
fs.mkdir(userDir,done);
});
});
describe("flows", function() {
it('returns empty result', function(done) {
request(RED.httpAdmin)
.get('/library/flows')
.expect(200)
.end(function(err,res) {
if (err) {
throw err;
}
res.body.should.not.have.property('f');
done();
});
});
it('returns 404 for non-existent entry', function(done) {
request(RED.httpAdmin)
.get('/library/flows/foo')
.expect(404)
.end(done);
});
it('can store and retrieve item', function(done) {
var flow = '[]';
request(RED.httpAdmin)
.post('/library/flows/foo')
.set('Content-Type', 'text/plain')
.send(flow)
.expect(204).end(function (err, res) {
if (err) {
throw err;
}
request(RED.httpAdmin)
.get('/library/flows/foo')
.expect(200)
.end(function(err,res) {
if (err) {
throw err;
}
res.text.should.equal(flow);
done();
});
});
});
it('lists a stored item', function(done) {
request(RED.httpAdmin)
.post('/library/flows/bar')
.expect(204)
.end(function () {
request(RED.httpAdmin)
.get('/library/flows')
.expect(200)
.end(function(err,res) {
if (err) {
throw err;
}
res.body.should.have.property('f');
should.deepEqual(res.body.f,['bar']);
done();
});
});
});
});
describe("type", function() {
before(function() {
RED.library.register('test');
});
it('returns empty result', function(done) {
request(RED.httpAdmin)
.get('/library/test')
.expect(200)
.end(function(err,res) {
if (err) {
throw err;
}
res.body.should.not.have.property('f');
done();
});
});
it('returns 404 for non-existent entry', function(done) {
request(RED.httpAdmin)
.get('/library/test/foo')
.expect(404)
.end(done);
});
it('can store and retrieve item', function(done) {
var flow = '[]';
request(RED.httpAdmin)
.post('/library/test/foo')
.set('Content-Type', 'text/plain')
.send(flow)
.expect(204).end(function (err, res) {
if (err) {
throw err;
}
request(RED.httpAdmin)
.get('/library/test/foo')
.expect(200)
.end(function(err,res) {
if (err) {
throw err;
}
res.text.should.equal(flow);
done();
});
});
});
it('lists a stored item', function(done) {
request(RED.httpAdmin)
.post('/library/test/bar')
.expect(204)
.end(function () {
request(RED.httpAdmin)
.get('/library/test')
.expect(200)
.end(function(err,res) {
if (err) {
throw err;
}
should.deepEqual(res.body,[{ fn: 'bar'}]);
done();
});
});
});
});
});