From eae2a0285ba24b0b74bd8d4992cfc28bd52cd13a Mon Sep 17 00:00:00 2001 From: gaba Date: Wed, 9 Nov 2016 12:35:59 -0800 Subject: [PATCH] More tests and move settings stuff to routes and out of models. --- models/comment.js | 37 +++++++++++++++--------------------- routes/api/comments/index.js | 19 +++++++++--------- tests/models/comment.js | 19 +++++------------- 3 files changed, 30 insertions(+), 45 deletions(-) diff --git a/models/comment.js b/models/comment.js index a84219043..03f3363be 100644 --- a/models/comment.js +++ b/models/comment.js @@ -1,7 +1,6 @@ const mongoose = require('../mongoose'); const uuid = require('uuid'); const Action = require('./action'); -const Setting = require('./setting'); const Schema = mongoose.Schema; @@ -115,27 +114,21 @@ CommentSchema.statics.findByStatus = function(status) { * Find comments that need to be moderated (aka moderation queue). * @param {String} moderationValue pre or post moderation setting. If it is undefined then look at the settings. */ -CommentSchema.statics.moderationQueue = function(moderationValue) { - - return Setting.getModerationSetting().then(function({moderation}){ - if (typeof moderationValue === 'undefined' || moderationValue === undefined) { - moderationValue = moderation; - } - switch(moderationValue){ - // Pre-moderation: New comments are shown in the moderator queues immediately. - case 'pre': - return Comment.findByStatus('').then((comments) => { - return comments; - }); - // Post-moderation: New comments do not appear in moderation queues unless they are flagged by other users. - case 'post': - return Comment.findByStatusByActionType('', 'flag').then((comments) => { - return comments; - }); - default: - throw new Error('Moderation setting not found.'); - } - }); +CommentSchema.statics.moderationQueue = function(moderation) { + switch(moderation){ + // Pre-moderation: New comments are shown in the moderator queues immediately. + case 'pre': + return Comment.findByStatus('').then((comments) => { + return comments; + }); + // Post-moderation: New comments do not appear in moderation queues unless they are flagged by other users. + case 'post': + return Comment.findByStatusByActionType('', 'flag').then((comments) => { + return comments; + }); + default: + throw new Error('Moderation setting not found.'); + } }; //============================================================================== diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index c51db9f26..73ae3dea3 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -1,6 +1,8 @@ const express = require('express'); const Comment = require('../../../models/comment'); +const Setting = require('../../../models/setting'); + const router = express.Router(); //============================================================================== @@ -50,8 +52,14 @@ router.get('/status/rejected', (req, res, next) => { // Pre-moderation: New comments are shown in the moderator queues immediately. // Post-moderation: New comments do not appear in moderation queues unless they are flagged by other users. router.get('/status/pending', (req, res, next) => { - Comment.moderationQueue(req.query.moderation).then((comments) => { - res.status(200).json(comments); + Setting.getModerationSetting().then(function({moderation}){ + let moderationValue = req.query.moderation; + if (typeof moderationValue === 'undefined' || moderationValue === undefined) { + moderationValue = moderation; + } + Comment.moderationQueue(moderationValue).then((comments) => { + res.status(200).json(comments); + }); }).catch(error => { next(error); }); @@ -68,13 +76,6 @@ router.post('/', (req, res, next) => { }).catch(error => { next(error); }); - - // let comment = new Comment({body, author_id, asset_id, parent_id, status, username}); - // comment.save().then(({id}) => { - // res.status(200).send({'id': id}); - // }).catch(error => { - // next(error); - // }); }); router.post('/:comment_id', (req, res, next) => { diff --git a/tests/models/comment.js b/tests/models/comment.js index a76cea23e..0efd0c960 100644 --- a/tests/models/comment.js +++ b/tests/models/comment.js @@ -128,19 +128,10 @@ describe('Comment: models', () => { expect(result[0]).to.have.property('body', 'comment 30'); }); }); - it('should find an array of new comments to moderate when pre-moderation in settings', () => { - return Comment.moderationQueue().then((result) => { - expect(result).to.not.be.null; - expect(result).to.have.lengthOf(2); - }); - }); - it('should find an array of new comments to moderate when post-moderation in settings', () => { - return Comment.moderationQueue('post').then((result) => { - expect(result).to.not.be.null; - expect(result).to.have.lengthOf(1); - expect(result[0]).to.have.property('body', 'comment 30'); - }); - }); - it('should fail when the moderation is not pre or post'); + // it('should fail when the moderation is not pre or post', () => { + // return Comment.moderationQueue('any').catch(function(error) { + // expect(error).to.not.be.null; + // }); + // }); }); });