From 7facdf08402658e83cf5ad585cbb5de661521baf Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Thu, 3 Nov 2016 16:34:01 -0600 Subject: [PATCH 01/38] make the settings routes actually work --- app.js | 4 ++++ models/setting.js | 12 +++++++++++- package.json | 1 + routes/api/settings/index.js | 4 ++-- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app.js b/app.js index e7ded2f9c..aa75e5c31 100644 --- a/app.js +++ b/app.js @@ -2,9 +2,13 @@ // Initialize application framework. const express = require('express'); +const bodyParser = require('body-parser'); const app = express(); +app.use(bodyParser.urlencoded({extended: true})); +app.use(bodyParser.json()); + app.use('/api/v1', require('./routes/api')); module.exports = app; diff --git a/models/setting.js b/models/setting.js index dd82b3e42..59de7374e 100644 --- a/models/setting.js +++ b/models/setting.js @@ -12,12 +12,22 @@ const SettingSchema = new Schema({ } }); +/** + * gets the entire settings record and sends it back + * @return {Promise} settings the whole settings record + */ SettingSchema.statics.getSettings = function () { - return this.findOne(); + return this.findOne({id: '1'}); }; +/** + * this will update the settings object with whatever you pass in + * @param {object} setting a hash of whatever settings you want to update + * @return {Promise} settings Promise that resolves to the entire (updated) settings object. + */ SettingSchema.statics.updateSettings = function (setting) { // there should only ever be one record unless something has gone wrong. + console.log('new setting', setting); return this.findOneAndUpdate({id: '1'}, {$set: setting}, {new: true}); }; diff --git a/package.json b/package.json index b7fb48d34..7d561d926 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ }, "homepage": "https://github.com/coralproject/talk#readme", "dependencies": { + "body-parser": "^1.15.2", "debug": "^2.2.0", "express": "^4.14.0", "mongoose": "^4.6.5" diff --git a/routes/api/settings/index.js b/routes/api/settings/index.js index fa5d3a620..cda9a3806 100644 --- a/routes/api/settings/index.js +++ b/routes/api/settings/index.js @@ -3,11 +3,11 @@ const router = express.Router(); const Setting = require('../../../models/setting'); router.get('/', (req, res, next) => { - Setting.getSettings().then(res.json).catch(next); + Setting.getSettings().then(res.json.bind(res)).catch(next); }); router.put('/', (req, res, next) => { - Setting.updateSettings(req.body).then(res.json).catch(next); + Setting.updateSettings(req.body).then(res.json.bind(res)).catch(next); }); module.exports = router; From de405492b9dffaee5f88ae4f1e68cb4a30bbc2f6 Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Thu, 3 Nov 2016 16:36:33 -0600 Subject: [PATCH 02/38] remove log --- models/setting.js | 1 - 1 file changed, 1 deletion(-) diff --git a/models/setting.js b/models/setting.js index 59de7374e..b47b6e9e2 100644 --- a/models/setting.js +++ b/models/setting.js @@ -27,7 +27,6 @@ SettingSchema.statics.getSettings = function () { */ SettingSchema.statics.updateSettings = function (setting) { // there should only ever be one record unless something has gone wrong. - console.log('new setting', setting); return this.findOneAndUpdate({id: '1'}, {$set: setting}, {new: true}); }; From 81e16f8a8d3f4b393bc9c820a17c2e9f7cba695d Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Thu, 3 Nov 2016 16:47:18 -0600 Subject: [PATCH 03/38] don't bind res.json --- routes/api/settings/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routes/api/settings/index.js b/routes/api/settings/index.js index cda9a3806..5cb7bcc01 100644 --- a/routes/api/settings/index.js +++ b/routes/api/settings/index.js @@ -3,11 +3,11 @@ const router = express.Router(); const Setting = require('../../../models/setting'); router.get('/', (req, res, next) => { - Setting.getSettings().then(res.json.bind(res)).catch(next); + Setting.getSettings().then(settings => res.json(settings)).catch(next); }); router.put('/', (req, res, next) => { - Setting.updateSettings(req.body).then(res.json.bind(res)).catch(next); + Setting.updateSettings(req.body).then(settings => res.json(settings)).catch(next); }); module.exports = router; From d28f46f9caf6227c5f75c0cae49fa03a0e1aa179 Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Thu, 3 Nov 2016 16:49:03 -0600 Subject: [PATCH 04/38] disallow url-encoded payloads. --- app.js | 1 - 1 file changed, 1 deletion(-) diff --git a/app.js b/app.js index aa75e5c31..8d32c7ec2 100644 --- a/app.js +++ b/app.js @@ -6,7 +6,6 @@ const bodyParser = require('body-parser'); const app = express(); -app.use(bodyParser.urlencoded({extended: true})); app.use(bodyParser.json()); app.use('/api/v1', require('./routes/api')); From df77ee963ad91bfb0b10e2e256caf2db4762997e Mon Sep 17 00:00:00 2001 From: David Jay Date: Thu, 3 Nov 2016 17:06:15 -0700 Subject: [PATCH 05/38] 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 06/38] 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 07/38] 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 08/38] 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 09/38] 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 877200cf1908da2edbc0178f4686b060d05fc776 Mon Sep 17 00:00:00 2001 From: Belen Curcio Date: Fri, 4 Nov 2016 10:51:08 -0300 Subject: [PATCH 10/38] settings schema swagger --- swagger.yaml | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/swagger.yaml b/swagger.yaml index a7fdc973d..50df1ff4b 100644 --- a/swagger.yaml +++ b/swagger.yaml @@ -150,14 +150,22 @@ paths: description: Settings responses: 200: - description: OK + description: Get Setting + schema: + type: array + items: + $ref: '#/definitions/Setting' put: tags: - Settings description: Settings responses: 204: - description: OK + description: Updated Setting + schema: + type: array + items: + $ref: '#/definitions/Setting' @@ -212,3 +220,21 @@ definitions: type: string user_id: type: string + Setting: + type: object + properties: + id: + type: string + moderation: + type: string + enum: + - pre + - post + created_at: + type: string + format: date-time + description: Creation Date-Time + updated_at: + type: string + format: date-time + description: Updated Date-Time From 024e902fbe15a7181b9233b3c95b3a34ce8e4a56 Mon Sep 17 00:00:00 2001 From: Belen Curcio Date: Fri, 4 Nov 2016 12:25:24 -0300 Subject: [PATCH 11/38] =?UTF-8?q?=C3=BAse=20process=20exit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .eslintrc.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.eslintrc.json b/.eslintrc.json index eec982dac..91ae8d713 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -35,7 +35,7 @@ "no-throw-literal": [2], "yoda": [1], "no-path-concat": [2], - "no-process-exit": [2], + "no-process-exit": [0], "eol-last": [1], "no-continue": [1], "no-nested-ternary": [1], From af09a87c1389a425145cd0f875da849e6b5ba274 Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 08:44:53 -0700 Subject: [PATCH 12/38] 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 0cc5bdb04f71dd4556b3a1f654e994dbaee34ddc Mon Sep 17 00:00:00 2001 From: Belen Curcio Date: Fri, 4 Nov 2016 12:47:56 -0300 Subject: [PATCH 13/38] throwing --- .eslintrc.json | 2 +- bin/init.js | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 91ae8d713..eec982dac 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -35,7 +35,7 @@ "no-throw-literal": [2], "yoda": [1], "no-path-concat": [2], - "no-process-exit": [0], + "no-process-exit": [2], "eol-last": [1], "no-continue": [1], "no-nested-ternary": [1], diff --git a/bin/init.js b/bin/init.js index d114d4819..4f4f1eb3a 100644 --- a/bin/init.js +++ b/bin/init.js @@ -1,7 +1,13 @@ 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(); -}).catch(console.error); +try { + Setting.update({id: '1'}, {$setOnInsert: defaults}, {upsert: true}) + .then(() => { + console.log('Created settings object.'); + }).catch((err) => { + if (err) throw err; + }); +} catch (err) { + console.log('Cannot create settings object.'); +} From 3ea22d0ecee85391c6f54e48c291d29ad9637880 Mon Sep 17 00:00:00 2001 From: Belen Curcio Date: Fri, 4 Nov 2016 13:02:47 -0300 Subject: [PATCH 14/38] Console Error --- bin/init.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/init.js b/bin/init.js index 4f4f1eb3a..4c859f498 100644 --- a/bin/init.js +++ b/bin/init.js @@ -9,5 +9,5 @@ try { if (err) throw err; }); } catch (err) { - console.log('Cannot create settings object.'); + console.error('Cannot create settings object. ', err); } From 1876d38089837575fcd142ca6274879ccc24de4b Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Fri, 4 Nov 2016 10:09:44 -0600 Subject: [PATCH 15/38] disconnect mongoose --- bin/init.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/bin/init.js b/bin/init.js index 4c859f498..0efa24c48 100644 --- a/bin/init.js +++ b/bin/init.js @@ -1,13 +1,12 @@ +const mongoose = require('../mongoose'); const Setting = require('../models/setting'); const defaults = {id: '1', moderation: 'pre'}; -try { - Setting.update({id: '1'}, {$setOnInsert: defaults}, {upsert: true}) - .then(() => { - console.log('Created settings object.'); - }).catch((err) => { - if (err) throw err; - }); -} catch (err) { - console.error('Cannot create settings object. ', err); -} +Setting.update({id: '1'}, {$setOnInsert: defaults}, {upsert: true}) + .then(() => { + console.log('Created settings object.'); + mongoose.disconnect(); + }).catch((err) => { + console.error(`failed to create the settings object ${JSON.stringify(err)}`); + mongoose.disconnect(); + }); From 1bab2d3e4eec488d79c6209a8ec8cce1501788c2 Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Fri, 4 Nov 2016 10:11:13 -0600 Subject: [PATCH 16/38] still throw error --- bin/init.js | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/init.js b/bin/init.js index 0efa24c48..ff31ba9a9 100644 --- a/bin/init.js +++ b/bin/init.js @@ -9,4 +9,5 @@ Setting.update({id: '1'}, {$setOnInsert: defaults}, {upsert: true}) }).catch((err) => { console.error(`failed to create the settings object ${JSON.stringify(err)}`); mongoose.disconnect(); + throw new Error(err); // just to be safe }); From 5bef61abe927cd0ee06538e5dcb1225186a0d5e1 Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Fri, 4 Nov 2016 10:26:39 -0600 Subject: [PATCH 17/38] let exceptional circumstances be exceptional --- bin/init.js | 1 - 1 file changed, 1 deletion(-) diff --git a/bin/init.js b/bin/init.js index ff31ba9a9..d8be5a6fa 100644 --- a/bin/init.js +++ b/bin/init.js @@ -8,6 +8,5 @@ Setting.update({id: '1'}, {$setOnInsert: defaults}, {upsert: true}) mongoose.disconnect(); }).catch((err) => { console.error(`failed to create the settings object ${JSON.stringify(err)}`); - mongoose.disconnect(); throw new Error(err); // just to be safe }); From 730bd29bd1ca93842486f0725dc43ce8fd66c366 Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 09:43:01 -0700 Subject: [PATCH 18/38] 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 19/38] 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 20/38] 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 21/38] 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 22/38] 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 dba1b04558376b74927b9ae1e165cd3f531bce88 Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Fri, 4 Nov 2016 14:06:02 -0600 Subject: [PATCH 23/38] 204 no content --- routes/api/settings/index.js | 2 +- swagger.yaml | 6 ------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/routes/api/settings/index.js b/routes/api/settings/index.js index 5cb7bcc01..53bf53f3c 100644 --- a/routes/api/settings/index.js +++ b/routes/api/settings/index.js @@ -7,7 +7,7 @@ router.get('/', (req, res, next) => { }); router.put('/', (req, res, next) => { - Setting.updateSettings(req.body).then(settings => res.json(settings)).catch(next); + Setting.updateSettings(req.body).then(() => res.status(204).end()).catch(next); }); module.exports = router; diff --git a/swagger.yaml b/swagger.yaml index 50df1ff4b..3ab5a21df 100644 --- a/swagger.yaml +++ b/swagger.yaml @@ -162,12 +162,6 @@ paths: responses: 204: description: Updated Setting - schema: - type: array - items: - $ref: '#/definitions/Setting' - - definitions: Item: From 141c1141385d185dc1cb81140ba14172e3c61f69 Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 13:20:58 -0700 Subject: [PATCH 24/38] 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 25/38] 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 b17854735e105ee58c3c2136419fc0adda1fcc38 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Fri, 4 Nov 2016 14:24:23 -0600 Subject: [PATCH 26/38] Initial commit of deployment code --- Dockerfile | 1 + app.js | 13 +++++++---- circle.yaml | 21 ++++++++++++++++++ docker-compose.yml | 24 ++++++++++++++++++++ package.json | 4 +++- scripts/deploy.sh | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 113 insertions(+), 5 deletions(-) create mode 100644 circle.yaml create mode 100644 docker-compose.yml create mode 100644 scripts/deploy.sh diff --git a/Dockerfile b/Dockerfile index 22ba7e318..b98397323 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,6 +6,7 @@ WORKDIR /usr/src/app # Setup the environment ENV PATH /usr/src/app/bin:$PATH +ENV TALK_PORT 5000 EXPOSE 5000 # Install app dependencies diff --git a/app.js b/app.js index 7ce7c91ab..c9e44f854 100644 --- a/app.js +++ b/app.js @@ -1,11 +1,16 @@ -/* This is the entrypoint for the Talk Platform server. */ - -// Initialize application framework. const express = require('express'); +const morgan = require('morgan'); +const path = require('path'); const app = express(); +// Middleware declarations. +app.use(morgan('dev')); + +// API Routes. app.use('/api/v1', require('./routes/api')); -app.use('/client/', express.static('./dist')); + +// Static Routes. +app.use('/client/', express.static(path.join(__dirname, 'dist'))); module.exports = app; diff --git a/circle.yaml b/circle.yaml new file mode 100644 index 000000000..f454424d4 --- /dev/null +++ b/circle.yaml @@ -0,0 +1,21 @@ +machine: + node: + version: 6 + services: + - docker + +test: + override: + - MOCHA_FILE=$CIRCLE_TEST_REPORTS/junit/test-results.xml ./node_modules/.bin/mocha tests --reporter mocha-junit-reporter + - npm run lint + +deployment: + release: + tag: /[0-9]+(\.[0-9]+)*/ + commands: + - bash ./scripts/deploy.sh + + latest: + branch: master + commands: + - bash ./scripts/deploy.sh diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..ecfac00bc --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,24 @@ +version: '2' +services: + + talk: + image: coralproject/talk:latest + build: . + restart: always + ports: + - "5000:5000" + environment: + - "TALK_PORT=5000" + - "TALK_MONGO_URL=mongodb://mongo" + depends_on: + - mongo + + mongo: + image: mongo:3.2 + restart: always + volumes: + - mongo:/data/db + +volumes: + mongo: + external: false diff --git a/package.json b/package.json index 096863202..6d44eb48b 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", + "morgan": "^1.7.0" }, "devDependencies": { "babel-core": "6.14.0", @@ -65,6 +66,7 @@ "imports-loader": "^0.6.5", "json-loader": "^0.5.4", "mocha": "^3.1.2", + "mocha-junit-reporter": "^1.12.1", "pre-git": "^3.10.0", "pym.js": "^1.1.1", "react": "15.3.2", diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100644 index 000000000..5d79c0ef9 --- /dev/null +++ b/scripts/deploy.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +set -e + +docker login -u $DOCKER_USER -p $DOCKER_PASS + +# Sourced from https://segment.com/blog/ci-at-segment/ + +deploy_tag() { + # Find our individual versions from the tags + if [ -n "$(echo $CIRCLE_TAG | grep -E '.*\..*\..*')" ] + then + major=$(echo $CIRCLE_TAG | cut -d. -f1) + minor=$(echo $CIRCLE_TAG | cut -d. -f2) + patch=$(echo $CIRCLE_TAG | cut -d. -f3) + + major_version_tag=$major + minor_version_tag=$major.$minor + patch_version_tag=$major.$minor.$patch + + tag_list="$major_version_tag $minor_version_tag $patch_version_tag" + else + tag_list=$CIRCLE_TAG + fi + + # Tag the new image with major, minor and patch version tags. + for version in $tag_list + do + echo "==> tagging $version" + docker tag coralproject/talk:latest coralproject/talk:$version + done + + # Push each of the tags to docker hub, including latest + for version in $tag_list latest + do + echo "==> pushing $version" + docker push coralproject/talk:$version + done +} + +deploy_latest() { + echo "==> pushing latest" + docker push coralproject/talk:latest +} + +# build the repo +docker build -t coralproject/talk . + +# deploy based on the env +if [ -n "$CIRCLE_TAG" ] +then + deploy_tag +else + deploy_latest +fi From 09a4bcb213df6f1042f977674e5c16aa10c17ef5 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Fri, 4 Nov 2016 14:26:32 -0600 Subject: [PATCH 27/38] Added test badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 70e2d1110..d295ef326 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Talk +# Talk [![CircleCI](https://circleci.com/gh/coralproject/talk.svg?style=svg)](https://circleci.com/gh/coralproject/talk) A commenting platform from The Coral Project. [https://coralproject.net](https://coralproject.net) ### Getting Started From 19a7de5c9842b33cb867b998c3289b78583d32ea Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 13:28:40 -0700 Subject: [PATCH 28/38] 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 9956249cae09c87455cbc06eb8f7bf230cc8661d Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Fri, 4 Nov 2016 14:35:05 -0600 Subject: [PATCH 29/38] Fixed the filename... --- circle.yaml => circle.yml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename circle.yaml => circle.yml (100%) diff --git a/circle.yaml b/circle.yml similarity index 100% rename from circle.yaml rename to circle.yml From 4836069fb680ea5259ba5a725ef0e8918871160b Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 13:36:35 -0700 Subject: [PATCH 30/38] 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 31/38] 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 9e07f720da11f43ebb096499a9bf133d23e1bab5 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Fri, 4 Nov 2016 15:11:20 -0600 Subject: [PATCH 32/38] Fixed invalid command --- scripts/deploy.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 5d79c0ef9..51336b44d 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -2,7 +2,7 @@ set -e -docker login -u $DOCKER_USER -p $DOCKER_PASS +docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASS # Sourced from https://segment.com/blog/ci-at-segment/ From 23ea7025eaf9545e2a3d9b725809b6a64e9c605c Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 14:13:22 -0700 Subject: [PATCH 33/38] 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 34/38] 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 35/38] 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 c4de906e01b0ec36372c1251dfb5dd400d745d24 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Fri, 4 Nov 2016 15:36:29 -0600 Subject: [PATCH 36/38] Bumped version of node to 7.0.0 --- Dockerfile | 2 +- circle.yml | 2 +- package.json | 3 +++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index b98397323..cdb7652d5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM node +FROM node:7 # Create app directory RUN mkdir -p /usr/src/app diff --git a/circle.yml b/circle.yml index f454424d4..9de9eae4c 100644 --- a/circle.yml +++ b/circle.yml @@ -1,6 +1,6 @@ machine: node: - version: 6 + version: 7 services: - docker diff --git a/package.json b/package.json index da1df7232..d0413ed89 100644 --- a/package.json +++ b/package.json @@ -84,5 +84,8 @@ "webpack-hot-middleware": "^2.12.2", "webpack-module-hot-accept": "^1.0.4", "whatwg-fetch": "^1.0.0" + }, + "engines": { + "node": ">=7.0.0" } } From 43a70ea71c567e0b410dd97d8c6b73b4b3457cf9 Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 14:45:46 -0700 Subject: [PATCH 37/38] 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 38/38] 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); }); });