Files
NodeBB/test/topics.js

481 lines
13 KiB
JavaScript
Raw Normal View History

2014-03-06 14:51:43 -05:00
'use strict';
2015-01-03 20:07:09 -05:00
/*global require, before, beforeEach, after*/
2013-11-15 16:16:50 -05:00
var async = require('async');
2016-02-18 18:32:08 +02:00
var assert = require('assert');
var validator = require('validator');
var nconf = require('nconf');
2016-02-18 18:32:08 +02:00
var db = require('./mocks/databasemock');
var topics = require('../src/topics');
var categories = require('../src/categories');
var User = require('../src/user');
var groups = require('../src/groups');
2013-11-15 16:16:50 -05:00
describe('Topic\'s', function () {
var topic;
var categoryObj;
2014-03-06 14:51:43 -05:00
before(function (done) {
var userData = {
username: 'John Smith',
password: 'swordfish',
email: 'john@example.com',
callback: undefined
};
User.create({username: userData.username, password: userData.password, email: userData.email}, function (err, uid) {
2016-08-16 19:46:59 +02:00
if (err) {
return done(err);
}
categories.create({
name: 'Test Category',
description: 'Test category created by testing script',
icon: 'fa-check',
blockclass: 'category-blue',
order: '5'
}, function (err, category) {
2016-08-16 19:46:59 +02:00
if (err) {
return done(err);
}
categoryObj = category;
topic = {
userId: uid,
categoryId: categoryObj.cid,
title: 'Test Topic Title',
content: 'The content of test topic'
};
done();
});
2014-03-06 14:51:43 -05:00
});
2014-11-11 19:47:56 -05:00
});
2013-11-15 16:16:50 -05:00
describe('.post', function () {
2013-11-15 16:16:50 -05:00
it('should create a new topic with proper parameters', function (done) {
topics.post({uid: topic.userId, title: topic.title, content: topic.content, cid: topic.categoryId}, function (err, result) {
2013-11-15 16:16:50 -05:00
assert.equal(err, null, 'was created with error');
assert.ok(result);
done();
});
});
it('should fail to create new topic with invalid user id', function (done) {
topics.post({uid: null, title: topic.title, content: topic.content, cid: topic.categoryId}, function (err) {
2014-11-11 23:03:20 -05:00
assert.equal(err.message, '[[error:no-privileges]]');
2013-11-15 16:16:50 -05:00
done();
});
});
2014-11-11 23:12:37 -05:00
it('should fail to create new topic with empty title', function (done) {
topics.post({uid: topic.userId, title: '', content: topic.content, cid: topic.categoryId}, function (err) {
2014-11-12 01:19:40 -05:00
assert.ok(err);
2014-11-11 23:12:37 -05:00
done();
});
});
it('should fail to create new topic with empty content', function (done) {
topics.post({uid: topic.userId, title: topic.title, content: '', cid: topic.categoryId}, function (err) {
2014-11-12 01:19:40 -05:00
assert.ok(err);
2014-11-11 23:12:37 -05:00
done();
});
});
it('should fail to create new topic with non-existant category id', function (done) {
topics.post({uid: topic.userId, title: topic.title, content: topic.content, cid: 99}, function (err) {
2014-11-12 01:22:50 -05:00
assert.equal(err.message, '[[error:no-category]]', 'received no error');
2014-11-11 23:12:37 -05:00
done();
});
});
2013-11-15 16:16:50 -05:00
});
describe('.reply', function () {
2014-11-12 14:34:02 -05:00
var newTopic;
var newPost;
before(function (done) {
topics.post({uid: topic.userId, title: topic.title, content: topic.content, cid: topic.categoryId}, function (err, result) {
2016-08-16 19:46:59 +02:00
if (err) {
return done(err);
}
2014-11-12 14:34:02 -05:00
newTopic = result.topicData;
newPost = result.postData;
done();
});
});
it('should create a new reply with proper parameters', function (done) {
topics.reply({uid: topic.userId, content: 'test post', tid: newTopic.tid}, function (err, result) {
2014-11-12 14:34:02 -05:00
assert.equal(err, null, 'was created with error');
assert.ok(result);
done();
});
});
it('should fail to create new reply with invalid user id', function (done) {
topics.reply({uid: null, content: 'test post', tid: newTopic.tid}, function (err) {
assert.equal(err.message, '[[error:no-privileges]]');
done();
});
});
it('should fail to create new reply with empty content', function (done) {
topics.reply({uid: topic.userId, content: '', tid: newTopic.tid}, function (err) {
assert.ok(err);
done();
});
});
it('should fail to create new reply with invalid topic id', function (done) {
topics.reply({uid: null, content: 'test post', tid: 99}, function (err) {
assert.equal(err.message, '[[error:no-topic]]');
done();
});
});
2014-11-12 14:34:02 -05:00
});
describe('Get methods', function () {
var newTopic;
var newPost;
before(function (done) {
topics.post({uid: topic.userId, title: topic.title, content: topic.content, cid: topic.categoryId}, function (err, result) {
2016-08-16 19:46:59 +02:00
if (err) {
return done(err);
}
newTopic = result.topicData;
newPost = result.postData;
done();
2013-11-15 16:16:50 -05:00
});
});
describe('.getTopicData', function () {
it('should not receive errors', function (done) {
2014-03-06 14:51:43 -05:00
topics.getTopicData(newTopic.tid, done);
});
});
2016-08-13 01:20:19 +03:00
describe('.getTopicWithPosts', function () {
it('should get a topic with posts and other data', function (done) {
topics.getTopicData(newTopic.tid, function (err, topicData) {
2016-08-13 01:20:19 +03:00
if (err) {
return done(err);
}
topics.getTopicWithPosts(topicData, 'tid:' + newTopic.tid + ':posts', topic.userId, 0, -1, false, function (err, data) {
2016-08-13 01:20:19 +03:00
if (err) {
return done(err);
}
assert(data);
assert.equal(data.category.cid, topic.categoryId);
assert.equal(data.unreplied, true);
assert.equal(data.deleted, false);
assert.equal(data.locked, false);
assert.equal(data.pinned, false);
done();
});
});
});
});
2013-11-15 16:16:50 -05:00
});
describe('Title escaping', function () {
2016-02-18 18:32:08 +02:00
it('should properly escape topic title', function (done) {
2016-02-18 18:32:08 +02:00
var title = '"<script>alert(\'ok1\');</script> new topic test';
var titleEscaped = validator.escape(title);
topics.post({uid: topic.userId, title: title, content: topic.content, cid: topic.categoryId}, function (err, result) {
2016-02-18 18:32:08 +02:00
assert.ifError(err);
topics.getTopicData(result.topicData.tid, function (err, topicData) {
2016-02-18 18:32:08 +02:00
assert.ifError(err);
2016-02-18 20:26:09 +02:00
assert.strictEqual(topicData.titleRaw, title);
assert.strictEqual(topicData.title, titleEscaped);
2016-02-18 18:46:51 +02:00
done();
2016-02-18 18:32:08 +02:00
});
});
});
});
describe('delete/restore/purge', function () {
2016-02-25 18:04:32 +02:00
var newTopic;
2016-08-17 12:12:07 +03:00
var followerUid;
before(function (done) {
2016-08-17 12:12:07 +03:00
async.waterfall([
function (next) {
topics.post({uid: topic.userId, title: topic.title, content: topic.content, cid: topic.categoryId}, function (err, result) {
2016-08-17 12:12:07 +03:00
assert.ifError(err);
newTopic = result.topicData;
next();
});
},
function (next) {
2016-08-17 12:12:07 +03:00
User.create({username: 'topicFollower', password: '123456'}, next);
},
function (_uid, next) {
2016-08-17 12:12:07 +03:00
followerUid = _uid;
topics.follow(newTopic.tid, _uid, next);
}
], done);
2016-02-25 18:04:32 +02:00
});
it('should delete the topic', function (done) {
topics.delete(newTopic.tid, 1, function (err) {
2016-02-25 18:04:32 +02:00
assert.ifError(err);
done();
});
});
it('should restore the topic', function (done) {
topics.restore(newTopic.tid, 1, function (err) {
assert.ifError(err);
done();
});
});
it('should purge the topic', function (done) {
topics.purge(newTopic.tid, 1, function (err) {
2016-02-25 18:04:32 +02:00
assert.ifError(err);
db.isSortedSetMember('uid:' + followerUid + ':followed_tids', newTopic.tid, function (err, isMember) {
2016-08-17 12:12:07 +03:00
assert.ifError(err);
assert.strictEqual(false, isMember);
done();
});
2016-02-25 18:04:32 +02:00
});
});
});
describe('.ignore', function () {
2016-05-18 19:02:43 +03:00
var newTid;
var uid;
var newTopic;
before(function (done) {
2016-05-18 19:02:43 +03:00
uid = topic.userId;
async.waterfall([
function (done) {
topics.post({uid: topic.userId, title: 'Topic to be ignored', content: 'Just ignore me, please!', cid: topic.categoryId}, function (err, result) {
2016-08-16 19:46:59 +02:00
if (err) {
return done(err);
}
2016-05-18 19:02:43 +03:00
newTopic = result.topicData;
newTid = newTopic.tid;
done();
});
},
function (done) {
2016-05-18 19:02:43 +03:00
topics.markUnread( newTid, uid, done );
}
],done);
});
it('should not appear in the unread list', function (done) {
2016-05-18 19:02:43 +03:00
async.waterfall([
function (done) {
2016-05-18 19:02:43 +03:00
topics.ignore( newTid, uid, done );
},
function (done) {
2016-05-18 19:22:26 +03:00
topics.getUnreadTopics(0, uid, 0, -1, '', done );
2016-05-18 19:02:43 +03:00
},
function (results, done) {
2016-05-18 19:02:43 +03:00
var topics = results.topics;
var tids = topics.map( function (topic) { return topic.tid; } );
2016-05-18 19:02:43 +03:00
assert.equal(tids.indexOf(newTid), -1, 'The topic appeared in the unread list.');
done();
}
], done);
});
it('should not appear as unread in the recent list', function (done) {
2016-05-18 19:02:43 +03:00
async.waterfall([
function (done) {
2016-05-18 19:02:43 +03:00
topics.ignore( newTid, uid, done );
},
function (done) {
2016-05-18 19:02:43 +03:00
topics.getLatestTopics( uid, 0, -1, 'year', done );
},
function (results, done) {
2016-05-18 19:02:43 +03:00
var topics = results.topics;
var topic;
var i;
for(i = 0; i < topics.length; ++i) {
if( topics[i].tid == newTid ) {
2016-05-18 19:02:43 +03:00
assert.equal(false, topics[i].unread, 'ignored topic was marked as unread in recent list');
return done();
}
}
assert.ok(topic, 'topic didn\'t appear in the recent list');
done();
}
], done);
});
it('should appear as unread again when marked as reading', function (done) {
2016-05-18 19:02:43 +03:00
async.waterfall([
function (done) {
2016-05-18 19:02:43 +03:00
topics.ignore( newTid, uid, done );
},
function (done) {
2016-05-18 19:02:43 +03:00
topics.follow( newTid, uid, done );
},
function (done) {
2016-05-18 19:22:26 +03:00
topics.getUnreadTopics(0, uid, 0, -1, '', done );
2016-05-18 19:02:43 +03:00
},
function (results, done) {
2016-05-18 19:02:43 +03:00
var topics = results.topics;
var tids = topics.map( function (topic) { return topic.tid; } );
2016-05-18 19:02:43 +03:00
assert.notEqual(tids.indexOf(newTid), -1, 'The topic did not appear in the unread list.');
done();
}
], done);
});
it('should appear as unread again when marked as following', function (done) {
2016-05-18 19:02:43 +03:00
async.waterfall([
function (done) {
2016-05-18 19:02:43 +03:00
topics.ignore( newTid, uid, done );
},
function (done) {
2016-05-18 19:02:43 +03:00
topics.follow( newTid, uid, done );
},
function (done) {
2016-05-18 19:22:26 +03:00
topics.getUnreadTopics(0, uid, 0, -1, '', done );
2016-05-18 19:02:43 +03:00
},
function (results, done) {
2016-05-18 19:02:43 +03:00
var topics = results.topics;
var tids = topics.map( function (topic) { return topic.tid; } );
2016-05-18 19:02:43 +03:00
assert.notEqual(tids.indexOf(newTid), -1, 'The topic did not appear in the unread list.');
done();
}
], done);
});
});
2016-02-25 18:04:32 +02:00
describe('.fork', function () {
var newTopic;
2016-05-18 19:02:43 +03:00
var replies = [];
var topicPids;
var originalBookmark = 5;
function postReply( next ) {
topics.reply({uid: topic.userId, content: 'test post ' + replies.length, tid: newTopic.tid},
function (err, result) {
assert.equal(err, null, 'was created with error');
assert.ok(result);
replies.push( result );
next();
}
);
}
before( function (done) {
async.waterfall(
[
function (next) {
groups.join('administrators', topic.userId, next);
},
function ( next ) {
topics.post({uid: topic.userId, title: topic.title, content: topic.content, cid: topic.categoryId}, function (err, result) {
assert.ifError( err );
newTopic = result.topicData;
next();
});
},
function ( next ) { postReply( next );},
function ( next ) { postReply( next );},
function ( next ) { postReply( next );},
function ( next ) { postReply( next );},
function ( next ) { postReply( next );},
function ( next ) { postReply( next );},
function ( next ) { postReply( next );},
function ( next ) { postReply( next );},
function ( next ) { postReply( next );},
function ( next ) { postReply( next );},
function ( next ) { postReply( next );},
function ( next ) { postReply( next );},
function ( next ) {
topicPids = replies.map( function ( reply ) { return reply.pid; } );
topics.setUserBookmark( newTopic.tid, topic.userId, originalBookmark, next );
}],
done );
});
it('should have 12 replies', function (done) {
assert.equal( 12, replies.length );
done();
});
it('should not update the user\'s bookmark', function (done) {
async.waterfall([
function (next) {
topics.createTopicFromPosts(
topic.userId,
'Fork test, no bookmark update',
topicPids.slice( -2 ),
newTopic.tid,
next );
},
function ( forkedTopicData, next) {
topics.getUserBookmark( newTopic.tid, topic.userId, next );
},
function ( bookmark, next ) {
assert.equal( originalBookmark, bookmark );
next();
}
],done);
});
it('should update the user\'s bookmark ', function (done) {
async.waterfall([
function (next) {
topics.createTopicFromPosts(
topic.userId,
'Fork test, no bookmark update',
topicPids.slice( 1, 3 ),
newTopic.tid,
next );
},
function ( forkedTopicData, next) {
topics.getUserBookmark( newTopic.tid, topic.userId, next );
},
function ( bookmark, next ) {
assert.equal( originalBookmark - 2, bookmark );
next();
}
],done);
});
});
it('should load topic', function (done) {
topics.post({
uid: topic.userId,
title: 'topic for controller test',
content: 'topic content',
cid: topic.categoryId,
thumb: 'http://i.imgur.com/64iBdBD.jpg'
}, function (err, result) {
assert.ifError(err);
assert.ok(result);
var request = require('request');
request(nconf.get('url') + '/topic/' + result.topicData.slug, function (err, response, body) {
assert.ifError(err);
assert.equal(response.statusCode, 200);
assert(body);
done();
});
});
});
after(function (done) {
db.emptydb(done);
2013-11-15 16:16:50 -05:00
});
2014-04-10 20:31:57 +01:00
});