Skip to content

Node.js based forum software built for the modern web

more topic tests

barisusaklicd448f90cd2d
diff --git a/src/socket.io/topics.js b/src/socket.io/topics.js
index 5eb9ce61dd..3b8a69d0f4 100644
--- a/src/socket.io/topics.js
+++ b/src/socket.io/topics.js
@@ -1,12 +1,14 @@
'use strict';
+var async = require('async');
+
var topics = require('../topics');
var websockets = require('./index');
var user = require('../user');
var apiController = require('../controllers/api');
var socketHelpers = require('./helpers');
-var SocketTopics = {};
+var SocketTopics = module.exports;
require('./topics/unread')(SocketTopics);
require('./topics/move')(SocketTopics);
@@ -23,18 +25,19 @@ SocketTopics.post = function (socket, data, callback) {
data.req = websockets.reqFromSocket(socket);
data.timestamp = Date.now();
- topics.post(data, function (err, result) {
- if (err) {
- return callback(err);
- }
-
- callback(null, result.topicData);
+ async.waterfall([
+ function (next) {
+ topics.post(data, next);
+ },
+ function (result, next) {
+ next(null, result.topicData);
- socket.emit('event:new_post', { posts: [result.postData] });
- socket.emit('event:new_topic', result.topicData);
+ socket.emit('event:new_post', { posts: [result.postData] });
+ socket.emit('event:new_topic', result.topicData);
- socketHelpers.notifyNew(socket.uid, 'newTopic', { posts: [result.postData], topic: result.topicData });
- });
+ socketHelpers.notifyNew(socket.uid, 'newTopic', { posts: [result.postData], topic: result.topicData });
+ },
+ ], callback);
};
SocketTopics.postcount = function (socket, tid, callback) {
@@ -61,7 +64,7 @@ SocketTopics.createTopicFromPosts = function (socket, data, callback) {
};
SocketTopics.changeWatching = function (socket, data, callback) {
- if (!data.tid || !data.type) {
+ if (!data || !data.tid || !data.type) {
return callback(new Error('[[error:invalid-data]]'));
}
var commands = ['follow', 'unfollow', 'ignore'];
@@ -90,20 +93,23 @@ SocketTopics.isFollowed = function (socket, tid, callback) {
};
SocketTopics.search = function (socket, data, callback) {
+ if (!data) {
+ return callback(new Error('[[error:invalid-data]]'));
+ }
topics.search(data.tid, data.term, callback);
};
SocketTopics.isModerator = function (socket, tid, callback) {
- topics.getTopicField(tid, 'cid', function (err, cid) {
- if (err) {
- return callback(err);
- }
- user.isModerator(socket.uid, cid, callback);
- });
+ async.waterfall([
+ function (next) {
+ topics.getTopicField(tid, 'cid', next);
+ },
+ function (cid, next) {
+ user.isModerator(socket.uid, cid, next);
+ },
+ ], callback);
};
SocketTopics.getTopic = function (socket, tid, callback) {
apiController.getTopicData(tid, socket.uid, callback);
};
-
-module.exports = SocketTopics;
diff --git a/src/topics.js b/src/topics.js
index 0eba6545fd..77cea4ec98 100644
--- a/src/topics.js
+++ b/src/topics.js
@@ -317,7 +317,7 @@ var social = require('./social');
term: term,
}, callback);
} else {
- callback(new Error('no-plugins-available'), []);
+ callback(new Error('[[error:no-plugins-available]]'), []);
}
};
}(exports));
diff --git a/test/topics.js b/test/topics.js
index 2573225511..ea2ac6ecfc 100644
--- a/test/topics.js
+++ b/test/topics.js
@@ -12,6 +12,7 @@ var User = require('../src/user');
var groups = require('../src/groups');
var helpers = require('./helpers');
var socketPosts = require('../src/socket.io/posts');
+var socketTopics = require('../src/socket.io/topics');
describe('Topic\'s', function () {
var topic;
@@ -49,11 +50,34 @@ describe('Topic\'s', function () {
});
describe('.post', function () {
+ it('should fail to create topic with invalid data', function (done) {
+ socketTopics.post({ uid: 0 }, null, function (err) {
+ assert.equal(err.message, '[[error:invalid-data]]');
+ done();
+ });
+ });
+
it('should create a new topic with proper parameters', function (done) {
topics.post({ uid: topic.userId, title: topic.title, content: topic.content, cid: topic.categoryId }, function (err, result) {
- assert.equal(err, null, 'was created with error');
- assert.ok(result);
+ assert.ifError(err);
+ assert(result);
+ topic.tid = result.topicData.tid;
+ done();
+ });
+ });
+
+ it('should get post count', function (done) {
+ socketTopics.postcount({ uid: adminUid }, topic.tid, function (err, count) {
+ assert.ifError(err);
+ assert.equal(count, 1);
+ done();
+ });
+ });
+ it('should load topic', function (done) {
+ socketTopics.getTopic({ uid: adminUid }, topic.tid, function (err, data) {
+ assert.ifError(err);
+ assert.equal(data.tid, topic.tid);
done();
});
});
@@ -246,7 +270,7 @@ describe('Topic\'s', function () {
var newTopic;
var followerUid;
var moveCid;
- var socketTopics = require('../src/socket.io/topics');
+
before(function (done) {
async.waterfall([
function (next) {
@@ -589,8 +613,7 @@ describe('Topic\'s', function () {
assert.ok(result);
replies.push(result);
next();
- }
- );
+ });
}
before(function (done) {
@@ -619,7 +642,7 @@ describe('Topic\'s', function () {
function (next) { postReply(next); },
function (next) {
topicPids = replies.map(function (reply) { return reply.pid; });
- topics.setUserBookmark(newTopic.tid, topic.userId, originalBookmark, next);
+ socketTopics.bookmark({ uid: topic.userId }, { tid: newTopic.tid, index: originalBookmark }, next);
}],
done);
});
@@ -629,15 +652,28 @@ describe('Topic\'s', function () {
done();
});
+ it('should fail with invalid data', function (done) {
+ socketTopics.createTopicFromPosts({ uid: 0 }, null, function (err) {
+ assert.equal(err.message, '[[error:not-logged-in]]');
+ done();
+ });
+ });
+
+ it('should fail with invalid data', function (done) {
+ socketTopics.createTopicFromPosts({ uid: 1 }, null, function (err) {
+ assert.equal(err.message, '[[error:invalid-data]]');
+ done();
+ });
+ });
+
it('should not update the user\'s bookmark', function (done) {
async.waterfall([
function (next) {
- topics.createTopicFromPosts(
- topic.userId,
- 'Fork test, no bookmark update',
- topicPids.slice(-2),
- newTopic.tid,
- next);
+ socketTopics.createTopicFromPosts({ uid: topic.userId }, {
+ title: 'Fork test, no bookmark update',
+ pids: topicPids.slice(-2),
+ fromTid: newTopic.tid,
+ }, next);
},
function (forkedTopicData, next) {
topics.getUserBookmark(newTopic.tid, topic.userId, next);
@@ -1388,6 +1424,13 @@ describe('Topic\'s', function () {
});
});
+ it('should error if not logged in', function (done) {
+ socketTopics.changeWatching({ uid: 0 }, { tid: tid, type: 'ignore' }, function (err) {
+ assert.equal(err.message, '[[error:not-logged-in]]');
+ done();
+ });
+ });
+
it('should filter ignoring uids', function (done) {
socketTopics.changeWatching({ uid: followerUid }, { tid: tid, type: 'ignore' }, function (err) {
assert.ifError(err);
@@ -1418,7 +1461,7 @@ describe('Topic\'s', function () {
topics.toggleFollow(tid, followerUid, function (err, isFollowing) {
assert.ifError(err);
assert(isFollowing);
- topics.isFollowing([tid], followerUid, function (err, isFollowing) {
+ socketTopics.isFollowed({ uid: followerUid }, tid, function (err, isFollowing) {
assert.ifError(err);
assert(isFollowing);
done();
@@ -1427,6 +1470,44 @@ describe('Topic\'s', function () {
});
});
+ describe('topics search', function () {
+ it('should error with invalid data', function (done) {
+ socketTopics.search({ uid: adminUid }, null, function (err) {
+ assert.equal(err.message, '[[error:invalid-data]]');
+ done();
+ });
+ });
+
+ it('should error if no search plugin', function (done) {
+ socketTopics.search({ uid: adminUid }, { tid: topic.tid, term: 'test' }, function (err) {
+ assert.equal(err.message, '[[error:no-plugins-available]]');
+ done();
+ });
+ });
+
+ it('should return results', function (done) {
+ var plugins = require('../src/plugins');
+ plugins.registerHook('myTestPlugin', {
+ hook: 'filter:topic.search',
+ method: function (data, callback) {
+ callback(null, [1, 2, 3]);
+ },
+ });
+ socketTopics.search({ uid: adminUid }, { tid: topic.tid, term: 'test' }, function (err, results) {
+ assert.ifError(err);
+ assert.deepEqual(results, [1, 2, 3]);
+ done();
+ });
+ });
+ });
+
+ it('should check if user is moderator', function (done) {
+ socketTopics.isModerator({ uid: adminUid }, topic.tid, function (err, isModerator) {
+ assert.ifError(err);
+ assert(!isModerator);
+ done();
+ });
+ });
after(function (done) {
db.emptydb(done);