mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 20:16:04 +01:00
post parse test
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
var async = require('async');
|
||||||
var nconf = require('nconf');
|
var nconf = require('nconf');
|
||||||
var url = require('url');
|
var url = require('url');
|
||||||
var winston = require('winston');
|
var winston = require('winston');
|
||||||
@@ -14,31 +15,26 @@ var urlRegex = /href="([^"]+)"/g;
|
|||||||
|
|
||||||
module.exports = function (Posts) {
|
module.exports = function (Posts) {
|
||||||
Posts.parsePost = function (postData, callback) {
|
Posts.parsePost = function (postData, callback) {
|
||||||
postData.content = postData.content || '';
|
postData.content = String(postData.content || '');
|
||||||
|
|
||||||
if (postData.pid && cache.has(String(postData.pid))) {
|
if (postData.pid && cache.has(String(postData.pid))) {
|
||||||
postData.content = cache.get(String(postData.pid));
|
postData.content = cache.get(String(postData.pid));
|
||||||
return callback(null, postData);
|
return callback(null, postData);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Casting post content into a string, just in case
|
async.waterfall([
|
||||||
if (typeof postData.content !== 'string') {
|
function (next) {
|
||||||
postData.content = postData.content.toString();
|
plugins.fireHook('filter:parse.post', { postData: postData }, next);
|
||||||
}
|
},
|
||||||
|
function (data, next) {
|
||||||
|
data.postData.content = translator.escape(data.postData.content);
|
||||||
|
|
||||||
plugins.fireHook('filter:parse.post', { postData: postData }, function (err, data) {
|
if (global.env === 'production' && data.postData.pid) {
|
||||||
if (err) {
|
cache.set(String(data.postData.pid), data.postData.content);
|
||||||
return callback(err);
|
}
|
||||||
}
|
next(null, data.postData);
|
||||||
|
},
|
||||||
data.postData.content = translator.escape(data.postData.content);
|
], callback);
|
||||||
|
|
||||||
if (global.env === 'production' && data.postData.pid) {
|
|
||||||
cache.set(String(data.postData.pid), data.postData.content);
|
|
||||||
}
|
|
||||||
|
|
||||||
callback(null, data.postData);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Posts.parseSignature = function (userData, uid, callback) {
|
Posts.parseSignature = function (userData, uid, callback) {
|
||||||
@@ -51,7 +47,6 @@ module.exports = function (Posts) {
|
|||||||
var parsed;
|
var parsed;
|
||||||
var current = urlRegex.exec(content);
|
var current = urlRegex.exec(content);
|
||||||
var absolute;
|
var absolute;
|
||||||
|
|
||||||
while (current !== null) {
|
while (current !== null) {
|
||||||
if (current[1]) {
|
if (current[1]) {
|
||||||
try {
|
try {
|
||||||
@@ -78,7 +73,7 @@ module.exports = function (Posts) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function sanitizeSignature(signature) {
|
function sanitizeSignature(signature) {
|
||||||
var string = S(signature);
|
var string = S(signature);
|
||||||
var tagsToStrip = [];
|
var tagsToStrip = [];
|
||||||
|
|
||||||
if (parseInt(meta.config['signatures:disableLinks'], 10) === 1) {
|
if (parseInt(meta.config['signatures:disableLinks'], 10) === 1) {
|
||||||
|
|||||||
@@ -743,6 +743,48 @@ describe('Post\'s', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('parse', function () {
|
||||||
|
it('should store post content in cache', function (done) {
|
||||||
|
var oldValue = global.env;
|
||||||
|
global.env = 'production';
|
||||||
|
var postData = {
|
||||||
|
pid: 9999,
|
||||||
|
content: 'some post content',
|
||||||
|
};
|
||||||
|
posts.parsePost(postData, function (err) {
|
||||||
|
assert.ifError(err);
|
||||||
|
posts.parsePost(postData, function (err) {
|
||||||
|
assert.ifError(err);
|
||||||
|
global.env = oldValue;
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should parse signature and remove links and images', function (done) {
|
||||||
|
var meta = require('../src/meta');
|
||||||
|
meta.config['signatures:disableLinks'] = 1;
|
||||||
|
meta.config['signatures:disableImages'] = 1;
|
||||||
|
var userData = {
|
||||||
|
signature: '<img src="boop"/><a href="link">test</a> derp',
|
||||||
|
};
|
||||||
|
|
||||||
|
posts.parseSignature(userData, 1, function (err, data) {
|
||||||
|
assert.ifError(err);
|
||||||
|
assert.equal(data.userData.signature, 'test derp');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should turn relative links in post body to absolute urls', function (done) {
|
||||||
|
var nconf = require('nconf');
|
||||||
|
var content = '<a href="/users">test</a> <a href="youtube.com">youtube</a>';
|
||||||
|
var parsedContent = posts.relativeToAbsolute(content);
|
||||||
|
assert.equal(parsedContent, '<a href="' + nconf.get('url') + '/users">test</a> <a href="//youtube.com">youtube</a>');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('socket methods', function () {
|
describe('socket methods', function () {
|
||||||
var pid;
|
var pid;
|
||||||
before(function (done) {
|
before(function (done) {
|
||||||
@@ -809,7 +851,7 @@ describe('Post\'s', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('shold error with invalid data', function (done) {
|
it('shold error with invalid data', function (done) {
|
||||||
socketPosts.loadMoreBookmarks({ uid: voterUid }, { uid: voterUid, after: null }, function (err, postData) {
|
socketPosts.loadMoreBookmarks({ uid: voterUid }, { uid: voterUid, after: null }, function (err) {
|
||||||
assert.equal(err.message, '[[error:invalid-data]]');
|
assert.equal(err.message, '[[error:invalid-data]]');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user