diff --git a/package.json b/package.json index b490ca856..4dc96b911 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ "babel-preset-es2015-minimal": "^2.1.0", "babel-preset-stage-0": "^6.16.0", "chai": "^3.5.0", + "chai-http": "^1.0.0", "copy-webpack-plugin": "^3.0.1", "eslint": "^3.9.1", "exports-loader": "^0.6.3", diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index 7a8202cba..c1dedcb68 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -22,34 +22,28 @@ router.get('/', (req, res) => { }); router.get('/:comment_id', (req, res, next) => { - Comment.findById(req.params.comment_id, function(err, comment) { - if(err) { - res.status(500); - return next(err); - } - res.status(200); - res.send(comment); - next(); + Comment.findById(req.params.comment_id).then((comment) => { + res.status(200).json(comment); + }).catch(error => { + next(error); }); }); router.post('/', (req, res, next) => { let comment = new Comment({ - body: req.query.comment, + body: req.query.body, author_id: req.query.author_id, asset_id: req.query.asset_id, parent_id: req.query.parent_id, status: req.query.status }); - comment.save(function(err, comment) { - if(err) { - res.status(500); - return next(err); - } - res.status(201); - res.send(comment.id); - next(); + comment.save().then(({id}) => { + res.status(201).send(id); + }).catch(error => { + console.log(error); + next(error); }); + }); router.put('/:comment_id', (req, res) => { diff --git a/tests/routes/api/comments/index.js b/tests/routes/api/comments/index.js index 21010d2ab..ad21e54ec 100644 --- a/tests/routes/api/comments/index.js +++ b/tests/routes/api/comments/index.js @@ -1,9 +1,19 @@ +process.env.NODE_ENV = 'test'; + require('../../../utils/mongoose'); +const app = require('../../../../app'); +const chai = require('chai'); +const chaiHttp = require('chai-http'); +chai.use(chaiHttp); +var expect = chai.expect; + + +const Comment = require('../../../../models/comment'); const Action = require('../../../../models/action'); const User = require('../../../../models/user'); -describe('Post a Comment: /comments', () => { +describe('Post /comments', () => { const users = [{ id: '123', display_name: 'John', @@ -26,6 +36,71 @@ describe('Post a Comment: /comments', () => { }) }) - it('it should create a comment') + it('it should create a comment', function(done) { + chai.request(app) + .post('/api/v1/comments') + .query({'body': 'Something body.', 'author_id': '123', 'asset_id': '1', 'parent_id': ''}) + .end(function(err, res){ + expect(res).to.have.status(201) + done() + }) + }) + +}) + +describe('Get /:comment_id', () => { + const comments = [{ + id: 'abc', + body: 'comment 10', + asset_id: 'asset', + author_id: '123' + },{ + id: 'def', + body: 'comment 20', + asset_id: 'asset', + author_id: '456' + },{ + id: 'hij', + body: 'comment 30', + asset_id: '456' + }] + + const users = [{ + id: '123', + display_name: 'John', + },{ + id: '456', + display_name: 'Paul', + }] + + const actions = [{ + action_type: 'flag', + item_id: 'abc' + },{ + action_type: 'like', + item_id: 'hij' + }] + + beforeEach(() => { + return Comment.create(comments).then(() => { + return User.create(users) + }).then(() => { + return Action.create(actions) + }) + }) + + it('should return the right comment for the comment_id', function(done){ + chai.request(app) + .get('/api/v1/comments') + .query({'comment_id': 'abc'}) + .end(function(err, res){ + expect(err).to.be.null; + expect(res).to.have.status(200); + if (err) return done(err); + done(); + }); + }) + + })