| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | const	assert = require('assert'); | 
					
						
							|  |  |  | const async = require('async'); | 
					
						
							|  |  |  | const request = require('request'); | 
					
						
							|  |  |  | const nconf = require('nconf'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const db = require('./mocks/databasemock'); | 
					
						
							|  |  |  | const topics = require('../src/topics'); | 
					
						
							|  |  |  | const categories = require('../src/categories'); | 
					
						
							|  |  |  | const user = require('../src/user'); | 
					
						
							|  |  |  | const search = require('../src/search'); | 
					
						
							|  |  |  | const privileges = require('../src/privileges'); | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | describe('Search', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 	let phoebeUid; | 
					
						
							|  |  |  | 	let gingerUid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	let topic1Data; | 
					
						
							|  |  |  | 	let topic2Data; | 
					
						
							|  |  |  | 	let post1Data; | 
					
						
							|  |  |  | 	let post2Data; | 
					
						
							|  |  |  | 	let post3Data; | 
					
						
							|  |  |  | 	let cid1; | 
					
						
							|  |  |  | 	let cid2; | 
					
						
							|  |  |  | 	let cid3; | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	before((done) => { | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 		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) { | 
					
						
							| 
									
										
										
										
											2018-10-18 12:50:24 -04:00
										 |  |  | 						categories.create({ | 
					
						
							|  |  |  | 							name: 'Child Test Category', | 
					
						
							|  |  |  | 							description: 'Test category created by testing script', | 
					
						
							|  |  |  | 							parentCid: cid2, | 
					
						
							|  |  |  | 						}, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					function (category, next) { | 
					
						
							|  |  |  | 						cid3 = category.cid; | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 						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); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should search term in titles and posts', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const meta = require('../src/meta'); | 
					
						
							|  |  |  | 		const qs = `/api/search?term=cucumber&in=titlesposts&categories[]=${cid1}&by=phoebe&replies=1&repliesFilter=atleast&sortBy=timestamp&sortDirection=desc&showAs=posts`; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		privileges.global.give(['groups:search:content'], 'guests', (err) => { | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 			assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2018-05-28 11:29:37 -04:00
										 |  |  | 			request({ | 
					
						
							|  |  |  | 				url: nconf.get('url') + qs, | 
					
						
							|  |  |  | 				json: true, | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			}, (err, response, body) => { | 
					
						
							| 
									
										
										
										
											2018-05-28 11:29:37 -04:00
										 |  |  | 				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); | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-26 21:57:38 -04:00
										 |  |  | 				privileges.global.rescind(['groups:search:content'], 'guests', done); | 
					
						
							| 
									
										
										
										
											2018-05-28 11:29:37 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should search for a user', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 		search.search({ | 
					
						
							|  |  |  | 			query: 'gin', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			searchIn: 'users', | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		}, (err, data) => { | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 			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(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should search for a tag', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 		search.search({ | 
					
						
							|  |  |  | 			query: 'plug', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			searchIn: 'tags', | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		}, (err, data) => { | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 			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(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-17 13:03:14 -05:00
										 |  |  | 	it('should search for a category', async () => { | 
					
						
							|  |  |  | 		await categories.create({ | 
					
						
							|  |  |  | 			name: 'foo category', | 
					
						
							|  |  |  | 			description: 'Test category created by testing script', | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		await categories.create({ | 
					
						
							|  |  |  | 			name: 'baz category', | 
					
						
							|  |  |  | 			description: 'Test category created by testing script', | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		const result = await search.search({ | 
					
						
							|  |  |  | 			query: 'baz', | 
					
						
							|  |  |  | 			searchIn: 'categories', | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		assert.strictEqual(result.matchCount, 1); | 
					
						
							|  |  |  | 		assert.strictEqual(result.categories[0].name, 'baz category'); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 11:39:10 -05:00
										 |  |  | 	it('should search for categories', async () => { | 
					
						
							|  |  |  | 		const socketCategories = require('../src/socket.io/categories'); | 
					
						
							|  |  |  | 		let data = await socketCategories.categorySearch({ uid: phoebeUid }, { query: 'baz', parentCid: 0 }); | 
					
						
							|  |  |  | 		assert.strictEqual(data[0].name, 'baz category'); | 
					
						
							|  |  |  | 		data = await socketCategories.categorySearch({ uid: phoebeUid }, { query: '', parentCid: 0 }); | 
					
						
							|  |  |  | 		assert.strictEqual(data.length, 5); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should fail if searchIn is wrong', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 		search.search({ | 
					
						
							|  |  |  | 			query: 'plug', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			searchIn: 'invalidfilter', | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		}, (err) => { | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 			assert.equal(err.message, '[[error:unknown-search-filter]]'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should search with tags filter', (done) => { | 
					
						
							| 
									
										
										
										
											2016-12-19 21:40:09 +03:00
										 |  |  | 		search.search({ | 
					
						
							|  |  |  | 			query: 'mongodb', | 
					
						
							|  |  |  | 			searchIn: 'titles', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			hasTags: ['nodebb', 'javascript'], | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		}, (err, data) => { | 
					
						
							| 
									
										
										
										
											2016-12-19 21:40:09 +03:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(data.posts[0].tid, topic2Data.tid); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-12-11 11:21:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should not crash if tags is not an array', (done) => { | 
					
						
							| 
									
										
										
										
											2017-12-11 11:21:22 -05:00
										 |  |  | 		search.search({ | 
					
						
							|  |  |  | 			query: 'mongodb', | 
					
						
							|  |  |  | 			searchIn: 'titles', | 
					
						
							|  |  |  | 			hasTags: 'nodebb,javascript', | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		}, (err, data) => { | 
					
						
							| 
									
										
										
										
											2017-12-11 11:21:22 -05:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2018-10-18 12:50:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should not find anything', (done) => { | 
					
						
							| 
									
										
										
										
											2018-10-18 12:50:24 -04:00
										 |  |  | 		search.search({ | 
					
						
							|  |  |  | 			query: 'xxxxxxxxxxxxxx', | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		}, (err, data) => { | 
					
						
							| 
									
										
										
										
											2018-10-18 12:50:24 -04:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert(Array.isArray(data.posts)); | 
					
						
							|  |  |  | 			assert(!data.matchCount); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should search child categories', (done) => { | 
					
						
							| 
									
										
										
										
											2018-10-18 12:50:24 -04:00
										 |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				topics.post({ | 
					
						
							|  |  |  | 					uid: gingerUid, | 
					
						
							|  |  |  | 					cid: cid3, | 
					
						
							|  |  |  | 					title: 'child category topic', | 
					
						
							|  |  |  | 					content: 'avocado cucumber carrot armadillo', | 
					
						
							|  |  |  | 				}, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (result, next) { | 
					
						
							|  |  |  | 				search.search({ | 
					
						
							|  |  |  | 					query: 'avocado', | 
					
						
							|  |  |  | 					searchIn: 'titlesposts', | 
					
						
							|  |  |  | 					categories: [cid2], | 
					
						
							|  |  |  | 					searchChildren: true, | 
					
						
							|  |  |  | 					sortBy: 'topic.timestamp', | 
					
						
							|  |  |  | 					sortDirection: 'desc', | 
					
						
							|  |  |  | 				}, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (result, next) { | 
					
						
							|  |  |  | 				assert(result.posts.length, 2); | 
					
						
							|  |  |  | 				assert(result.posts[0].topic.title === 'child category topic'); | 
					
						
							|  |  |  | 				assert(result.posts[1].topic.title === 'java mongodb redis'); | 
					
						
							|  |  |  | 				next(); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		], done); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2018-11-09 15:09:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return json search data with no categories', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const qs = '/api/search?term=cucumber&in=titlesposts&searchOnly=1'; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		privileges.global.give(['groups:search:content'], 'guests', (err) => { | 
					
						
							| 
									
										
										
										
											2018-11-09 15:09:40 -05:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			request({ | 
					
						
							|  |  |  | 				url: nconf.get('url') + qs, | 
					
						
							|  |  |  | 				json: true, | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			}, (err, response, body) => { | 
					
						
							| 
									
										
										
										
											2018-11-09 15:09:40 -05:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(body); | 
					
						
							|  |  |  | 				assert(body.hasOwnProperty('matchCount')); | 
					
						
							|  |  |  | 				assert(body.hasOwnProperty('pagination')); | 
					
						
							|  |  |  | 				assert(body.hasOwnProperty('pageCount')); | 
					
						
							|  |  |  | 				assert(body.hasOwnProperty('posts')); | 
					
						
							|  |  |  | 				assert(!body.hasOwnProperty('categories')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-26 21:57:38 -04:00
										 |  |  | 				privileges.global.rescind(['groups:search:content'], 'guests', done); | 
					
						
							| 
									
										
										
										
											2018-11-09 15:09:40 -05:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | }); |