v1.18.x
Baris Usakli 6 years ago
parent 307abaa8cc
commit ecf39727cc

@ -53,7 +53,7 @@ topicsController.get = async function getTopic(req, res, callback) {
postIndex = await topics.getUserBookmark(tid, req.uid); postIndex = await topics.getUserBookmark(tid, req.uid);
} }
if (utils.isNumber(postIndex) && (postIndex < 1 || postIndex > topicData.postcount)) { if (utils.isNumber(postIndex) && topicData.postcount > 0 && (postIndex < 1 || postIndex > topicData.postcount)) {
return helpers.redirect(res, '/topic/' + req.params.topic_id + '/' + req.params.slug + (postIndex > topicData.postcount ? '/' + topicData.postcount : '')); return helpers.redirect(res, '/topic/' + req.params.topic_id + '/' + req.params.slug + (postIndex > topicData.postcount ? '/' + topicData.postcount : ''));
} }
postIndex = Math.max(1, postIndex); postIndex = Math.max(1, postIndex);

@ -160,3 +160,5 @@ helpers.copyFile = function (source, target, callback) {
} }
} }
}; };
require('../../src/promisify')(helpers);

@ -1,54 +1,44 @@
'use strict'; 'use strict';
var async = require('async'); const async = require('async');
var assert = require('assert'); const assert = require('assert');
var validator = require('validator'); const validator = require('validator');
var nconf = require('nconf'); const nconf = require('nconf');
const request = require('request');
var db = require('./mocks/databasemock');
var topics = require('../src/topics'); const db = require('./mocks/databasemock');
var posts = require('../src/posts'); const topics = require('../src/topics');
var categories = require('../src/categories'); const posts = require('../src/posts');
var privileges = require('../src/privileges'); const categories = require('../src/categories');
var meta = require('../src/meta'); const privileges = require('../src/privileges');
var User = require('../src/user'); const meta = require('../src/meta');
var groups = require('../src/groups'); const User = require('../src/user');
var helpers = require('./helpers'); const groups = require('../src/groups');
var socketPosts = require('../src/socket.io/posts'); const helpers = require('./helpers');
var socketTopics = require('../src/socket.io/topics'); const socketPosts = require('../src/socket.io/posts');
const socketTopics = require('../src/socket.io/topics');
describe('Topic\'s', function () { describe('Topic\'s', function () {
var topic; var topic;
var categoryObj; var categoryObj;
var adminUid; var adminUid;
var adminJar;
before(function (done) {
User.create({ username: 'admin', password: '123456' }, function (err, uid) { before(async function () {
if (err) { adminUid = await User.create({ username: 'admin', password: '123456' });
return done(err); await groups.join('administrators', adminUid);
} adminJar = await helpers.loginUser('admin', '123456');
adminUid = uid; categoryObj = await categories.create({
name: 'Test Category',
categories.create({ description: 'Test category created by testing script',
name: 'Test Category', });
description: 'Test category created by testing script', topic = {
}, function (err, category) { userId: adminUid,
if (err) { categoryId: categoryObj.cid,
return done(err); title: 'Test Topic Title',
} content: 'The content of test topic',
};
categoryObj = category;
topic = {
userId: uid,
categoryId: categoryObj.cid,
title: 'Test Topic Title',
content: 'The content of test topic',
};
done();
});
});
}); });
describe('.post', function () { describe('.post', function () {
@ -309,9 +299,6 @@ describe('Topic\'s', function () {
before(function (done) { before(function (done) {
async.waterfall([ async.waterfall([
function (next) {
groups.join('administrators', adminUid, next);
},
function (next) { function (next) {
topics.post({ uid: topic.userId, title: topic.title, content: topic.content, cid: topic.categoryId }, function (err, result) { topics.post({ uid: topic.userId, title: topic.title, content: topic.content, cid: topic.categoryId }, function (err, result) {
assert.ifError(err); assert.ifError(err);
@ -905,7 +892,6 @@ describe('Topic\'s', function () {
}); });
describe('controller', function () { describe('controller', function () {
var request = require('request');
var topicData; var topicData;
before(function (done) { before(function (done) {
@ -991,18 +977,15 @@ describe('Topic\'s', function () {
}); });
it('should mark topic read', function (done) { it('should mark topic read', function (done) {
helpers.loginUser('admin', '123456', function (err, jar) { request(nconf.get('url') + '/topic/' + topicData.slug, {
jar: adminJar,
}, function (err, res) {
assert.ifError(err); assert.ifError(err);
request(nconf.get('url') + '/topic/' + topicData.slug, { assert.equal(res.statusCode, 200);
jar: jar, topics.hasReadTopics([topicData.tid], adminUid, function (err, hasRead) {
}, function (err, res) {
assert.ifError(err); assert.ifError(err);
assert.equal(res.statusCode, 200); assert.equal(hasRead[0], true);
topics.hasReadTopics([topicData.tid], adminUid, function (err, hasRead) { done();
assert.ifError(err);
assert.equal(hasRead[0], true);
done();
});
}); });
}); });
}); });
@ -2070,47 +2053,35 @@ describe('Topic\'s', function () {
}); });
}); });
it('should merge 2 topics', function (done) { it('should merge 2 topics', async function () {
async.waterfall([ await socketTopics.merge({ uid: adminUid }, [topic2Data.tid, topic1Data.tid]);
function (next) { async function getTopic(tid) {
socketTopics.merge({ uid: adminUid }, [topic2Data.tid, topic1Data.tid], next); const topicData = await topics.getTopicData(tid);
}, return await topics.getTopicWithPosts(topicData, 'tid:' + topicData.tid + ':posts', adminUid, 0, 19, false);
function (next) { }
async.parallel({ const [topic1, topic2] = await Promise.all([
topic1: function (next) { getTopic(topic1Data.tid),
async.waterfall([ getTopic(topic2Data.tid),
function (next) { ]);
topics.getTopicData(topic1Data.tid, next);
}, assert.equal(topic1.posts.length, 4);
function (topicData, next) { assert.equal(topic2.posts.length, 0);
topics.getTopicWithPosts(topicData, 'tid:' + topicData.tid + ':posts', adminUid, 0, 19, false, next); assert.equal(topic2.deleted, true);
},
], next); assert.equal(topic1.posts[0].content, 'topic 1 OP');
}, assert.equal(topic1.posts[1].content, 'topic 2 OP');
topic2: function (next) { assert.equal(topic1.posts[2].content, 'topic 1 reply');
async.waterfall([ assert.equal(topic1.posts[3].content, 'topic 2 reply');
function (next) { });
topics.getTopicData(topic2Data.tid, next);
}, it('should return properly for merged topic', function (done) {
function (topicData, next) { request(nconf.get('url') + '/api/topic/' + topic2Data.slug, { jar: adminJar, json: true }, function (err, response, body) {
topics.getTopicWithPosts(topicData, 'tid:' + topicData.tid + ':posts', adminUid, 0, 19, false, next); assert.ifError(err);
}, assert.equal(response.statusCode, 200);
], next); assert(body);
}, assert.deepStrictEqual(body.posts, []);
}, next); done();
}, });
function (results, next) {
assert.equal(results.topic1.posts.length, 4);
assert.equal(results.topic2.posts.length, 0);
assert.equal(results.topic2.deleted, true);
assert.equal(results.topic1.posts[0].content, 'topic 1 OP');
assert.equal(results.topic1.posts[1].content, 'topic 2 OP');
assert.equal(results.topic1.posts[2].content, 'topic 1 reply');
assert.equal(results.topic1.posts[3].content, 'topic 2 reply');
next();
},
], done);
}); });
}); });

Loading…
Cancel
Save