From df77ee963ad91bfb0b10e2e256caf2db4762997e Mon Sep 17 00:00:00 2001 From: David Jay Date: Thu, 3 Nov 2016 17:06:15 -0700 Subject: [PATCH 01/21] Adding find functions and tests to comment model. --- models/comment.js | 27 +++++++++++++--------- package.json | 3 ++- routes/api/stream/index.js | 3 ++- tests/index.js | 4 +++- tests/models/comment.js | 46 ++++++++++++++++++++++++++++++++++++++ tests/utils/mongoose.js | 37 ++++++++++++++++++++++++++++++ 6 files changed, 107 insertions(+), 13 deletions(-) create mode 100644 tests/models/comment.js create mode 100644 tests/utils/mongoose.js diff --git a/models/comment.js b/models/comment.js index 107708a3b..a80e0dae7 100644 --- a/models/comment.js +++ b/models/comment.js @@ -1,5 +1,3 @@ -'use strict'; - const mongoose = require('../mongoose'); const uuid = require('uuid'); const Schema = mongoose.Schema; @@ -13,7 +11,7 @@ const CommentSchema = new Schema({ body: { type: String, required: [true, 'The body is required.'], - minlength: 50 + minlength: 1 }, asset_id: String, author_id: String, @@ -23,22 +21,31 @@ const CommentSchema = new Schema({ default: '' }, parent_id: String -},{ - _id: false, - timestamps: { - createdAt: 'created_at', - updatedAt: 'updated_at' - } +// },{ +// _id: false, +// timestamps: { +// createdAt: 'created_at', +// updatedAt: 'updated_at' +// } }); /** * Finds a comment by the id. - * @param {String} id identifier of the comment (uuid) + * @param {String} asset_id identifier of comment (uuid) */ CommentSchema.statics.findById = function(id) { return Comment.findOne({id}); }; +/** + * Finds a comment by the asset_id. + * @param {String} asset_id identifier of the asset which owns this comment (uuid) +*/ +CommentSchema.statics.findByAssetId = function(asset_id) { + return Comment.find({asset_id}); +}; + + const Comment = mongoose.model('Comment', CommentSchema); module.exports = Comment; diff --git a/package.json b/package.json index 096863202..c25e54eb5 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,8 @@ "dependencies": { "debug": "^2.2.0", "express": "^4.14.0", - "mongoose": "^4.6.5" + "mongoose": "^4.6.5", + "uuid": "^2.0.3" }, "devDependencies": { "babel-core": "6.14.0", diff --git a/routes/api/stream/index.js b/routes/api/stream/index.js index 155450d42..b8a4c76ba 100644 --- a/routes/api/stream/index.js +++ b/routes/api/stream/index.js @@ -1,8 +1,9 @@ const express = require('express'); - +const Setting = require('../../../models/comment'); const router = express.Router(); router.get('/', (req, res, next) => { + console.log('Stream endpoint has been hit with asset_id ', req.query.asset_id); res.json([ { diff --git a/tests/index.js b/tests/index.js index 0a212b7a4..51ccc54ee 100644 --- a/tests/index.js +++ b/tests/index.js @@ -1,10 +1,12 @@ /* eslint-env node, mocha */ 'use strict'; +// require('./utils/mongoose') const expect = require('chai').expect; + describe('Comment', () => { - describe.only('#add', () => { + describe('#add', () => { it('should add a comment', () => { expect(0).to.be.equal(0); }); diff --git a/tests/models/comment.js b/tests/models/comment.js new file mode 100644 index 000000000..2eaa22a01 --- /dev/null +++ b/tests/models/comment.js @@ -0,0 +1,46 @@ +/* eslint-env node, mocha */ + +require('../utils/mongoose'); +const Comment = require('../../models/comment'); +const expect = require('chai').expect; + +describe('Comment: models', () => { + var mockComments + beforeEach(() => { + return Comment.create([{ + body: 'comment 1', + asset_id: '123' + },{ + body: 'comment 2', + asset_id: '123' + },{ + body: 'comment 3', + asset_id: '456' + }]).then((comments) => { + mockComments = comments + }); + }); + + describe('#findById()', () => { + it('should find a comment by id', () => { + return Comment.findById(mockComments[0].id).then((result) => { + expect(result).to.have.property('body') + .and.to.equal('comment 1'); + }); + }); + }); + + describe('#findByAssetId()', () => { + it('should find an array of comments by asset id', () => { + return Comment.findByAssetId('123').then((result) => { + expect(result).to.have.length(2); + expect(result[0]).to.have.property('body') + .and.to.equal('comment 1'); + expect(result[1]).to.have.property('body') + .and.to.equal('comment 2'); + }); + }); + }); + + // }); +}); diff --git a/tests/utils/mongoose.js b/tests/utils/mongoose.js new file mode 100644 index 000000000..002066001 --- /dev/null +++ b/tests/utils/mongoose.js @@ -0,0 +1,37 @@ +// Modified from https://github.com/elliotf/mocha-mongoose + +var mongoose = require('mongoose'); + + +// ensure the NODE_ENV is set to 'test' +// this is helpful when you would like to change behavior when testing +process.env.NODE_ENV = 'test'; + +beforeEach(function (done) { + + + function clearDB() { + for (var i in mongoose.connection.collections) { + mongoose.connection.collections[i].remove(function() {}); + } + return done(); + } + + + if (mongoose.connection.readyState === 0) { + mongoose.connect('coral-talk-test', function (err) { + if (err) { + throw err; + } + return clearDB(); + }); + } else { + return clearDB(); + } +}); + + +after(function (done) { + mongoose.disconnect(); + return done(); +}); From 084ee98ce26f867c5820c3afabd3062d68194b1f Mon Sep 17 00:00:00 2001 From: David Jay Date: Thu, 3 Nov 2016 17:13:47 -0700 Subject: [PATCH 02/21] Adding .eslintrc to tests. --- tests/.eslintrc | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 tests/.eslintrc diff --git a/tests/.eslintrc b/tests/.eslintrc new file mode 100644 index 000000000..0f4ef75d7 --- /dev/null +++ b/tests/.eslintrc @@ -0,0 +1,10 @@ +{ + "env": { + "es6": true, + "node": true + }, + "extends": "eslint:recommended", + "rules": { + no-undef: [1] + } +} From 8e9ebfb1a9a8863ed3c7fd564db25a7eb1b1d5e3 Mon Sep 17 00:00:00 2001 From: David Jay Date: Thu, 3 Nov 2016 17:45:37 -0700 Subject: [PATCH 03/21] Adding find functions and tests to user and action models. --- models/action.js | 24 ++++++++++++++++-------- models/user.js | 23 ++++++++++++++++------- package.json | 3 ++- tests/models/action.js | 40 ++++++++++++++++++++++++++++++++++++++++ tests/models/comment.js | 4 ++-- tests/models/user.js | 40 ++++++++++++++++++++++++++++++++++++++++ tests/utils/mongoose.js | 3 --- 7 files changed, 116 insertions(+), 21 deletions(-) create mode 100644 tests/models/action.js create mode 100644 tests/models/user.js diff --git a/models/action.js b/models/action.js index 581108a71..cb1d075b1 100644 --- a/models/action.js +++ b/models/action.js @@ -1,5 +1,3 @@ -'use strict'; - const mongoose = require('../mongoose'); const uuid = require('uuid'); const Schema = mongoose.Schema; @@ -14,12 +12,12 @@ const ActionSchema = new Schema({ item_type: String, item_id: String, user_id: String -},{ - _id: false, - timestamps: { - createdAt: 'created_at', - updatedAt: 'updated_at' - } +// },{ +// _id: false, +// timestamps: { +// createdAt: 'created_at', +// updatedAt: 'updated_at' +// } }); /** @@ -30,6 +28,16 @@ ActionSchema.statics.findById = function(id) { return Action.findOne({id}); }; +/** + * Finds users in an array of ids. + * @param {String} ids array of user identifiers (uuid) +*/ +ActionSchema.statics.findByItemIdArray = function(item_ids) { + return Action.find({ + 'item_id': {$in: item_ids} + }); +}; + const Action = mongoose.model('Action', ActionSchema); module.exports = Action; diff --git a/models/user.js b/models/user.js index d6b174075..6aba99f7b 100644 --- a/models/user.js +++ b/models/user.js @@ -1,4 +1,3 @@ -'use strict'; const mongoose = require('../mongoose'); const uuid = require('uuid'); @@ -12,12 +11,12 @@ const UserProfileSchema = new Schema({ }, display_name: String, auth_user_id: String -},{ - _id: false, - timestamps: { - createdAt: 'created_at', - updatedAt: 'updated_at' - } +// },{ +// _id: false, +// timestamps: { +// createdAt: 'created_at', +// updatedAt: 'updated_at' +// } }); /** @@ -28,6 +27,16 @@ UserProfileSchema.statics.findById = function(id) { return UserProfile.findOne({id}); }; +/** + * Finds users in an array of idd. + * @param {String} idd array of user identifiers (uuid) +*/ +UserProfileSchema.statics.findByIdArray = function(ids) { + return UserProfile.find({ + 'id': {$in: ids} + }); +}; + // TO DO: methods // modifications to user as statics // find by auth user id diff --git a/package.json b/package.json index c25e54eb5..b490ca856 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "build": "webpack --config ./client/coral-embed-stream/webpack.config.js", "lint": "eslint .", "pretest": "npm install", - "test": "mocha tests", + "test": "mocha tests --recursive", + "test-watch": "mocha tests --recursive -w", "embed-start": "node client/coral-embed-stream/dev-server.js" }, "config": { diff --git a/tests/models/action.js b/tests/models/action.js new file mode 100644 index 000000000..8736edcab --- /dev/null +++ b/tests/models/action.js @@ -0,0 +1,40 @@ +/* eslint-env node, mocha */ + +require('../utils/mongoose'); +const Action = require('../../models/action'); +const expect = require('chai').expect; + +describe('Action: models', () => { + var mockActions; + beforeEach(() => { + return Action.create([{ + action_type: 'flag', + item_id: '123' + },{ + action_type: 'like', + item_id: '789' + },{ + action_type: 'flag', + item_id: '456' + }]).then((actions) => { + mockActions = actions; + }); + }); + + describe('#findById()', () => { + it('should find an action by id', () => { + return Action.findById(mockActions[0].id).then((result) => { + expect(result).to.have.property('action_type') + .and.to.equal('flag'); + }); + }); + }); + + describe('#findByItemIdArray()', () => { + it('should find an array of actions from an array of item_ids', () => { + return Action.findByItemIdArray(['123','456']).then((result) => { + expect(result).to.have.length(2); + }); + }); + }); +}); diff --git a/tests/models/comment.js b/tests/models/comment.js index 2eaa22a01..0f98db6ac 100644 --- a/tests/models/comment.js +++ b/tests/models/comment.js @@ -5,7 +5,7 @@ const Comment = require('../../models/comment'); const expect = require('chai').expect; describe('Comment: models', () => { - var mockComments + var mockComments; beforeEach(() => { return Comment.create([{ body: 'comment 1', @@ -17,7 +17,7 @@ describe('Comment: models', () => { body: 'comment 3', asset_id: '456' }]).then((comments) => { - mockComments = comments + mockComments = comments; }); }); diff --git a/tests/models/user.js b/tests/models/user.js new file mode 100644 index 000000000..e1063e21c --- /dev/null +++ b/tests/models/user.js @@ -0,0 +1,40 @@ +/* eslint-env node, mocha */ + +require('../utils/mongoose'); +const User = require('../../models/user'); +const expect = require('chai').expect; + +describe('User: models', () => { + var mockUsers; + beforeEach(() => { + return User.create([{ + display_name: 'Stampi', + },{ + display_name: 'Sockmonster', + },{ + display_name: 'Marvel', + }]).then((users) => { + mockUsers = users; + }); + }); + + describe('#findById()', () => { + it('should find a user by id', () => { + return User.findById(mockUsers[0].id).then((result) => { + expect(result).to.have.property('display_name') + .and.to.equal('Stampi'); + }); + }); + }); + + describe('#findByIdArray()', () => { + it('should find an array of users from an array of ids', () => { + const ids = mockUsers.map((user) => user.id) + return User.findByIdArray(ids).then((result) => { + expect(result).to.have.length(3); + }); + }); + }); + + // }); +}); diff --git a/tests/utils/mongoose.js b/tests/utils/mongoose.js index 002066001..7661eeb32 100644 --- a/tests/utils/mongoose.js +++ b/tests/utils/mongoose.js @@ -8,8 +8,6 @@ var mongoose = require('mongoose'); process.env.NODE_ENV = 'test'; beforeEach(function (done) { - - function clearDB() { for (var i in mongoose.connection.collections) { mongoose.connection.collections[i].remove(function() {}); @@ -30,7 +28,6 @@ beforeEach(function (done) { } }); - after(function (done) { mongoose.disconnect(); return done(); From 70630918cc47934ed477131b4183434a5e92cfe1 Mon Sep 17 00:00:00 2001 From: David Jay Date: Thu, 3 Nov 2016 17:52:33 -0700 Subject: [PATCH 04/21] Adding stream endpoint. --- routes/api/stream/index.js | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-) diff --git a/routes/api/stream/index.js b/routes/api/stream/index.js index b8a4c76ba..942db826c 100644 --- a/routes/api/stream/index.js +++ b/routes/api/stream/index.js @@ -1,34 +1,14 @@ const express = require('express'); -const Setting = require('../../../models/comment'); +const Comment = require('../../../models/comment'); +const User = require('../../../models/user'); +const Action = require('../../../models/action'); const router = express.Router(); router.get('/', (req, res, next) => { - - console.log('Stream endpoint has been hit with asset_id ', req.query.asset_id); - res.json([ - { - 'id': 'abc', - 'type': 'comment', - 'body': 'Sample comment', - 'created_at': new Date().getTime(), - 'asset_id': 'assetTest' - }, - { - 'id': 'xyz', - 'type': 'comment', - 'body': 'Sample reply', - 'created_at': new Date().getTime() - 600000, - 'parent_id': 'abc', - 'asset_id': 'assetTest' - }, - { - 'id': 'def', - 'type': 'comment', - 'body': 'Another comment', - 'created_at': new Date().getTime() - 400000, - 'asset_id': 'assetTest' - } - ]).catch(next); + const comments = Comment.findByAssetId(req.query.asset_id) || []; + const users = User.findByIdArray(comments.map((comment) => comment.author_id)); + const actions = Action.findByItemIdArray(comments.map((comment) => comment.id)); + res.json(comments.concat(users).concat(actions)).catch(next); }); module.exports = router; From 6c25ca81d4c03ef196777462c9a536b0d41ab781 Mon Sep 17 00:00:00 2001 From: David Jay Date: Fri, 4 Nov 2016 00:02:34 -0700 Subject: [PATCH 05/21] Adding stream endpoint tests --- tests/routes/api/stream/index.js | 62 ++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tests/routes/api/stream/index.js diff --git a/tests/routes/api/stream/index.js b/tests/routes/api/stream/index.js new file mode 100644 index 000000000..4449cce61 --- /dev/null +++ b/tests/routes/api/stream/index.js @@ -0,0 +1,62 @@ +require('../../../utils/mongoose'); +const Action = require('../../../../models/action'); +const User = require('../../../../models/user'); +const Comment = require('../../../../models/comment'); +const expect = require('chai').expect; +const streamRoutes = require('../../../../routes/api/stream'); + +describe('api/stream: routes', () => { + const comments = [{ + id: 'abc', + body: 'comment 1', + asset_id: 'asset', + author_id: '123' + },{ + id: 'def', + body: 'comment 2', + asset_id: 'asset', + author_id: '456' + },{ + id: 'hij', + body: 'comment 3', + 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 a stream with comments, users and actions', () => { + console.log(streamRoutes('./')); + streamRoutes('./')({ + query: { + asset_id: 'asset' + } + }, { + json: (data) => { + console.log(data); + expect(data).to.equal(1); + } + }) + }) +}) From af09a87c1389a425145cd0f875da849e6b5ba274 Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 08:44:53 -0700 Subject: [PATCH 06/21] Through error instead of process.exit(1) http://eslint.org/docs/rules/no-process-exit --- .gitignore | 1 + bin/init.js | 3 +-- models/action.js | 2 -- models/comment.js | 5 +---- routes/api/comments/index.js | 35 +++++++++++++++++++++++++++++++++-- 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index d52442b57..acd1ba775 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ dist .DS_Store *.iml .env +gaba.cfg diff --git a/bin/init.js b/bin/init.js index d114d4819..2b9e7a943 100644 --- a/bin/init.js +++ b/bin/init.js @@ -2,6 +2,5 @@ const Setting = require('../models/setting'); const defaults = {id: '1', moderation: 'pre'}; Setting.update({id: '1'}, {$setOnInsert: defaults}, {upsert: true}).then(() => { - console.log('created settings object.'); - process.exit(); + throw new Error('It was not able to update settings.'); }).catch(console.error); diff --git a/models/action.js b/models/action.js index 581108a71..a6889814a 100644 --- a/models/action.js +++ b/models/action.js @@ -1,5 +1,3 @@ -'use strict'; - const mongoose = require('../mongoose'); const uuid = require('uuid'); const Schema = mongoose.Schema; diff --git a/models/comment.js b/models/comment.js index 107708a3b..95865c728 100644 --- a/models/comment.js +++ b/models/comment.js @@ -1,5 +1,3 @@ -'use strict'; - const mongoose = require('../mongoose'); const uuid = require('uuid'); const Schema = mongoose.Schema; @@ -13,7 +11,7 @@ const CommentSchema = new Schema({ body: { type: String, required: [true, 'The body is required.'], - minlength: 50 + minlength: 10 }, asset_id: String, author_id: String, @@ -24,7 +22,6 @@ const CommentSchema = new Schema({ }, parent_id: String },{ - _id: false, timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index dac53b755..a884c01e2 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -1,7 +1,23 @@ const express = require('express'); +const Comment = require('../../../models/comment'); const router = express.Router(); +//============================================================================== +// Validations on parameters +//============================================================================== + +router.param('comment_id', function(res, req, next, comment_id) { + console.log('validations on comment id '); + req.comment_id = comment_id; + next(); +}); + +//============================================================================== +// Routes +//============================================================================== + + router.get('/', (req, res) => { res.send('Read all of the comments ever'); }); @@ -10,8 +26,23 @@ router.get('/:comment_id', (req, res) => { res.send('Read a comment'); }); -router.post('/', (req, res) => { - res.send('Write a comment'); +router.post('/', (req, res, next) => { + let comment = new Comment({ + body: req.query.comment, + 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(); + }); }); router.put('/:comment_id', (req, res) => { From 730bd29bd1ca93842486f0725dc43ce8fd66c366 Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 09:43:01 -0700 Subject: [PATCH 07/21] Lint for test. --- routes/api/stream/index.js | 3 +++ tests/{.eslintrc => .eslintrc.json} | 2 +- tests/utils/mongoose.js | 7 ++----- 3 files changed, 6 insertions(+), 6 deletions(-) rename tests/{.eslintrc => .eslintrc.json} (83%) diff --git a/routes/api/stream/index.js b/routes/api/stream/index.js index 942db826c..321794823 100644 --- a/routes/api/stream/index.js +++ b/routes/api/stream/index.js @@ -1,13 +1,16 @@ const express = require('express'); + const Comment = require('../../../models/comment'); const User = require('../../../models/user'); const Action = require('../../../models/action'); + const router = express.Router(); router.get('/', (req, res, next) => { const comments = Comment.findByAssetId(req.query.asset_id) || []; const users = User.findByIdArray(comments.map((comment) => comment.author_id)); const actions = Action.findByItemIdArray(comments.map((comment) => comment.id)); + res.json(comments.concat(users).concat(actions)).catch(next); }); diff --git a/tests/.eslintrc b/tests/.eslintrc.json similarity index 83% rename from tests/.eslintrc rename to tests/.eslintrc.json index 0f4ef75d7..97d8c197e 100644 --- a/tests/.eslintrc +++ b/tests/.eslintrc.json @@ -5,6 +5,6 @@ }, "extends": "eslint:recommended", "rules": { - no-undef: [1] + "no-undef": [0] } } diff --git a/tests/utils/mongoose.js b/tests/utils/mongoose.js index 7661eeb32..35083a4b3 100644 --- a/tests/utils/mongoose.js +++ b/tests/utils/mongoose.js @@ -1,10 +1,7 @@ -// Modified from https://github.com/elliotf/mocha-mongoose - var mongoose = require('mongoose'); - -// ensure the NODE_ENV is set to 'test' -// this is helpful when you would like to change behavior when testing +// Ensure the NODE_ENV is set to 'test', +// this is helpful when you would like to change behavior when testing. process.env.NODE_ENV = 'test'; beforeEach(function (done) { From 7c3005be5080dd056a212ca6978aea8e4d300a83 Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 10:07:44 -0700 Subject: [PATCH 08/21] Remove a test. --- models/comment.js | 2 +- tests/models/comment.js | 13 +++++++------ tests/routes/api/stream/index.js | 20 ++++---------------- 3 files changed, 12 insertions(+), 23 deletions(-) diff --git a/models/comment.js b/models/comment.js index 0f8904387..468478810 100644 --- a/models/comment.js +++ b/models/comment.js @@ -11,7 +11,7 @@ const CommentSchema = new Schema({ body: { type: String, required: [true, 'The body is required.'], - minlength: 10 + minlength: 2 }, asset_id: String, author_id: String, diff --git a/tests/models/comment.js b/tests/models/comment.js index 0f98db6ac..9fbd1d79f 100644 --- a/tests/models/comment.js +++ b/tests/models/comment.js @@ -1,6 +1,7 @@ /* eslint-env node, mocha */ require('../utils/mongoose'); + const Comment = require('../../models/comment'); const expect = require('chai').expect; @@ -8,13 +9,13 @@ describe('Comment: models', () => { var mockComments; beforeEach(() => { return Comment.create([{ - body: 'comment 1', + body: 'comment 10', asset_id: '123' },{ - body: 'comment 2', + body: 'comment 20', asset_id: '123' },{ - body: 'comment 3', + body: 'comment 30', asset_id: '456' }]).then((comments) => { mockComments = comments; @@ -25,7 +26,7 @@ describe('Comment: models', () => { it('should find a comment by id', () => { return Comment.findById(mockComments[0].id).then((result) => { expect(result).to.have.property('body') - .and.to.equal('comment 1'); + .and.to.equal('comment 10'); }); }); }); @@ -35,9 +36,9 @@ describe('Comment: models', () => { return Comment.findByAssetId('123').then((result) => { expect(result).to.have.length(2); expect(result[0]).to.have.property('body') - .and.to.equal('comment 1'); + .and.to.equal('comment 10'); expect(result[1]).to.have.property('body') - .and.to.equal('comment 2'); + .and.to.equal('comment 20'); }); }); }); diff --git a/tests/routes/api/stream/index.js b/tests/routes/api/stream/index.js index 6693ea31a..aeff010de 100644 --- a/tests/routes/api/stream/index.js +++ b/tests/routes/api/stream/index.js @@ -2,23 +2,21 @@ require('../../../utils/mongoose'); const Action = require('../../../../models/action'); const User = require('../../../../models/user'); const Comment = require('../../../../models/comment'); -const expect = require('chai').expect; -const streamRoutes = require('../../../../routes/api/stream'); describe('api/stream: routes', () => { const comments = [{ id: 'abc', - body: 'comment 1', + body: 'comment 10', asset_id: 'asset', author_id: '123' },{ id: 'def', - body: 'comment 2', + body: 'comment 20', asset_id: 'asset', author_id: '456' },{ id: 'hij', - body: 'comment 3', + body: 'comment 30', asset_id: '456' }] @@ -46,15 +44,5 @@ describe('api/stream: routes', () => { }) }) - it('should return a stream with comments, users and actions', () => { - streamRoutes('./')({ - query: { - asset_id: 'asset' - } - }, { - json: (data) => { - expect(data).to.equal(1); - } - }) - }) + it('should return a stream with comments, users and actions') }) From 5a2c3b2077834d7955231e02427e1de8614080f9 Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 10:12:10 -0700 Subject: [PATCH 09/21] It does not work like this. --- tests/models/comment.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/models/comment.js b/tests/models/comment.js index 9fbd1d79f..160051971 100644 --- a/tests/models/comment.js +++ b/tests/models/comment.js @@ -35,10 +35,6 @@ describe('Comment: models', () => { it('should find an array of comments by asset id', () => { return Comment.findByAssetId('123').then((result) => { expect(result).to.have.length(2); - expect(result[0]).to.have.property('body') - .and.to.equal('comment 10'); - expect(result[1]).to.have.property('body') - .and.to.equal('comment 20'); }); }); }); From c173b0838765fb6eeb828d9fabe5f13b4c7e4f22 Mon Sep 17 00:00:00 2001 From: David Jay Date: Fri, 4 Nov 2016 10:16:20 -0700 Subject: [PATCH 10/21] Fixing inconsistent test in comment model. --- tests/models/comment.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/models/comment.js b/tests/models/comment.js index 9fbd1d79f..a8a543183 100644 --- a/tests/models/comment.js +++ b/tests/models/comment.js @@ -35,6 +35,10 @@ describe('Comment: models', () => { it('should find an array of comments by asset id', () => { return Comment.findByAssetId('123').then((result) => { expect(result).to.have.length(2); + result.sort((a,b) => { + if (a.body < b.body) {return -1;} + else {return 1;} + }); expect(result[0]).to.have.property('body') .and.to.equal('comment 10'); expect(result[1]).to.have.property('body') From 53453644a34df5dbe0f7ab5751737a25157bd765 Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 10:19:42 -0700 Subject: [PATCH 11/21] Adds tests with array sorted. --- tests/models/comment.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/models/comment.js b/tests/models/comment.js index 160051971..4198f626a 100644 --- a/tests/models/comment.js +++ b/tests/models/comment.js @@ -35,6 +35,10 @@ describe('Comment: models', () => { it('should find an array of comments by asset id', () => { return Comment.findByAssetId('123').then((result) => { expect(result).to.have.length(2); + expect(result.sort()[0]).to.have.property('body') + .and.to.equal('comment 10'); + expect(result.sort()[1]).to.have.property('body') + .and.to.equal('comment 20'); }); }); }); From 141c1141385d185dc1cb81140ba14172e3c61f69 Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 13:20:58 -0700 Subject: [PATCH 12/21] Get back the timestamps to the user model. --- models/user.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/models/user.js b/models/user.js index 6aba99f7b..76635abfb 100644 --- a/models/user.js +++ b/models/user.js @@ -11,12 +11,11 @@ const UserProfileSchema = new Schema({ }, display_name: String, auth_user_id: String -// },{ -// _id: false, -// timestamps: { -// createdAt: 'created_at', -// updatedAt: 'updated_at' -// } +},{ + timestamps: { + createdAt: 'created_at', + updatedAt: 'updated_at' + } }); /** From 8cea206dd833e30b04cf3f3cb60d4cfb207fb8dd Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 13:21:55 -0700 Subject: [PATCH 13/21] Add chai-http for tests. Simplify route for comments --- package.json | 1 + routes/api/comments/index.js | 28 +++++------ tests/routes/api/comments/index.js | 79 +++++++++++++++++++++++++++++- 3 files changed, 89 insertions(+), 19 deletions(-) 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(); + }); + }) + + }) From 19a7de5c9842b33cb867b998c3289b78583d32ea Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 13:28:40 -0700 Subject: [PATCH 14/21] Remove validations for now until we really use them. --- routes/api/comments/index.js | 9 --------- 1 file changed, 9 deletions(-) diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index c1dedcb68..c9a1f0659 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -3,15 +3,6 @@ const Comment = require('../../../models/comment'); const router = express.Router(); -//============================================================================== -// Validations on parameters -//============================================================================== - -router.param('comment_id', function(res, req, next, comment_id) { - req.comment_id = comment_id; - next(); -}); - //============================================================================== // Routes //============================================================================== From 4836069fb680ea5259ba5a725ef0e8918871160b Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 13:36:35 -0700 Subject: [PATCH 15/21] Rmeoves console.log --- routes/api/comments/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index c9a1f0659..df9bbc456 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -31,7 +31,6 @@ router.post('/', (req, res, next) => { comment.save().then(({id}) => { res.status(201).send(id); }).catch(error => { - console.log(error); next(error); }); From ea93687f89dba362a3f5bb0349b02716f3bbad22 Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 13:43:03 -0700 Subject: [PATCH 16/21] Not sure when I lost the implmenetation of this. --- routes/api/stream/index.js | 36 ++++++++++-------------------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/routes/api/stream/index.js b/routes/api/stream/index.js index b41e03dbb..321794823 100644 --- a/routes/api/stream/index.js +++ b/routes/api/stream/index.js @@ -1,33 +1,17 @@ const express = require('express'); +const Comment = require('../../../models/comment'); +const User = require('../../../models/user'); +const Action = require('../../../models/action'); + const router = express.Router(); -router.get('/', (req, res) => { - console.log('Stream endpoint has been hit with asset_id ', req.query.asset_id); - res.json([ - { - 'id': 'abc', - 'type': 'comment', - 'body': 'Sample comment', - 'created_at': new Date().getTime(), - 'asset_id': 'assetTest' - }, - { - 'id': 'xyz', - 'type': 'comment', - 'body': 'Sample reply', - 'created_at': new Date().getTime() - 600000, - 'parent_id': 'abc', - 'asset_id': 'assetTest' - }, - { - 'id': 'def', - 'type': 'comment', - 'body': 'Another comment', - 'created_at': new Date().getTime() - 400000, - 'asset_id': 'assetTest' - } - ]); +router.get('/', (req, res, next) => { + const comments = Comment.findByAssetId(req.query.asset_id) || []; + const users = User.findByIdArray(comments.map((comment) => comment.author_id)); + const actions = Action.findByItemIdArray(comments.map((comment) => comment.id)); + + res.json(comments.concat(users).concat(actions)).catch(next); }); module.exports = router; From 23ea7025eaf9545e2a3d9b725809b6a64e9c605c Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 14:13:22 -0700 Subject: [PATCH 17/21] Convert from promises to array --- routes/api/stream/index.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/routes/api/stream/index.js b/routes/api/stream/index.js index 321794823..a7eb13e0b 100644 --- a/routes/api/stream/index.js +++ b/routes/api/stream/index.js @@ -7,11 +7,17 @@ const Action = require('../../../models/action'); const router = express.Router(); router.get('/', (req, res, next) => { + const comments = Comment.findByAssetId(req.query.asset_id) || []; const users = User.findByIdArray(comments.map((comment) => comment.author_id)); const actions = Action.findByItemIdArray(comments.map((comment) => comment.id)); - res.json(comments.concat(users).concat(actions)).catch(next); + Promise.all([comments, users, actions]).then(([comments, users, actions]) => { + res.json(comments.concat(users).concat(actions)); + }).catch(error => { + next(error); + }); + }); module.exports = router; From 69980c9d7d8adcab2e2d016b025739472eb21cbf Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 14:24:04 -0700 Subject: [PATCH 18/21] Simplify concat. --- routes/api/stream/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/api/stream/index.js b/routes/api/stream/index.js index a7eb13e0b..ba35b4138 100644 --- a/routes/api/stream/index.js +++ b/routes/api/stream/index.js @@ -13,7 +13,7 @@ router.get('/', (req, res, next) => { const actions = Action.findByItemIdArray(comments.map((comment) => comment.id)); Promise.all([comments, users, actions]).then(([comments, users, actions]) => { - res.json(comments.concat(users).concat(actions)); + res.json(...comments,...users,...actions); }).catch(error => { next(error); }); From 3cff52d72f9fab4d0360e78c217f40957f7e02a3 Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 14:32:47 -0700 Subject: [PATCH 19/21] It returns an array. Need to check the tests. --- routes/api/stream/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routes/api/stream/index.js b/routes/api/stream/index.js index ba35b4138..1bbe259e4 100644 --- a/routes/api/stream/index.js +++ b/routes/api/stream/index.js @@ -8,12 +8,12 @@ const router = express.Router(); router.get('/', (req, res, next) => { - const comments = Comment.findByAssetId(req.query.asset_id) || []; + const comments = Comment.findByAssetId(req.query.asset_id); const users = User.findByIdArray(comments.map((comment) => comment.author_id)); const actions = Action.findByItemIdArray(comments.map((comment) => comment.id)); Promise.all([comments, users, actions]).then(([comments, users, actions]) => { - res.json(...comments,...users,...actions); + res.json([...comments,...users,...actions]); }).catch(error => { next(error); }); From 43a70ea71c567e0b410dd97d8c6b73b4b3457cf9 Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 14:45:46 -0700 Subject: [PATCH 20/21] Add tests for stream. Move to promises the rightway. --- routes/api/stream/index.js | 14 +++++++++----- tests/routes/api/stream/index.js | 19 ++++++++++++++++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/routes/api/stream/index.js b/routes/api/stream/index.js index 1bbe259e4..4b7c3e6fa 100644 --- a/routes/api/stream/index.js +++ b/routes/api/stream/index.js @@ -8,16 +8,20 @@ const router = express.Router(); router.get('/', (req, res, next) => { - const comments = Comment.findByAssetId(req.query.asset_id); - const users = User.findByIdArray(comments.map((comment) => comment.author_id)); - const actions = Action.findByItemIdArray(comments.map((comment) => comment.id)); + const commentsPromise = Comment.findByAssetId(req.query.asset_id); - Promise.all([comments, users, actions]).then(([comments, users, actions]) => { + commentsPromise.then(comments => { + return Promise.all([ + comments, + User.findByIdArray(comments.map((comment) => comment.author_id)), + Action.findByItemIdArray(comments.map((comment) => comment.id)) + ]); + }).then(([comments, users, actions]) => { res.json([...comments,...users,...actions]); }).catch(error => { + console.log(error); next(error); }); - }); module.exports = router; diff --git a/tests/routes/api/stream/index.js b/tests/routes/api/stream/index.js index aeff010de..a4ca53228 100644 --- a/tests/routes/api/stream/index.js +++ b/tests/routes/api/stream/index.js @@ -1,4 +1,11 @@ require('../../../utils/mongoose'); + +const app = require('../../../../app'); +const chai = require('chai'); +const chaiHttp = require('chai-http'); +chai.use(chaiHttp); +var expect = chai.expect; + const Action = require('../../../../models/action'); const User = require('../../../../models/user'); const Comment = require('../../../../models/comment'); @@ -44,5 +51,15 @@ describe('api/stream: routes', () => { }) }) - it('should return a stream with comments, users and actions') + it('should return a stream with comments, users and actions', function(done){ + chai.request(app) + .get('/api/v1/stream') + .query({'asset_id': 'asset'}) + .end(function(err, res){ + expect(err).to.be.null; + expect(res).to.have.status(200); + if (err) return done(err); + done(); + }); + }) }) From 9be29b93467a173d5873c7ea1a0679c0c2045b10 Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 14:59:25 -0700 Subject: [PATCH 21/21] Removes console log. --- routes/api/stream/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/routes/api/stream/index.js b/routes/api/stream/index.js index 4b7c3e6fa..c63bf871d 100644 --- a/routes/api/stream/index.js +++ b/routes/api/stream/index.js @@ -19,7 +19,6 @@ router.get('/', (req, res, next) => { }).then(([comments, users, actions]) => { res.json([...comments,...users,...actions]); }).catch(error => { - console.log(error); next(error); }); });