Skip to content
This repository was archived by the owner on Aug 30, 2021. It is now read-only.

Commit 71167b0

Browse files
committed
The article middleware was calling getErrorMessage with a null argument, causing a crash when this method tried to access 'code' on an null parameter.
The bug was not exposed by the original test, since it was mixing two (related) aspects: * An invalid Id (a badly formed mongodb identifier) * An non-existent Id (an identifier with no corresponding document in the database) Modifications: - Fixed the message property in the article controller (the error message follows the wording of the error message in "users.password.server.controller.js", in case of username not found) - Added a new test to check modifications and avoid regressions
1 parent 0369db4 commit 71167b0

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

modules/articles/server/controllers/articles.server.controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ exports.articleByID = function(req, res, next, id) {
100100
if (err) return next(err);
101101
if (!article) {
102102
return res.status(404).send({
103-
message: errorHandler.getErrorMessage(err)
103+
message: 'No article with that identifier has been found'
104104
});
105105
}
106106
req.article = article;

modules/articles/tests/server/article.server.routes.tests.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ describe('Article CRUD tests', function () {
223223
});
224224
});
225225

226-
it('should return proper error for single article which doesnt exist, if not signed in', function (done) {
226+
it('should return proper error for single article with an invalid Id, if not signed in', function (done) {
227+
// test is not a valid mongoose Id
227228
request(app).get('/api/articles/test')
228229
.end(function (req, res) {
229230
// Set assertion
@@ -234,6 +235,18 @@ describe('Article CRUD tests', function () {
234235
});
235236
});
236237

238+
it('should return proper error for single article which doesnt exist, if not signed in', function (done) {
239+
// This is a valid mongoose Id but a non-existent article
240+
request(app).get('/api/articles/559e9cd815f80b4c256a8f41')
241+
.end(function (req, res) {
242+
// Set assertion
243+
res.body.should.be.instanceof(Object).and.have.property('message', 'No article with that identifier has been found');
244+
245+
// Call the assertion callback
246+
done();
247+
});
248+
});
249+
237250
it('should be able to delete an article if signed in', function (done) {
238251
agent.post('/api/auth/signin')
239252
.send(credentials)

0 commit comments

Comments
 (0)