diff --git a/lib/plugin-loader.js b/lib/plugin-loader.js index 17d84203c..5e54110dd 100644 --- a/lib/plugin-loader.js +++ b/lib/plugin-loader.js @@ -22,7 +22,7 @@ function load_plugins(config, plugin_configs, params, sanity_check) { if (plugin === null && p.match(/^[^\.\/]/)) { plugin = try_load('verdaccio-' + p) // compatibility for old sinopia plugins - if(!plugin) { + if (!plugin) { plugin = try_load('sinopia-' + p) } } @@ -37,18 +37,18 @@ function load_plugins(config, plugin_configs, params, sanity_check) { } if (plugin === null) { - throw Error('"' + p + '" plugin not found\ntry "npm install verdaccio-' + p + '"') + throw new Error('"' + p + '" plugin not found\ntry "npm install verdaccio-' + p + '"') } if (typeof(plugin) !== 'function') - throw Error('"' + p + '" doesn\'t look like a valid plugin') + throw new Error('"' + p + '" doesn\'t look like a valid plugin') plugin = plugin(plugin_configs[p], params) if (plugin === null || !sanity_check(plugin)) - throw Error('"' + p + '" doesn\'t look like a valid plugin') + throw new Error('"' + p + '" doesn\'t look like a valid plugin') - return plugin + return plugin; }) return plugins diff --git a/test/unit/partials/test-plugin-storage/invalid-package/package.json b/test/unit/partials/test-plugin-storage/invalid-package/package.json new file mode 100644 index 000000000..0233c3de0 --- /dev/null +++ b/test/unit/partials/test-plugin-storage/invalid-package/package.json @@ -0,0 +1,11 @@ +{ + "name": "invalid-package", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC" +} diff --git a/test/unit/partials/test-plugin-storage/invalid-plugin-sanity/index.js b/test/unit/partials/test-plugin-storage/invalid-plugin-sanity/index.js new file mode 100644 index 000000000..97336662e --- /dev/null +++ b/test/unit/partials/test-plugin-storage/invalid-plugin-sanity/index.js @@ -0,0 +1,8 @@ +function ValidVerdaccioPlugin() { + return { + // not valid method + authenticate__: function(){} + } +}; + +module.exports = ValidVerdaccioPlugin; diff --git a/test/unit/partials/test-plugin-storage/invalid-plugin-sanity/package.json b/test/unit/partials/test-plugin-storage/invalid-plugin-sanity/package.json new file mode 100644 index 000000000..d3d2e6d53 --- /dev/null +++ b/test/unit/partials/test-plugin-storage/invalid-plugin-sanity/package.json @@ -0,0 +1,11 @@ +{ + "name": "invalid-plugin-sanity", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC" +} diff --git a/test/unit/partials/test-plugin-storage/invalid-plugin/index.js b/test/unit/partials/test-plugin-storage/invalid-plugin/index.js new file mode 100644 index 000000000..f053ebf79 --- /dev/null +++ b/test/unit/partials/test-plugin-storage/invalid-plugin/index.js @@ -0,0 +1 @@ +module.exports = {}; diff --git a/test/unit/partials/test-plugin-storage/invalid-plugin/package.json b/test/unit/partials/test-plugin-storage/invalid-plugin/package.json new file mode 100644 index 000000000..45b96c189 --- /dev/null +++ b/test/unit/partials/test-plugin-storage/invalid-plugin/package.json @@ -0,0 +1,11 @@ +{ + "name": "invalid-plugin", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC" +} diff --git a/test/unit/partials/test-plugin-storage/verdaccio-plugin/index.js b/test/unit/partials/test-plugin-storage/verdaccio-plugin/index.js new file mode 100644 index 000000000..540501af0 --- /dev/null +++ b/test/unit/partials/test-plugin-storage/verdaccio-plugin/index.js @@ -0,0 +1,7 @@ +function ValidVerdaccioPlugin() { + return { + authenticate: function(){} + } +}; + +module.exports = ValidVerdaccioPlugin; diff --git a/test/unit/partials/test-plugin-storage/verdaccio-plugin/package.json b/test/unit/partials/test-plugin-storage/verdaccio-plugin/package.json new file mode 100644 index 000000000..56dc78d45 --- /dev/null +++ b/test/unit/partials/test-plugin-storage/verdaccio-plugin/package.json @@ -0,0 +1,11 @@ +{ + "name": "verdaccio-plugin", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC" +} diff --git a/test/unit/plugin_loader.js b/test/unit/plugin_loader.js new file mode 100644 index 000000000..4bca68f59 --- /dev/null +++ b/test/unit/plugin_loader.js @@ -0,0 +1,71 @@ +'use strict'; + +const assert = require('assert'); +const load_plugins = require('../../lib/plugin-loader').load_plugins; +const path = require('path'); + +describe('plugin loader', function() { + + it('testing auth valid plugin loader', function() { + let _config = { + self_path: path.join(__dirname, './'), + max_users: 0, + auth: { + './unit/partials/test-plugin-storage/verdaccio-plugin': {} + } + } + let p = load_plugins(_config, _config.auth, {}, function (p) { + return p.authenticate || p.allow_access || p.allow_publish; + }); + assert(p.length === 1); + }); + + it('testing auth plugin invalid plugin', function() { + let _config = { + self_path: path.join(__dirname, './'), + auth: { + './unit/partials/test-plugin-storage/invalid-plugin': {} + } + } + try { + load_plugins(_config, _config.auth, {}, function (p) { + return p.authenticate || p.allow_access || p.allow_publish; + }); + } catch(e) { + assert(e.message === '"./unit/partials/test-plugin-storage/invalid-plugin" doesn\'t look like a valid plugin'); + } + }); + + it('testing auth plugin invalid plugin sanityCheck', function() { + let _config = { + self_path: path.join(__dirname, './'), + auth: { + './unit/partials/test-plugin-storage/invalid-plugin-sanity': {} + } + } + try { + load_plugins(_config, _config.auth, {}, function (p) { + return p.authenticate || p.allow_access || p.allow_publish; + }); + } catch(e) { + assert(e.message === '"./unit/partials/test-plugin-storage/invalid-plugin-sanity" doesn\'t look like a valid plugin'); + } + }); + + it('testing auth plugin no plugins', function() { + let _config = { + self_path: path.join(__dirname, './'), + auth: { + './unit/partials/test-plugin-storage/invalid-package': {} + } + } + try { + load_plugins(_config, _config.auth, {}, function (p) { + return p.authenticate || p.allow_access || p.allow_publish; + }); + } catch(e) { + assert(e.message === `"./unit/partials/test-plugin-storage/invalid-package" plugin not found\ntry "npm install verdaccio-./unit/partials/test-plugin-storage/invalid-package"`); + } + }); + +});