mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 11:05:54 +01:00 
			
		
		
		
	search tests
new method emptydb use emptydb instead of flushdb, flushdb removes indices for mongo which breaks search tests initialize meta.config properly in tests enable nodebb-plugin-dbsearch for tests
This commit is contained in:
		| @@ -53,7 +53,7 @@ | |||||||
|     "mousetrap": "^1.5.3", |     "mousetrap": "^1.5.3", | ||||||
|     "nconf": "~0.8.2", |     "nconf": "~0.8.2", | ||||||
|     "nodebb-plugin-composer-default": "4.2.9", |     "nodebb-plugin-composer-default": "4.2.9", | ||||||
|     "nodebb-plugin-dbsearch": "1.0.2", |     "nodebb-plugin-dbsearch": "1.0.3", | ||||||
|     "nodebb-plugin-emoji-extended": "1.1.1", |     "nodebb-plugin-emoji-extended": "1.1.1", | ||||||
|     "nodebb-plugin-emoji-one": "1.1.5", |     "nodebb-plugin-emoji-one": "1.1.5", | ||||||
|     "nodebb-plugin-markdown": "6.0.2", |     "nodebb-plugin-markdown": "6.0.2", | ||||||
|   | |||||||
| @@ -12,6 +12,13 @@ module.exports = function (db, module) { | |||||||
| 		}); | 		}); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
|  | 	module.emptydb = function (callback) { | ||||||
|  | 		callback = callback || helpers.noop; | ||||||
|  | 		db.collection('objects').remove({}, function (err) { | ||||||
|  | 			callback(err); | ||||||
|  | 		}); | ||||||
|  | 	}; | ||||||
|  |  | ||||||
| 	module.exists = function (key, callback) { | 	module.exists = function (key, callback) { | ||||||
| 		if (!key) { | 		if (!key) { | ||||||
| 			return callback(); | 			return callback(); | ||||||
|   | |||||||
| @@ -10,6 +10,10 @@ module.exports = function (redisClient, module) { | |||||||
| 		}); | 		}); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
|  | 	module.emptydb = function (callback) { | ||||||
|  | 		module.flushdb(callback); | ||||||
|  | 	}; | ||||||
|  |  | ||||||
| 	module.exists = function (key, callback) { | 	module.exists = function (key, callback) { | ||||||
| 		redisClient.exists(key, function (err, exists) { | 		redisClient.exists(key, function (err, exists) { | ||||||
| 			callback(err, exists === 1); | 			callback(err, exists === 1); | ||||||
|   | |||||||
| @@ -127,7 +127,7 @@ describe('authentication', function () { | |||||||
|  |  | ||||||
|  |  | ||||||
| 	after(function (done) { | 	after(function (done) { | ||||||
| 		db.flushdb(done); | 		db.emptydb(done); | ||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
| }); | }); | ||||||
|   | |||||||
| @@ -174,6 +174,6 @@ describe('Categories', function () { | |||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
| 	after(function (done) { | 	after(function (done) { | ||||||
| 		db.flushdb(done); | 		db.emptydb(done); | ||||||
| 	}); | 	}); | ||||||
| }); | }); | ||||||
|   | |||||||
| @@ -375,6 +375,6 @@ describe('Hash methods', function () { | |||||||
|  |  | ||||||
|  |  | ||||||
| 	after(function (done) { | 	after(function (done) { | ||||||
| 		db.flushdb(done); | 		db.emptydb(done); | ||||||
| 	}); | 	}); | ||||||
| }); | }); | ||||||
|   | |||||||
| @@ -143,6 +143,6 @@ describe('Key methods', function () { | |||||||
|  |  | ||||||
|  |  | ||||||
| 	after(function (done) { | 	after(function (done) { | ||||||
| 		db.flushdb(done); | 		db.emptydb(done); | ||||||
| 	}); | 	}); | ||||||
| }); | }); | ||||||
|   | |||||||
| @@ -161,6 +161,6 @@ describe('List methods', function () { | |||||||
|  |  | ||||||
|  |  | ||||||
| 	after(function (done) { | 	after(function (done) { | ||||||
| 		db.flushdb(done); | 		db.emptydb(done); | ||||||
| 	}); | 	}); | ||||||
| }); | }); | ||||||
|   | |||||||
| @@ -230,6 +230,6 @@ describe('Set methods', function () { | |||||||
|  |  | ||||||
|  |  | ||||||
| 	after(function (done) { | 	after(function (done) { | ||||||
| 		db.flushdb(done); | 		db.emptydb(done); | ||||||
| 	}); | 	}); | ||||||
| }); | }); | ||||||
|   | |||||||
| @@ -696,6 +696,6 @@ describe('Sorted Set methods', function () { | |||||||
|  |  | ||||||
|  |  | ||||||
| 	after(function (done) { | 	after(function (done) { | ||||||
| 		db.flushdb(done); | 		db.emptydb(done); | ||||||
| 	}); | 	}); | ||||||
| }); | }); | ||||||
|   | |||||||
| @@ -345,6 +345,6 @@ describe('Groups', function () { | |||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
| 	after(function (done) { | 	after(function (done) { | ||||||
| 		db.flushdb(done); | 		db.emptydb(done); | ||||||
| 	}); | 	}); | ||||||
| }); | }); | ||||||
|   | |||||||
| @@ -13,9 +13,9 @@ describe('Messaging Library', function () { | |||||||
| 	before(function (done) { | 	before(function (done) { | ||||||
| 		// Create 3 users: 1 admin, 2 regular | 		// Create 3 users: 1 admin, 2 regular | ||||||
| 		async.parallel([ | 		async.parallel([ | ||||||
| 			async.apply(User.create, { username: 'foo', password: 'bar' }),	// admin | 			async.apply(User.create, { username: 'foo', password: 'barbar' }),	// admin | ||||||
| 			async.apply(User.create, { username: 'baz', password: 'quux' }),	// restricted user | 			async.apply(User.create, { username: 'baz', password: 'quuxquux' }),	// restricted user | ||||||
| 			async.apply(User.create, { username: 'herp', password: 'derp' })	// regular user | 			async.apply(User.create, { username: 'herp', password: 'derpderp' })	// regular user | ||||||
| 		], function (err, uids) { | 		], function (err, uids) { | ||||||
| 			if (err) { | 			if (err) { | ||||||
| 				return done(err); | 				return done(err); | ||||||
| @@ -71,6 +71,6 @@ describe('Messaging Library', function () { | |||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
| 	after(function (done) { | 	after(function (done) { | ||||||
| 		db.flushdb(done); | 		db.emptydb(done); | ||||||
| 	}); | 	}); | ||||||
| }); | }); | ||||||
|   | |||||||
| @@ -90,13 +90,23 @@ | |||||||
| 				db.init(next); | 				db.init(next); | ||||||
| 			}, | 			}, | ||||||
| 			function (next) { | 			function (next) { | ||||||
| 				db.flushdb(next); | 				db.emptydb(next); | ||||||
| 			}, | 			}, | ||||||
| 			function (next) { | 			function (next) { | ||||||
| 				winston.info('test_database flushed'); | 				winston.info('test_database flushed'); | ||||||
| 				meta = require('../../src/meta'); | 				meta = require('../../src/meta'); | ||||||
|  | 				setupDefaultConfigs(meta, next); | ||||||
|  | 			}, | ||||||
|  | 			function (next) { | ||||||
| 				meta.configs.init(next); | 				meta.configs.init(next); | ||||||
| 			}, | 			}, | ||||||
|  | 			function (next) { | ||||||
|  | 				meta.config.postDelay = 0; | ||||||
|  | 				meta.config.initialPostDelay = 0; | ||||||
|  | 				meta.config.newbiePostDelay = 0; | ||||||
|  |  | ||||||
|  | 				enableDefaultPlugins(next); | ||||||
|  | 			}, | ||||||
| 			function (next) { | 			function (next) { | ||||||
| 				// nconf defaults, if not set in config | 				// nconf defaults, if not set in config | ||||||
| 				if (!nconf.get('upload_path')) { | 				if (!nconf.get('upload_path')) { | ||||||
| @@ -131,6 +141,26 @@ | |||||||
| 		], done); | 		], done); | ||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
|  | 	function setupDefaultConfigs(meta, next) { | ||||||
|  | 		winston.info('Populating database with default configs, if not already set...\n'); | ||||||
|  |  | ||||||
|  | 		var defaults = require(path.join(nconf.get('base_dir'), 'install/data/defaults.json')); | ||||||
|  |  | ||||||
|  | 		meta.configs.setOnEmpty(defaults, next); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	function enableDefaultPlugins(callback) { | ||||||
|  | 		winston.info('Enabling default plugins\n'); | ||||||
|  |  | ||||||
|  | 		var defaultEnabled = [ | ||||||
|  | 			'nodebb-plugin-dbsearch' | ||||||
|  | 		]; | ||||||
|  |  | ||||||
|  | 		winston.info('[install/enableDefaultPlugins] activating default plugins', defaultEnabled); | ||||||
|  |  | ||||||
|  | 		db.sortedSetAdd('plugins:active', [0], defaultEnabled, callback); | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	module.exports = db; | 	module.exports = db; | ||||||
|  |  | ||||||
| }(module)); | }(module)); | ||||||
|   | |||||||
| @@ -97,6 +97,6 @@ describe('Notifications', function () { | |||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
| 	after(function (done) { | 	after(function (done) { | ||||||
| 		db.flushdb(done); | 		db.emptydb(done); | ||||||
| 	}); | 	}); | ||||||
| }); | }); | ||||||
|   | |||||||
| @@ -18,7 +18,7 @@ describe('Post\'s', function () { | |||||||
| 	var cid; | 	var cid; | ||||||
|  |  | ||||||
| 	before(function (done) { | 	before(function (done) { | ||||||
| 		async.parallel({ | 		async.series({ | ||||||
| 			voterUid: function (next) { | 			voterUid: function (next) { | ||||||
| 				user.create({username: 'upvoter'}, next); | 				user.create({username: 'upvoter'}, next); | ||||||
| 			}, | 			}, | ||||||
| @@ -206,6 +206,6 @@ describe('Post\'s', function () { | |||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
| 	after(function (done) { | 	after(function (done) { | ||||||
| 		db.flushdb(done); | 		db.emptydb(done); | ||||||
| 	}); | 	}); | ||||||
| }); | }); | ||||||
|   | |||||||
							
								
								
									
										160
									
								
								test/search.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										160
									
								
								test/search.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,160 @@ | |||||||
|  | 'use strict'; | ||||||
|  | /*global require, before, after*/ | ||||||
|  |  | ||||||
|  | var	assert = require('assert'); | ||||||
|  | var async = require('async'); | ||||||
|  | var request = require('request'); | ||||||
|  | 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 search = require('../src/search'); | ||||||
|  |  | ||||||
|  | describe('Search', function () { | ||||||
|  | 	var phoebeUid; | ||||||
|  | 	var gingerUid; | ||||||
|  |  | ||||||
|  | 	var topic1Data; | ||||||
|  | 	var topic2Data; | ||||||
|  | 	var post1Data; | ||||||
|  | 	var post2Data; | ||||||
|  | 	var post3Data; | ||||||
|  | 	var cid1; | ||||||
|  | 	var cid2; | ||||||
|  |  | ||||||
|  | 	before(function (done) { | ||||||
|  | 		async.waterfall([ | ||||||
|  | 			function (next) { | ||||||
|  | 				async.series({ | ||||||
|  | 					phoebe: function (next) { | ||||||
|  | 						user.create({username: 'phoebe'}, next); | ||||||
|  | 					}, | ||||||
|  | 					ginger: function (next) { | ||||||
|  | 						user.create({username: 'ginger'}, next); | ||||||
|  | 					}, | ||||||
|  | 					category1: function (next) { | ||||||
|  | 						categories.create({ | ||||||
|  | 							name: 'Test Category', | ||||||
|  | 							description: 'Test category created by testing script' | ||||||
|  | 						}, next); | ||||||
|  | 					}, | ||||||
|  | 					category2: function (next) { | ||||||
|  | 						categories.create({ | ||||||
|  | 							name: 'Test Category', | ||||||
|  | 							description: 'Test category created by testing script' | ||||||
|  | 						}, next); | ||||||
|  | 					} | ||||||
|  | 				}, next); | ||||||
|  | 			}, | ||||||
|  | 			function (results, next) { | ||||||
|  | 				phoebeUid = results.phoebe; | ||||||
|  | 				gingerUid = results.ginger; | ||||||
|  | 				cid1 = results.category1.cid; | ||||||
|  | 				cid2 = results.category2.cid; | ||||||
|  |  | ||||||
|  | 				async.waterfall([ | ||||||
|  | 					function (next) { | ||||||
|  | 						topics.post({ | ||||||
|  | 							uid: phoebeUid, | ||||||
|  | 							cid: cid1, | ||||||
|  | 							title: 'nodebb mongodb bugs', | ||||||
|  | 							content: 'avocado cucumber apple orange fox', | ||||||
|  | 							tags: ['nodebb', 'bug', 'plugin', 'nodebb-plugin'] | ||||||
|  | 						}, next); | ||||||
|  | 					}, | ||||||
|  | 					function (results, next) { | ||||||
|  | 						topic1Data = results.topicData; | ||||||
|  | 						post1Data = results.postData; | ||||||
|  |  | ||||||
|  | 						topics.post({ | ||||||
|  | 							uid: gingerUid, | ||||||
|  | 							cid: cid2, | ||||||
|  | 							title: 'java mongodb redis', | ||||||
|  | 							content: 'avocado cucumber carrot armadillo', | ||||||
|  | 							tags: ['nodebb', 'bug', 'plugin', 'nodebb-plugin'] | ||||||
|  | 						}, next); | ||||||
|  | 					}, | ||||||
|  | 					function (results, next) { | ||||||
|  | 						topic2Data = results.topicData; | ||||||
|  | 						post2Data = results.postData; | ||||||
|  | 						topics.reply({ | ||||||
|  | 							uid: phoebeUid, | ||||||
|  | 							content: 'reply post apple', | ||||||
|  | 							tid: topic2Data.tid | ||||||
|  | 						}, next); | ||||||
|  | 					}, | ||||||
|  | 					function (_post3Data, next) { | ||||||
|  | 						post3Data = _post3Data; | ||||||
|  | 						setTimeout(next, 500); | ||||||
|  | 					} | ||||||
|  | 				], next); | ||||||
|  | 			} | ||||||
|  | 		], done); | ||||||
|  | 	}); | ||||||
|  |  | ||||||
|  | 	it('should search term in titles and posts', function (done) { | ||||||
|  |  | ||||||
|  | 		var meta = require('../src/meta'); | ||||||
|  | 		meta.config.allowGuestSearching = 1; | ||||||
|  |  | ||||||
|  | 		request({ | ||||||
|  | 			url: nconf.get('url') + '/api/search?term=cucumber&in=titlesposts&by=phoebe&replies=1&repliesFilter=atleast&sortBy=timestamp&sortDirection=desc&showAs=posts', | ||||||
|  | 			json: true | ||||||
|  | 		}, function (err, response, body) { | ||||||
|  | 			assert.ifError(err); | ||||||
|  | 			assert(body); | ||||||
|  | 			assert.equal(body.matchCount, 1); | ||||||
|  | 			assert.equal(body.posts.length, 1); | ||||||
|  | 			assert.equal(body.posts[0].pid, post1Data.pid); | ||||||
|  | 			assert.equal(body.posts[0].uid, phoebeUid); | ||||||
|  |  | ||||||
|  | 			done(); | ||||||
|  | 		}); | ||||||
|  | 	}); | ||||||
|  |  | ||||||
|  | 	it('should search for a user', function (done) { | ||||||
|  | 		search.search({ | ||||||
|  | 			query: 'gin', | ||||||
|  | 			searchIn: 'users' | ||||||
|  | 		}, function (err, data) { | ||||||
|  | 			assert.ifError(err); | ||||||
|  | 			assert(data); | ||||||
|  | 			assert.equal(data.matchCount, 1); | ||||||
|  | 			assert.equal(data.users.length, 1); | ||||||
|  | 			assert.equal(data.users[0].uid, gingerUid); | ||||||
|  | 			assert.equal(data.users[0].username, 'ginger'); | ||||||
|  | 			done(); | ||||||
|  | 		}); | ||||||
|  | 	}); | ||||||
|  |  | ||||||
|  | 	it('should search for a tag', function (done) { | ||||||
|  | 		search.search({ | ||||||
|  | 			query: 'plug', | ||||||
|  | 			searchIn: 'tags' | ||||||
|  | 		}, function (err, data) { | ||||||
|  | 			assert.ifError(err); | ||||||
|  | 			assert(data); | ||||||
|  | 			assert.equal(data.matchCount, 1); | ||||||
|  | 			assert.equal(data.tags.length, 1); | ||||||
|  | 			assert.equal(data.tags[0].value, 'plugin'); | ||||||
|  | 			assert.equal(data.tags[0].score, 2); | ||||||
|  | 			done(); | ||||||
|  | 		}); | ||||||
|  | 	}); | ||||||
|  |  | ||||||
|  | 	it('should fail if searchIn is wrong', function (done) { | ||||||
|  | 		search.search({ | ||||||
|  | 			query: 'plug', | ||||||
|  | 			searchIn: 'invalidfilter' | ||||||
|  | 		}, function (err) { | ||||||
|  | 			assert.equal(err.message, '[[error:unknown-search-filter]]'); | ||||||
|  | 			done(); | ||||||
|  | 		}); | ||||||
|  | 	}); | ||||||
|  |  | ||||||
|  | 	after(function (done) { | ||||||
|  | 		db.emptydb(done); | ||||||
|  | 	}); | ||||||
|  | }); | ||||||
| @@ -102,7 +102,7 @@ describe('socket.io', function () { | |||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
| 	after(function (done) { | 	after(function (done) { | ||||||
| 		done(); | 		db.emptydb(done); | ||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
| }); | }); | ||||||
|   | |||||||
| @@ -475,6 +475,6 @@ describe('Topic\'s', function () { | |||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
| 	after(function (done) { | 	after(function (done) { | ||||||
| 		db.flushdb(done); | 		db.emptydb(done); | ||||||
| 	}); | 	}); | ||||||
| }); | }); | ||||||
|   | |||||||
| @@ -327,6 +327,6 @@ describe('User', function () { | |||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
| 	after(function (done) { | 	after(function (done) { | ||||||
| 		db.flushdb(done); | 		db.emptydb(done); | ||||||
| 	}); | 	}); | ||||||
| }); | }); | ||||||
		Reference in New Issue
	
	Block a user