diff --git a/package.json b/package.json index 9d61de2de2..1f17bb490e 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,7 @@ "mousetrap": "^1.5.3", "nconf": "~0.8.2", "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-one": "1.1.5", "nodebb-plugin-markdown": "6.0.2", diff --git a/src/database/mongo/main.js b/src/database/mongo/main.js index 2743d4c7b9..0aebaf75b2 100644 --- a/src/database/mongo/main.js +++ b/src/database/mongo/main.js @@ -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) { if (!key) { return callback(); diff --git a/src/database/redis/main.js b/src/database/redis/main.js index 66379f1774..10f26cfd3e 100644 --- a/src/database/redis/main.js +++ b/src/database/redis/main.js @@ -10,6 +10,10 @@ module.exports = function (redisClient, module) { }); }; + module.emptydb = function (callback) { + module.flushdb(callback); + }; + module.exists = function (key, callback) { redisClient.exists(key, function (err, exists) { callback(err, exists === 1); diff --git a/test/authentication.js b/test/authentication.js index f63ab85f38..583c5907ba 100644 --- a/test/authentication.js +++ b/test/authentication.js @@ -127,7 +127,7 @@ describe('authentication', function () { after(function (done) { - db.flushdb(done); + db.emptydb(done); }); }); diff --git a/test/categories.js b/test/categories.js index 86c158eb3e..d922f0c3d6 100644 --- a/test/categories.js +++ b/test/categories.js @@ -174,6 +174,6 @@ describe('Categories', function () { }); after(function (done) { - db.flushdb(done); + db.emptydb(done); }); }); diff --git a/test/database/hash.js b/test/database/hash.js index b1fd87390d..d00d746e77 100644 --- a/test/database/hash.js +++ b/test/database/hash.js @@ -375,6 +375,6 @@ describe('Hash methods', function () { after(function (done) { - db.flushdb(done); + db.emptydb(done); }); }); diff --git a/test/database/keys.js b/test/database/keys.js index 0166ced52d..430ceebdb5 100644 --- a/test/database/keys.js +++ b/test/database/keys.js @@ -143,6 +143,6 @@ describe('Key methods', function () { after(function (done) { - db.flushdb(done); + db.emptydb(done); }); }); diff --git a/test/database/list.js b/test/database/list.js index 706fc2ef40..cbac21984f 100644 --- a/test/database/list.js +++ b/test/database/list.js @@ -161,6 +161,6 @@ describe('List methods', function () { after(function (done) { - db.flushdb(done); + db.emptydb(done); }); }); diff --git a/test/database/sets.js b/test/database/sets.js index 4e899c8265..aa3eda0c3d 100644 --- a/test/database/sets.js +++ b/test/database/sets.js @@ -230,6 +230,6 @@ describe('Set methods', function () { after(function (done) { - db.flushdb(done); + db.emptydb(done); }); }); diff --git a/test/database/sorted.js b/test/database/sorted.js index 29ed4cf95e..f8d6d0312d 100644 --- a/test/database/sorted.js +++ b/test/database/sorted.js @@ -696,6 +696,6 @@ describe('Sorted Set methods', function () { after(function (done) { - db.flushdb(done); + db.emptydb(done); }); }); diff --git a/test/groups.js b/test/groups.js index 31195719b6..b1163d819c 100644 --- a/test/groups.js +++ b/test/groups.js @@ -345,6 +345,6 @@ describe('Groups', function () { }); after(function (done) { - db.flushdb(done); + db.emptydb(done); }); }); diff --git a/test/messaging.js b/test/messaging.js index 4d331eaeac..3dc256e7a0 100644 --- a/test/messaging.js +++ b/test/messaging.js @@ -13,9 +13,9 @@ describe('Messaging Library', function () { before(function (done) { // Create 3 users: 1 admin, 2 regular async.parallel([ - async.apply(User.create, { username: 'foo', password: 'bar' }), // admin - async.apply(User.create, { username: 'baz', password: 'quux' }), // restricted user - async.apply(User.create, { username: 'herp', password: 'derp' }) // regular user + async.apply(User.create, { username: 'foo', password: 'barbar' }), // admin + async.apply(User.create, { username: 'baz', password: 'quuxquux' }), // restricted user + async.apply(User.create, { username: 'herp', password: 'derpderp' }) // regular user ], function (err, uids) { if (err) { return done(err); @@ -71,6 +71,6 @@ describe('Messaging Library', function () { }); after(function (done) { - db.flushdb(done); + db.emptydb(done); }); }); diff --git a/test/mocks/databasemock.js b/test/mocks/databasemock.js index 672e72bc4b..1338cde8cc 100644 --- a/test/mocks/databasemock.js +++ b/test/mocks/databasemock.js @@ -90,13 +90,23 @@ db.init(next); }, function (next) { - db.flushdb(next); + db.emptydb(next); }, function (next) { winston.info('test_database flushed'); meta = require('../../src/meta'); + setupDefaultConfigs(meta, next); + }, + function (next) { meta.configs.init(next); }, + function (next) { + meta.config.postDelay = 0; + meta.config.initialPostDelay = 0; + meta.config.newbiePostDelay = 0; + + enableDefaultPlugins(next); + }, function (next) { // nconf defaults, if not set in config if (!nconf.get('upload_path')) { @@ -131,6 +141,26 @@ ], 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)); diff --git a/test/notifications.js b/test/notifications.js index 482af2d9bb..7a3c82382e 100644 --- a/test/notifications.js +++ b/test/notifications.js @@ -97,6 +97,6 @@ describe('Notifications', function () { }); after(function (done) { - db.flushdb(done); + db.emptydb(done); }); }); diff --git a/test/posts.js b/test/posts.js index e1bc67c6f1..866879ccdd 100644 --- a/test/posts.js +++ b/test/posts.js @@ -18,7 +18,7 @@ describe('Post\'s', function () { var cid; before(function (done) { - async.parallel({ + async.series({ voterUid: function (next) { user.create({username: 'upvoter'}, next); }, @@ -206,6 +206,6 @@ describe('Post\'s', function () { }); after(function (done) { - db.flushdb(done); + db.emptydb(done); }); }); diff --git a/test/search.js b/test/search.js new file mode 100644 index 0000000000..d387f818c1 --- /dev/null +++ b/test/search.js @@ -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); + }); +}); diff --git a/test/socket.io.js b/test/socket.io.js index d4c582969a..8c70e9a6c9 100644 --- a/test/socket.io.js +++ b/test/socket.io.js @@ -102,7 +102,7 @@ describe('socket.io', function () { }); after(function (done) { - done(); + db.emptydb(done); }); }); diff --git a/test/topics.js b/test/topics.js index 56671a8f45..83b026909b 100644 --- a/test/topics.js +++ b/test/topics.js @@ -475,6 +475,6 @@ describe('Topic\'s', function () { }); after(function (done) { - db.flushdb(done); + db.emptydb(done); }); }); diff --git a/test/user.js b/test/user.js index 4de38c461a..bc805d79a0 100644 --- a/test/user.js +++ b/test/user.js @@ -327,6 +327,6 @@ describe('User', function () { }); after(function (done) { - db.flushdb(done); + db.emptydb(done); }); }); \ No newline at end of file