| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2017-02-17 21:55:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 						user.create({ username: 'phoebe' }, next); | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 					}, | 
					
						
							|  |  |  | 					ginger: function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 						user.create({ username: 'ginger' }, next); | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 					}, | 
					
						
							|  |  |  | 					category1: function (next) { | 
					
						
							|  |  |  | 						categories.create({ | 
					
						
							|  |  |  | 							name: 'Test Category', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 							description: 'Test category created by testing script', | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 						}, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					category2: function (next) { | 
					
						
							|  |  |  | 						categories.create({ | 
					
						
							|  |  |  | 							name: 'Test Category', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 							description: 'Test category created by testing script', | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 						}, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 				}, 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', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 							tags: ['nodebb', 'bug', 'plugin', 'nodebb-plugin', 'jquery'], | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 						}, 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', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 							tags: ['nodebb', 'bug', 'plugin', 'nodebb-plugin', 'javascript'], | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 						}, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					function (results, next) { | 
					
						
							|  |  |  | 						topic2Data = results.topicData; | 
					
						
							|  |  |  | 						post2Data = results.postData; | 
					
						
							|  |  |  | 						topics.reply({ | 
					
						
							|  |  |  | 							uid: phoebeUid, | 
					
						
							|  |  |  | 							content: 'reply post apple', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 							tid: topic2Data.tid, | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 						}, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					function (_post3Data, next) { | 
					
						
							|  |  |  | 						post3Data = _post3Data; | 
					
						
							|  |  |  | 						setTimeout(next, 500); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 				], next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 		], done); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should search term in titles and posts', function (done) { | 
					
						
							|  |  |  | 		var meta = require('../src/meta'); | 
					
						
							|  |  |  | 		meta.config.allowGuestSearching = 1; | 
					
						
							| 
									
										
										
										
											2016-10-17 23:54:06 +03:00
										 |  |  | 		var qs = '/api/search?term=cucumber&in=titlesposts&categories[]=' + cid1 + '&by=phoebe&replies=1&repliesFilter=atleast&sortBy=timestamp&sortDirection=desc&showAs=posts'; | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		request({ | 
					
						
							| 
									
										
										
										
											2016-10-17 23:54:06 +03:00
										 |  |  | 			url: nconf.get('url') + qs, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			json: true, | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 		}, 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', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			searchIn: 'users', | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 		}, 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', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			searchIn: 'tags', | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 		}, 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', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			searchIn: 'invalidfilter', | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 		}, function (err) { | 
					
						
							|  |  |  | 			assert.equal(err.message, '[[error:unknown-search-filter]]'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-19 21:40:09 +03:00
										 |  |  | 	it('should search with tags filter', function (done) { | 
					
						
							|  |  |  | 		search.search({ | 
					
						
							|  |  |  | 			query: 'mongodb', | 
					
						
							|  |  |  | 			searchIn: 'titles', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			hasTags: ['nodebb', 'javascript'], | 
					
						
							| 
									
										
										
										
											2016-12-19 21:40:09 +03:00
										 |  |  | 		}, function (err, data) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(data.posts[0].tid, topic2Data.tid); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 	after(function (done) { | 
					
						
							|  |  |  | 		db.emptydb(done); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }); |