diff --git a/src/plugins/install.js b/src/plugins/install.js index acde7a22e7..44b9c3747b 100644 --- a/src/plugins/install.js +++ b/src/plugins/install.js @@ -1,15 +1,15 @@ 'use strict'; -var winston = require('winston'), - async = require('async'), - path = require('path'), - fs = require('fs'), - nconf = require('nconf'), - os = require('os'), +var winston = require('winston'); +var async = require('async'); +var path = require('path'); +var fs = require('fs'); +var nconf = require('nconf'); +var os = require('os'); - db = require('../database'), - meta = require('../meta'), - pubsub = require('../pubsub'); +var db = require('../database'); +var meta = require('../meta'); +var pubsub = require('../pubsub'); module.exports = function (Plugins) { @@ -68,43 +68,38 @@ module.exports = function (Plugins) { }; function toggleInstall(id, version, callback) { - Plugins.isInstalled(id, function (err, installed) { - if (err) { - return callback(err); - } - var type = installed ? 'uninstall' : 'install'; - async.waterfall([ - function (next) { - Plugins.isActive(id, next); - }, - function (active, next) { - if (active) { - Plugins.toggleActive(id, function (err, status) { - next(err); - }); - return; - } - next(); - }, - function (next) { - var command = installed ? ('npm uninstall ' + id) : ('npm install ' + id + '@' + (version || 'latest')); - runNpmCommand(command, next); - } - ], function (err) { - if (err) { - return callback(err); + var type; + var installed; + async.waterfall([ + function (next) { + Plugins.isInstalled(id, next); + }, + function (_installed, next) { + installed = _installed; + type = installed ? 'uninstall' : 'install'; + Plugins.isActive(id, next); + }, + function (active, next) { + if (active) { + Plugins.toggleActive(id, function (err, status) { + next(err); + }); + return; } - - Plugins.get(id, function (err, pluginData) { - if (err) { - return callback(err); - } - - Plugins.fireHook('action:plugin.' + type, id); - callback(null, pluginData); - }); - }); - }); + next(); + }, + function (next) { + var command = installed ? ('npm uninstall ' + id) : ('npm install ' + id + '@' + (version || 'latest')); + runNpmCommand(command, next); + }, + function (next) { + Plugins.get(id, next); + }, + function (pluginData, next) { + Plugins.fireHook('action:plugin.' + type, id); + next(null, pluginData); + } + ], callback); } function runNpmCommand(command, callback) { @@ -113,7 +108,7 @@ module.exports = function (Plugins) { return callback(err); } winston.info('[plugins] ' + stdout); - callback(err); + callback(); }); } diff --git a/test/plugins.js b/test/plugins.js index 8cb76e80bb..9af8224dee 100644 --- a/test/plugins.js +++ b/test/plugins.js @@ -96,5 +96,60 @@ describe('Plugins', function () { }); }); + describe('install/activate/uninstall', function () { + var latest; + var pluginName = 'nodebb-plugin-imgur'; + it('should install a plugin', function (done) { + plugins.toggleInstall(pluginName, '1.0.16', function (err, pluginData) { + assert.ifError(err); + + latest = pluginData.latest; + + assert.equal(pluginData.name, pluginName); + assert.equal(pluginData.id, pluginName); + assert.equal(pluginData.url, 'https://github.com/barisusakli/nodebb-plugin-imgur#readme'); + assert.equal(pluginData.description, 'A Plugin that uploads images to imgur'); + assert.equal(pluginData.active, false); + assert.equal(pluginData.installed, true); + + done(); + }); + }); + + it('should activate plugin', function (done) { + plugins.toggleActive(pluginName, function (err) { + assert.ifError(err); + plugins.isActive(pluginName, function (err, isActive) { + assert.ifError(err); + assert(isActive); + done(); + }); + }); + }); + + it('should upgrade plugin', function (done) { + plugins.upgrade(pluginName, 'latest', function (err, isActive) { + assert.ifError(err); + assert(isActive); + plugins.loadPluginInfo(path.join(nconf.get('base_dir'), 'node_modules', pluginName), function (err, pluginInfo) { + assert.ifError(err); + assert.equal(pluginInfo.version, latest); + done(); + }); + }); + }); + + it('should uninstall a plugin', function (done) { + plugins.toggleInstall(pluginName, 'latest', function (err, pluginData) { + assert.ifError(err); + assert.equal(pluginData.installed, false); + assert.equal(pluginData.active, false); + done(); + }); + }); + }); + + + });