Skip to content

Commit

Permalink
Always strip off namespace even if sails-hook- prefix is not present.
Browse files Browse the repository at this point in the history
Also, combine ternary into a single expression.  No reason to do both .match() and .replace()!
  • Loading branch information
sgress454 committed Jan 28, 2016
1 parent c2925ec commit fc38cba
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 30 deletions.
5 changes: 3 additions & 2 deletions lib/hooks/moduleloader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,10 @@ module.exports = function(sails) {
else if (sails.config.installedHooks && sails.config.installedHooks[identity] && sails.config.installedHooks[identity].name) {
hookName = sails.config.installedHooks[identity].name;
}
// Otherwise use the module name, with initial "sails-hook" stripped off if it exists
// Otherwise use the module name, with namespacing and initial "sails-hook-" stripped off if it exists
else {
hookName = identity.match(/^(sails-hook-)|(@.+\/sails-hook-)/) ? identity.replace(/^(sails-hook-)|(@.+\/sails-hook-)/,'') : identity;
// Strip off any NPM namespacing and/or sails-hook- prefix
hookName = identity.replace(/^(@.+?\/)?(sails-hook-)?/, '');
}

if (sails.config.hooks[hookName] === false) {
Expand Down
1 change: 1 addition & 0 deletions test/integration/fixtures/hooks/installable/shout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = function(sails) {

initialize: function(cb) {
phrase = sails.config[this.configKey].phrase;
this.isShoutyHook = true;
cb();
},

Expand Down
200 changes: 172 additions & 28 deletions test/integration/hook.3rdparty.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,24 @@ describe('hooks :: ', function() {

});

xdescribe('setting the hook name to `views` (an existing hook)', function(){
describe('setting the hook name to `csrf` (an existing hook)', function(){

it ('should throw an error', function(done) {
appHelper.liftQuiet({installedHooks: {'sails-hook-shout': {name: 'views'}}}, function(err, _sails) {
assert(err && err.code == 'E_INVALID_HOOK_NAME');
done();
var sails;
before(function(done) {
appHelper.liftQuiet({installedHooks: {'sails-hook-shout': {name: 'csrf'}}}, function(err, _sails) {
sails = _sails;
done(err);
});
});

after(function(done) {
sails.lower(done);
});

it('should replace the core `csrf` hook', function() {
assert(sails.hooks.csrf.isShoutyHook);
});

});


Expand All @@ -191,12 +200,13 @@ describe('hooks :: ', function() {
});
});

after(function() {
after(function(done) {
process.chdir('../');
// Sleep for 500ms--otherwise we get timing errors for this test on Windows
setTimeout(function() {
appHelper.teardown();
});
return done();
}, 500);
});

describe('with default settings', function() {
Expand Down Expand Up @@ -236,61 +246,195 @@ describe('hooks :: ', function() {

});

describe('with `hookName` set to `csrf` in the package.json', function() {
var sails;
before(function(done) {
var packageJson = JSON.parse(fs.readFileSync(path.resolve(__dirname,'../../testApp/node_modules/shouty','package.json')));
packageJson.sails.hookName = 'csrf';
fs.writeFileSync(path.resolve(__dirname,'../../testApp/node_modules/shouty','package.json'), JSON.stringify(packageJson));
appHelper.liftQuiet(function(err, _sails) {
if (err) {return done(err);}
sails = _sails;
return done();
});
});

after(function(done) {
sails.lower(done);
});

it('should replace the core `csrf` hook', function() {
assert(sails.hooks.csrf.isShoutyHook);
});
});

});

xdescribe('into node_modules/sails-hook-views', function(){
describe('into node_modules/sails-hook-csrf', function(){

var sails;
before(function(done) {
this.timeout(5000);
fs.mkdirs(path.resolve(__dirname, "../..", appName, "node_modules"), function(err) {
if (err) {return done(err);}
wrench.copyDirSyncRecursive(path.resolve(__dirname, 'fixtures/hooks/installable/shout'), path.resolve(__dirname,'../../testApp/node_modules/sails-hook-views'));
wrench.copyDirSyncRecursive(path.resolve(__dirname, 'fixtures/hooks/installable/shout'), path.resolve(__dirname,'../../testApp/node_modules/sails-hook-csrf'));
process.chdir(path.resolve(__dirname, "../..", appName));
done();
appHelper.liftQuiet(function(err, _sails) {
if (err) {return done(err);}
sails = _sails;
return done();
});
});
});

after(function() {
process.chdir('../');
appHelper.teardown();
after(function(done) {
sails.lower(function(err) {
process.chdir('../');
appHelper.teardown();
return done(err);
});
});

it ('should throw an error', function(done) {
appHelper.liftQuiet(function(err, _sails) {
assert(err && err.code == 'E_INVALID_HOOK_NAME');
done();
});
it('should replace the core `csrf` hook', function() {
assert(sails.hooks.csrf.isShoutyHook);
});

});


xdescribe('into node_modules/views', function(){
describe('into node_modules/csrf', function(){

var sails;
before(function(done) {
this.timeout(5000);
fs.mkdirs(path.resolve(__dirname, "../..", appName, "node_modules"), function(err) {
if (err) {return done(err);}
wrench.copyDirSyncRecursive(path.resolve(__dirname, 'fixtures/hooks/installable/shout'), path.resolve(__dirname,'../../testApp/node_modules/views'));
wrench.copyDirSyncRecursive(path.resolve(__dirname, 'fixtures/hooks/installable/shout'), path.resolve(__dirname,'../../testApp/node_modules/csrf'));
process.chdir(path.resolve(__dirname, "../..", appName));
done();
appHelper.liftQuiet(function(err, _sails) {
if (err) {return done(err);}
sails = _sails;
return done();
});
});
});

after(function() {
process.chdir('../');
appHelper.teardown();
after(function(done) {
sails.lower(function(err) {
process.chdir('../');
appHelper.teardown();
return done(err);
});
});

it ('should throw an error', function(done) {
appHelper.liftQuiet(function(err, _sails) {
assert(err && err.code == 'E_INVALID_HOOK_NAME');
done();
it('should replace the core `csrf` hook', function() {
assert(sails.hooks.csrf.isShoutyHook);
});

});

describe('into node_modules/@my-modules/shouty', function(){

describe('with default settings', function() {

var sails;
before(function(done) {
this.timeout(5000);
fs.mkdirs(path.resolve(__dirname, "../..", appName, "node_modules", "@my-modules"), function(err) {
if (err) {return done(err);}
wrench.copyDirSyncRecursive(path.resolve(__dirname, 'fixtures/hooks/installable/shout'), path.resolve(__dirname,'../../testApp/node_modules/@my-modules/shouty'));
process.chdir(path.resolve(__dirname, "../..", appName));
appHelper.liftQuiet(function(err, _sails) {
if (err) {return done(err);}
sails = _sails;
return done();
});
});
});

after(function(done) {
sails.lower(function(err) {
process.chdir('../');
appHelper.teardown();
return done(err);
});
});

it('should install a hook into `sails.hooks.shouty`', function() {
assert(sails.hooks.shouty);
});

});

describe('with `hookName` set to `csrf` in the package.json', function() {

var sails;
before(function(done) {
this.timeout(5000);
fs.mkdirs(path.resolve(__dirname, "../..", appName, "node_modules", "@my-modules"), function(err) {
if (err) {return done(err);}
wrench.copyDirSyncRecursive(path.resolve(__dirname, 'fixtures/hooks/installable/shout'), path.resolve(__dirname,'../../testApp/node_modules/@my-modules/shouty'));
var packageJson = JSON.parse(fs.readFileSync(path.resolve(__dirname,'../../testApp/node_modules/@my-modules/shouty','package.json')));
packageJson.sails.hookName = 'csrf';
fs.writeFileSync(path.resolve(__dirname,'../../testApp/node_modules/@my-modules/shouty','package.json'), JSON.stringify(packageJson));
process.chdir(path.resolve(__dirname, "../..", appName));
appHelper.liftQuiet(function(err, _sails) {
if (err) {return done(err);}
sails = _sails;
return done();
});
});
});

after(function(done) {
sails.lower(function(err) {
process.chdir('../');
appHelper.teardown();
return done(err);
});
});

it('should replace the core `csrf` hook', function() {
assert(sails.hooks.csrf.isShoutyHook);
});

});

});

describe('into node_modules/@my-modules/sails-hook-csrf', function(){

var sails;
before(function(done) {
this.timeout(5000);
fs.mkdirs(path.resolve(__dirname, "../..", appName, "node_modules", "@my-modules"), function(err) {
if (err) {return done(err);}
wrench.copyDirSyncRecursive(path.resolve(__dirname, 'fixtures/hooks/installable/shout'), path.resolve(__dirname,'../../testApp/node_modules/@my-modules/sails-hook-csrf'));
process.chdir(path.resolve(__dirname, "../..", appName));
appHelper.liftQuiet(function(err, _sails) {
if (err) {return done(err);}
sails = _sails;
return done();
});
});
});

after(function(done) {
sails.lower(function(err) {
process.chdir('../');
appHelper.teardown();
return done(err);
});
});

it('should replace the core `csrf` hook', function() {
assert(sails.hooks.csrf.isShoutyHook);
});

});

});



});

0 comments on commit fc38cba

Please sign in to comment.