mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-02-28 09:31:17 +01:00
vote socket tests
This commit is contained in:
@@ -13,59 +13,64 @@ helpers.postCommand = function (socket, command, eventName, notification, data,
|
||||
if (!socket.uid) {
|
||||
return callback(new Error('[[error:not-logged-in]]'));
|
||||
}
|
||||
|
||||
if (!data || !data.pid || !data.room_id) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
async.parallel({
|
||||
exists: function (next) {
|
||||
posts.exists(data.pid, next);
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
exists: function (next) {
|
||||
posts.exists(data.pid, next);
|
||||
},
|
||||
deleted: function (next) {
|
||||
posts.getPostField(data.pid, 'deleted', next);
|
||||
}
|
||||
}, next);
|
||||
},
|
||||
deleted: function (next) {
|
||||
posts.getPostField(data.pid, 'deleted', next);
|
||||
}
|
||||
}, function (err, results) {
|
||||
if (err || !results.exists) {
|
||||
return callback(err || new Error('[[error:invalid-pid]]'));
|
||||
}
|
||||
|
||||
if (parseInt(results.deleted, 10) === 1) {
|
||||
return callback(new Error('[[error:post-deleted]]'));
|
||||
}
|
||||
|
||||
/*
|
||||
hooks:
|
||||
filter:post.upvote
|
||||
filter:post.downvote
|
||||
filter:post.unvote
|
||||
filter:post.bookmark
|
||||
filter:post.unbookmark
|
||||
*/
|
||||
plugins.fireHook('filter:post.' + command, {data: data, uid: socket.uid}, function (err, filteredData) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
function (results, next) {
|
||||
if (!results.exists) {
|
||||
return next(new Error('[[error:invalid-pid]]'));
|
||||
}
|
||||
|
||||
executeCommand(socket, command, eventName, notification, filteredData.data, callback);
|
||||
});
|
||||
});
|
||||
if (parseInt(results.deleted, 10) === 1) {
|
||||
return next(new Error('[[error:post-deleted]]'));
|
||||
}
|
||||
|
||||
/*
|
||||
hooks:
|
||||
filter:post.upvote
|
||||
filter:post.downvote
|
||||
filter:post.unvote
|
||||
filter:post.bookmark
|
||||
filter:post.unbookmark
|
||||
*/
|
||||
plugins.fireHook('filter:post.' + command, {data: data, uid: socket.uid}, next);
|
||||
},
|
||||
function (filteredData, next) {
|
||||
executeCommand(socket, command, eventName, notification, filteredData.data, next);
|
||||
}
|
||||
], callback);
|
||||
};
|
||||
|
||||
function executeCommand(socket, command, eventName, notification, data, callback) {
|
||||
posts[command](data.pid, socket.uid, function (err, result) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
posts[command](data.pid, socket.uid, next);
|
||||
},
|
||||
function (result, next) {
|
||||
if (result && eventName) {
|
||||
websockets.in('uid_' + socket.uid).emit('posts.' + command, result);
|
||||
websockets.in(data.room_id).emit('event:' + eventName, result);
|
||||
}
|
||||
|
||||
if (result && eventName) {
|
||||
socket.emit('posts.' + command, result);
|
||||
websockets.in(data.room_id).emit('event:' + eventName, result);
|
||||
if (result && notification) {
|
||||
socketHelpers.sendNotificationToPostOwner(data.pid, socket.uid, command, notification);
|
||||
} else if (result && command === 'unvote') {
|
||||
socketHelpers.rescindUpvoteNotification(data.pid, socket.uid);
|
||||
}
|
||||
next(null, result);
|
||||
}
|
||||
|
||||
if (result && notification) {
|
||||
socketHelpers.sendNotificationToPostOwner(data.pid, socket.uid, command, notification);
|
||||
} else if (result && command === 'unvote') {
|
||||
socketHelpers.rescindUpvoteNotification(data.pid, socket.uid);
|
||||
}
|
||||
callback();
|
||||
});
|
||||
], callback);
|
||||
}
|
||||
@@ -66,9 +66,9 @@ describe('Post\'s', function () {
|
||||
});
|
||||
|
||||
describe('voting', function () {
|
||||
|
||||
var socketPosts = require('../src/socket.io/posts');
|
||||
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.equal(result.post.upvotes, 1);
|
||||
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) {
|
||||
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.equal(result.post.upvotes, 0);
|
||||
assert.equal(result.post.downvotes, 0);
|
||||
@@ -100,7 +120,7 @@ describe('Post\'s', function () {
|
||||
});
|
||||
|
||||
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.equal(result.post.upvotes, 0);
|
||||
assert.equal(result.post.downvotes, 1);
|
||||
|
||||
Reference in New Issue
Block a user