diff --git a/src/controllers/topics.js b/src/controllers/topics.js index 1224de5bcd..0677c0a8c3 100644 --- a/src/controllers/topics.js +++ b/src/controllers/topics.js @@ -299,7 +299,7 @@ topicsController.teaser = function (req, res, next) { var tid = req.params.topic_id; if (!utils.isNumber(tid)) { - return next(new Error('[[error:invalid-tid]]')); + return next(); } async.waterfall([ diff --git a/src/privileges/topics.js b/src/privileges/topics.js index 39ad054462..2350299e72 100644 --- a/src/privileges/topics.js +++ b/src/privileges/topics.js @@ -63,13 +63,14 @@ module.exports = function (privileges) { }; privileges.topics.can = function (privilege, tid, uid, callback) { - topics.getTopicField(tid, 'cid', function (err, cid) { - if (err) { - return callback(err); + async.waterfall([ + function (next) { + topics.getTopicField(tid, 'cid', next); + }, + function (cid, next) { + privileges.categories.can(privilege, cid, uid, next); } - - privileges.categories.can(privilege, cid, uid, callback); - }); + ], callback); }; privileges.topics.filterTids = function (privilege, tids, uid, callback) { diff --git a/test/topics.js b/test/topics.js index 9f430c995a..cfb77ed581 100644 --- a/test/topics.js +++ b/test/topics.js @@ -10,6 +10,7 @@ var topics = require('../src/topics'); var categories = require('../src/categories'); var User = require('../src/user'); var groups = require('../src/groups'); +var helpers = require('./helpers'); var socketPosts = require('../src/socket.io/posts'); describe('Topic\'s', function () { @@ -19,7 +20,7 @@ describe('Topic\'s', function () { before(function (done) { groups.resetCache(); - User.create({username: 'admin'}, function (err, uid) { + User.create({username: 'admin', password: '123456'}, function (err, uid) { if (err) { return done(err); } @@ -182,7 +183,7 @@ describe('Topic\'s', function () { }); }); - + it('should not receive errors', function (done) { topics.getTopicData(newTopic.tid, done); }); @@ -202,7 +203,7 @@ describe('Topic\'s', function () { done(); }); }); - + describe('.getTopicWithPosts', function () { it('should get a topic with posts and other data', function (done) { topics.getTopicData(newTopic.tid, function (err, topicData) { @@ -672,26 +673,177 @@ describe('Topic\'s', function () { }); }); - 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) { + describe('controller', function () { + var request = require('request'); + var topicData; + + before(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); + topicData = result.topicData; + done(); + }); + }); + + it('should load topic', function (done) { + request(nconf.get('url') + '/topic/' + topicData.slug, function (err, response, body) { assert.ifError(err); assert.equal(response.statusCode, 200); assert(body); done(); }); }); + + it('should 404 if post index is invalid', function (done) { + request(nconf.get('url') + '/topic/' + topicData.slug + '/derp', function (err, response) { + assert.ifError(err); + assert.equal(response.statusCode, 404); + done(); + }); + }); + + it('should 404 if topic does not exist', function (done) { + request(nconf.get('url') + '/topic/123123/does-not-exist', function (err, response) { + assert.ifError(err); + assert.equal(response.statusCode, 404); + done(); + }); + }); + + it('should 401 if not allowed to read as guest', function (done) { + var privileges = require('../src/privileges'); + privileges.categories.rescind(['read'], topicData.cid, 'guests', function (err) { + assert.ifError(err); + request(nconf.get('url') + '/api/topic/' + topicData.slug, function (err, response, body) { + assert.ifError(err); + assert.equal(response.statusCode, 401); + assert(body); + privileges.categories.give(['read'], topicData.cid, 'guests', done); + }); + }); + }); + + it('should redirect to correct topic if slug is missing', function (done) { + request(nconf.get('url') + '/topic/' + topicData.tid + '/herpderp/1?page=2', function (err, response, body) { + assert.ifError(err); + assert.equal(response.statusCode, 200); + assert(body); + done(); + }); + }); + + it('should redirect if post index is out of range', function (done) { + request(nconf.get('url') + '/api/topic/' + topicData.slug + '/-1', function (err, response, body) { + assert.ifError(err); + assert.equal(response.statusCode, 308); + assert.equal(body, '"/topic/13/topic-for-controller-test"'); + done(); + }); + }); + + it('should 404 if page is out of bounds', function (done) { + var meta = require('../src/meta'); + meta.config.usePagination = 1; + request(nconf.get('url') + '/topic/' + topicData.slug + '?page=100', function (err, response) { + assert.ifError(err); + assert.equal(response.statusCode, 404); + done(); + }); + }); + + it('should mark topic read', function (done) { + helpers.loginUser('admin', '123456', function (err, jar) { + assert.ifError(err); + request(nconf.get('url') + '/topic/' + topicData.slug, { + jar: jar + }, function (err, res) { + 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(); + }); + }); + }); + }); + + it('should 404 if tid is not a number', function (done) { + request(nconf.get('url') + '/api/topic/teaser/nan', {json: true}, function (err, response, body) { + assert.ifError(err); + assert.equal(response.statusCode, 404); + done(); + }); + }); + + it('should 403 if cant read', function (done) { + request(nconf.get('url') + '/api/topic/teaser/' + 123123, {json: true}, function (err, response, body) { + assert.ifError(err); + assert.equal(response.statusCode, 403); + assert.equal(body, '[[error:no-privileges]]'); + + done(); + }); + }); + + it('should load topic teaser', function (done) { + request(nconf.get('url') + '/api/topic/teaser/' + topicData.tid, {json: true}, function (err, response, body) { + assert.ifError(err); + assert.equal(response.statusCode, 200); + assert(body); + assert.equal(body.tid, topicData.tid); + assert.equal(body.content, 'topic content'); + assert(body.user); + assert(body.topic); + assert(body.category); + done(); + }); + }); + + + it('should 404 if tid is not a number', function (done) { + request(nconf.get('url') + '/api/topic/pagination/nan', {json: true}, function (err, response, body) { + assert.ifError(err); + assert.equal(response.statusCode, 404); + done(); + }); + }); + + it('should 404 if tid does not exist', function (done) { + request(nconf.get('url') + '/api/topic/pagination/1231231', {json: true}, function (err, response, body) { + assert.ifError(err); + assert.equal(response.statusCode, 404); + done(); + }); + }); + + it('should load pagination', function (done) { + request(nconf.get('url') + '/api/topic/pagination/' + topicData.tid, {json: true}, function (err, response, body) { + assert.ifError(err); + assert.equal(response.statusCode, 200); + assert(body); + assert.deepEqual(body, { + prev: { page: 1, active: false }, + next: { page: 1, active: false }, + rel: [], + pages: [], + currentPage: 1, + pageCount: 1 + }); + done(); + }); + }); + }); + describe('infinitescroll', function () { var socketTopics = require('../src/socket.io/topics'); var tid;