mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 11:35:55 +01:00
post edit tests
This commit is contained in:
@@ -65,7 +65,7 @@ module.exports = function (Posts) {
|
||||
results = _results;
|
||||
|
||||
postData.cid = results.topic.cid;
|
||||
|
||||
postData.topic = results.topic;
|
||||
plugins.fireHook('action:post.edit', _.clone(postData));
|
||||
|
||||
cache.del(String(postData.pid));
|
||||
|
||||
@@ -200,13 +200,14 @@ var ratelimit = require('../middleware/ratelimit');
|
||||
|
||||
|
||||
Sockets.reqFromSocket = function (socket, payload, event) {
|
||||
var headers = socket.request.headers;
|
||||
var headers = socket.request ? socket.request.headers : {};
|
||||
var encrypted = socket.request ? !!socket.request.connection.encrypted : false;
|
||||
var host = headers.host;
|
||||
var referer = headers.referer || '';
|
||||
var data = ((payload || {}).data || []);
|
||||
|
||||
if (!host) {
|
||||
host = url.parse(referer).host;
|
||||
host = url.parse(referer).host || '';
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -216,8 +217,8 @@ var ratelimit = require('../middleware/ratelimit');
|
||||
body: payload,
|
||||
ip: headers['x-forwarded-for'] || socket.ip,
|
||||
host: host,
|
||||
protocol: socket.request.connection.encrypted ? 'https' : 'http',
|
||||
secure: !!socket.request.connection.encrypted,
|
||||
protocol: encrypted ? 'https' : 'http',
|
||||
secure: encrypted,
|
||||
url: referer,
|
||||
path: referer.substr(referer.indexOf(host) + host.length),
|
||||
headers: headers
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var winston = require('winston');
|
||||
var validator = require('validator');
|
||||
var _ = require('underscore');
|
||||
|
||||
var posts = require('../../posts');
|
||||
var groups = require('../../groups');
|
||||
@@ -33,45 +33,43 @@ module.exports = function (SocketPosts) {
|
||||
|
||||
data.uid = socket.uid;
|
||||
data.req = websockets.reqFromSocket(socket);
|
||||
posts.edit(data, function (err, result) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (result.topic.renamed) {
|
||||
events.log({
|
||||
type: 'topic-rename',
|
||||
uid: socket.uid,
|
||||
ip: socket.ip,
|
||||
oldTitle: validator.escape(String(result.topic.oldTitle)),
|
||||
newTitle: validator.escape(String(result.topic.title))
|
||||
});
|
||||
}
|
||||
|
||||
if (parseInt(result.post.deleted) !== 1) {
|
||||
websockets.in('topic_' + result.topic.tid).emit('event:post_edited', result);
|
||||
return callback(null, result.post);
|
||||
}
|
||||
|
||||
socket.emit('event:post_edited', result);
|
||||
callback(null, result.post);
|
||||
|
||||
async.parallel({
|
||||
admins: async.apply(groups.getMembers, 'administrators', 0, -1),
|
||||
moderators: async.apply(groups.getMembers, 'cid:' + result.topic.cid + ':privileges:mods', 0, -1)
|
||||
}, function (err, results) {
|
||||
if (err) {
|
||||
return winston.error(err);
|
||||
var editResult;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
posts.edit(data, next);
|
||||
},
|
||||
function (result, next) {
|
||||
editResult = result;
|
||||
if (result.topic.renamed) {
|
||||
events.log({
|
||||
type: 'topic-rename',
|
||||
uid: socket.uid,
|
||||
ip: socket.ip,
|
||||
oldTitle: validator.escape(String(result.topic.oldTitle)),
|
||||
newTitle: validator.escape(String(result.topic.title))
|
||||
});
|
||||
}
|
||||
|
||||
var uids = results.admins.concat(results.moderators).filter(function (uid, index, array) {
|
||||
return uid && array.indexOf(uid) === index;
|
||||
});
|
||||
if (parseInt(result.post.deleted) !== 1) {
|
||||
websockets.in('topic_' + result.topic.tid).emit('event:post_edited', result);
|
||||
return callback(null, result.post);
|
||||
}
|
||||
|
||||
groups.getMembersOfGroups([
|
||||
'administrators',
|
||||
'Global Moderators',
|
||||
'cid:' + result.topic.cid + ':privileges:mods',
|
||||
'cid:' + result.topic.cid + ':privileges:groups:moderate'
|
||||
], next);
|
||||
},
|
||||
function (results, next) {
|
||||
var uids = _.unique(_.flatten(results).concat(socket.uid.toString()));
|
||||
uids.forEach(function (uid) {
|
||||
websockets.in('uid_' + uid).emit('event:post_edited', result);
|
||||
websockets.in('uid_' + uid).emit('event:post_edited', editResult);
|
||||
});
|
||||
});
|
||||
});
|
||||
next(null, editResult.post);
|
||||
}
|
||||
], callback);
|
||||
};
|
||||
};
|
||||
132
test/posts.js
132
test/posts.js
@@ -11,6 +11,7 @@ var categories = require('../src/categories');
|
||||
var privileges = require('../src/privileges');
|
||||
var user = require('../src/user');
|
||||
|
||||
|
||||
describe('Post\'s', function () {
|
||||
var voterUid;
|
||||
var voteeUid;
|
||||
@@ -184,6 +185,137 @@ describe('Post\'s', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('edit', function () {
|
||||
var pid;
|
||||
var replyPid;
|
||||
var tid;
|
||||
var socketPosts = require('../src/socket.io/posts');
|
||||
var meta = require('../src/meta');
|
||||
before(function (done) {
|
||||
topics.post({
|
||||
uid: voterUid,
|
||||
cid: cid,
|
||||
title: 'topic to edit',
|
||||
content: 'A post to edit'
|
||||
}, function (err, data) {
|
||||
assert.ifError(err);
|
||||
pid = data.postData.pid;
|
||||
tid = data.topicData.tid;
|
||||
topics.reply({
|
||||
uid: voterUid,
|
||||
tid: tid,
|
||||
timestamp: Date.now(),
|
||||
content: 'A reply to edit'
|
||||
}, function (err, data) {
|
||||
assert.ifError(err);
|
||||
replyPid = data.pid;
|
||||
privileges.categories.give(['posts:edit'], cid, 'registered-users', done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should error if user is not logged in', function (done) {
|
||||
socketPosts.edit({uid: 0}, {}, function (err) {
|
||||
assert.equal(err.message, '[[error:not-logged-in]]');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should error if data is invalid or missing', function (done) {
|
||||
socketPosts.edit({uid: voterUid}, {}, function (err) {
|
||||
assert.equal(err.message, '[[error:invalid-data]]');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should error if title is too short', function (done) {
|
||||
socketPosts.edit({uid: voterUid}, {pid: pid, content: 'edited post content', title: 'a'}, function (err) {
|
||||
assert.equal(err.message, '[[error:title-too-short, ' + meta.config.minimumTitleLength + ']]');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should error if title is too long', function (done) {
|
||||
var longTitle = new Array(parseInt(meta.config.maximumTitleLength, 10) + 2).join('a');
|
||||
socketPosts.edit({uid: voterUid}, {pid: pid, content: 'edited post content', title: longTitle}, function (err) {
|
||||
assert.equal(err.message, '[[error:title-too-long, ' + meta.config.maximumTitleLength + ']]');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should error with too few tags', function (done) {
|
||||
var oldValue = meta.config.minimumTagsPerTopic;
|
||||
meta.config.minimumTagsPerTopic = 1;
|
||||
socketPosts.edit({uid: voterUid}, {pid: pid, content: 'edited post content', tags: []}, function (err) {
|
||||
assert.equal(err.message, '[[error:not-enough-tags, ' + meta.config.minimumTagsPerTopic + ']]');
|
||||
meta.config.minimumTagsPerTopic = oldValue;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should error with too many tags', function (done) {
|
||||
var tags = [];
|
||||
for(var i = 0; i < meta.config.maximumTagsPerTopic + 1; ++i) {
|
||||
tags.push('tag' + i);
|
||||
}
|
||||
socketPosts.edit({uid: voterUid}, {pid: pid, content: 'edited post content', tags: tags}, function (err) {
|
||||
assert.equal(err.message, '[[error:too-many-tags, ' + meta.config.maximumTagsPerTopic + ']]');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should error if content is too short', function (done) {
|
||||
socketPosts.edit({uid: voterUid}, {pid: pid, content: 'e'}, function (err) {
|
||||
assert.equal(err.message, '[[error:content-too-short, ' + meta.config.minimumPostLength + ']]');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should error if content is too long', function (done) {
|
||||
var longContent = new Array(parseInt(meta.config.maximumPostLength, 10) + 2).join('a');
|
||||
socketPosts.edit({uid: voterUid}, {pid: pid, content: longContent}, function (err) {
|
||||
assert.equal(err.message, '[[error:content-too-long, ' + meta.config.maximumPostLength + ']]');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should edit post', function (done) {
|
||||
socketPosts.edit({uid: voterUid}, {pid: pid, content: 'edited post content', title: 'edited title', tags: ['edited']}, function (err, data) {
|
||||
assert.ifError(err);
|
||||
assert.equal(data.content, 'edited post content');
|
||||
assert.equal(data.editor, voterUid);
|
||||
assert.equal(data.topic.title, 'edited title');
|
||||
assert.equal(data.topic.tags[0].value, 'edited');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should edit a deleted post', function (done) {
|
||||
socketPosts.delete({uid: voterUid}, {pid: pid, tid: tid}, function (err) {
|
||||
assert.ifError(err);
|
||||
socketPosts.edit({uid: voterUid}, {pid: pid, content: 'edited deleted content', title: 'edited deleted title', tags: ['deleted']}, function (err, data) {
|
||||
assert.ifError(err);
|
||||
assert.equal(data.content, 'edited deleted content');
|
||||
assert.equal(data.editor, voterUid);
|
||||
assert.equal(data.topic.title, 'edited deleted title');
|
||||
assert.equal(data.topic.tags[0].value, 'deleted');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should edit a reply post', function (done) {
|
||||
socketPosts.edit({uid: voterUid}, {pid: replyPid, content: 'edited reply'}, function (err, data) {
|
||||
assert.ifError(err);
|
||||
assert.equal(data.content, 'edited reply');
|
||||
assert.equal(data.editor, voterUid);
|
||||
assert.equal(data.topic.isMainPost, false);
|
||||
assert.equal(data.topic.renamed, false);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('flagging a post', function () {
|
||||
it('should flag a post', function (done) {
|
||||
flagPost(function (err) {
|
||||
|
||||
Reference in New Issue
Block a user