mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	@@ -49,7 +49,8 @@
 | 
				
			|||||||
        "grunt-contrib-jshint": "0.10.0",
 | 
					        "grunt-contrib-jshint": "0.10.0",
 | 
				
			||||||
        "mocha": "1.18.2",
 | 
					        "mocha": "1.18.2",
 | 
				
			||||||
        "should": "3.3.1",
 | 
					        "should": "3.3.1",
 | 
				
			||||||
        "sinon": "1.9.1"
 | 
					        "sinon": "1.9.1",
 | 
				
			||||||
 | 
					        "supertest": "^0.13.0"
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    "engines": {
 | 
					    "engines": {
 | 
				
			||||||
        "node": ">=0.8"
 | 
					        "node": ">=0.8"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,10 +13,180 @@
 | 
				
			|||||||
 * See the License for the specific language governing permissions and
 | 
					 * See the License for the specific language governing permissions and
 | 
				
			||||||
 * limitations under the License.
 | 
					 * limitations under the License.
 | 
				
			||||||
 **/
 | 
					 **/
 | 
				
			||||||
var should = require("should");
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
describe("red/library", function() {
 | 
					var should = require("should");
 | 
				
			||||||
    it('can be required without errors', function() {
 | 
					var sinon = require('sinon');
 | 
				
			||||||
        require("../../red/library");
 | 
					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();
 | 
				
			||||||
 | 
					                        });
 | 
				
			||||||
 | 
					                });
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user