diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index 168b89fc3..3ca2f4877 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -97,7 +97,13 @@ router.post('/', wordlist.filter('body'), (req, res, next) => { // Return `premod` if pre-moderation is enabled and an empty "new" status // in the event that it is not in pre-moderation mode. - .then(({moderation}) => moderation === 'pre' ? 'premod' : ''); + .then(({moderation, charCountEnable, charCount}) => { + // Reject if the comment is too long + if (charCountEnable && body.length > charCount) { + return 'rejected'; + } + return moderation === 'pre' ? 'premod' : ''; + }); } status.then((status) => Comment.publicCreate({ diff --git a/tests/routes/api/comments/index.js b/tests/routes/api/comments/index.js index b3e4e2675..fdd4e4bbe 100644 --- a/tests/routes/api/comments/index.js +++ b/tests/routes/api/comments/index.js @@ -197,6 +197,28 @@ describe('/api/v1/comments', () => { }); }); + it('should create a rejected comment if the body is above the character count', () => { + return Asset + .findOrCreateByUrl('https://coralproject.net/article1') + .then((asset) => { + return Asset + .overrideSettings(asset.id, {charCountEnable: true, charCount: 10}) + .then(() => asset); + }) + .then((asset) => { + return chai.request(app) + .post('/api/v1/comments') + .set(passport.inject({roles: []})) + .send({'body': 'This is way way way way way too long.', 'author_id': '123', 'asset_id': asset.id, 'parent_id': ''}); + }) + .then((res) => { + expect(res).to.have.status(201); + expect(res.body).to.have.property('id'); + expect(res.body).to.have.property('asset_id'); + expect(res.body).to.have.property('status', 'rejected'); + }); + }); + it('shouldn\'t create a comment when the asset has expired commenting', () => { return Asset.create({ closedAt: new Date().setDate(0),