From 09d0ce4778b3d61246718f1253f5f75fbdfcc74d Mon Sep 17 00:00:00 2001 From: barisusakli Date: Thu, 23 Feb 2017 17:02:54 +0300 Subject: [PATCH] post tests --- src/socket.io/posts.js | 33 +++++----- test/posts.js | 138 +++++++++++++++++++++++++++++++++++++++++ test/topics.js | 7 +++ 3 files changed, 162 insertions(+), 16 deletions(-) diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index fe729a5c11..6b1e5ce71b 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -31,25 +31,26 @@ SocketPosts.reply = function (socket, data, callback) { data.req = websockets.reqFromSocket(socket); data.timestamp = Date.now(); - topics.reply(data, function (err, postData) { - if (err) { - return callback(err); - } - - var result = { - posts: [postData], - 'reputation:disabled': parseInt(meta.config['reputation:disabled'], 10) === 1, - 'downvote:disabled': parseInt(meta.config['downvote:disabled'], 10) === 1, - }; + async.waterfall([ + function (next) { + topics.reply(data, next); + }, + function (postData, next) { + var result = { + posts: [postData], + 'reputation:disabled': parseInt(meta.config['reputation:disabled'], 10) === 1, + 'downvote:disabled': parseInt(meta.config['downvote:disabled'], 10) === 1, + }; - callback(null, postData); + next(null, postData); - websockets.in('uid_' + socket.uid).emit('event:new_post', result); + websockets.in('uid_' + socket.uid).emit('event:new_post', result); - user.updateOnlineUsers(socket.uid); + user.updateOnlineUsers(socket.uid); - socketHelpers.notifyNew(socket.uid, 'newPost', result); - }); + socketHelpers.notifyNew(socket.uid, 'newPost', result); + } + ], callback); }; SocketPosts.getRawPost = function (socket, pid, callback) { @@ -120,7 +121,7 @@ SocketPosts.getPidIndex = function (socket, data, callback) { SocketPosts.getReplies = function (socket, pid, callback) { if (!utils.isNumber(pid)) { - return callback(new Error('[[error:invalid-data]')); + return callback(new Error('[[error:invalid-data]]')); } var postPrivileges; async.waterfall([ diff --git a/test/posts.js b/test/posts.js index 84b5934724..42ebbde345 100644 --- a/test/posts.js +++ b/test/posts.js @@ -755,6 +755,144 @@ describe('Post\'s', function () { }); }); + describe('socket methods', function () { + + var pid; + before(function (done) { + topics.reply({ + uid: voterUid, + tid: topicData.tid, + timestamp: Date.now(), + content: 'raw content' + }, function (err, postData) { + assert.ifError(err); + pid = postData.pid; + privileges.categories.rescind(['read'], cid, 'guests', done); + }); + }); + + var socketPosts = require('../src/socket.io/posts'); + it('should error with invalid data', function (done) { + socketPosts.reply({uid: 0}, null, function (err) { + assert.equal(err.message, '[[error:invalid-data]]'); + done(); + }); + }); + + it('should error with invalid tid', function (done) { + socketPosts.reply({uid: 0}, {tid: 0, content: 'derp'}, function (err) { + assert.equal(err.message, '[[error:invalid-data]]'); + done(); + }); + }); + + it('should fail to get raw post because of privilege', function (done) { + socketPosts.getRawPost({uid: 0}, pid, function (err) { + assert.equal(err.message, '[[error:no-privileges]]'); + done(); + }); + }); + + it('should fail to get raw post because post is deleted', function (done) { + posts.setPostField(pid, 'deleted', 1, function (err) { + assert.ifError(err); + socketPosts.getRawPost({uid: voterUid}, pid, function (err) { + assert.equal(err.message, '[[error:no-post]]'); + done(); + }); + }); + }); + + it('should get raw post content', function (done) { + posts.setPostField(pid, 'deleted', 0, function (err) { + assert.ifError(err); + socketPosts.getRawPost({uid: voterUid}, pid, function (err, postContent) { + assert.ifError(err); + assert.equal(postContent, 'raw content'); + done(); + }); + }); + }); + + it('should get post', function (done) { + socketPosts.getPost({uid: voterUid}, pid, function (err, postData) { + assert.ifError(err); + assert(postData); + done(); + }); + }); + + it('shold error with invalid data', function (done) { + socketPosts.loadMoreBookmarks({uid: voterUid}, {uid: voterUid, after: null}, function (err, postData) { + assert.equal(err.message, '[[error:invalid-data]]'); + done(); + }); + }); + + it('should load more bookmarks', function (done) { + socketPosts.loadMoreBookmarks({uid: voterUid}, {uid: voterUid, after: 0}, function (err, data) { + assert.ifError(err); + assert(data); + done(); + }); + }); + + it('should load more user posts', function (done) { + socketPosts.loadMoreUserPosts({uid: voterUid}, {uid: voterUid, after: 0}, function (err, data) { + assert.ifError(err); + assert(data); + done(); + }); + }); + + it('should load more best posts', function (done) { + socketPosts.loadMoreBestPosts({uid: voterUid}, {uid: voterUid, after: 0}, function (err, data) { + assert.ifError(err); + assert(data); + done(); + }); + }); + + it('should load more up voted posts', function (done) { + socketPosts.loadMoreUpVotedPosts({uid: voterUid}, {uid: voterUid, after: 0}, function (err, data) { + assert.ifError(err); + assert(data); + done(); + }); + }); + + it('should load more down voted posts', function (done) { + socketPosts.loadMoreDownVotedPosts({uid: voterUid}, {uid: voterUid, after: 0}, function (err, data) { + assert.ifError(err); + assert(data); + done(); + }); + }); + + it('should get post category', function (done) { + socketPosts.getCategory({uid: voterUid}, pid, function (err, postCid) { + assert.ifError(err); + assert.equal(cid, postCid); + done(); + }); + }); + + it('should error with invalid data', function (done) { + socketPosts.getPidIndex({uid: voterUid}, null, function (err) { + assert.equal(err.message, '[[error:invalid-data]]'); + done(); + }); + }); + + it('should get pid index', function (done) { + socketPosts.getPidIndex({uid: voterUid}, {pid: pid, tid: topicData.tid, topicPostSort: 'oldest-to-newest'}, function (err, index) { + assert.ifError(err); + assert.equal(index, 2); + done(); + }); + }); + }); + after(function (done) { db.emptydb(done); }); diff --git a/test/topics.js b/test/topics.js index 01af1f5f2e..1ade4de261 100644 --- a/test/topics.js +++ b/test/topics.js @@ -130,6 +130,13 @@ describe('Topic\'s', function () { }); }); + it('should error if pid is not a number', function (done) { + socketPosts.getReplies({uid: 0}, 'abc', function (err) { + assert.equal(err.message, '[[error:invalid-data]]'); + done(); + }); + }); + it('should fail to create new reply with invalid user id', function (done) { topics.reply({uid: null, content: 'test post', tid: newTopic.tid}, function (err) { assert.equal(err.message, '[[error:no-privileges]]');