This commit is contained in:
Baris Usakli
2019-09-20 11:44:49 -04:00
parent 307abaa8cc
commit ecf39727cc
3 changed files with 68 additions and 95 deletions

View File

@@ -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);

View File

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

View File

@@ -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'); const db = require('./mocks/databasemock');
var topics = require('../src/topics'); const topics = require('../src/topics');
var posts = require('../src/posts'); const posts = require('../src/posts');
var categories = require('../src/categories'); const categories = require('../src/categories');
var privileges = require('../src/privileges'); const privileges = require('../src/privileges');
var meta = require('../src/meta'); const meta = require('../src/meta');
var User = require('../src/user'); const User = require('../src/user');
var groups = require('../src/groups'); const groups = require('../src/groups');
var helpers = require('./helpers'); const helpers = require('./helpers');
var socketPosts = require('../src/socket.io/posts'); const socketPosts = require('../src/socket.io/posts');
var socketTopics = require('../src/socket.io/topics'); 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) { before(async function () {
User.create({ username: 'admin', password: '123456' }, function (err, uid) { adminUid = await User.create({ username: 'admin', password: '123456' });
if (err) { await groups.join('administrators', adminUid);
return done(err); 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',
}, 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();
});
}); });
topic = {
userId: adminUid,
categoryId: categoryObj.cid,
title: 'Test Topic Title',
content: 'The content of test topic',
};
}); });
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);
},
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(topic1.posts.length, 4);
assert.equal(results.topic1.posts[1].content, 'topic 2 OP'); assert.equal(topic2.posts.length, 0);
assert.equal(results.topic1.posts[2].content, 'topic 1 reply'); assert.equal(topic2.deleted, true);
assert.equal(results.topic1.posts[3].content, 'topic 2 reply');
next(); assert.equal(topic1.posts[0].content, 'topic 1 OP');
}, assert.equal(topic1.posts[1].content, 'topic 2 OP');
], done); 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();
});
}); });
}); });