mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 03:26:04 +01:00
post parse test
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var nconf = require('nconf');
|
||||
var url = require('url');
|
||||
var winston = require('winston');
|
||||
@@ -14,31 +15,26 @@ var urlRegex = /href="([^"]+)"/g;
|
||||
|
||||
module.exports = function (Posts) {
|
||||
Posts.parsePost = function (postData, callback) {
|
||||
postData.content = postData.content || '';
|
||||
postData.content = String(postData.content || '');
|
||||
|
||||
if (postData.pid && cache.has(String(postData.pid))) {
|
||||
postData.content = cache.get(String(postData.pid));
|
||||
return callback(null, postData);
|
||||
}
|
||||
|
||||
// Casting post content into a string, just in case
|
||||
if (typeof postData.content !== 'string') {
|
||||
postData.content = postData.content.toString();
|
||||
}
|
||||
|
||||
plugins.fireHook('filter:parse.post', { postData: postData }, function (err, data) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
plugins.fireHook('filter:parse.post', { postData: postData }, next);
|
||||
},
|
||||
function (data, next) {
|
||||
data.postData.content = translator.escape(data.postData.content);
|
||||
|
||||
if (global.env === 'production' && data.postData.pid) {
|
||||
cache.set(String(data.postData.pid), data.postData.content);
|
||||
}
|
||||
|
||||
callback(null, data.postData);
|
||||
});
|
||||
next(null, data.postData);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
Posts.parseSignature = function (userData, uid, callback) {
|
||||
@@ -51,7 +47,6 @@ module.exports = function (Posts) {
|
||||
var parsed;
|
||||
var current = urlRegex.exec(content);
|
||||
var absolute;
|
||||
|
||||
while (current !== null) {
|
||||
if (current[1]) {
|
||||
try {
|
||||
|
||||
@@ -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 () {
|
||||
var pid;
|
||||
before(function (done) {
|
||||
@@ -809,7 +851,7 @@ describe('Post\'s', function () {
|
||||
});
|
||||
|
||||
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]]');
|
||||
done();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user