| 
									
										
										
										
											2016-10-14 21:48:38 +03:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2017-02-17 21:55:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-14 21:48:38 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | var	assert = require('assert'); | 
					
						
							|  |  |  | var path = require('path'); | 
					
						
							|  |  |  | var nconf = require('nconf'); | 
					
						
							| 
									
										
										
										
											2016-11-30 18:21:30 +03:00
										 |  |  | var request = require('request'); | 
					
						
							| 
									
										
										
										
											2016-10-14 21:48:38 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | var db = require('./mocks/databasemock'); | 
					
						
							|  |  |  | var plugins = require('../src/plugins'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('Plugins', function () { | 
					
						
							|  |  |  | 	it('should load plugin data', function (done) { | 
					
						
							|  |  |  | 		var pluginId = 'nodebb-plugin-markdown'; | 
					
						
							|  |  |  | 		plugins.loadPlugin(path.join(nconf.get('base_dir'), 'node_modules/' + pluginId), function (err) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert(plugins.libraries[pluginId]); | 
					
						
							|  |  |  | 			assert(plugins.loadedHooks['static:app.load']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 11:57:07 +03:00
										 |  |  | 	it('should return true if hook has listeners', function (done) { | 
					
						
							| 
									
										
										
										
											2016-10-14 21:48:38 +03:00
										 |  |  | 		assert(plugins.hasListeners('filter:parse.post')); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should register and fire a filter hook', function (done) { | 
					
						
							|  |  |  | 		function filterMethod1(data, callback) { | 
					
						
							| 
									
										
										
										
											2017-02-18 01:12:18 -07:00
										 |  |  | 			data.foo += 1; | 
					
						
							| 
									
										
										
										
											2016-10-14 21:48:38 +03:00
										 |  |  | 			callback(null, data); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		function filterMethod2(data, callback) { | 
					
						
							|  |  |  | 			data.foo += 5; | 
					
						
							|  |  |  | 			callback(null, data); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 		plugins.registerHook('test-plugin', { hook: 'filter:test.hook', method: filterMethod1 }); | 
					
						
							|  |  |  | 		plugins.registerHook('test-plugin', { hook: 'filter:test.hook', method: filterMethod2 }); | 
					
						
							| 
									
										
										
										
											2016-10-14 21:48:38 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 		plugins.fireHook('filter:test.hook', { foo: 1 }, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-10-14 21:48:38 +03:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(data.foo, 7); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should register and fire an action hook', function (done) { | 
					
						
							|  |  |  | 		function actionMethod(data) { | 
					
						
							|  |  |  | 			assert.equal(data.bar, 'test'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 		plugins.registerHook('test-plugin', { hook: 'action:test.hook', method: actionMethod }); | 
					
						
							|  |  |  | 		plugins.fireHook('action:test.hook', { bar: 'test' }); | 
					
						
							| 
									
										
										
										
											2016-10-14 21:48:38 +03:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should register and fire a static hook', function (done) { | 
					
						
							|  |  |  | 		function actionMethod(data, callback) { | 
					
						
							|  |  |  | 			assert.equal(data.bar, 'test'); | 
					
						
							|  |  |  | 			callback(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 		plugins.registerHook('test-plugin', { hook: 'static:test.hook', method: actionMethod }); | 
					
						
							|  |  |  | 		plugins.fireHook('static:test.hook', { bar: 'test' }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-10-14 21:48:38 +03:00
										 |  |  | 			assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 			done(); | 
					
						
							| 
									
										
										
										
											2016-10-14 21:48:38 +03:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 11:57:07 +03:00
										 |  |  | 	it('should get plugin data from nbbpm', function (done) { | 
					
						
							|  |  |  | 		plugins.get('nodebb-plugin-markdown', function (err, data) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			var keys = ['id', 'name', 'url', 'description', 'latest', 'installed', 'active', 'latest']; | 
					
						
							|  |  |  | 			assert.equal(data.name, 'nodebb-plugin-markdown'); | 
					
						
							|  |  |  | 			assert.equal(data.id, 'nodebb-plugin-markdown'); | 
					
						
							|  |  |  | 			keys.forEach(function (key) { | 
					
						
							|  |  |  | 				assert(data.hasOwnProperty(key)); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should get a list of plugins', function (done) { | 
					
						
							|  |  |  | 		plugins.list(function (err, data) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			var keys = ['id', 'name', 'url', 'description', 'latest', 'installed', 'active', 'latest']; | 
					
						
							|  |  |  | 			assert(Array.isArray(data)); | 
					
						
							|  |  |  | 			keys.forEach(function (key) { | 
					
						
							|  |  |  | 				assert(data[0].hasOwnProperty(key)); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 14:03:06 +03:00
										 |  |  | 	describe('install/activate/uninstall', function () { | 
					
						
							|  |  |  | 		var latest; | 
					
						
							|  |  |  | 		var pluginName = 'nodebb-plugin-imgur'; | 
					
						
							|  |  |  | 		it('should install a plugin', function (done) { | 
					
						
							| 
									
										
										
										
											2016-12-22 01:48:41 +03:00
										 |  |  | 			this.timeout(20000); | 
					
						
							| 
									
										
										
										
											2016-11-18 14:03:06 +03:00
										 |  |  | 			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(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-30 18:21:30 +03:00
										 |  |  | 	describe('static assets', function () { | 
					
						
							|  |  |  | 		it('should 404 if resource does not exist', function (done) { | 
					
						
							|  |  |  | 			request.get(nconf.get('url') + '/plugins/doesnotexist/should404.tpl', function (err, res, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(res.statusCode, 404); | 
					
						
							|  |  |  | 				assert(body); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should 404 if resource does not exist', function (done) { | 
					
						
							|  |  |  | 			request.get(nconf.get('url') + '/plugins/nodebb-plugin-dbsearch/dbsearch/templates/admin/plugins/should404.tpl', function (err, res, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(res.statusCode, 404); | 
					
						
							|  |  |  | 				assert(body); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should get resource', function (done) { | 
					
						
							|  |  |  | 			request.get(nconf.get('url') + '/plugins/nodebb-plugin-dbsearch/dbsearch/templates/admin/plugins/dbsearch.tpl', function (err, res, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(res.statusCode, 200); | 
					
						
							|  |  |  | 				assert(body); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-10-14 21:48:38 +03:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 |