You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
452 lines
12 KiB
JavaScript
452 lines
12 KiB
JavaScript
11 years ago
|
'use strict';
|
||
10 years ago
|
/*global require, before, beforeEach, after*/
|
||
11 years ago
|
|
||
9 years ago
|
var assert = require('assert');
|
||
|
var validator = require('validator');
|
||
|
var db = require('./mocks/databasemock');
|
||
|
var topics = require('../src/topics');
|
||
|
var categories = require('../src/categories');
|
||
|
var User = require('../src/user');
|
||
9 years ago
|
var groups = require('../src/groups');
|
||
|
var async = require('async');
|
||
11 years ago
|
|
||
11 years ago
|
describe('Topic\'s', function() {
|
||
11 years ago
|
var topic,
|
||
|
categoryObj;
|
||
|
|
||
|
before(function(done) {
|
||
11 years ago
|
var userData = {
|
||
|
username: 'John Smith',
|
||
|
password: 'swordfish',
|
||
|
email: '[email protected]',
|
||
|
callback: undefined
|
||
11 years ago
|
};
|
||
11 years ago
|
|
||
|
User.create({username: userData.username, password: userData.password, email: userData.email}, function(err, uid) {
|
||
9 years ago
|
if (err) {
|
||
|
return done(err);
|
||
|
}
|
||
|
|
||
11 years ago
|
categories.create({
|
||
|
name: 'Test Category',
|
||
|
description: 'Test category created by testing script',
|
||
|
icon: 'fa-check',
|
||
|
blockclass: 'category-blue',
|
||
|
order: '5'
|
||
|
}, function(err, category) {
|
||
9 years ago
|
if (err) {
|
||
|
return done(err);
|
||
|
}
|
||
|
|
||
11 years ago
|
categoryObj = category;
|
||
|
|
||
|
topic = {
|
||
|
userId: uid,
|
||
|
categoryId: categoryObj.cid,
|
||
|
title: 'Test Topic Title',
|
||
|
content: 'The content of test topic'
|
||
|
};
|
||
|
done();
|
||
|
});
|
||
11 years ago
|
});
|
||
11 years ago
|
|
||
10 years ago
|
|
||
11 years ago
|
});
|
||
11 years ago
|
|
||
|
describe('.post', function() {
|
||
|
|
||
11 years ago
|
it('should create a new topic with proper parameters', function(done) {
|
||
11 years ago
|
topics.post({uid: topic.userId, title: topic.title, content: topic.content, cid: topic.categoryId}, function(err, result) {
|
||
11 years ago
|
assert.equal(err, null, 'was created with error');
|
||
|
assert.ok(result);
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
10 years ago
|
it('should fail to create new topic with invalid user id', function(done) {
|
||
9 years ago
|
topics.post({uid: null, title: topic.title, content: topic.content, cid: topic.categoryId}, function(err) {
|
||
10 years ago
|
assert.equal(err.message, '[[error:no-privileges]]');
|
||
11 years ago
|
done();
|
||
|
});
|
||
|
});
|
||
10 years ago
|
|
||
|
it('should fail to create new topic with empty title', function(done) {
|
||
9 years ago
|
topics.post({uid: topic.userId, title: '', content: topic.content, cid: topic.categoryId}, function(err) {
|
||
10 years ago
|
assert.ok(err);
|
||
10 years ago
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should fail to create new topic with empty content', function(done) {
|
||
9 years ago
|
topics.post({uid: topic.userId, title: topic.title, content: '', cid: topic.categoryId}, function(err) {
|
||
10 years ago
|
assert.ok(err);
|
||
10 years ago
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should fail to create new topic with non-existant category id', function(done) {
|
||
9 years ago
|
topics.post({uid: topic.userId, title: topic.title, content: topic.content, cid: 99}, function(err) {
|
||
10 years ago
|
assert.equal(err.message, '[[error:no-category]]', 'received no error');
|
||
10 years ago
|
done();
|
||
|
});
|
||
|
});
|
||
11 years ago
|
});
|
||
|
|
||
10 years ago
|
describe('.reply', 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) {
|
||
9 years ago
|
if (err) {
|
||
|
return done(err);
|
||
|
}
|
||
|
|
||
10 years ago
|
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) {
|
||
|
assert.equal(err, null, 'was created with error');
|
||
|
assert.ok(result);
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
10 years ago
|
|
||
|
it('should fail to create new reply with invalid user id', function(done) {
|
||
9 years ago
|
topics.reply({uid: null, content: 'test post', tid: newTopic.tid}, function(err) {
|
||
10 years ago
|
assert.equal(err.message, '[[error:no-privileges]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should fail to create new reply with empty content', function(done) {
|
||
9 years ago
|
topics.reply({uid: topic.userId, content: '', tid: newTopic.tid}, function(err) {
|
||
10 years ago
|
assert.ok(err);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should fail to create new reply with invalid topic id', function(done) {
|
||
9 years ago
|
topics.reply({uid: null, content: 'test post', tid: 99}, function(err) {
|
||
10 years ago
|
assert.equal(err.message, '[[error:no-topic]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
10 years ago
|
});
|
||
|
|
||
11 years ago
|
describe('Get methods', function() {
|
||
|
var newTopic;
|
||
|
var newPost;
|
||
|
|
||
9 years ago
|
before(function(done) {
|
||
11 years ago
|
topics.post({uid: topic.userId, title: topic.title, content: topic.content, cid: topic.categoryId}, function(err, result) {
|
||
9 years ago
|
if (err) {
|
||
|
return done(err);
|
||
|
}
|
||
|
|
||
11 years ago
|
newTopic = result.topicData;
|
||
|
newPost = result.postData;
|
||
|
done();
|
||
11 years ago
|
});
|
||
|
});
|
||
|
|
||
11 years ago
|
describe('.getTopicData', function() {
|
||
11 years ago
|
it('should not receive errors', function(done) {
|
||
11 years ago
|
topics.getTopicData(newTopic.tid, done);
|
||
11 years ago
|
});
|
||
|
});
|
||
9 years ago
|
|
||
|
describe('.getTopicWithPosts', function() {
|
||
|
it('should get a topic with posts and other data', function(done) {
|
||
|
topics.getTopicData(newTopic.tid, function(err, topicData) {
|
||
|
if (err) {
|
||
|
return done(err);
|
||
|
}
|
||
|
topics.getTopicWithPosts(topicData, 'tid:' + newTopic.tid + ':posts', topic.userId, 0, -1, false, function(err, data) {
|
||
|
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();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|
||
11 years ago
|
});
|
||
|
|
||
9 years ago
|
describe('Title escaping', function() {
|
||
|
|
||
|
it('should properly escape topic title', function(done) {
|
||
|
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) {
|
||
|
assert.ifError(err);
|
||
|
topics.getTopicData(result.topicData.tid, function(err, topicData) {
|
||
|
assert.ifError(err);
|
||
9 years ago
|
assert.strictEqual(topicData.titleRaw, title);
|
||
|
assert.strictEqual(topicData.title, titleEscaped);
|
||
9 years ago
|
done();
|
||
9 years ago
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
9 years ago
|
describe('.purge/.delete', function() {
|
||
|
var newTopic;
|
||
9 years ago
|
var followerUid;
|
||
9 years ago
|
before(function(done) {
|
||
9 years ago
|
async.waterfall([
|
||
|
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) {
|
||
|
User.create({username: 'topicFollower', password: '123456'}, next);
|
||
|
},
|
||
|
function(_uid, next) {
|
||
|
followerUid = _uid;
|
||
|
topics.follow(newTopic.tid, _uid, next);
|
||
|
}
|
||
|
], done);
|
||
9 years ago
|
});
|
||
|
|
||
|
it('should delete the topic', function(done) {
|
||
9 years ago
|
topics.delete(newTopic.tid, 1, function(err) {
|
||
9 years ago
|
assert.ifError(err);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should purge the topic', function(done) {
|
||
9 years ago
|
topics.purge(newTopic.tid, 1, function(err) {
|
||
9 years ago
|
assert.ifError(err);
|
||
9 years ago
|
db.isSortedSetMember('uid:' + followerUid + ':followed_tids', newTopic.tid, function(err, isMember) {
|
||
|
assert.ifError(err);
|
||
|
assert.strictEqual(false, isMember);
|
||
|
done();
|
||
|
});
|
||
9 years ago
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
9 years ago
|
describe('.ignore', function(){
|
||
|
var newTid;
|
||
|
var uid;
|
||
|
var newTopic;
|
||
|
before(function(done){
|
||
|
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) {
|
||
9 years ago
|
if (err) {
|
||
|
return done(err);
|
||
|
}
|
||
|
|
||
9 years ago
|
newTopic = result.topicData;
|
||
|
newTid = newTopic.tid;
|
||
|
done();
|
||
|
});
|
||
|
},
|
||
|
function(done){
|
||
|
topics.markUnread( newTid, uid, done );
|
||
|
}
|
||
|
],done);
|
||
|
});
|
||
|
|
||
|
it('should not appear in the unread list', function(done){
|
||
|
async.waterfall([
|
||
|
function(done){
|
||
|
topics.ignore( newTid, uid, done );
|
||
|
},
|
||
|
function(done){
|
||
9 years ago
|
topics.getUnreadTopics(0, uid, 0, -1, '', done );
|
||
9 years ago
|
},
|
||
|
function(results, done){
|
||
|
var topics = results.topics;
|
||
|
var tids = topics.map( function(topic){ return topic.tid; } );
|
||
|
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){
|
||
|
async.waterfall([
|
||
|
function(done){
|
||
|
topics.ignore( newTid, uid, done );
|
||
|
},
|
||
|
function(done){
|
||
|
topics.getLatestTopics( uid, 0, -1, 'year', done );
|
||
|
},
|
||
|
function(results, done){
|
||
|
var topics = results.topics;
|
||
|
var topic;
|
||
|
var i;
|
||
|
for(i = 0; i < topics.length; ++i){
|
||
|
if( topics[i].tid == newTid ){
|
||
|
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){
|
||
|
async.waterfall([
|
||
|
function(done){
|
||
|
topics.ignore( newTid, uid, done );
|
||
|
},
|
||
|
function(done){
|
||
|
topics.follow( newTid, uid, done );
|
||
|
},
|
||
|
function(done){
|
||
9 years ago
|
topics.getUnreadTopics(0, uid, 0, -1, '', done );
|
||
9 years ago
|
},
|
||
|
function(results, done){
|
||
|
var topics = results.topics;
|
||
|
var tids = topics.map( function(topic){ return topic.tid; } );
|
||
|
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){
|
||
|
async.waterfall([
|
||
|
function(done){
|
||
|
topics.ignore( newTid, uid, done );
|
||
|
},
|
||
|
function(done){
|
||
|
topics.follow( newTid, uid, done );
|
||
|
},
|
||
|
function(done){
|
||
9 years ago
|
topics.getUnreadTopics(0, uid, 0, -1, '', done );
|
||
9 years ago
|
},
|
||
|
function(results, done){
|
||
|
var topics = results.topics;
|
||
|
var tids = topics.map( function(topic){ return topic.tid; } );
|
||
|
assert.notEqual(tids.indexOf(newTid), -1, 'The topic did not appear in the unread list.');
|
||
|
done();
|
||
|
}
|
||
|
], done);
|
||
|
});
|
||
|
});
|
||
|
|
||
|
|
||
9 years ago
|
|
||
9 years ago
|
describe('.fork', function(){
|
||
|
var newTopic;
|
||
9 years ago
|
var replies = [];
|
||
9 years ago
|
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);
|
||
|
});
|
||
|
});
|
||
|
|
||
9 years ago
|
after(function(done) {
|
||
|
db.flushdb(done);
|
||
11 years ago
|
});
|
||
11 years ago
|
});
|