From 9c0b65a4c2378bb2c7df66405a3becd7a2ab70a2 Mon Sep 17 00:00:00 2001 From: hbeeken Date: Thu, 31 Jul 2014 08:41:43 +0100 Subject: [PATCH] Adding tests for credentials.registerEndpoint --- test/red/nodes/credentials_spec.js | 129 +++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) diff --git a/test/red/nodes/credentials_spec.js b/test/red/nodes/credentials_spec.js index a139db437..aecb2bd87 100644 --- a/test/red/nodes/credentials_spec.js +++ b/test/red/nodes/credentials_spec.js @@ -351,6 +351,135 @@ 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); + }); + }); + + }) })