Skip to content

Node.js based forum software built for the modern web

fix: #7912

Baris Usakliecf39727cc4c
diff --git a/src/controllers/topics.js b/src/controllers/topics.js
index 4867556b9d..1f72f04152 100644
--- a/src/controllers/topics.js
+++ b/src/controllers/topics.js
@@ -53,7 +53,7 @@ topicsController.get = async function getTopic(req, res, callback) {
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 : ''));
}
postIndex = Math.max(1, postIndex);
diff --git a/test/helpers/index.js b/test/helpers/index.js
index 34b0980f35..f1cf53d071 100644
--- a/test/helpers/index.js
+++ b/test/helpers/index.js
@@ -160,3 +160,5 @@ helpers.copyFile = function (source, target, callback) {
}
}
};
+
+require('../../src/promisify')(helpers);
diff --git a/test/topics.js b/test/topics.js
index 09524b9bc4..96825e1296 100644
--- a/test/topics.js
+++ b/test/topics.js
@@ -1,54 +1,44 @@
'use strict';
-var async = require('async');
-var assert = require('assert');
-var validator = require('validator');
-var nconf = require('nconf');
-
-var db = require('./mocks/databasemock');
-var topics = require('../src/topics');
-var posts = require('../src/posts');
-var categories = require('../src/categories');
-var privileges = require('../src/privileges');
-var meta = require('../src/meta');
-var User = require('../src/user');
-var groups = require('../src/groups');
-var helpers = require('./helpers');
-var socketPosts = require('../src/socket.io/posts');
-var socketTopics = require('../src/socket.io/topics');
+const async = require('async');
+const assert = require('assert');
+const validator = require('validator');
+const nconf = require('nconf');
+const request = require('request');
+
+const db = require('./mocks/databasemock');
+const topics = require('../src/topics');
+const posts = require('../src/posts');
+const categories = require('../src/categories');
+const privileges = require('../src/privileges');
+const meta = require('../src/meta');
+const User = require('../src/user');
+const groups = require('../src/groups');
+const helpers = require('./helpers');
+const socketPosts = require('../src/socket.io/posts');
+const socketTopics = require('../src/socket.io/topics');
describe('Topic\'s', function () {
var topic;
var categoryObj;
var adminUid;
-
- before(function (done) {
- User.create({ username: 'admin', password: '123456' }, function (err, uid) {
- if (err) {
- return done(err);
- }
-
- adminUid = uid;
-
- categories.create({
- name: 'Test Category',
- description: 'Test category created by testing script',
- }, function (err, category) {
- if (err) {
- return done(err);
- }
-
- categoryObj = category;
-
- topic = {
- userId: uid,
- categoryId: categoryObj.cid,
- title: 'Test Topic Title',
- content: 'The content of test topic',
- };
- done();
- });
- });
+ var adminJar;
+
+ before(async function () {
+ adminUid = await User.create({ username: 'admin', password: '123456' });
+ await groups.join('administrators', adminUid);
+ adminJar = await helpers.loginUser('admin', '123456');
+
+ categoryObj = await categories.create({
+ name: 'Test Category',
+ description: 'Test category created by testing script',
+ });
+ topic = {
+ userId: adminUid,
+ categoryId: categoryObj.cid,
+ title: 'Test Topic Title',
+ content: 'The content of test topic',
+ };
});
describe('.post', function () {
@@ -309,9 +299,6 @@ describe('Topic\'s', function () {
before(function (done) {
async.waterfall([
- function (next) {
- groups.join('administrators', adminUid, next);
- },
function (next) {
topics.post({ uid: topic.userId, title: topic.title, content: topic.content, cid: topic.categoryId }, function (err, result) {
assert.ifError(err);
@@ -905,7 +892,6 @@ describe('Topic\'s', function () {
});
describe('controller', function () {
- var request = require('request');
var topicData;
before(function (done) {
@@ -991,18 +977,15 @@ describe('Topic\'s', function () {
});
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);
- request(nconf.get('url') + '/topic/' + topicData.slug, {
- jar: jar,
- }, function (err, res) {
+ assert.equal(res.statusCode, 200);
+ topics.hasReadTopics([topicData.tid], adminUid, function (err, hasRead) {
assert.ifError(err);
- assert.equal(res.statusCode, 200);
- topics.hasReadTopics([topicData.tid], adminUid, function (err, hasRead) {
- assert.ifError(err);
- assert.equal(hasRead[0], true);
- done();
- });
+ assert.equal(hasRead[0], true);
+ done();
});
});
});
@@ -2070,47 +2053,35 @@ describe('Topic\'s', function () {
});
});
- it('should merge 2 topics', function (done) {
- async.waterfall([
- function (next) {
- socketTopics.merge({ uid: adminUid }, [topic2Data.tid, topic1Data.tid], next);
- },
- function (next) {
- async.parallel({
- topic1: function (next) {
- async.waterfall([
- function (next) {
- topics.getTopicData(topic1Data.tid, next);
- },
- function (topicData, next) {
- topics.getTopicWithPosts(topicData, 'tid:' + topicData.tid + ':posts', adminUid, 0, 19, false, next);
- },
- ], next);
- },
- topic2: function (next) {
- async.waterfall([
- function (next) {
- topics.getTopicData(topic2Data.tid, next);
- },
- function (topicData, next) {
- topics.getTopicWithPosts(topicData, 'tid:' + topicData.tid + ':posts', adminUid, 0, 19, false, next);
- },
- ], next);
- },
- }, next);
- },
- 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);
+ it('should merge 2 topics', async function () {
+ await socketTopics.merge({ uid: adminUid }, [topic2Data.tid, topic1Data.tid]);
+ async function getTopic(tid) {
+ const topicData = await topics.getTopicData(tid);
+ return await topics.getTopicWithPosts(topicData, 'tid:' + topicData.tid + ':posts', adminUid, 0, 19, false);
+ }
+ const [topic1, topic2] = await Promise.all([
+ getTopic(topic1Data.tid),
+ getTopic(topic2Data.tid),
+ ]);
+
+ assert.equal(topic1.posts.length, 4);
+ assert.equal(topic2.posts.length, 0);
+ assert.equal(topic2.deleted, true);
+
+ assert.equal(topic1.posts[0].content, 'topic 1 OP');
+ assert.equal(topic1.posts[1].content, 'topic 2 OP');
+ assert.equal(topic1.posts[2].content, 'topic 1 reply');
+ assert.equal(topic1.posts[3].content, 'topic 2 reply');
+ });
+
+ it('should return properly for merged topic', function (done) {
+ request(nconf.get('url') + '/api/topic/' + topic2Data.slug, { jar: adminJar, json: true }, function (err, response, body) {
+ assert.ifError(err);
+ assert.equal(response.statusCode, 200);
+ assert(body);
+ assert.deepStrictEqual(body.posts, []);
+ done();
+ });
});
});