Skip to content

Commit

Permalink
fix: arguments weren't parsed corectly
Browse files Browse the repository at this point in the history
  • Loading branch information
mastilver committed Jun 15, 2015
1 parent 5499c9d commit eae59c7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = function(sails){
if(functionMatches !== null){

var functionName = functionMatches[1];
var args = functionMatches[2].split(/\s*,\s*/);
var args = JSON.parse('[' + functionMatches[2].replace(/'/g, '"') + ']');

var policyFactory = require(sails.config.paths.policiesFactories + '/' + functionName);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sails-hook-parametized-policies",
"version": "0.1.0",
"version": "0.1.1",
"description": "",
"main": "index.js",
"directories": {
Expand Down
4 changes: 4 additions & 0 deletions test/controllers/MainController.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ module.exports.user = function(req, res){
module.exports.ok = function(req, res){
res.send();
}

module.exports.multiple = function(req, res){
res.send();
};
12 changes: 10 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ describe('hook annotation router', function(){
'GET /ok': 'MainController.ok',
'GET /user': 'MainController.user',
'GET /admin': 'MainController.admin',
'GET /multiple': 'MainController.multiple',
},
policies: {
'MainController': {
'admin': ['is(\'Admin\')'],
'user': 'is(\'User\')',
'ok': 'accept',
'multiple': 'multiple(1, \'one\')',
}
}
}, function(err, _sails){
Expand Down Expand Up @@ -71,13 +73,19 @@ describe('hook annotation router', function(){
it('should parse function policy', function(done){
request(sails.hooks.http.app)
.get('/user')
.expect('\'User\'', done);
.expect('User', done);
})

it('should parse function policy into an array', function(done){
request(sails.hooks.http.app)
.get('/admin')
.expect('\'Admin\'', done);
.expect('Admin', done);
})

it('shoule handle multiple arguments policy', function(done){
request(sails.hooks.http.app)
.get('/multiple')
.expect('1-one', done);
});

})
6 changes: 6 additions & 0 deletions test/policiesFactories/multiple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = function(first, second){
return function(req, res, next){
res.write(first + '-' + second);
next();
}
};

0 comments on commit eae59c7

Please sign in to comment.