Skip to content

Commit

Permalink
fix: #7040
Browse files Browse the repository at this point in the history
if category doesn't exist return null and 404
  • Loading branch information
barisusakli committed Dec 1, 2018
1 parent ec0c50d commit a63ddbe
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/categories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Categories.getCategoryById = function (data, callback) {
},
function (categories, next) {
if (!categories[0]) {
return next(new Error('[[error:invalid-cid]]'));
return callback(null, null);
}
category = categories[0];
data.category = category;
Expand Down
3 changes: 3 additions & 0 deletions src/controllers/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ categoryController.get = function (req, res, callback) {
}, next);
},
function (categoryData, next) {
if (!categoryData) {
return callback();
}
categories.modifyTopicsByPrivilege(categoryData.topics, userPrivileges);

if (categoryData.link) {
Expand Down
9 changes: 6 additions & 3 deletions src/routes/feeds.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ function generateForTopic(req, res, callback) {
], callback);
}

function generateForCategory(req, res, next) {
function generateForCategory(req, res, callback) {
if (meta.config['feeds:disableRSS']) {
return controllers404.send404(req, res);
}
var cid = req.params.category_id;
var category;
if (!parseInt(cid, 10)) {
return next();
return setImmediate(callback);
}
async.waterfall([
function (next) {
Expand All @@ -177,6 +177,9 @@ function generateForCategory(req, res, next) {
},
function (results, next) {
category = results.category;
if (!category) {
return callback();
}
validateTokenIfRequiresLogin(!results.privileges.read, cid, req, res, next);
},
function (next) {
Expand All @@ -191,7 +194,7 @@ function generateForCategory(req, res, next) {
function (feed) {
sendFeed(feed, res);
},
], next);
], callback);
}

function generateForTopics(req, res, next) {
Expand Down
13 changes: 12 additions & 1 deletion test/categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('Categories', function () {
stop: -1,
uid: 0,
}, function (err, categoryData) {
assert.equal(err, null);
assert.ifError(err);

assert(categoryData);
assert.equal('Test Category & NodeBB', categoryData.name);
Expand All @@ -67,6 +67,17 @@ describe('Categories', function () {
});
});

it('should return null if category does not exist', function (done) {
Categories.getCategoryById({
cid: 123123123,
start: 0,
stop: -1,
}, function (err, categoryData) {
assert.ifError(err);
assert.strictEqual(categoryData, null);
done();
});
});

it('should load a category route', function (done) {
request(nconf.get('url') + '/api/category/' + categoryObj.cid + '/test-category', { json: true }, function (err, response, body) {
Expand Down

0 comments on commit a63ddbe

Please sign in to comment.