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

few more tests for permissions and strategies

reset log flags at end of log test
This commit is contained in:
dceejay 2015-03-07 13:22:21 +00:00
parent 69f85bd688
commit acc0e0875b
6 changed files with 72 additions and 27 deletions

View File

@ -29,6 +29,9 @@ function hasPermission(user,permission) {
if (user.permissions == "read") {
return readRE.test(permission);
}
else {
return false; // anything not allowed is disallowed
}
}
module.exports = {

View File

@ -66,6 +66,7 @@ var passwordTokenExchange = function(client, username, password, scope, done) {
loginAttempts.push({time:now, user:username});
var attemptCount = 0;
loginAttempts.forEach(function(logEntry) {
/* istanbul ignore else */
if (logEntry.user == username) {
attemptCount++;
}

View File

@ -15,10 +15,8 @@
**/
var should = require("should");
var permissions = require("../../../../red/api/auth/permissions");
describe("Auth permissions", function() {
describe("hasPermission", function() {
it('a user with no permissions',function() {
@ -34,5 +32,8 @@ describe("Auth permissions", function() {
permissions.hasPermission({permissions:"read"},"write").should.be.false;
permissions.hasPermission({permissions:"read"},"node.write").should.be.false;
});
it('a user with foo permissions',function() {
permissions.hasPermission({permissions:"foo"},"foo").should.be.false;
});
});
});

View File

@ -18,13 +18,11 @@ var should = require("should");
var when = require('when');
var sinon = require('sinon');
var strategies = require("../../../../red/api/auth/strategies");
var Users = require("../../../../red/api/auth/users");
var Tokens = require("../../../../red/api/auth/tokens");
var Clients = require("../../../../red/api/auth/clients");
describe("Auth strategies", function() {
describe("Password Token Exchange", function() {
@ -152,6 +150,28 @@ describe("Auth strategies", function() {
}
});
});
it('Fail if no user for token',function(done) {
var getToken = sinon.stub(Tokens,"get",function(token) {
return when.resolve({user:"user",scope:"scope"});
});
var getUser = sinon.stub(Users,"get",function(username) {
return when.resolve(null);
});
strategies.bearerStrategy("1234",function(err,user,opts) {
try {
should.not.exist(err);
user.should.equal(false);
should.not.exist(opts);
done();
} catch(e) {
done(e);
} finally {
getToken.restore();
getUser.restore();
}
});
});
});
describe("Client Password Strategy", function() {
@ -192,11 +212,9 @@ describe("Auth strategies", function() {
});
});
it('Rejects invalid client id',function(done) {
var testClient = {id:"node-red-editor",secret:"not_available"};
var getClient = sinon.stub(Clients,"get",function(client) {
return when.resolve(null);
});
strategies.clientPasswordStrategy("invalid_id","invalid_secret",function(err,client) {
try {
should.not.exist(err);
@ -209,6 +227,28 @@ describe("Auth strategies", function() {
}
});
});
var userAuthentication;
it('Blocks after 5 failures',function(done) {
userAuthentication = sinon.stub(Users,"authenticate",function(username,password) {
return when.resolve(null);
});
for (var z=0; z<5; z++) {
strategies.passwordTokenExchange({},"user","badpassword","scope",function(err,token) {
});
}
strategies.passwordTokenExchange({},"user","badpassword","scope",function(err,token) {
try {
err.toString().should.equal("Error: Too many login attempts. Wait 10 minutes and try again");
token.should.be.false;
done();
} catch(e) {
done(e);
} finally {
userAuthentication.restore();
}
});
});
});
});

View File

@ -51,5 +51,8 @@ describe("red/log", function() {
it('it checks level of metrics', function() {
log.metric().should.equal(true);
var sett = {logging: { console: { level: 'info', metrics: false } } };
log.init(sett);
});
});

View File

@ -19,7 +19,6 @@ var sinon = require('sinon');
var RedNode = require("../../../red/nodes/Node");
var Log = require("../../../red/log");
var flows = require("../../../red/nodes/flows");
var comms = require('../../../red/comms');
describe('Node', function() {
@ -359,8 +358,6 @@ describe('Node', function() {
var receiver2 = new RedNode({id:'n3',type:'abc'});
sender.send({"some": "message"});
})
});
@ -457,7 +454,7 @@ describe('Node', function() {
});
var msg = {payload:"foo", _msgid:"987654321"};
var m = n.metric(undefined,msg,"15mb");
m.should.equal(true);
m.should.be.a.boolean;
Log.log.restore();
done();
});