From ab68b81f21762d89d0912c67026b810e47f21e3a Mon Sep 17 00:00:00 2001 From: gaba Date: Wed, 9 Nov 2016 09:33:15 -0800 Subject: [PATCH] Add tests to stream on moderated comments. --- models/comment.js | 14 ++++++++++++-- routes/api/stream/index.js | 4 ++++ tests/models/comment.js | 14 ++++++++++++-- tests/routes/api/comments/index.js | 24 ++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/models/comment.js b/models/comment.js index caefaa65f..1e89a3f92 100644 --- a/models/comment.js +++ b/models/comment.js @@ -58,13 +58,23 @@ CommentSchema.statics.findById = function(id) { }; /** - * Finds a comment by the asset_id. + * Finds ALL the comments by the asset_id. * @param {String} asset_id identifier of the asset which owns this comment (uuid) */ -CommentSchema.statics.findByAssetId = function(asset_id) { +CommentSchema.statics.findAllByAssetId = function(asset_id) { return Comment.find({asset_id}); }; +/** + * Finds the comments by the asset_id. + * get the comments that are approved. + * if post: get the comments that new and not flagged. + * @param {String} asset_id identifier of the asset which owns the comments (uuid) +*/ +CommentSchema.statics.findByAssetId = function(asset_id) { + return Comment.find({asset_id: asset_id, status:'accepted'}); +}; + /** * Find comments by an action that was performed on them. * @param {String} action_type the type of action that was performed on the comment diff --git a/routes/api/stream/index.js b/routes/api/stream/index.js index 67216a252..221a14c4d 100644 --- a/routes/api/stream/index.js +++ b/routes/api/stream/index.js @@ -8,8 +8,12 @@ const router = express.Router(); router.get('/', (req, res, next) => { + // find all the comments by a specific asset_id. + // - get the comments that are approved. + // - if post: get the comments that new and not flagged. const commentsPromise = Comment.findByAssetId(req.query.asset_id); + // get all the users and actions for those comments. commentsPromise.then(comments => { return Promise.all([ comments, diff --git a/tests/models/comment.js b/tests/models/comment.js index d76277d6e..6f051af39 100644 --- a/tests/models/comment.js +++ b/tests/models/comment.js @@ -73,8 +73,8 @@ describe('Comment: models', () => { }); describe('#findByAssetId()', () => { - it('should find an array of comments by asset id', () => { - return Comment.findByAssetId('123').then((result) => { + it('should find an array of all comments by asset id', () => { + return Comment.findAllByAssetId('123').then((result) => { expect(result).to.have.length(2); result.sort((a, b) => { if (a.body < b.body) {return -1;} @@ -84,6 +84,16 @@ describe('Comment: models', () => { expect(result[1]).to.have.property('body', 'comment 20'); }); }); + it('should find an array of approved comments by asset id', () => { + return Comment.findByAssetId('123').then((result) => { + expect(result).to.have.length(1); + result.sort((a, b) => { + if (a.body < b.body) {return -1;} + else {return 1;} + }); + expect(result[0]).to.have.property('body', 'comment 20'); + }); + }); }); describe('#moderationQueue()', () => { diff --git a/tests/routes/api/comments/index.js b/tests/routes/api/comments/index.js index 5dc6d2d0b..35959a182 100644 --- a/tests/routes/api/comments/index.js +++ b/tests/routes/api/comments/index.js @@ -140,6 +140,30 @@ describe('Get moderation queues rejected, pending, flags', () => { }); }); + it('should return all the pending comments as pre moderated', function(done){ + chai.request(app) + .get('/api/v1/comments/status/pending') + .query({'moderation': 'pre'}) + .end(function(err, res){ + expect(err).to.be.null; + expect(res).to.have.status(200); + expect(res.body[0]).to.have.property('id', 'def'); + done(); + }); + }); + + it('should return all the pending comments as post moderated', function(done){ + chai.request(app) + .get('/api/v1/comments/status/pending') + .query({'moderation': 'post'}) + .end(function(err, res){ + expect(err).to.be.null; + expect(res).to.have.status(200); + expect(res.body).to.have.lengthOf(0); + done(); + }); + }); + it('should return all the flagged comments', function(done){ chai.request(app) .get('/api/v1/comments/action/flag')