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

Adding tests for credentials.registerEndpoint

This commit is contained in:
hbeeken 2014-07-31 08:41:43 +01:00
parent 47f7cb52d8
commit 9c0b65a4c2

View File

@ -352,5 +352,134 @@ describe('Credentials', function() {
}) })
describe('registerEndpoint', function() {
var path = require('path');
var fs = require('fs-extra');
var http = require('http');
var express = require('express');
var sinon = require('sinon');
var request = require('supertest');
var server = require("../../../red/server");
var localfilesystem = require("../../../red/storage/localfilesystem");
var app = express();
var RED = require("../../../red/red.js");
var userDir = path.join(__dirname,".testUserHome");
before(function(done) {
fs.remove(userDir,function(err) {
fs.mkdir(userDir,function() {
sinon.stub(index, 'load', function() {
return when.promise(function(resolve,reject){
resolve([]);
});
});
sinon.stub(localfilesystem, 'getCredentials', function() {
return when.promise(function(resolve,reject) {
resolve({"tab1":{"foo": 2, "pswd":'sticks'}});
});
}) ;
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();
index.load.restore();
localfilesystem.getCredentials.restore();
});
function TestNode(n) {
index.createNode(this, n);
var node = this;
this.on("log", function() {
// do nothing
});
}
it(': valid credential type', function(done) {
index.registerType('test', TestNode, {
credentials: {
foo: {type:"test"}
}
});
index.loadFlows().then(function() {
var testnode = new TestNode({id:'tab1',type:'foo',name:'barney'});
request(RED.httpAdmin).get('/credentials/test/tab1').expect(200).end(function(err,res) {
if (err) {
done(err);
}
res.body.should.have.property('foo', 2);
done();
});
}).otherwise(function(err){
done(err);
});
});
it(': password credential type', function(done) {
index.registerType('password', TestNode, {
credentials: {
pswd: {type:"password"}
}
});
index.loadFlows().then(function() {
var testnode = new TestNode({id:'tab1',type:'pswd',name:'barney'});
request(RED.httpAdmin).get('/credentials/password/tab1').expect(200).end(function(err,res) {
if (err) {
done(err);
}
res.body.should.have.property('has_pswd', true);
res.body.should.not.have.property('pswd');
done();
});
}).otherwise(function(err){
done(err);
});
});
it(': returns 404 for undefined credential type', function(done) {
index.registerType('test', TestNode, {
credentials: {
foo: {type:"test"}
}
});
index.loadFlows().then(function() {
var testnode = new TestNode({id:'tab1',type:'foo',name:'barney'});
request(RED.httpAdmin).get('/credentials/unknownType/tab1').expect(404).end(done);
}).otherwise(function(err){
done(err);
});
});
it(': undefined nodeID', function(done) {
index.registerType('test', TestNode, {
credentials: {
foo: {type:"test"}
}
});
index.loadFlows().then(function() {
var testnode = new TestNode({id:'tab1',type:'foo',name:'barney'});
request(RED.httpAdmin).get('/credentials/test/unknownNode').expect(200).end(function(err,res) {
if (err) {
done(err);
}
var b = res.body;
res.body.should.not.have.property('foo');
done();
});
}).otherwise(function(err){
done(err);
});
});
})
}) })