vote socket tests

v1.18.x
barisusakli 8 years ago
parent 1658ebbe20
commit d2fa0c3e6a

@ -13,9 +13,13 @@ helpers.postCommand = function (socket, command, eventName, notification, data,
if (!socket.uid) { if (!socket.uid) {
return callback(new Error('[[error:not-logged-in]]')); return callback(new Error('[[error:not-logged-in]]'));
} }
if (!data || !data.pid || !data.room_id) { if (!data || !data.pid || !data.room_id) {
return callback(new Error('[[error:invalid-data]]')); return callback(new Error('[[error:invalid-data]]'));
} }
async.waterfall([
function (next) {
async.parallel({ async.parallel({
exists: function (next) { exists: function (next) {
posts.exists(data.pid, next); posts.exists(data.pid, next);
@ -23,13 +27,15 @@ helpers.postCommand = function (socket, command, eventName, notification, data,
deleted: function (next) { deleted: function (next) {
posts.getPostField(data.pid, 'deleted', next); posts.getPostField(data.pid, 'deleted', next);
} }
}, function (err, results) { }, next);
if (err || !results.exists) { },
return callback(err || new Error('[[error:invalid-pid]]')); function (results, next) {
if (!results.exists) {
return next(new Error('[[error:invalid-pid]]'));
} }
if (parseInt(results.deleted, 10) === 1) { if (parseInt(results.deleted, 10) === 1) {
return callback(new Error('[[error:post-deleted]]')); return next(new Error('[[error:post-deleted]]'));
} }
/* /*
@ -40,24 +46,22 @@ helpers.postCommand = function (socket, command, eventName, notification, data,
filter:post.bookmark filter:post.bookmark
filter:post.unbookmark filter:post.unbookmark
*/ */
plugins.fireHook('filter:post.' + command, {data: data, uid: socket.uid}, function (err, filteredData) { plugins.fireHook('filter:post.' + command, {data: data, uid: socket.uid}, next);
if (err) { },
return callback(err); function (filteredData, next) {
executeCommand(socket, command, eventName, notification, filteredData.data, next);
} }
], callback);
executeCommand(socket, command, eventName, notification, filteredData.data, callback);
});
});
}; };
function executeCommand(socket, command, eventName, notification, data, callback) { function executeCommand(socket, command, eventName, notification, data, callback) {
posts[command](data.pid, socket.uid, function (err, result) { async.waterfall([
if (err) { function (next) {
return callback(err); posts[command](data.pid, socket.uid, next);
} },
function (result, next) {
if (result && eventName) { if (result && eventName) {
socket.emit('posts.' + command, result); websockets.in('uid_' + socket.uid).emit('posts.' + command, result);
websockets.in(data.room_id).emit('event:' + eventName, result); websockets.in(data.room_id).emit('event:' + eventName, result);
} }
@ -66,6 +70,7 @@ function executeCommand(socket, command, eventName, notification, data, callback
} else if (result && command === 'unvote') { } else if (result && command === 'unvote') {
socketHelpers.rescindUpvoteNotification(data.pid, socket.uid); socketHelpers.rescindUpvoteNotification(data.pid, socket.uid);
} }
callback(); next(null, result);
}); }
], callback);
} }

@ -66,9 +66,9 @@ describe('Post\'s', function () {
}); });
describe('voting', function () { describe('voting', function () {
var socketPosts = require('../src/socket.io/posts');
it('should upvote a post', function (done) { it('should upvote a post', function (done) {
posts.upvote(postData.pid, voterUid, function (err, result) { socketPosts.upvote({uid: voterUid}, {pid: postData.pid, room_id: 'topic_1'}, function (err, result) {
assert.ifError(err); assert.ifError(err);
assert.equal(result.post.upvotes, 1); assert.equal(result.post.upvotes, 1);
assert.equal(result.post.downvotes, 0); assert.equal(result.post.downvotes, 0);
@ -83,8 +83,28 @@ describe('Post\'s', function () {
}); });
}); });
it('should get voters', function (done) {
socketPosts.getVoters({uid: globalModUid}, {pid: postData.pid, cid: cid}, function (err, data) {
assert.ifError(err);
assert.equal(data.upvoteCount, 1);
assert.equal(data.downvoteCount, 0);
assert(Array.isArray(data.upvoters));
assert.equal(data.upvoters[0].username, 'upvoter');
done();
});
});
it('should get upvoters', function (done) {
socketPosts.getUpvoters({uid: globalModUid}, [postData.pid], function (err, data) {
assert.ifError(err);
assert.equal(data[0].otherCount, 0);
assert.equal(data[0].usernames, 'upvoter');
done();
});
});
it('should unvote a post', function (done) { it('should unvote a post', function (done) {
posts.unvote(postData.pid, voterUid, function (err, result) { socketPosts.unvote({uid: voterUid}, {pid: postData.pid, room_id: 'topic_1'}, function (err, result) {
assert.ifError(err); assert.ifError(err);
assert.equal(result.post.upvotes, 0); assert.equal(result.post.upvotes, 0);
assert.equal(result.post.downvotes, 0); assert.equal(result.post.downvotes, 0);
@ -100,7 +120,7 @@ describe('Post\'s', function () {
}); });
it('should downvote a post', function (done) { it('should downvote a post', function (done) {
posts.downvote(postData.pid, voterUid, function (err, result) { socketPosts.downvote({uid: voterUid}, {pid: postData.pid, room_id: 'topic_1'}, function (err, result) {
assert.ifError(err); assert.ifError(err);
assert.equal(result.post.upvotes, 0); assert.equal(result.post.upvotes, 0);
assert.equal(result.post.downvotes, 1); assert.equal(result.post.downvotes, 1);

Loading…
Cancel
Save