From b8a3ca98cb6f9d650f21e123a0cd10129dea00cf Mon Sep 17 00:00:00 2001
From: barisusakli <barisusakli@gmail.com>
Date: Sun, 16 Oct 2016 06:55:12 +0300
Subject: [PATCH] added callback to webserver.listen

wait for webserver to be ready to run tests
added topic restore test
added test to load topic via controller
---
 src/webserver.js           | 96 ++++++++++++++++++++------------------
 test/mocks/databasemock.js | 23 +++++++--
 test/topics.js             | 47 +++++++++++++++----
 3 files changed, 108 insertions(+), 58 deletions(-)

diff --git a/src/webserver.js b/src/webserver.js
index 3436f575c8..edffb47641 100644
--- a/src/webserver.js
+++ b/src/webserver.js
@@ -52,8 +52,8 @@ server.on('error', function (err) {
 	}
 });
 
-
-module.exports.listen = function () {
+module.exports.listen = function (callback) {
+	callback = callback || function () {};
 	emailer.registerApp(app);
 
 	setupExpressApp(app);
@@ -65,7 +65,7 @@ module.exports.listen = function () {
 	emitter.all(['templates:compiled', 'meta:js.compiled', 'meta:css.compiled'], function () {
 		winston.info('NodeBB Ready');
 		emitter.emit('nodebb:ready');
-		listen();
+		listen(callback);
 	});
 
 	initializeNodeBB(function (err) {
@@ -81,6 +81,50 @@ module.exports.listen = function () {
 	});
 };
 
+function initializeNodeBB(callback) {
+	winston.info('initializing NodeBB ...');
+
+	var skipJS;
+	var fromFile = nconf.get('from-file') || '';
+	var middleware = require('./middleware');
+
+	if (fromFile.match('js')) {
+		winston.info('[minifier] Minifying client-side JS skipped');
+		skipJS = true;
+	}
+
+	async.waterfall([
+		async.apply(meta.themes.setupPaths),
+		function (next) {
+			plugins.init(app, middleware, next);
+		},
+		async.apply(plugins.fireHook, 'static:assets.prepare', {}),
+		async.apply(meta.js.bridgeModules, app),
+		function (next) {
+			async.series([
+				async.apply(meta.templates.compile),
+				async.apply(!skipJS ? meta.js.minify : meta.js.getFromFile, 'nodebb.min.js'),
+				async.apply(!skipJS ? meta.js.minify : meta.js.getFromFile, 'acp.min.js'),
+				async.apply(meta.css.minify),
+				async.apply(meta.sounds.init),
+				async.apply(languages.init),
+				async.apply(meta.blacklist.load)
+			], next);
+		},
+		function (results, next) {
+			plugins.fireHook('static:app.preload', {
+				app: app,
+				middleware: middleware
+			}, next);
+		},
+		async.apply(plugins.fireHook, 'filter:hotswap.prepare', []),
+		function (hotswapIds, next) {
+			routes(app, middleware, hotswapIds);
+			next();
+		}
+	], callback);
+}
+
 function setupExpressApp(app) {
 	var middleware = require('./middleware');
 
@@ -156,49 +200,8 @@ function setupCookie() {
 	return cookie;
 }
 
-function initializeNodeBB(callback) {
-	var skipJS;
-	var fromFile = nconf.get('from-file') || '';
-	var middleware = require('./middleware');
-
-	if (fromFile.match('js')) {
-		winston.info('[minifier] Minifying client-side JS skipped');
-		skipJS = true;
-	}
-
-	async.waterfall([
-		async.apply(meta.themes.setupPaths),
-		function (next) {
-			plugins.init(app, middleware, next);
-		},
-		async.apply(plugins.fireHook, 'static:assets.prepare', {}),
-		async.apply(meta.js.bridgeModules, app),
-		function (next) {
-			async.series([
-				async.apply(meta.templates.compile),
-				async.apply(!skipJS ? meta.js.minify : meta.js.getFromFile, 'nodebb.min.js'),
-				async.apply(!skipJS ? meta.js.minify : meta.js.getFromFile, 'acp.min.js'),
-				async.apply(meta.css.minify),
-				async.apply(meta.sounds.init),
-				async.apply(languages.init),
-				async.apply(meta.blacklist.load)
-			], next);
-		},
-		function (results, next) {
-			plugins.fireHook('static:app.preload', {
-				app: app,
-				middleware: middleware
-			}, next);
-		},
-		async.apply(plugins.fireHook, 'filter:hotswap.prepare', []),
-		function (hotswapIds, next) {
-			routes(app, middleware, hotswapIds);
-			next();
-		}
-	], callback);
-}
-
-function listen() {
+function listen(callback) {
+	callback = callback || function () {};
 	var port = parseInt(nconf.get('port'), 10);
 	var isSocket = isNaN(port);
 	var socketPath = isSocket ? nconf.get('port') : '';
@@ -242,6 +245,7 @@ function listen() {
 		if (oldUmask) {
 			process.umask(oldUmask);
 		}
+		callback();
 	});
 
 	// Alter umask if necessary
diff --git a/test/mocks/databasemock.js b/test/mocks/databasemock.js
index 16bd39ba0d..e61015eec1 100644
--- a/test/mocks/databasemock.js
+++ b/test/mocks/databasemock.js
@@ -10,6 +10,7 @@
 	var async = require('async');
 	var path  = require('path');
 	var nconf = require('nconf');
+	var url = require('url');
 	var winston = require('winston');
 	var errorText;
 
@@ -79,6 +80,7 @@
 	var meta = require('../../src/meta');
 
 	before(function (done) {
+		this.timeout(30000);
 		async.waterfall([
 			function (next) {
 				db.init(next);
@@ -91,7 +93,23 @@
 				meta.configs.init(next);
 			},
 			function (next) {
-				nconf.set('url', nconf.get('base_url') + (nconf.get('use_port') ? ':' + nconf.get('port') : '') + nconf.get('relative_path'));
+				// nconf defaults, if not set in config
+				if (!nconf.get('upload_path')) {
+					nconf.set('upload_path', '/public/uploads');
+				}
+				if (!nconf.get('sessionKey')) {
+					nconf.set('sessionKey', 'express.sid');
+				}
+				// Parse out the relative_url and other goodies from the configured URL
+				var urlObject = url.parse(nconf.get('url'));
+				var relativePath = urlObject.pathname !== '/' ? urlObject.pathname : '';
+				nconf.set('base_url', urlObject.protocol + '//' + urlObject.host);
+				nconf.set('secure', urlObject.protocol === 'https:');
+				nconf.set('use_port', !!urlObject.port);
+				nconf.set('relative_path', relativePath);
+				nconf.set('port', urlObject.port || nconf.get('port') || nconf.get('PORT') || (nconf.get('PORT_ENV_VAR') ? nconf.get(nconf.get('PORT_ENV_VAR')) : false) || 4567);
+				nconf.set('upload_url', nconf.get('upload_path').replace(/^\/public/, ''));
+
 				nconf.set('core_templates_path', path.join(__dirname, '../../src/views'));
 				nconf.set('base_templates_path', path.join(nconf.get('themes_path'), 'nodebb-theme-vanilla/templates'));
 				nconf.set('theme_templates_path', meta.config['theme:templates'] ? path.join(nconf.get('themes_path'), meta.config['theme:id'], meta.config['theme:templates']) : nconf.get('base_templates_path'));
@@ -103,8 +121,7 @@
 				require('../../src/notifications').init();
 				require('../../src/user').startJobs();
 
-				webserver.listen();
-				next();
+				webserver.listen(next);
 			}
 		], done);
 	});
diff --git a/test/topics.js b/test/topics.js
index d0af967a0f..56671a8f45 100644
--- a/test/topics.js
+++ b/test/topics.js
@@ -1,26 +1,28 @@
 'use strict';
 /*global require, before, beforeEach, after*/
 
+var async = require('async');
 var	assert = require('assert');
 var validator = require('validator');
+var nconf = require('nconf');
+
 var db = require('./mocks/databasemock');
 var topics = require('../src/topics');
 var categories = require('../src/categories');
 var User = require('../src/user');
 var groups = require('../src/groups');
-var async = require('async');
 
 describe('Topic\'s', function () {
-	var topic,
-		categoryObj;
+	var topic;
+	var categoryObj;
 
 	before(function (done) {
 		var userData = {
-				username: 'John Smith',
-				password: 'swordfish',
-				email: 'john@example.com',
-				callback: undefined
-			};
+			username: 'John Smith',
+			password: 'swordfish',
+			email: 'john@example.com',
+			callback: undefined
+		};
 
 		User.create({username: userData.username, password: userData.password, email: userData.email}, function (err, uid) {
 			if (err) {
@@ -202,7 +204,7 @@ describe('Topic\'s', function () {
 		});
 	});
 
-	describe('.purge/.delete', function () {
+	describe('delete/restore/purge', function () {
 		var newTopic;
 		var followerUid;
 		before(function (done) {
@@ -231,6 +233,13 @@ describe('Topic\'s', function () {
 			});
 		});
 
+		it('should restore the topic', function (done) {
+			topics.restore(newTopic.tid, 1, function (err) {
+				assert.ifError(err);
+				done();
+			});
+		});
+
 		it('should purge the topic', function (done) {
 			topics.purge(newTopic.tid, 1, function (err) {
 				assert.ifError(err);
@@ -445,6 +454,26 @@ describe('Topic\'s', function () {
 		});
 	});
 
+	it('should load topic', function (done) {
+		topics.post({
+			uid: topic.userId,
+			title: 'topic for controller test',
+			content: 'topic content',
+			cid: topic.categoryId,
+			thumb: 'http://i.imgur.com/64iBdBD.jpg'
+		}, function (err, result) {
+			assert.ifError(err);
+			assert.ok(result);
+			var request = require('request');
+			request(nconf.get('url') + '/topic/' + result.topicData.slug, function (err, response, body) {
+				assert.ifError(err);
+				assert.equal(response.statusCode, 200);
+				assert(body);
+				done();
+			});
+		});
+	});
+
 	after(function (done) {
 		db.flushdb(done);
 	});