mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-11-03 20:45:58 +01:00 
			
		
		
		
	more topic tests
This commit is contained in:
		@@ -1,12 +1,14 @@
 | 
				
			|||||||
'use strict';
 | 
					'use strict';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var async = require('async');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var topics = require('../topics');
 | 
					var topics = require('../topics');
 | 
				
			||||||
var websockets = require('./index');
 | 
					var websockets = require('./index');
 | 
				
			||||||
var user = require('../user');
 | 
					var user = require('../user');
 | 
				
			||||||
var apiController = require('../controllers/api');
 | 
					var apiController = require('../controllers/api');
 | 
				
			||||||
var socketHelpers = require('./helpers');
 | 
					var socketHelpers = require('./helpers');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var SocketTopics = {};
 | 
					var SocketTopics = module.exports;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require('./topics/unread')(SocketTopics);
 | 
					require('./topics/unread')(SocketTopics);
 | 
				
			||||||
require('./topics/move')(SocketTopics);
 | 
					require('./topics/move')(SocketTopics);
 | 
				
			||||||
@@ -23,18 +25,19 @@ SocketTopics.post = function (socket, data, callback) {
 | 
				
			|||||||
	data.req = websockets.reqFromSocket(socket);
 | 
						data.req = websockets.reqFromSocket(socket);
 | 
				
			||||||
	data.timestamp = Date.now();
 | 
						data.timestamp = Date.now();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	topics.post(data, function (err, result) {
 | 
						async.waterfall([
 | 
				
			||||||
		if (err) {
 | 
							function (next) {
 | 
				
			||||||
			return callback(err);
 | 
								topics.post(data, next);
 | 
				
			||||||
		}
 | 
							},
 | 
				
			||||||
 | 
							function (result, next) {
 | 
				
			||||||
		callback(null, result.topicData);
 | 
								next(null, result.topicData);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			socket.emit('event:new_post', { posts: [result.postData] });
 | 
								socket.emit('event:new_post', { posts: [result.postData] });
 | 
				
			||||||
			socket.emit('event:new_topic', result.topicData);
 | 
								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) {
 | 
					SocketTopics.postcount = function (socket, tid, callback) {
 | 
				
			||||||
@@ -61,7 +64,7 @@ SocketTopics.createTopicFromPosts = function (socket, data, callback) {
 | 
				
			|||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
SocketTopics.changeWatching = 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]]'));
 | 
							return callback(new Error('[[error:invalid-data]]'));
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	var commands = ['follow', 'unfollow', 'ignore'];
 | 
						var commands = ['follow', 'unfollow', 'ignore'];
 | 
				
			||||||
@@ -90,20 +93,23 @@ SocketTopics.isFollowed = function (socket, tid, callback) {
 | 
				
			|||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
SocketTopics.search = function (socket, data, callback) {
 | 
					SocketTopics.search = function (socket, data, callback) {
 | 
				
			||||||
 | 
						if (!data) {
 | 
				
			||||||
 | 
							return callback(new Error('[[error:invalid-data]]'));
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	topics.search(data.tid, data.term, callback);
 | 
						topics.search(data.tid, data.term, callback);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
SocketTopics.isModerator = function (socket, tid, callback) {
 | 
					SocketTopics.isModerator = function (socket, tid, callback) {
 | 
				
			||||||
	topics.getTopicField(tid, 'cid', function (err, cid) {
 | 
						async.waterfall([
 | 
				
			||||||
		if (err) {
 | 
							function (next) {
 | 
				
			||||||
			return callback(err);
 | 
								topics.getTopicField(tid, 'cid', next);
 | 
				
			||||||
		}
 | 
							},
 | 
				
			||||||
		user.isModerator(socket.uid, cid, callback);
 | 
							function (cid, next) {
 | 
				
			||||||
	});
 | 
								user.isModerator(socket.uid, cid, next);
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						], callback);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
SocketTopics.getTopic = function (socket, tid, callback) {
 | 
					SocketTopics.getTopic = function (socket, tid, callback) {
 | 
				
			||||||
	apiController.getTopicData(tid, socket.uid, callback);
 | 
						apiController.getTopicData(tid, socket.uid, callback);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					 | 
				
			||||||
module.exports = SocketTopics;
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
@@ -317,7 +317,7 @@ var social = require('./social');
 | 
				
			|||||||
				term: term,
 | 
									term: term,
 | 
				
			||||||
			}, callback);
 | 
								}, callback);
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			callback(new Error('no-plugins-available'), []);
 | 
								callback(new Error('[[error:no-plugins-available]]'), []);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
}(exports));
 | 
					}(exports));
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										107
									
								
								test/topics.js
									
									
									
									
									
								
							
							
						
						
									
										107
									
								
								test/topics.js
									
									
									
									
									
								
							@@ -12,6 +12,7 @@ var User = require('../src/user');
 | 
				
			|||||||
var groups = require('../src/groups');
 | 
					var groups = require('../src/groups');
 | 
				
			||||||
var helpers = require('./helpers');
 | 
					var helpers = require('./helpers');
 | 
				
			||||||
var socketPosts = require('../src/socket.io/posts');
 | 
					var socketPosts = require('../src/socket.io/posts');
 | 
				
			||||||
 | 
					var socketTopics = require('../src/socket.io/topics');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
describe('Topic\'s', function () {
 | 
					describe('Topic\'s', function () {
 | 
				
			||||||
	var topic;
 | 
						var topic;
 | 
				
			||||||
@@ -49,11 +50,34 @@ describe('Topic\'s', function () {
 | 
				
			|||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	describe('.post', 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) {
 | 
							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) {
 | 
								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.ifError(err);
 | 
				
			||||||
				assert.ok(result);
 | 
									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();
 | 
									done();
 | 
				
			||||||
			});
 | 
								});
 | 
				
			||||||
		});
 | 
							});
 | 
				
			||||||
@@ -246,7 +270,7 @@ describe('Topic\'s', function () {
 | 
				
			|||||||
		var newTopic;
 | 
							var newTopic;
 | 
				
			||||||
		var followerUid;
 | 
							var followerUid;
 | 
				
			||||||
		var moveCid;
 | 
							var moveCid;
 | 
				
			||||||
		var socketTopics = require('../src/socket.io/topics');
 | 
					
 | 
				
			||||||
		before(function (done) {
 | 
							before(function (done) {
 | 
				
			||||||
			async.waterfall([
 | 
								async.waterfall([
 | 
				
			||||||
				function (next) {
 | 
									function (next) {
 | 
				
			||||||
@@ -589,8 +613,7 @@ describe('Topic\'s', function () {
 | 
				
			|||||||
				assert.ok(result);
 | 
									assert.ok(result);
 | 
				
			||||||
				replies.push(result);
 | 
									replies.push(result);
 | 
				
			||||||
				next();
 | 
									next();
 | 
				
			||||||
			}
 | 
								});
 | 
				
			||||||
			);
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		before(function (done) {
 | 
							before(function (done) {
 | 
				
			||||||
@@ -619,7 +642,7 @@ describe('Topic\'s', function () {
 | 
				
			|||||||
				function (next) { postReply(next); },
 | 
									function (next) { postReply(next); },
 | 
				
			||||||
				function (next) {
 | 
									function (next) {
 | 
				
			||||||
					topicPids = replies.map(function (reply) { return reply.pid; });
 | 
										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);
 | 
									done);
 | 
				
			||||||
		});
 | 
							});
 | 
				
			||||||
@@ -629,15 +652,28 @@ describe('Topic\'s', function () {
 | 
				
			|||||||
			done();
 | 
								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) {
 | 
							it('should not update the user\'s bookmark', function (done) {
 | 
				
			||||||
			async.waterfall([
 | 
								async.waterfall([
 | 
				
			||||||
				function (next) {
 | 
									function (next) {
 | 
				
			||||||
					topics.createTopicFromPosts(
 | 
										socketTopics.createTopicFromPosts({ uid: topic.userId }, {
 | 
				
			||||||
						topic.userId,
 | 
											title: 'Fork test, no bookmark update',
 | 
				
			||||||
						'Fork test, no bookmark update',
 | 
											pids: topicPids.slice(-2),
 | 
				
			||||||
						topicPids.slice(-2),
 | 
											fromTid: newTopic.tid,
 | 
				
			||||||
						newTopic.tid,
 | 
										}, next);
 | 
				
			||||||
						next);
 | 
					 | 
				
			||||||
				},
 | 
									},
 | 
				
			||||||
				function (forkedTopicData, next) {
 | 
									function (forkedTopicData, next) {
 | 
				
			||||||
					topics.getUserBookmark(newTopic.tid, topic.userId, 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) {
 | 
							it('should filter ignoring uids', function (done) {
 | 
				
			||||||
			socketTopics.changeWatching({ uid: followerUid }, { tid: tid, type: 'ignore' }, function (err) {
 | 
								socketTopics.changeWatching({ uid: followerUid }, { tid: tid, type: 'ignore' }, function (err) {
 | 
				
			||||||
				assert.ifError(err);
 | 
									assert.ifError(err);
 | 
				
			||||||
@@ -1418,7 +1461,7 @@ describe('Topic\'s', function () {
 | 
				
			|||||||
			topics.toggleFollow(tid, followerUid, function (err, isFollowing) {
 | 
								topics.toggleFollow(tid, followerUid, function (err, isFollowing) {
 | 
				
			||||||
				assert.ifError(err);
 | 
									assert.ifError(err);
 | 
				
			||||||
				assert(isFollowing);
 | 
									assert(isFollowing);
 | 
				
			||||||
				topics.isFollowing([tid], followerUid, function (err, isFollowing) {
 | 
									socketTopics.isFollowed({ uid: followerUid }, tid, function (err, isFollowing) {
 | 
				
			||||||
					assert.ifError(err);
 | 
										assert.ifError(err);
 | 
				
			||||||
					assert(isFollowing);
 | 
										assert(isFollowing);
 | 
				
			||||||
					done();
 | 
										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) {
 | 
						after(function (done) {
 | 
				
			||||||
		db.emptydb(done);
 | 
							db.emptydb(done);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user