From fccd2dacdc3a7eabbf98b3b3f517c7a4c0c59db2 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Mon, 23 May 2016 20:39:42 -0700 Subject: [PATCH 01/35] post request working, all files initialized --- stefanie-hansen/.eslintrc.js | 33 +++++++++++++++ stefanie-hansen/.gitignore | 1 + stefanie-hansen/data/me.json | 1 + stefanie-hansen/gulpfile.js | 63 ++++++++++++++++++++++++++++ stefanie-hansen/index.js | 0 stefanie-hansen/lib/plant-routes.js | 33 +++++++++++++++ stefanie-hansen/lib/server.js | 14 +++++++ stefanie-hansen/package.json | 28 +++++++++++++ stefanie-hansen/test/express-test.js | 0 9 files changed, 173 insertions(+) create mode 100644 stefanie-hansen/.eslintrc.js create mode 100644 stefanie-hansen/.gitignore create mode 100644 stefanie-hansen/data/me.json create mode 100644 stefanie-hansen/gulpfile.js create mode 100644 stefanie-hansen/index.js create mode 100644 stefanie-hansen/lib/plant-routes.js create mode 100644 stefanie-hansen/lib/server.js create mode 100644 stefanie-hansen/package.json create mode 100644 stefanie-hansen/test/express-test.js diff --git a/stefanie-hansen/.eslintrc.js b/stefanie-hansen/.eslintrc.js new file mode 100644 index 0000000..f897c32 --- /dev/null +++ b/stefanie-hansen/.eslintrc.js @@ -0,0 +1,33 @@ +module.exports = { + "env": { + "browser": true, + "es6": true, + "node": true, + "mocha": true + }, + "globals": { + "require": true + }, + "extends": "eslint:recommended", + "parserOptions": { + "sourceType": "module" + }, + "rules": { + "indent": [ + "error", + 2 + ], + "linebreak-style": [ + "error", + "unix" + ], + "quotes": [ + "error", + "single" + ], + "semi": [ + "error", + "always" + ] + } +}; diff --git a/stefanie-hansen/.gitignore b/stefanie-hansen/.gitignore new file mode 100644 index 0000000..07e6e47 --- /dev/null +++ b/stefanie-hansen/.gitignore @@ -0,0 +1 @@ +/node_modules diff --git a/stefanie-hansen/data/me.json b/stefanie-hansen/data/me.json new file mode 100644 index 0000000..69da3ec --- /dev/null +++ b/stefanie-hansen/data/me.json @@ -0,0 +1 @@ +{"test":"test"} \ No newline at end of file diff --git a/stefanie-hansen/gulpfile.js b/stefanie-hansen/gulpfile.js new file mode 100644 index 0000000..021c2c1 --- /dev/null +++ b/stefanie-hansen/gulpfile.js @@ -0,0 +1,63 @@ +const gulp = require('gulp'); +const mocha = require('gulp-mocha'); +const lint = require('gulp-eslint'); +const opts = { + 'extends': 'eslint:recommended', + 'ecmaFeatures': { + 'modules': true + }, + 'rules': { + 'no-alert': 0, + 'no-bitwise': 0, + 'camelcase': 1, + 'no-console': 1, + 'curly': 1, + 'eqeqeq': 0, + 'no-eq-null': 0, + 'guard-for-in': 1, + 'no-empty': 1, + 'no-use-before-define': 0, + 'no-obj-calls': 2, + 'no-unused-vars': 0, + 'new-cap': 1, + 'no-shadow': 0, + 'strict': 1, + 'no-invalid-regexp': 2, + 'comma-dangle': 2, + 'no-undef': 1, + 'no-new': 1, + 'no-extra-semi': 1, + 'no-debugger': 2, + 'no-caller': 1, + 'semi': 1, + 'quotes': 0, + 'no-unreachable': 2 + }, + 'globals': { + '$': false + }, + 'env': { + 'node': true, + 'es6': true + } +}; + + +gulp.task('linter' , () => { + return gulp.src(['./*.js', './test/*.js', './lib/*.js']) + .pipe(lint(opts)) + .pipe(lint.format()); +}); + +gulp.task('tests', () => { + return gulp.src(['./*.js', './test/*.js', './lib/*.js'], {read: false}) + .pipe(mocha({reporter: 'nyan'})); +}); + +gulp.task('watch', () => { + gulp.watch(['./*.js', './test/*.js', './lib/*.js'], ['linter', 'tests']); +}); + +gulp.task('default', ['linter', 'tests', 'watch'], () => { + +}); diff --git a/stefanie-hansen/index.js b/stefanie-hansen/index.js new file mode 100644 index 0000000..e69de29 diff --git a/stefanie-hansen/lib/plant-routes.js b/stefanie-hansen/lib/plant-routes.js new file mode 100644 index 0000000..f51d79d --- /dev/null +++ b/stefanie-hansen/lib/plant-routes.js @@ -0,0 +1,33 @@ +'use strict'; + +const express = require('express'); +const router = express.Router(); +const fs = require('fs'); + +router.get('/:id', (req, res) => { + let id = req.params.id; + res.send(`Get received for path ${id}`); +}); + +router.post('/:id', (req, res) => { + let id = req.params.id; + if (req.body) { + fs.writeFile(__dirname + `/../data/${id}.json`, JSON.stringify(req.body), (err) => { + if (err) console.log(err); + return res.send('File received'); + }); + } + res.end('No body'); +}); + +router.put('/:id', (req, res) => { + res.send(`Put received for path ${id}`); + let id = req.params.id; +}); + +router.delete('/:id', (req, res) => { + res.send(`Delete received for path ${id}`); + let id = req.params.id; +}); + +module.exports = router; diff --git a/stefanie-hansen/lib/server.js b/stefanie-hansen/lib/server.js new file mode 100644 index 0000000..c9ce036 --- /dev/null +++ b/stefanie-hansen/lib/server.js @@ -0,0 +1,14 @@ +'use strict'; + +const express = require('express'); +const app = express(); +const bodyParser = require('body-parser'); +const jsonParser = bodyParser.json(); +const plantRouter = require('./plant-routes'); + +app.use(jsonParser); +app.use('/plants', plantRouter); + +app.listen(3000, () => { + console.log('listening on 3000'); +}); diff --git a/stefanie-hansen/package.json b/stefanie-hansen/package.json new file mode 100644 index 0000000..938aa73 --- /dev/null +++ b/stefanie-hansen/package.json @@ -0,0 +1,28 @@ +{ + "name": "stefanie-hansen", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "express": "^4.13.4" + }, + "devDependencies": { + "body-parser": "^1.15.1", + "chai": "^3.5.0", + "chai-http": "^2.0.1", + "eslint": "^2.10.2", + "eslint-config-standard": "^5.3.1", + "eslint-plugin-promise": "^1.1.0", + "eslint-plugin-standard": "^1.3.2", + "gulp": "^3.9.1", + "gulp-eslint": "^2.0.0", + "gulp-mocha": "^2.2.0", + "mocha": "^2.5.1" + } +} diff --git a/stefanie-hansen/test/express-test.js b/stefanie-hansen/test/express-test.js new file mode 100644 index 0000000..e69de29 From 24be540757e0e38aff95365313770399354d6c2e Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Mon, 23 May 2016 21:10:09 -0700 Subject: [PATCH 02/35] get and post requests working --- stefanie-hansen/data/{me.json => mesda.json} | 0 stefanie-hansen/lib/plant-routes.js | 31 +++++++++++++++----- 2 files changed, 24 insertions(+), 7 deletions(-) rename stefanie-hansen/data/{me.json => mesda.json} (100%) diff --git a/stefanie-hansen/data/me.json b/stefanie-hansen/data/mesda.json similarity index 100% rename from stefanie-hansen/data/me.json rename to stefanie-hansen/data/mesda.json diff --git a/stefanie-hansen/lib/plant-routes.js b/stefanie-hansen/lib/plant-routes.js index f51d79d..570f8b1 100644 --- a/stefanie-hansen/lib/plant-routes.js +++ b/stefanie-hansen/lib/plant-routes.js @@ -6,18 +6,35 @@ const fs = require('fs'); router.get('/:id', (req, res) => { let id = req.params.id; - res.send(`Get received for path ${id}`); + fs.readdir(__dirname + '/../data', (err, files) => { + if (err) { + console.log('Error reading directory', err); + res.end('Error: Cannot read directory'); + } + if (files.indexOf(`${id}.json`) > -1) { + fs.readFile(__dirname + `/../data/${id}.json`, (err, data) => { + if (err) { + console.log('Error reading file', err); + res.end('Error: Cannot read file'); + } + res.send(`You requested ${id}.json: `, data.toString()); + res.end(); + }); + } + }); }); router.post('/:id', (req, res) => { let id = req.params.id; - if (req.body) { - fs.writeFile(__dirname + `/../data/${id}.json`, JSON.stringify(req.body), (err) => { - if (err) console.log(err); - return res.send('File received'); - }); + if (!req.body) { + res.send('Error: No body sent with request'); + res.end(); } - res.end('No body'); + fs.writeFile(__dirname + `/../data/${id}.json`, JSON.stringify(req.body), (err) => { + if (err) console.log(err); + res.send('received'); + res.end(); + }); }); router.put('/:id', (req, res) => { From fa363bd780ce9d7b16258475514adf443ea389ad Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Mon, 23 May 2016 21:28:06 -0700 Subject: [PATCH 03/35] delete route working --- stefanie-hansen/data/mesda.json | 1 - stefanie-hansen/lib/plant-routes.js | 34 ++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 11 deletions(-) delete mode 100644 stefanie-hansen/data/mesda.json diff --git a/stefanie-hansen/data/mesda.json b/stefanie-hansen/data/mesda.json deleted file mode 100644 index 69da3ec..0000000 --- a/stefanie-hansen/data/mesda.json +++ /dev/null @@ -1 +0,0 @@ -{"test":"test"} \ No newline at end of file diff --git a/stefanie-hansen/lib/plant-routes.js b/stefanie-hansen/lib/plant-routes.js index 570f8b1..d179ccf 100644 --- a/stefanie-hansen/lib/plant-routes.js +++ b/stefanie-hansen/lib/plant-routes.js @@ -9,16 +9,19 @@ router.get('/:id', (req, res) => { fs.readdir(__dirname + '/../data', (err, files) => { if (err) { console.log('Error reading directory', err); - res.end('Error: Cannot read directory'); + res.sendStatus(404); + res.end('Error: Cannot read file directory'); } - if (files.indexOf(`${id}.json`) > -1) { + else if (files.indexOf(`${id}.json`) > -1) { fs.readFile(__dirname + `/../data/${id}.json`, (err, data) => { if (err) { console.log('Error reading file', err); + res.sendStatus(404); res.end('Error: Cannot read file'); + } else { + res.send(`You requested ${id}.json: `, data.toString()); + res.end(); } - res.send(`You requested ${id}.json: `, data.toString()); - res.end(); }); } }); @@ -27,14 +30,16 @@ router.get('/:id', (req, res) => { router.post('/:id', (req, res) => { let id = req.params.id; if (!req.body) { + res.sendStatus(400); res.send('Error: No body sent with request'); res.end(); + } else { + fs.writeFile(__dirname + `/../data/${id}.json`, JSON.stringify(req.body), (err) => { + if (err) console.log(err); + res.send('received'); + res.end(); + }); } - fs.writeFile(__dirname + `/../data/${id}.json`, JSON.stringify(req.body), (err) => { - if (err) console.log(err); - res.send('received'); - res.end(); - }); }); router.put('/:id', (req, res) => { @@ -43,8 +48,17 @@ router.put('/:id', (req, res) => { }); router.delete('/:id', (req, res) => { - res.send(`Delete received for path ${id}`); let id = req.params.id; + fs.unlink(__dirname + `/../data/${id}.json`, (err) => { + if (err) { + console.log('Error deleting file', err); + res.sendStatus(404); + res.end('Error: Cannot delete file'); + } else { + res.send(`File ${id}.json successfully deleted`); + res.end(); + } + }); }); module.exports = router; From 97ed4cbbc17ec71dae3a63eec0f9b2859b17c36d Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Mon, 23 May 2016 21:35:25 -0700 Subject: [PATCH 04/35] put request finished --- stefanie-hansen/data/mesda.json | 1 + stefanie-hansen/lib/plant-routes.js | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 stefanie-hansen/data/mesda.json diff --git a/stefanie-hansen/data/mesda.json b/stefanie-hansen/data/mesda.json new file mode 100644 index 0000000..69da3ec --- /dev/null +++ b/stefanie-hansen/data/mesda.json @@ -0,0 +1 @@ +{"test":"test"} \ No newline at end of file diff --git a/stefanie-hansen/lib/plant-routes.js b/stefanie-hansen/lib/plant-routes.js index d179ccf..9b73248 100644 --- a/stefanie-hansen/lib/plant-routes.js +++ b/stefanie-hansen/lib/plant-routes.js @@ -19,7 +19,7 @@ router.get('/:id', (req, res) => { res.sendStatus(404); res.end('Error: Cannot read file'); } else { - res.send(`You requested ${id}.json: `, data.toString()); + res.send(`You requested the contents of ${id}.json: \n${data.toString()}`); res.end(); } }); @@ -36,15 +36,25 @@ router.post('/:id', (req, res) => { } else { fs.writeFile(__dirname + `/../data/${id}.json`, JSON.stringify(req.body), (err) => { if (err) console.log(err); - res.send('received'); + res.send(`Received request and wrote file ${id}.json with the following contents: \n${JSON.stringify(req.body)}`); res.end(); }); } }); router.put('/:id', (req, res) => { - res.send(`Put received for path ${id}`); let id = req.params.id; + if (!req.body) { + res.sendStatus(400); + res.send('Error: No body sent with request'); + res.end(); + } else { + fs.writeFile(__dirname + `/../data/${id}.json`, JSON.stringify(req.body), (err) => { + if (err) console.log(err); + res.send(`Received request and wrote file ${id}.json with the following contents: \n${JSON.stringify(req.body)}`); + res.end(); + }); + } }); router.delete('/:id', (req, res) => { From 5d7d38d5d46d775192b00324e2f7403c108425fd Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Mon, 23 May 2016 21:58:29 -0700 Subject: [PATCH 05/35] made all generic methods dealing with reading and writing files into their own modules --- stefanie-hansen/lib/delete.js | 16 ++++++ stefanie-hansen/lib/get.js | 25 ++++++++++ stefanie-hansen/lib/plant-routes.js | 76 ++++------------------------- stefanie-hansen/lib/post.js | 16 ++++++ stefanie-hansen/lib/put.js | 16 ++++++ stefanie-hansen/lib/server.js | 5 ++ 6 files changed, 87 insertions(+), 67 deletions(-) create mode 100644 stefanie-hansen/lib/delete.js create mode 100644 stefanie-hansen/lib/get.js create mode 100644 stefanie-hansen/lib/post.js create mode 100644 stefanie-hansen/lib/put.js diff --git a/stefanie-hansen/lib/delete.js b/stefanie-hansen/lib/delete.js new file mode 100644 index 0000000..1bb23e1 --- /dev/null +++ b/stefanie-hansen/lib/delete.js @@ -0,0 +1,16 @@ +'use strict'; +const fs = require('fs'); + +module.exports = function del(req, res) { + let id = req.params.id; + fs.unlink(__dirname + `/../data/${id}.json`, (err) => { + if (err) { + console.log('Error deleting file', err); + res.sendStatus(404); + res.end(); + } else { + res.send(`File ${id}.json successfully deleted`); + res.end(); + } + }); +}; diff --git a/stefanie-hansen/lib/get.js b/stefanie-hansen/lib/get.js new file mode 100644 index 0000000..68ffdad --- /dev/null +++ b/stefanie-hansen/lib/get.js @@ -0,0 +1,25 @@ +'use strict'; +const fs = require('fs'); + +module.exports = function get(req, res) { + let id = req.params.id; + fs.readdir(__dirname + '/../data', (err, files) => { + if (err) { + console.log('Error reading directory', err); + res.sendStatus(404); + res.end(); + } + else if (files.indexOf(`${id}.json`) > -1) { + fs.readFile(__dirname + `/../data/${id}.json`, (err, data) => { + if (err) { + console.log('Error reading file', err); + res.sendStatus(404); + res.end(); + } else { + res.send(`You requested the contents of ${id}.json: \n${data.toString()}`); + res.end(); + } + }); + } + }); +}; diff --git a/stefanie-hansen/lib/plant-routes.js b/stefanie-hansen/lib/plant-routes.js index 9b73248..f6f1b61 100644 --- a/stefanie-hansen/lib/plant-routes.js +++ b/stefanie-hansen/lib/plant-routes.js @@ -2,73 +2,15 @@ const express = require('express'); const router = express.Router(); -const fs = require('fs'); +const get = require('./get'); +const post = require('./post'); +const put = require('./put'); +const del = require('./delete'); -router.get('/:id', (req, res) => { - let id = req.params.id; - fs.readdir(__dirname + '/../data', (err, files) => { - if (err) { - console.log('Error reading directory', err); - res.sendStatus(404); - res.end('Error: Cannot read file directory'); - } - else if (files.indexOf(`${id}.json`) > -1) { - fs.readFile(__dirname + `/../data/${id}.json`, (err, data) => { - if (err) { - console.log('Error reading file', err); - res.sendStatus(404); - res.end('Error: Cannot read file'); - } else { - res.send(`You requested the contents of ${id}.json: \n${data.toString()}`); - res.end(); - } - }); - } - }); -}); - -router.post('/:id', (req, res) => { - let id = req.params.id; - if (!req.body) { - res.sendStatus(400); - res.send('Error: No body sent with request'); - res.end(); - } else { - fs.writeFile(__dirname + `/../data/${id}.json`, JSON.stringify(req.body), (err) => { - if (err) console.log(err); - res.send(`Received request and wrote file ${id}.json with the following contents: \n${JSON.stringify(req.body)}`); - res.end(); - }); - } -}); - -router.put('/:id', (req, res) => { - let id = req.params.id; - if (!req.body) { - res.sendStatus(400); - res.send('Error: No body sent with request'); - res.end(); - } else { - fs.writeFile(__dirname + `/../data/${id}.json`, JSON.stringify(req.body), (err) => { - if (err) console.log(err); - res.send(`Received request and wrote file ${id}.json with the following contents: \n${JSON.stringify(req.body)}`); - res.end(); - }); - } -}); - -router.delete('/:id', (req, res) => { - let id = req.params.id; - fs.unlink(__dirname + `/../data/${id}.json`, (err) => { - if (err) { - console.log('Error deleting file', err); - res.sendStatus(404); - res.end('Error: Cannot delete file'); - } else { - res.send(`File ${id}.json successfully deleted`); - res.end(); - } - }); -}); +router.route('/:id') + .get(get) + .put(put) + .post(post) + .delete(del); module.exports = router; diff --git a/stefanie-hansen/lib/post.js b/stefanie-hansen/lib/post.js new file mode 100644 index 0000000..e536287 --- /dev/null +++ b/stefanie-hansen/lib/post.js @@ -0,0 +1,16 @@ +'use strict'; +const fs = require('fs'); + +module.exports = function post(req, res) { + let id = req.params.id; + if (!req.body) { + res.sendStatus(400); + res.end(); + } else { + fs.writeFile(__dirname + `/../data/${id}.json`, JSON.stringify(req.body), (err) => { + if (err) console.log(err); + res.send(`Received request and wrote file ${id}.json with the following contents: \n${JSON.stringify(req.body)}`); + res.end(); + }); + } +}; diff --git a/stefanie-hansen/lib/put.js b/stefanie-hansen/lib/put.js new file mode 100644 index 0000000..36d37c1 --- /dev/null +++ b/stefanie-hansen/lib/put.js @@ -0,0 +1,16 @@ +'use strict'; +const fs = require('fs'); + +module.exports = function put(req, res) { + let id = req.params.id; + if (!req.body) { + res.sendStatus(400); + res.end(); + } else { + fs.writeFile(__dirname + `/../data/${id}.json`, JSON.stringify(req.body), (err) => { + if (err) console.log(err); + res.send(`Received request and wrote file ${id}.json with the following contents: \n${JSON.stringify(req.body)}`); + res.end(); + }); + } +}; diff --git a/stefanie-hansen/lib/server.js b/stefanie-hansen/lib/server.js index c9ce036..e39be6a 100644 --- a/stefanie-hansen/lib/server.js +++ b/stefanie-hansen/lib/server.js @@ -9,6 +9,11 @@ const plantRouter = require('./plant-routes'); app.use(jsonParser); app.use('/plants', plantRouter); +app.all('*', (req, res) => { + res.sendStatus(404); + res.end(); +}); + app.listen(3000, () => { console.log('listening on 3000'); }); From 93588a64ba7f31ef9cafbcdb1a8d34a7dc90d796 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Tue, 24 May 2016 10:42:52 -0700 Subject: [PATCH 06/35] tests complete for all methods --- stefanie-hansen/test/express-test.js | 112 +++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/stefanie-hansen/test/express-test.js b/stefanie-hansen/test/express-test.js index e69de29..cb55a38 100644 --- a/stefanie-hansen/test/express-test.js +++ b/stefanie-hansen/test/express-test.js @@ -0,0 +1,112 @@ +'use strict'; + +const chai = require('chai'); +const expect = chai.expect; +const chaiHTTP = require('chai-http'); +chai.use(chaiHTTP); +const request = chai.request; +const fs = require('fs'); +require('../lib/server'); + +describe('Express router tests', () => { + + describe('Catch all tests', () => { + it('should respond to a request to a random route with an error', (done) => { + request('localhost:3000') + .get('/test') + .end((err, res) => { + expect(err).to.not.eql(null); + expect(res).to.have.status(404); + done(); + }); + }); + }); + + describe('POST route tests', () => { + it('should respond to a POST request to /plants/:id without errors', (done) => { + request('localhost:3000') + .post('/plants/test') + .end((err, res) => { + expect(err).to.eql(null); + expect(res).to.have.status(200); + done(); + }); + }); + it('should respond to a POST request to /plants/:id by creating a new JSON file and sending a response with the contents of that file/request to confirm', (done) => { + request('localhost:3000') + .post('/plants/test') + .send({"test":"test"}) + .end((err, res) => { + expect(fs.readFileSync(__dirname + '/../data/test.json').toString()).to.eql('{"test":"test"}'); + expect(res.text.split(' ').pop().trim()).to.eql('{"test":"test"}'); + done(); + }); + }); + }); + + describe('GET route tests', () => { + it('should respond to a GET request to /plants/:id without errors', (done) => { + request('localhost:3000') + .get('/plants/test') + .end((err, res) => { + expect(err).to.eql(null); + expect(res).to.have.status(200); + done(); + }); + }); + it('should respond to a GET request to /plants/:id with the contents of the requested file.', (done) => { + request('localhost:3000') + .get('/plants/test') + .end((err, res) => { + expect(res.text.split(' ').pop().trim()).to.eql('{"test":"test"}'); + done(); + }); + }); + }); + + describe('PUT route tests', () => { + it('should respond to a PUT request to /plants/:id without errors', (done) => { + request('localhost:3000') + .put('/plants/test') + .end((err, res) => { + expect(err).to.eql(null); + expect(res).to.have.status(200); + done(); + }); + }); + it('should respond to a PUT request to /plants/:id by creating or replacing a JSON file and sending a response with the contents of that file/request to confirm', (done) => { + request('localhost:3000') + .post('/plants/test') + .send({"test":"test"}) + .end((err, res) => { + expect(fs.readFileSync(__dirname + '/../data/test.json').toString()).to.eql('{"test":"test"}'); + expect(res.text.split(' ').pop().trim()).to.eql('{"test":"test"}'); + done(); + }); + }); + }); + + describe('DELETE route tests', () => { + let oldFileArray = []; + + before('checking to make sure file is there before delete request', (done) => { + fs.readdir(__dirname + '/../data', (err, files) => { + oldFileArray = files; + }); + done(); + }); + + it('should respond to a DELETE request to /plants/:id without errors and delete the file defined by the url path', (done) => { + request('localhost:3000') + .delete('/plants/test') + .end((err, res) => { + fs.readdir(__dirname + '/../data', (err, files) => { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(oldFileArray.length).to.not.eql(files.length); + }); + done(); + }); + }); + }); +}); From 51a793ca3d8b553f15ebf7f8b5f301edff482e51 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Tue, 24 May 2016 11:24:01 -0700 Subject: [PATCH 07/35] tests passing, now post doesn't overwrite a file if it already exists --- stefanie-hansen/data/mesda.json | 1 - stefanie-hansen/lib/get.js | 1 + stefanie-hansen/lib/post.js | 23 +++++++++++++++++++---- stefanie-hansen/test/express-test.js | 28 +++++++++++++++++----------- 4 files changed, 37 insertions(+), 16 deletions(-) delete mode 100644 stefanie-hansen/data/mesda.json diff --git a/stefanie-hansen/data/mesda.json b/stefanie-hansen/data/mesda.json deleted file mode 100644 index 69da3ec..0000000 --- a/stefanie-hansen/data/mesda.json +++ /dev/null @@ -1 +0,0 @@ -{"test":"test"} \ No newline at end of file diff --git a/stefanie-hansen/lib/get.js b/stefanie-hansen/lib/get.js index 68ffdad..2685723 100644 --- a/stefanie-hansen/lib/get.js +++ b/stefanie-hansen/lib/get.js @@ -16,6 +16,7 @@ module.exports = function get(req, res) { res.sendStatus(404); res.end(); } else { + console.log('data', data.toString()) res.send(`You requested the contents of ${id}.json: \n${data.toString()}`); res.end(); } diff --git a/stefanie-hansen/lib/post.js b/stefanie-hansen/lib/post.js index e536287..d13555a 100644 --- a/stefanie-hansen/lib/post.js +++ b/stefanie-hansen/lib/post.js @@ -7,10 +7,25 @@ module.exports = function post(req, res) { res.sendStatus(400); res.end(); } else { - fs.writeFile(__dirname + `/../data/${id}.json`, JSON.stringify(req.body), (err) => { - if (err) console.log(err); - res.send(`Received request and wrote file ${id}.json with the following contents: \n${JSON.stringify(req.body)}`); - res.end(); + // Does file exist already? + fs.readFile(__dirname + `/../data/${id}.json`, (err) => { + // If no files exists, write one. + if (err) { + fs.writeFile(__dirname + `/../data/${id}.json`, JSON.stringify(req.body), (err) => { + if (err) { + console.log(err); + res.sendStatus(404); + res.send(); + } else { + res.send(`Received request and wrote file ${id}.json with the following contents: \n${JSON.stringify(req.body)}`); + res.end(); + } + }); + // If file already exists, don't overwrite and send back a bad request. + } else { + res.sendStatus(400); + res.end(); + } }); } }; diff --git a/stefanie-hansen/test/express-test.js b/stefanie-hansen/test/express-test.js index cb55a38..e9b95de 100644 --- a/stefanie-hansen/test/express-test.js +++ b/stefanie-hansen/test/express-test.js @@ -23,22 +23,28 @@ describe('Express router tests', () => { }); describe('POST route tests', () => { - it('should respond to a POST request to /plants/:id without errors', (done) => { - request('localhost:3000') - .post('/plants/test') - .end((err, res) => { - expect(err).to.eql(null); - expect(res).to.have.status(200); - done(); + let fileArray = []; + before('checking to make sure file is there before delete request', (done) => { + fs.readdir(__dirname + '/../data', (err, files) => { + fileArray = files; }); + done(); }); - it('should respond to a POST request to /plants/:id by creating a new JSON file and sending a response with the contents of that file/request to confirm', (done) => { + + it('should respond to a POST request to /plants/:id without errors if a file with the url-defined name does not already exist and with an error if a file already exists. If a new file is created, it should have the content defined by the request body', (done) => { request('localhost:3000') .post('/plants/test') .send({"test":"test"}) .end((err, res) => { - expect(fs.readFileSync(__dirname + '/../data/test.json').toString()).to.eql('{"test":"test"}'); - expect(res.text.split(' ').pop().trim()).to.eql('{"test":"test"}'); + if (fileArray.indexOf('test.json') === -1) { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(fs.readFileSync(__dirname + '/../data/test.json').toString()).to.eql('{"test":"test"}'); + expect(res.text.split(' ').pop().trim()).to.eql('{"test":"test"}'); + } else { + expect(err).to.not.eql(null); + expect(res).to.have.status(400); + } done(); }); }); @@ -76,7 +82,7 @@ describe('Express router tests', () => { }); it('should respond to a PUT request to /plants/:id by creating or replacing a JSON file and sending a response with the contents of that file/request to confirm', (done) => { request('localhost:3000') - .post('/plants/test') + .put('/plants/test') .send({"test":"test"}) .end((err, res) => { expect(fs.readFileSync(__dirname + '/../data/test.json').toString()).to.eql('{"test":"test"}'); From a9bc017a496e3b8ca358c360208ccb8b4a5fb553 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Tue, 24 May 2016 11:26:37 -0700 Subject: [PATCH 08/35] reorganized --- stefanie-hansen/index.js | 0 stefanie-hansen/{lib => }/server.js | 2 +- stefanie-hansen/test/express-test.js | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) delete mode 100644 stefanie-hansen/index.js rename stefanie-hansen/{lib => }/server.js (87%) diff --git a/stefanie-hansen/index.js b/stefanie-hansen/index.js deleted file mode 100644 index e69de29..0000000 diff --git a/stefanie-hansen/lib/server.js b/stefanie-hansen/server.js similarity index 87% rename from stefanie-hansen/lib/server.js rename to stefanie-hansen/server.js index e39be6a..63579d4 100644 --- a/stefanie-hansen/lib/server.js +++ b/stefanie-hansen/server.js @@ -4,7 +4,7 @@ const express = require('express'); const app = express(); const bodyParser = require('body-parser'); const jsonParser = bodyParser.json(); -const plantRouter = require('./plant-routes'); +const plantRouter = require('./lib/plant-routes'); app.use(jsonParser); app.use('/plants', plantRouter); diff --git a/stefanie-hansen/test/express-test.js b/stefanie-hansen/test/express-test.js index e9b95de..e1d37c3 100644 --- a/stefanie-hansen/test/express-test.js +++ b/stefanie-hansen/test/express-test.js @@ -6,7 +6,7 @@ const chaiHTTP = require('chai-http'); chai.use(chaiHTTP); const request = chai.request; const fs = require('fs'); -require('../lib/server'); +require('../server'); describe('Express router tests', () => { From 247fb2bcd141683ad3206021a719a5e80af140e9 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Tue, 24 May 2016 11:27:44 -0700 Subject: [PATCH 09/35] removed unnecessary console log --- stefanie-hansen/lib/get.js | 1 - 1 file changed, 1 deletion(-) diff --git a/stefanie-hansen/lib/get.js b/stefanie-hansen/lib/get.js index 2685723..68ffdad 100644 --- a/stefanie-hansen/lib/get.js +++ b/stefanie-hansen/lib/get.js @@ -16,7 +16,6 @@ module.exports = function get(req, res) { res.sendStatus(404); res.end(); } else { - console.log('data', data.toString()) res.send(`You requested the contents of ${id}.json: \n${data.toString()}`); res.end(); } From 72875a18e7bae918144e848fc1e6ba91cd12e5d0 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Tue, 24 May 2016 13:27:13 -0700 Subject: [PATCH 10/35] took out unnecessary res.end()s --- stefanie-hansen/data/test.json | 1 + stefanie-hansen/lib/delete.js | 2 -- stefanie-hansen/lib/get.js | 5 ++--- stefanie-hansen/lib/post.js | 4 ---- stefanie-hansen/lib/put.js | 2 -- 5 files changed, 3 insertions(+), 11 deletions(-) create mode 100644 stefanie-hansen/data/test.json diff --git a/stefanie-hansen/data/test.json b/stefanie-hansen/data/test.json new file mode 100644 index 0000000..69da3ec --- /dev/null +++ b/stefanie-hansen/data/test.json @@ -0,0 +1 @@ +{"test":"test"} \ No newline at end of file diff --git a/stefanie-hansen/lib/delete.js b/stefanie-hansen/lib/delete.js index 1bb23e1..4352af5 100644 --- a/stefanie-hansen/lib/delete.js +++ b/stefanie-hansen/lib/delete.js @@ -7,10 +7,8 @@ module.exports = function del(req, res) { if (err) { console.log('Error deleting file', err); res.sendStatus(404); - res.end(); } else { res.send(`File ${id}.json successfully deleted`); - res.end(); } }); }; diff --git a/stefanie-hansen/lib/get.js b/stefanie-hansen/lib/get.js index 68ffdad..57c98fe 100644 --- a/stefanie-hansen/lib/get.js +++ b/stefanie-hansen/lib/get.js @@ -7,19 +7,18 @@ module.exports = function get(req, res) { if (err) { console.log('Error reading directory', err); res.sendStatus(404); - res.end(); } else if (files.indexOf(`${id}.json`) > -1) { fs.readFile(__dirname + `/../data/${id}.json`, (err, data) => { if (err) { console.log('Error reading file', err); res.sendStatus(404); - res.end(); } else { res.send(`You requested the contents of ${id}.json: \n${data.toString()}`); - res.end(); } }); + } else { + res.sendStatus(404); } }); }; diff --git a/stefanie-hansen/lib/post.js b/stefanie-hansen/lib/post.js index d13555a..9e1481a 100644 --- a/stefanie-hansen/lib/post.js +++ b/stefanie-hansen/lib/post.js @@ -5,7 +5,6 @@ module.exports = function post(req, res) { let id = req.params.id; if (!req.body) { res.sendStatus(400); - res.end(); } else { // Does file exist already? fs.readFile(__dirname + `/../data/${id}.json`, (err) => { @@ -15,16 +14,13 @@ module.exports = function post(req, res) { if (err) { console.log(err); res.sendStatus(404); - res.send(); } else { res.send(`Received request and wrote file ${id}.json with the following contents: \n${JSON.stringify(req.body)}`); - res.end(); } }); // If file already exists, don't overwrite and send back a bad request. } else { res.sendStatus(400); - res.end(); } }); } diff --git a/stefanie-hansen/lib/put.js b/stefanie-hansen/lib/put.js index 36d37c1..d0ab114 100644 --- a/stefanie-hansen/lib/put.js +++ b/stefanie-hansen/lib/put.js @@ -5,12 +5,10 @@ module.exports = function put(req, res) { let id = req.params.id; if (!req.body) { res.sendStatus(400); - res.end(); } else { fs.writeFile(__dirname + `/../data/${id}.json`, JSON.stringify(req.body), (err) => { if (err) console.log(err); res.send(`Received request and wrote file ${id}.json with the following contents: \n${JSON.stringify(req.body)}`); - res.end(); }); } }; From bae82707cae807f43a07a22ce07480ff440df2b8 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Tue, 24 May 2016 13:36:51 -0700 Subject: [PATCH 11/35] removed unnecessary res.end in server.js --- stefanie-hansen/server.js | 1 - 1 file changed, 1 deletion(-) diff --git a/stefanie-hansen/server.js b/stefanie-hansen/server.js index 63579d4..13f8561 100644 --- a/stefanie-hansen/server.js +++ b/stefanie-hansen/server.js @@ -11,7 +11,6 @@ app.use('/plants', plantRouter); app.all('*', (req, res) => { res.sendStatus(404); - res.end(); }); app.listen(3000, () => { From f2fb3478a483b9cdba5e86888e727150d3cc6567 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Thu, 26 May 2016 12:01:21 -0700 Subject: [PATCH 12/35] mongo works with all routes correctly --- stefanie-hansen/.gitignore | 1 + stefanie-hansen/data/test.json | 1 - stefanie-hansen/lib/delete.js | 14 ------- stefanie-hansen/lib/get.js | 24 ----------- stefanie-hansen/lib/plant-routes.js | 63 ++++++++++++++++++++++++----- stefanie-hansen/lib/post.js | 27 ------------- stefanie-hansen/lib/put.js | 14 ------- stefanie-hansen/package.json | 16 +++++--- stefanie-hansen/schema/plant.js | 12 ++++++ stefanie-hansen/server.js | 9 +++++ 10 files changed, 87 insertions(+), 94 deletions(-) delete mode 100644 stefanie-hansen/data/test.json delete mode 100644 stefanie-hansen/lib/delete.js delete mode 100644 stefanie-hansen/lib/get.js delete mode 100644 stefanie-hansen/lib/post.js delete mode 100644 stefanie-hansen/lib/put.js create mode 100644 stefanie-hansen/schema/plant.js diff --git a/stefanie-hansen/.gitignore b/stefanie-hansen/.gitignore index 07e6e47..4c497d6 100644 --- a/stefanie-hansen/.gitignore +++ b/stefanie-hansen/.gitignore @@ -1 +1,2 @@ /node_modules +/db diff --git a/stefanie-hansen/data/test.json b/stefanie-hansen/data/test.json deleted file mode 100644 index 69da3ec..0000000 --- a/stefanie-hansen/data/test.json +++ /dev/null @@ -1 +0,0 @@ -{"test":"test"} \ No newline at end of file diff --git a/stefanie-hansen/lib/delete.js b/stefanie-hansen/lib/delete.js deleted file mode 100644 index 4352af5..0000000 --- a/stefanie-hansen/lib/delete.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict'; -const fs = require('fs'); - -module.exports = function del(req, res) { - let id = req.params.id; - fs.unlink(__dirname + `/../data/${id}.json`, (err) => { - if (err) { - console.log('Error deleting file', err); - res.sendStatus(404); - } else { - res.send(`File ${id}.json successfully deleted`); - } - }); -}; diff --git a/stefanie-hansen/lib/get.js b/stefanie-hansen/lib/get.js deleted file mode 100644 index 57c98fe..0000000 --- a/stefanie-hansen/lib/get.js +++ /dev/null @@ -1,24 +0,0 @@ -'use strict'; -const fs = require('fs'); - -module.exports = function get(req, res) { - let id = req.params.id; - fs.readdir(__dirname + '/../data', (err, files) => { - if (err) { - console.log('Error reading directory', err); - res.sendStatus(404); - } - else if (files.indexOf(`${id}.json`) > -1) { - fs.readFile(__dirname + `/../data/${id}.json`, (err, data) => { - if (err) { - console.log('Error reading file', err); - res.sendStatus(404); - } else { - res.send(`You requested the contents of ${id}.json: \n${data.toString()}`); - } - }); - } else { - res.sendStatus(404); - } - }); -}; diff --git a/stefanie-hansen/lib/plant-routes.js b/stefanie-hansen/lib/plant-routes.js index f6f1b61..f9d5484 100644 --- a/stefanie-hansen/lib/plant-routes.js +++ b/stefanie-hansen/lib/plant-routes.js @@ -2,15 +2,60 @@ const express = require('express'); const router = express.Router(); -const get = require('./get'); -const post = require('./post'); -const put = require('./put'); -const del = require('./delete'); +const Plant = require('../schema/plant'); -router.route('/:id') - .get(get) - .put(put) - .post(post) - .delete(del); +router.get('/', (req, res, next) => { + Plant.find({}, (err, data) => { + if (err) return next(err); + else res.json(data); + }); +}); + +router.put('/', (req, res, next) => { + if (!req.body) return res.sendStatus(400); + let _id = req.body._id; + Plant.findOneAndUpdate({_id}, req.body, (err, data) => { + if (err) return next(err); + return res.json(data); + }); +}); + +router.post('/', (req, res, next) => { + if (!req.body) { + return res.sendStatus(400); + } + else { + Plant.findOne( + { + commonName: req.body.commonName, + scientificName: req.body.scientificName, + medicinalUses: req.body.medicinalUses, + zone: req.body.zone + }, (err, plant) => { + if (err) return next(err); + else { + if (!plant) { + let newPlant = new Plant(req.body); + newPlant.save((err, data) => { + if (err) return next(err); + else return res.json(data); + }); + } else { + return res.sendStatus(400); + } + } + }); + } +}); + +router.delete('/:id', (req, res, next) => { + let _id = req.params.id; + Plant.findOneAndRemove({_id}, null, (err, data) => { + if (err) return next(err); + else { + return res.send(`Deleted plant with ID of ${req.params.id}`); + } + }); +}); module.exports = router; diff --git a/stefanie-hansen/lib/post.js b/stefanie-hansen/lib/post.js deleted file mode 100644 index 9e1481a..0000000 --- a/stefanie-hansen/lib/post.js +++ /dev/null @@ -1,27 +0,0 @@ -'use strict'; -const fs = require('fs'); - -module.exports = function post(req, res) { - let id = req.params.id; - if (!req.body) { - res.sendStatus(400); - } else { - // Does file exist already? - fs.readFile(__dirname + `/../data/${id}.json`, (err) => { - // If no files exists, write one. - if (err) { - fs.writeFile(__dirname + `/../data/${id}.json`, JSON.stringify(req.body), (err) => { - if (err) { - console.log(err); - res.sendStatus(404); - } else { - res.send(`Received request and wrote file ${id}.json with the following contents: \n${JSON.stringify(req.body)}`); - } - }); - // If file already exists, don't overwrite and send back a bad request. - } else { - res.sendStatus(400); - } - }); - } -}; diff --git a/stefanie-hansen/lib/put.js b/stefanie-hansen/lib/put.js deleted file mode 100644 index d0ab114..0000000 --- a/stefanie-hansen/lib/put.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict'; -const fs = require('fs'); - -module.exports = function put(req, res) { - let id = req.params.id; - if (!req.body) { - res.sendStatus(400); - } else { - fs.writeFile(__dirname + `/../data/${id}.json`, JSON.stringify(req.body), (err) => { - if (err) console.log(err); - res.send(`Received request and wrote file ${id}.json with the following contents: \n${JSON.stringify(req.body)}`); - }); - } -}; diff --git a/stefanie-hansen/package.json b/stefanie-hansen/package.json index 938aa73..0051b1a 100644 --- a/stefanie-hansen/package.json +++ b/stefanie-hansen/package.json @@ -1,16 +1,17 @@ { "name": "stefanie-hansen", "version": "1.0.0", - "description": "", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node server.js" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { - "express": "^4.13.4" + "express": "^4.13.4", + "mongoose": "^4.4.19" }, "devDependencies": { "body-parser": "^1.15.1", @@ -23,6 +24,11 @@ "gulp": "^3.9.1", "gulp-eslint": "^2.0.0", "gulp-mocha": "^2.2.0", - "mocha": "^2.5.1" - } + "mocha": "^2.5.2", + "morgan": "^1.7.0" + }, + "directories": { + "test": "test" + }, + "description": "" } diff --git a/stefanie-hansen/schema/plant.js b/stefanie-hansen/schema/plant.js new file mode 100644 index 0000000..7b31c2f --- /dev/null +++ b/stefanie-hansen/schema/plant.js @@ -0,0 +1,12 @@ +'use strict'; + +const mongoose = require('mongoose'); + +const Plant = mongoose.Schema({ + commonName: {type: String, required: true}, + scientificName: {type: String, required: true}, + medicinalUses: {type: String, required: true}, + zone: {type: Number, required: true} +}); + +module.exports = mongoose.model('plant', Plant); diff --git a/stefanie-hansen/server.js b/stefanie-hansen/server.js index 13f8561..28def37 100644 --- a/stefanie-hansen/server.js +++ b/stefanie-hansen/server.js @@ -5,10 +5,19 @@ const app = express(); const bodyParser = require('body-parser'); const jsonParser = bodyParser.json(); const plantRouter = require('./lib/plant-routes'); +const mongoose = require('mongoose'); +const morgan = require('morgan'); +mongoose.connect('mongodb://localhost/dev_db'); + +app.use(morgan('dev')); app.use(jsonParser); app.use('/plants', plantRouter); +app.use((err, req, res, next) => { + res.send('Error: ', err.message); +}); + app.all('*', (req, res) => { res.sendStatus(404); }); From b7a5306c8f690abd7a064eb807cb4c9ab4d31368 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Thu, 26 May 2016 12:31:07 -0700 Subject: [PATCH 13/35] mapping out second resources- uses --- stefanie-hansen/lib/plant-routes.js | 2 +- stefanie-hansen/schema/plant.js | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/stefanie-hansen/lib/plant-routes.js b/stefanie-hansen/lib/plant-routes.js index f9d5484..38e4d9d 100644 --- a/stefanie-hansen/lib/plant-routes.js +++ b/stefanie-hansen/lib/plant-routes.js @@ -29,7 +29,7 @@ router.post('/', (req, res, next) => { { commonName: req.body.commonName, scientificName: req.body.scientificName, - medicinalUses: req.body.medicinalUses, + uses: req.body.uses, zone: req.body.zone }, (err, plant) => { if (err) return next(err); diff --git a/stefanie-hansen/schema/plant.js b/stefanie-hansen/schema/plant.js index 7b31c2f..411d7ee 100644 --- a/stefanie-hansen/schema/plant.js +++ b/stefanie-hansen/schema/plant.js @@ -2,10 +2,16 @@ const mongoose = require('mongoose'); +const Uses = mongoose.Schema({ + commonName: /*Plant.commonName,*/ + medicinal: Array, + nutritional: Array +}); + const Plant = mongoose.Schema({ commonName: {type: String, required: true}, scientificName: {type: String, required: true}, - medicinalUses: {type: String, required: true}, + uses: [Uses], zone: {type: Number, required: true} }); From 4b99def2068dd81adbd494806755230e254db130 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Thu, 26 May 2016 17:41:13 -0700 Subject: [PATCH 14/35] added second resource --- stefanie-hansen/lib/plant-routes.js | 2 +- stefanie-hansen/lib/supplement-routes.js | 61 ++++++++++++++++++++++++ stefanie-hansen/schema/plant.js | 1 - stefanie-hansen/schema/supplement.js | 11 +++++ stefanie-hansen/server.js | 2 + 5 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 stefanie-hansen/lib/supplement-routes.js create mode 100644 stefanie-hansen/schema/supplement.js diff --git a/stefanie-hansen/lib/plant-routes.js b/stefanie-hansen/lib/plant-routes.js index 38e4d9d..6863ecc 100644 --- a/stefanie-hansen/lib/plant-routes.js +++ b/stefanie-hansen/lib/plant-routes.js @@ -16,7 +16,7 @@ router.put('/', (req, res, next) => { let _id = req.body._id; Plant.findOneAndUpdate({_id}, req.body, (err, data) => { if (err) return next(err); - return res.json(data); + return res.json({"Message":"Successfully updated"}); }); }); diff --git a/stefanie-hansen/lib/supplement-routes.js b/stefanie-hansen/lib/supplement-routes.js new file mode 100644 index 0000000..c471d2f --- /dev/null +++ b/stefanie-hansen/lib/supplement-routes.js @@ -0,0 +1,61 @@ +'use strict'; + +const express = require('express'); +const router = express.Router(); +const Supplement = require('../schema/supplement'); + +router.get('/', (req, res, next) => { + Supplement.find({}, (err, data) => { + if (err) return next(err); + else res.json(data); + }); +}); + +router.put('/', (req, res, next) => { + if (!req.body) return res.sendStatus(400); + let _id = req.body._id; + Supplement.findOneAndUpdate({_id}, req.body, (err, data) => { + if (err) return next(err); + return res.json({"Message":"Successfully updated"}); + }); +}); + +router.post('/', (req, res, next) => { + if (!req.body) { + return res.sendStatus(400); + } + else { + Supplement.findOne( + { + name: req.body.name, + medicinalEffects: req.body.medicinalEffects, + sideEffects: req.body.sideEffects + sideEffects: Array + }, (err, supplement) => { + if (err) return next(err); + else { + if (!supplement) { + let newSupplement = new Supplement(req.body); + newSupplement.save((err, data) => { + if (err) return next(err); + else return res.json(data); + }); + } else { + return res.sendStatus(400); + } + } + }); + } +}); + +router.delete('/:id', (req, res, next) => { + let _id = req.params.id; + Supplement.findOneAndRemove({_id}, null, (err, data) => { + if (err) return next(err); + else { + return res.send(`Deleted supplement with ID of ${req.params.id}`); + } + }); +}); + +module.exports = router; diff --git a/stefanie-hansen/schema/plant.js b/stefanie-hansen/schema/plant.js index 411d7ee..38b5c44 100644 --- a/stefanie-hansen/schema/plant.js +++ b/stefanie-hansen/schema/plant.js @@ -3,7 +3,6 @@ const mongoose = require('mongoose'); const Uses = mongoose.Schema({ - commonName: /*Plant.commonName,*/ medicinal: Array, nutritional: Array }); diff --git a/stefanie-hansen/schema/supplement.js b/stefanie-hansen/schema/supplement.js new file mode 100644 index 0000000..e388452 --- /dev/null +++ b/stefanie-hansen/schema/supplement.js @@ -0,0 +1,11 @@ +'use strict'; + +const mongoose = require('mongoose'); + +const Supplement = mongoose.Schema({ + name: {type: String, required: true}, + medicinalEffects: {type: Array, required: true}, + sideEffects: Array +}); + +module.exports = mongoose.model('supplement', Supplement); diff --git a/stefanie-hansen/server.js b/stefanie-hansen/server.js index 28def37..e6d1a7b 100644 --- a/stefanie-hansen/server.js +++ b/stefanie-hansen/server.js @@ -5,6 +5,7 @@ const app = express(); const bodyParser = require('body-parser'); const jsonParser = bodyParser.json(); const plantRouter = require('./lib/plant-routes'); +const supplementRouter = require('./lib/supplement-routes') const mongoose = require('mongoose'); const morgan = require('morgan'); @@ -13,6 +14,7 @@ mongoose.connect('mongodb://localhost/dev_db'); app.use(morgan('dev')); app.use(jsonParser); app.use('/plants', plantRouter); +app.use('supplements', supplementRouter) app.use((err, req, res, next) => { res.send('Error: ', err.message); From c10bc7745f72ea6ee303df82e5878038144bd535 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Fri, 27 May 2016 10:11:45 -0700 Subject: [PATCH 15/35] tests mostly done, just need nonstandard crud route --- stefanie-hansen/.eslintrc | 33 ++++++ stefanie-hansen/.eslintrc.js | 33 ------ stefanie-hansen/lib/plant-routes.js | 1 - stefanie-hansen/lib/supplement-routes.js | 3 +- stefanie-hansen/schema/plant.js | 4 +- stefanie-hansen/schema/supplement.js | 2 +- stefanie-hansen/server.js | 7 +- stefanie-hansen/test/express-test.js | 118 ------------------- stefanie-hansen/test/plant-test.js | 143 +++++++++++++++++++++++ stefanie-hansen/test/supplement-test.js | 113 ++++++++++++++++++ 10 files changed, 297 insertions(+), 160 deletions(-) create mode 100644 stefanie-hansen/.eslintrc delete mode 100644 stefanie-hansen/.eslintrc.js delete mode 100644 stefanie-hansen/test/express-test.js create mode 100644 stefanie-hansen/test/plant-test.js create mode 100644 stefanie-hansen/test/supplement-test.js diff --git a/stefanie-hansen/.eslintrc b/stefanie-hansen/.eslintrc new file mode 100644 index 0000000..25322a1 --- /dev/null +++ b/stefanie-hansen/.eslintrc @@ -0,0 +1,33 @@ +{ + "env": { + "browser": true, + "es6": true, + "node": true, + "mocha": true + }, + "globals": { + "require": true + }, + "extends": "eslint:recommended", + "parserOptions": { + "sourceType": "module" + }, + "rules": { + "indent": [ + "error", + 2 + ], + "linebreak-style": [ + "error", + "unix" + ], + "quotes": [ + "error", + "single" + ], + "semi": [ + "error", + "always" + ] + } +} diff --git a/stefanie-hansen/.eslintrc.js b/stefanie-hansen/.eslintrc.js deleted file mode 100644 index f897c32..0000000 --- a/stefanie-hansen/.eslintrc.js +++ /dev/null @@ -1,33 +0,0 @@ -module.exports = { - "env": { - "browser": true, - "es6": true, - "node": true, - "mocha": true - }, - "globals": { - "require": true - }, - "extends": "eslint:recommended", - "parserOptions": { - "sourceType": "module" - }, - "rules": { - "indent": [ - "error", - 2 - ], - "linebreak-style": [ - "error", - "unix" - ], - "quotes": [ - "error", - "single" - ], - "semi": [ - "error", - "always" - ] - } -}; diff --git a/stefanie-hansen/lib/plant-routes.js b/stefanie-hansen/lib/plant-routes.js index 6863ecc..1c720dc 100644 --- a/stefanie-hansen/lib/plant-routes.js +++ b/stefanie-hansen/lib/plant-routes.js @@ -29,7 +29,6 @@ router.post('/', (req, res, next) => { { commonName: req.body.commonName, scientificName: req.body.scientificName, - uses: req.body.uses, zone: req.body.zone }, (err, plant) => { if (err) return next(err); diff --git a/stefanie-hansen/lib/supplement-routes.js b/stefanie-hansen/lib/supplement-routes.js index c471d2f..1e5e43c 100644 --- a/stefanie-hansen/lib/supplement-routes.js +++ b/stefanie-hansen/lib/supplement-routes.js @@ -29,8 +29,7 @@ router.post('/', (req, res, next) => { { name: req.body.name, medicinalEffects: req.body.medicinalEffects, - sideEffects: req.body.sideEffects - sideEffects: Array + sideEffects: req.body.sideEffects, }, (err, supplement) => { if (err) return next(err); else { diff --git a/stefanie-hansen/schema/plant.js b/stefanie-hansen/schema/plant.js index 38b5c44..aab9bf3 100644 --- a/stefanie-hansen/schema/plant.js +++ b/stefanie-hansen/schema/plant.js @@ -5,12 +5,12 @@ const mongoose = require('mongoose'); const Uses = mongoose.Schema({ medicinal: Array, nutritional: Array -}); +}) const Plant = mongoose.Schema({ commonName: {type: String, required: true}, scientificName: {type: String, required: true}, - uses: [Uses], + uses: Uses, zone: {type: Number, required: true} }); diff --git a/stefanie-hansen/schema/supplement.js b/stefanie-hansen/schema/supplement.js index e388452..4a5e055 100644 --- a/stefanie-hansen/schema/supplement.js +++ b/stefanie-hansen/schema/supplement.js @@ -5,7 +5,7 @@ const mongoose = require('mongoose'); const Supplement = mongoose.Schema({ name: {type: String, required: true}, medicinalEffects: {type: Array, required: true}, - sideEffects: Array + sideEffects: {type: Array, required: false} }); module.exports = mongoose.model('supplement', Supplement); diff --git a/stefanie-hansen/server.js b/stefanie-hansen/server.js index e6d1a7b..6f28ac0 100644 --- a/stefanie-hansen/server.js +++ b/stefanie-hansen/server.js @@ -5,16 +5,17 @@ const app = express(); const bodyParser = require('body-parser'); const jsonParser = bodyParser.json(); const plantRouter = require('./lib/plant-routes'); -const supplementRouter = require('./lib/supplement-routes') +const supplementRouter = require('./lib/supplement-routes'); const mongoose = require('mongoose'); const morgan = require('morgan'); -mongoose.connect('mongodb://localhost/dev_db'); +const dbPort = process.env.MONGOLAB_URI || 'mongodb://localhost/dev_db' +mongoose.connect(dbPort); app.use(morgan('dev')); app.use(jsonParser); app.use('/plants', plantRouter); -app.use('supplements', supplementRouter) +app.use('/supplements', supplementRouter) app.use((err, req, res, next) => { res.send('Error: ', err.message); diff --git a/stefanie-hansen/test/express-test.js b/stefanie-hansen/test/express-test.js deleted file mode 100644 index e1d37c3..0000000 --- a/stefanie-hansen/test/express-test.js +++ /dev/null @@ -1,118 +0,0 @@ -'use strict'; - -const chai = require('chai'); -const expect = chai.expect; -const chaiHTTP = require('chai-http'); -chai.use(chaiHTTP); -const request = chai.request; -const fs = require('fs'); -require('../server'); - -describe('Express router tests', () => { - - describe('Catch all tests', () => { - it('should respond to a request to a random route with an error', (done) => { - request('localhost:3000') - .get('/test') - .end((err, res) => { - expect(err).to.not.eql(null); - expect(res).to.have.status(404); - done(); - }); - }); - }); - - describe('POST route tests', () => { - let fileArray = []; - before('checking to make sure file is there before delete request', (done) => { - fs.readdir(__dirname + '/../data', (err, files) => { - fileArray = files; - }); - done(); - }); - - it('should respond to a POST request to /plants/:id without errors if a file with the url-defined name does not already exist and with an error if a file already exists. If a new file is created, it should have the content defined by the request body', (done) => { - request('localhost:3000') - .post('/plants/test') - .send({"test":"test"}) - .end((err, res) => { - if (fileArray.indexOf('test.json') === -1) { - expect(err).to.eql(null); - expect(res).to.have.status(200); - expect(fs.readFileSync(__dirname + '/../data/test.json').toString()).to.eql('{"test":"test"}'); - expect(res.text.split(' ').pop().trim()).to.eql('{"test":"test"}'); - } else { - expect(err).to.not.eql(null); - expect(res).to.have.status(400); - } - done(); - }); - }); - }); - - describe('GET route tests', () => { - it('should respond to a GET request to /plants/:id without errors', (done) => { - request('localhost:3000') - .get('/plants/test') - .end((err, res) => { - expect(err).to.eql(null); - expect(res).to.have.status(200); - done(); - }); - }); - it('should respond to a GET request to /plants/:id with the contents of the requested file.', (done) => { - request('localhost:3000') - .get('/plants/test') - .end((err, res) => { - expect(res.text.split(' ').pop().trim()).to.eql('{"test":"test"}'); - done(); - }); - }); - }); - - describe('PUT route tests', () => { - it('should respond to a PUT request to /plants/:id without errors', (done) => { - request('localhost:3000') - .put('/plants/test') - .end((err, res) => { - expect(err).to.eql(null); - expect(res).to.have.status(200); - done(); - }); - }); - it('should respond to a PUT request to /plants/:id by creating or replacing a JSON file and sending a response with the contents of that file/request to confirm', (done) => { - request('localhost:3000') - .put('/plants/test') - .send({"test":"test"}) - .end((err, res) => { - expect(fs.readFileSync(__dirname + '/../data/test.json').toString()).to.eql('{"test":"test"}'); - expect(res.text.split(' ').pop().trim()).to.eql('{"test":"test"}'); - done(); - }); - }); - }); - - describe('DELETE route tests', () => { - let oldFileArray = []; - - before('checking to make sure file is there before delete request', (done) => { - fs.readdir(__dirname + '/../data', (err, files) => { - oldFileArray = files; - }); - done(); - }); - - it('should respond to a DELETE request to /plants/:id without errors and delete the file defined by the url path', (done) => { - request('localhost:3000') - .delete('/plants/test') - .end((err, res) => { - fs.readdir(__dirname + '/../data', (err, files) => { - expect(err).to.eql(null); - expect(res).to.have.status(200); - expect(oldFileArray.length).to.not.eql(files.length); - }); - done(); - }); - }); - }); -}); diff --git a/stefanie-hansen/test/plant-test.js b/stefanie-hansen/test/plant-test.js new file mode 100644 index 0000000..21088d0 --- /dev/null +++ b/stefanie-hansen/test/plant-test.js @@ -0,0 +1,143 @@ +'use strict'; + +const chai = require('chai'); +const chaiHTTP = require('chai-http'); +chai.use(chaiHTTP); +const Plant = require('../schema/plant'); +const mongoose = require('mongoose'); + +const expect = chai.expect; +const request = chai.request; + +const dbPort = process.env.MONGLAB_URI; +require('../server'); + +describe('Plant router tests', () => { + + after((done) => { + process.env.MONGOLAB_URI = dbPort; + mongoose.connection.db.dropDatabase(() => { + done(); + }); + }); + + describe('Catch all test', () => { + + it('should respond to a request to a random route with an error', (done) => { + request('localhost:3000') + .get('/test') + .end((err, res) => { + expect(err).to.not.eql(null); + expect(res).to.have.status(404); + done(); + }); + }); + }); + + describe('tests that don\'t need data', () => { + it('should get a list of Plants', (done) => { + request('localhost:3000') + .get('/plants') + .end((err, res) => { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(Array.isArray(res.body)).to.eql(true); + done(); + }); + }); + + it('should post a plant', (done) => { + request('localhost:3000') + .post('/plants') + .send( + { + commonName:'test', + scientificName:'testus maximus', + uses: + { + medicinal:['test','test'], + nutritional:['test'] + }, + zone: 2 + }) + .end((err, res) => { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(res.body.commonName).to.eql('test'); + expect(res.body.scientificName).to.eql('testus maximus'); + expect(res.body.uses.medicinal).to.eql(['test', 'test']); + expect(res.body.uses.nutritional).to.eql(['test']); + expect(res.body.zone).to.eql(2); + done(); + }); + }); + + it('should respond with an error if an attempt is made to post a duplicate Plant', (done) => { + request('localhost:3000') + .post('/plants') + .send( + { + commonName:'test', + scientificName:'testus maximus', + uses: + { + medicinal:['test','test'], + nutritional:['test'] + }, + zone: 2 + }) + .end((err, res) => { + expect(err).to.not.eql(null); + expect(res).to.have.status(400); + done(); + }); + }); + }); + + describe('tests that need data', () => { + let testPlant; + + beforeEach((done) => { + testPlant = new Plant({ + commonName:'test', + scientificName:'testus maximus', + uses: + { + medicinal:['test','test'], + nutritional:['test'] + }, + zone: 2 + }); + + testPlant.save((err, plant) => { + if (err) return console.log('Error: ', err); + done(); + }); + }); + + it('should update a plant with a PUT request', (done) => { + testPlant.commonName = 'updatedByTest'; + request('localhost:3000') + .put('/plants/') + .send(testPlant) + .end((err, res) => { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(res.body).to.eql({Message:'Successfully updated'}); + done(); + }); + }); + + it('should delete a plant', (done) => { + console.log(testPlant); + request('localhost:3000') + .delete(`/plants/${testPlant._id}`) + .end((err, res) => { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(res.text).to.eql(`Deleted plant with ID of ${testPlant._id}`); + done(); + }); + }); + }); +}); diff --git a/stefanie-hansen/test/supplement-test.js b/stefanie-hansen/test/supplement-test.js new file mode 100644 index 0000000..9d54e86 --- /dev/null +++ b/stefanie-hansen/test/supplement-test.js @@ -0,0 +1,113 @@ +'use strict'; + +const chai = require('chai'); +const chaiHTTP = require('chai-http'); +chai.use(chaiHTTP); +const Supplement = require('../schema/supplement'); +const mongoose = require('mongoose'); + +const expect = chai.expect; +const request = chai.request; + +const dbPort = process.env.MONGLAB_URI; +require('../server'); + +describe('Supplement router tests', () => { + + after((done) => { + process.env.MONGOLAB_URI = dbPort; + mongoose.connection.db.dropDatabase(() => { + done(); + }); + }); + + describe('tests that don\'t need data', () => { + it('should get a list of Supplements', (done) => { + request('localhost:3000') + .get('/supplements') + .end((err, res) => { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(Array.isArray(res.body)).to.eql(true); + done(); + }); + }); + + it('should post a supplement', (done) => { + request('localhost:3000') + .post('/supplements') + .send( + { + name:'test', + medicinalEffects: ['test'], + sideEffects: ['test', 'test'] + }) + .end((err, res) => { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(res.body.name).to.eql('test'); + expect(res.body.medicinalEffects).to.eql(['test']); + expect(res.body.sideEffects).to.eql(['test', 'test']); + done(); + }); + }); + + it('should respond with an error if an attempt is made to post a duplicate Supplement', (done) => { + request('localhost:3000') + .post('/supplements') + .send( + { + name:'test', + medicinalEffects: ['test'], + sideEffects: ['test', 'test'] + }) + .end((err, res) => { + expect(err).to.not.eql(null); + expect(res).to.have.status(400); + done(); + }); + }); + }); + + describe('tests that need data', () => { + let testSupplement; + + beforeEach((done) => { + testSupplement = new Supplement({ + name:'test', + medicinalEffects: ['test'], + sideEffects: ['test', 'test'] + }); + + testSupplement.save((err, supplement) => { + if (err) return console.log('Error: ', err); + done(); + }); + }); + + it('should update a supplement with a PUT request', (done) => { + testSupplement.name = 'updatedByTest'; + request('localhost:3000') + .put('/supplements/') + .send(testSupplement) + .end((err, res) => { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(res.body).to.eql({Message:'Successfully updated'}); + done(); + }); + }); + + it('should delete a supplement', (done) => { + console.log(testSupplement); + request('localhost:3000') + .delete(`/supplements/${testSupplement._id}`) + .end((err, res) => { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(res.text).to.eql(`Deleted supplement with ID of ${testSupplement._id}`); + done(); + }); + }); + }); +}); From ba274e09c74b68c69902708326a97edd9800b519 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Fri, 27 May 2016 11:37:33 -0700 Subject: [PATCH 16/35] added non-crud type route to represent range of zones --- .../{lib => routes}/plant-routes.js | 15 ++++- .../{lib => routes}/supplement-routes.js | 0 stefanie-hansen/schema/plant.js | 8 +-- stefanie-hansen/server.js | 4 +- stefanie-hansen/test/plant-test.js | 65 ++++++++++++++----- 5 files changed, 66 insertions(+), 26 deletions(-) rename stefanie-hansen/{lib => routes}/plant-routes.js (76%) rename stefanie-hansen/{lib => routes}/supplement-routes.js (100%) diff --git a/stefanie-hansen/lib/plant-routes.js b/stefanie-hansen/routes/plant-routes.js similarity index 76% rename from stefanie-hansen/lib/plant-routes.js rename to stefanie-hansen/routes/plant-routes.js index 1c720dc..d76b1ec 100644 --- a/stefanie-hansen/lib/plant-routes.js +++ b/stefanie-hansen/routes/plant-routes.js @@ -3,6 +3,20 @@ const express = require('express'); const router = express.Router(); const Plant = require('../schema/plant'); +let plantsReturned; + +router.all('/zones', (req, res, next) => { + let minZone = 100; + let maxZone = 0; + Plant.find({}, (err, plants) => { + if (err) return next(err); + plants.forEach((plant) => { + if (plant.zone < minZone) minZone = plant.zone; + if (plant.zone > maxZone) maxZone = plant.zone; + }); + res.send(`The range of zones represented in the database includes ${minZone} - ${maxZone}`); + }); +}); router.get('/', (req, res, next) => { Plant.find({}, (err, data) => { @@ -29,7 +43,6 @@ router.post('/', (req, res, next) => { { commonName: req.body.commonName, scientificName: req.body.scientificName, - zone: req.body.zone }, (err, plant) => { if (err) return next(err); else { diff --git a/stefanie-hansen/lib/supplement-routes.js b/stefanie-hansen/routes/supplement-routes.js similarity index 100% rename from stefanie-hansen/lib/supplement-routes.js rename to stefanie-hansen/routes/supplement-routes.js diff --git a/stefanie-hansen/schema/plant.js b/stefanie-hansen/schema/plant.js index aab9bf3..37f1102 100644 --- a/stefanie-hansen/schema/plant.js +++ b/stefanie-hansen/schema/plant.js @@ -2,15 +2,11 @@ const mongoose = require('mongoose'); -const Uses = mongoose.Schema({ - medicinal: Array, - nutritional: Array -}) - const Plant = mongoose.Schema({ commonName: {type: String, required: true}, scientificName: {type: String, required: true}, - uses: Uses, + medicinalUses: {type: Array, required: true}, + nutritionalValue: {type: Array, required: true}, zone: {type: Number, required: true} }); diff --git a/stefanie-hansen/server.js b/stefanie-hansen/server.js index 6f28ac0..ad18424 100644 --- a/stefanie-hansen/server.js +++ b/stefanie-hansen/server.js @@ -4,8 +4,8 @@ const express = require('express'); const app = express(); const bodyParser = require('body-parser'); const jsonParser = bodyParser.json(); -const plantRouter = require('./lib/plant-routes'); -const supplementRouter = require('./lib/supplement-routes'); +const plantRouter = require('./routes/plant-routes'); +const supplementRouter = require('./routes/supplement-routes'); const mongoose = require('mongoose'); const morgan = require('morgan'); diff --git a/stefanie-hansen/test/plant-test.js b/stefanie-hansen/test/plant-test.js index 21088d0..3483320 100644 --- a/stefanie-hansen/test/plant-test.js +++ b/stefanie-hansen/test/plant-test.js @@ -53,11 +53,8 @@ describe('Plant router tests', () => { { commonName:'test', scientificName:'testus maximus', - uses: - { - medicinal:['test','test'], - nutritional:['test'] - }, + medicinalUses: ['test', 'test'], + nutritionalValue: ['test'], zone: 2 }) .end((err, res) => { @@ -65,8 +62,8 @@ describe('Plant router tests', () => { expect(res).to.have.status(200); expect(res.body.commonName).to.eql('test'); expect(res.body.scientificName).to.eql('testus maximus'); - expect(res.body.uses.medicinal).to.eql(['test', 'test']); - expect(res.body.uses.nutritional).to.eql(['test']); + expect(res.body.medicinalUses).to.eql(['test', 'test']); + expect(res.body.nutritionalValue).to.eql(['test']); expect(res.body.zone).to.eql(2); done(); }); @@ -79,11 +76,8 @@ describe('Plant router tests', () => { { commonName:'test', scientificName:'testus maximus', - uses: - { - medicinal:['test','test'], - nutritional:['test'] - }, + medicinalUses: ['test', 'test'], + nutritionalValue: ['test'], zone: 2 }) .end((err, res) => { @@ -96,16 +90,15 @@ describe('Plant router tests', () => { describe('tests that need data', () => { let testPlant; + let testPlant2; + let testPlant3; beforeEach((done) => { testPlant = new Plant({ commonName:'test', scientificName:'testus maximus', - uses: - { - medicinal:['test','test'], - nutritional:['test'] - }, + medicinalUses: ['test', 'test'], + nutritionalValue: ['test'], zone: 2 }); @@ -139,5 +132,43 @@ describe('Plant router tests', () => { done(); }); }); + + before((done) => { + + testPlant2 = new Plant({ + commonName:'test2', + scientificName:'testus maximus', + medicinalUses: ['test', 'test'], + nutritionalValue: ['test'], + zone: 100 + }); + + testPlant3 = new Plant({ + commonName:'test3', + scientificName:'testus maximus', + medicinalUses: ['test', 'test'], + nutritionalValue: ['test'], + zone: 40 + }); + + testPlant2.save((err, plant) => { + if (err) return console.log('Error: ', err); + }); + testPlant3.save((err, plant) => { + if (err) return console.log('Error: ', err); + done(); + }); + }); + + it('should get a range of zones included in the database upon any kind of request to the /plants/zones route', (done) => { + request('localhost:3000') + .get('/plants/zones') + .end((err, res) => { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(res.text).to.eql(`The range of zones represented in the database includes 2 - 100`); + done(); + }); + }); }); }); From 6a7032ff0542be73f7d843e032b4ea11e8309f89 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Fri, 27 May 2016 11:39:02 -0700 Subject: [PATCH 17/35] various linter fixes --- stefanie-hansen/routes/plant-routes.js | 1 - stefanie-hansen/server.js | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/stefanie-hansen/routes/plant-routes.js b/stefanie-hansen/routes/plant-routes.js index d76b1ec..9879bcb 100644 --- a/stefanie-hansen/routes/plant-routes.js +++ b/stefanie-hansen/routes/plant-routes.js @@ -3,7 +3,6 @@ const express = require('express'); const router = express.Router(); const Plant = require('../schema/plant'); -let plantsReturned; router.all('/zones', (req, res, next) => { let minZone = 100; diff --git a/stefanie-hansen/server.js b/stefanie-hansen/server.js index ad18424..230f30f 100644 --- a/stefanie-hansen/server.js +++ b/stefanie-hansen/server.js @@ -9,13 +9,13 @@ const supplementRouter = require('./routes/supplement-routes'); const mongoose = require('mongoose'); const morgan = require('morgan'); -const dbPort = process.env.MONGOLAB_URI || 'mongodb://localhost/dev_db' +const dbPort = process.env.MONGOLAB_URI || 'mongodb://localhost/dev_db'; mongoose.connect(dbPort); app.use(morgan('dev')); app.use(jsonParser); app.use('/plants', plantRouter); -app.use('/supplements', supplementRouter) +app.use('/supplements', supplementRouter); app.use((err, req, res, next) => { res.send('Error: ', err.message); From 0f9332de43fe71c3b8ba2159d35049942e7330ef Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Wed, 1 Jun 2016 20:15:30 -0700 Subject: [PATCH 18/35] reorganized project to fit class standards --- stefanie-hansen/.eslintrc | 70 ++++++++------- stefanie-hansen/.gitignore | 88 ++++++++++++++++++- stefanie-hansen/gulpfile.js | 9 +- stefanie-hansen/{schema => model}/plant.js | 0 .../{schema => model}/supplement.js | 0 .../{routes => route}/plant-routes.js | 0 .../{routes => route}/supplement-routes.js | 0 stefanie-hansen/server.js | 4 +- 8 files changed, 131 insertions(+), 40 deletions(-) rename stefanie-hansen/{schema => model}/plant.js (100%) rename stefanie-hansen/{schema => model}/supplement.js (100%) rename stefanie-hansen/{routes => route}/plant-routes.js (100%) rename stefanie-hansen/{routes => route}/supplement-routes.js (100%) diff --git a/stefanie-hansen/.eslintrc b/stefanie-hansen/.eslintrc index 25322a1..60c9f49 100644 --- a/stefanie-hansen/.eslintrc +++ b/stefanie-hansen/.eslintrc @@ -1,33 +1,41 @@ { - "env": { - "browser": true, - "es6": true, - "node": true, - "mocha": true - }, - "globals": { - "require": true - }, - "extends": "eslint:recommended", - "parserOptions": { - "sourceType": "module" - }, - "rules": { - "indent": [ - "error", - 2 - ], - "linebreak-style": [ - "error", - "unix" - ], - "quotes": [ - "error", - "single" - ], - "semi": [ - "error", - "always" - ] - } +rules: { +no-console: 0, +indent: [ +2, +2 +], +quotes: [ +2, +"single" +], +linebreak-style: [ +2, +"unix" +], +semi: [ +2, +"always" +] +}, +env: { +es6: true, +node: true, +browser: true, +mocha: true +}, +globals: { +describe: false, +it: false, +beforeEach: false, +afterEach: false, +before: false, +after: false +}, +ecmaFeatures: { +modules: true, +experimentalObjectRestSpread: true, +impliedStrict: true +}, +extends: "eslint:recommended" } diff --git a/stefanie-hansen/.gitignore b/stefanie-hansen/.gitignore index 4c497d6..800b29c 100644 --- a/stefanie-hansen/.gitignore +++ b/stefanie-hansen/.gitignore @@ -1,2 +1,86 @@ -/node_modules -/db + +# application specific +db/ + +# Created by https://www.gitignore.io/api/node,osx,vim + +### Node ### +# Logs +logs +*.log +npm-debug.log* + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules +jspm_packages + +# Optional npm cache directory +.npm + +# Optional REPL history +.node_repl_history + + +### OSX ### +*.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + + +### Vim ### +# swap +[._]*.s[a-w][a-z] +[._]s[a-w][a-z] +# session +Session.vim +# temporary +.netrwhist +*~ +# auto-generated tag files +tags diff --git a/stefanie-hansen/gulpfile.js b/stefanie-hansen/gulpfile.js index 021c2c1..80e8ba6 100644 --- a/stefanie-hansen/gulpfile.js +++ b/stefanie-hansen/gulpfile.js @@ -43,21 +43,20 @@ const opts = { }; -gulp.task('linter' , () => { - return gulp.src(['./*.js', './test/*.js', './lib/*.js']) +gulp.task('linter', () => { + return gulp.src(['./*.js', './schema/*.js', './route/*.js', './test/*.js', './lib/*.js']) .pipe(lint(opts)) .pipe(lint.format()); }); gulp.task('tests', () => { - return gulp.src(['./*.js', './test/*.js', './lib/*.js'], {read: false}) + return gulp.src(['./test/*.js'], {read: false}) .pipe(mocha({reporter: 'nyan'})); }); gulp.task('watch', () => { - gulp.watch(['./*.js', './test/*.js', './lib/*.js'], ['linter', 'tests']); + gulp.watch(['./*.js', './schema/*.js', './route/*.js', './test/*.js', './lib/*.js'], ['linter', 'tests']); }); gulp.task('default', ['linter', 'tests', 'watch'], () => { - }); diff --git a/stefanie-hansen/schema/plant.js b/stefanie-hansen/model/plant.js similarity index 100% rename from stefanie-hansen/schema/plant.js rename to stefanie-hansen/model/plant.js diff --git a/stefanie-hansen/schema/supplement.js b/stefanie-hansen/model/supplement.js similarity index 100% rename from stefanie-hansen/schema/supplement.js rename to stefanie-hansen/model/supplement.js diff --git a/stefanie-hansen/routes/plant-routes.js b/stefanie-hansen/route/plant-routes.js similarity index 100% rename from stefanie-hansen/routes/plant-routes.js rename to stefanie-hansen/route/plant-routes.js diff --git a/stefanie-hansen/routes/supplement-routes.js b/stefanie-hansen/route/supplement-routes.js similarity index 100% rename from stefanie-hansen/routes/supplement-routes.js rename to stefanie-hansen/route/supplement-routes.js diff --git a/stefanie-hansen/server.js b/stefanie-hansen/server.js index 230f30f..83bd017 100644 --- a/stefanie-hansen/server.js +++ b/stefanie-hansen/server.js @@ -1,7 +1,6 @@ 'use strict'; -const express = require('express'); -const app = express(); +const app = require('express')(); const bodyParser = require('body-parser'); const jsonParser = bodyParser.json(); const plantRouter = require('./routes/plant-routes'); @@ -19,6 +18,7 @@ app.use('/supplements', supplementRouter); app.use((err, req, res, next) => { res.send('Error: ', err.message); + next(err); }); app.all('*', (req, res) => { From 88a744ed2fae50968a282da88d06e4b97e4390b7 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Wed, 1 Jun 2016 21:06:05 -0700 Subject: [PATCH 19/35] added more files for user auth and project organization --- stefanie-hansen/.eslintignore | 5 +++++ stefanie-hansen/README.md | 1 + stefanie-hansen/lib/basic-auth.js | 0 stefanie-hansen/model/user.js | 11 +++++++++++ stefanie-hansen/route/auth-routes.js | 0 5 files changed, 17 insertions(+) create mode 100644 stefanie-hansen/.eslintignore create mode 100644 stefanie-hansen/README.md create mode 100644 stefanie-hansen/lib/basic-auth.js create mode 100644 stefanie-hansen/model/user.js create mode 100644 stefanie-hansen/route/auth-routes.js diff --git a/stefanie-hansen/.eslintignore b/stefanie-hansen/.eslintignore new file mode 100644 index 0000000..5b88cef --- /dev/null +++ b/stefanie-hansen/.eslintignore @@ -0,0 +1,5 @@ +**/node_modules/* +**/vendor/* +**/*.min.js +/*.md +/package.json diff --git a/stefanie-hansen/README.md b/stefanie-hansen/README.md new file mode 100644 index 0000000..bb8e157 --- /dev/null +++ b/stefanie-hansen/README.md @@ -0,0 +1 @@ +## Authenticated two-resource REST API diff --git a/stefanie-hansen/lib/basic-auth.js b/stefanie-hansen/lib/basic-auth.js new file mode 100644 index 0000000..e69de29 diff --git a/stefanie-hansen/model/user.js b/stefanie-hansen/model/user.js new file mode 100644 index 0000000..8fa0083 --- /dev/null +++ b/stefanie-hansen/model/user.js @@ -0,0 +1,11 @@ +'use strict'; + +const mongoose = require('mongoose'); +const bcrypt = require('bcrypt'); + +const User = new mongoose.Schema({ + username: {type: String, required: true}, + password: {type: String, required: true} +}); + +User.methods. diff --git a/stefanie-hansen/route/auth-routes.js b/stefanie-hansen/route/auth-routes.js new file mode 100644 index 0000000..e69de29 From 783666b529ba20e4729f63d5dcd5d5616712ad07 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Wed, 1 Jun 2016 21:15:30 -0700 Subject: [PATCH 20/35] added user model and bcrypt to package.json --- stefanie-hansen/model/user.js | 10 +++++++++- stefanie-hansen/package.json | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/stefanie-hansen/model/user.js b/stefanie-hansen/model/user.js index 8fa0083..bc90131 100644 --- a/stefanie-hansen/model/user.js +++ b/stefanie-hansen/model/user.js @@ -8,4 +8,12 @@ const User = new mongoose.Schema({ password: {type: String, required: true} }); -User.methods. +User.methods.hashPassword = function() { + return bcrypt.hashSync(this.password, 8); +}; + +User.methods.comparePassword = function(password) { + return bcrypt.compareSync(password, this.password); +}; + +module.exports = mongoose.model('user', User); diff --git a/stefanie-hansen/package.json b/stefanie-hansen/package.json index 0051b1a..798a903 100644 --- a/stefanie-hansen/package.json +++ b/stefanie-hansen/package.json @@ -10,6 +10,7 @@ "author": "", "license": "ISC", "dependencies": { + "bcrypt": "^0.8.6", "express": "^4.13.4", "mongoose": "^4.4.19" }, From dc47d38cade5208123dcce2acb9a3c4aaf40a8fd Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Wed, 1 Jun 2016 21:36:58 -0700 Subject: [PATCH 21/35] signin route complete, jwt added to dependencies --- stefanie-hansen/model/user.js | 6 ++++++ stefanie-hansen/package.json | 1 + stefanie-hansen/route/auth-routes.js | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/stefanie-hansen/model/user.js b/stefanie-hansen/model/user.js index bc90131..152eb5b 100644 --- a/stefanie-hansen/model/user.js +++ b/stefanie-hansen/model/user.js @@ -2,6 +2,8 @@ const mongoose = require('mongoose'); const bcrypt = require('bcrypt'); +const jwt = require('jsonwebtoken'); +const secret = process.env.SECRET || 'testPass'; const User = new mongoose.Schema({ username: {type: String, required: true}, @@ -16,4 +18,8 @@ User.methods.comparePassword = function(password) { return bcrypt.compareSync(password, this.password); }; +User.methods.generateToken = function() { + return jwt.sign({_id: this._id}, secret); +} + module.exports = mongoose.model('user', User); diff --git a/stefanie-hansen/package.json b/stefanie-hansen/package.json index 798a903..c8d3abb 100644 --- a/stefanie-hansen/package.json +++ b/stefanie-hansen/package.json @@ -12,6 +12,7 @@ "dependencies": { "bcrypt": "^0.8.6", "express": "^4.13.4", + "jsonwebtoken": "^7.0.0", "mongoose": "^4.4.19" }, "devDependencies": { diff --git a/stefanie-hansen/route/auth-routes.js b/stefanie-hansen/route/auth-routes.js index e69de29..9197616 100644 --- a/stefanie-hansen/route/auth-routes.js +++ b/stefanie-hansen/route/auth-routes.js @@ -0,0 +1,18 @@ +'use strict'; + +const express = require('express'); +const bodyParser = require('body-parser').json(); +const basicAuth = require('../lib/basic-auth.js'); +const User = require('../model/user'); +const router = module.exports = express.Router(); + +router.get('/signin', basicAuth, (req, res, next) => { + let username = req.auth.username; + User.findOne({username}, (err, user) => { + if (err || !user) return next(new Error('Cannot find user')); + if (!user.comparePassword(req.auth.password)) { + return next(new Error('Invalid password')); + } + return res.json({token: user.generateToken()}); + }); +}); From 6fe3fb6d26a2dbf4b67694730bfd4079491d6b47 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Wed, 1 Jun 2016 21:39:05 -0700 Subject: [PATCH 22/35] basic auth middleware finished --- stefanie-hansen/lib/basic-auth.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/stefanie-hansen/lib/basic-auth.js b/stefanie-hansen/lib/basic-auth.js index e69de29..8de30dc 100644 --- a/stefanie-hansen/lib/basic-auth.js +++ b/stefanie-hansen/lib/basic-auth.js @@ -0,0 +1,18 @@ +'use strict'; + +module.exports = function(req, res, next) { + let authString = req.headers.authorization.split(' ').pop(); + let userBuf = new Buffer(authString, 'base64'); + let userArr = userBuf.toString().split(':'); + userBuf.fill(0); + + req.auth = { + username: userArr[0], + password: userArr[1] + }; + + if (!req.auth.username || !req.auth.password) { + return next(new Error('Username or Password missing')); + } + next(); +}; From 956d37cb4b485cc8320f3afd9870858a227f7c87 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Wed, 1 Jun 2016 21:47:30 -0700 Subject: [PATCH 23/35] signup route finished --- stefanie-hansen/route/auth-routes.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/stefanie-hansen/route/auth-routes.js b/stefanie-hansen/route/auth-routes.js index 9197616..b80c136 100644 --- a/stefanie-hansen/route/auth-routes.js +++ b/stefanie-hansen/route/auth-routes.js @@ -16,3 +16,16 @@ router.get('/signin', basicAuth, (req, res, next) => { return res.json({token: user.generateToken()}); }); }); + +router.post('signup', bodyParser, (req, res, next) => { + let newUser = new User(req.body); + newUser.password = newUser.hashPassword(); + req.body.password = null; + User.findOne({username: req.body.username}, (err, user) => { + if (err || user) return next(new Error('Could not create user')); + newUser.save((err, user) => { + if (err) return next(new Error('Could not create user')); + res.json({token: user.generateToken()}); + }); + }); +}); From f1ac52b84edf1d20a574848ae060fa1fd2f45786 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Wed, 1 Jun 2016 21:57:54 -0700 Subject: [PATCH 24/35] added jwt-auth helper --- stefanie-hansen/lib/jwt-auth.js | 22 ++++++++++++++++++++++ stefanie-hansen/route/auth-routes.js | 7 ++++--- stefanie-hansen/server.js | 10 ++++++++-- 3 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 stefanie-hansen/lib/jwt-auth.js diff --git a/stefanie-hansen/lib/jwt-auth.js b/stefanie-hansen/lib/jwt-auth.js new file mode 100644 index 0000000..b44b4b1 --- /dev/null +++ b/stefanie-hansen/lib/jwt-auth.js @@ -0,0 +1,22 @@ +'use strict'; + +const jwt = require('jsonwebtoken'); +const User = require('../model/user'); +const secret = process.env.SECRET || 'testPass'; + +module.exports = function(req, res, next) { + let token = req.body.token || req.headers.token; + if (!token) return next(new Error('No token provided')); + + try { + token = jwt.verify(token, secret); + } catch(err) { + return next(new Error('Invalid token')); + } + + User.findOne({_id: token}, (err, user) => { + if (err) return next(new Error('Cannot find user')); + req.user = user; + next(); + }); +}; diff --git a/stefanie-hansen/route/auth-routes.js b/stefanie-hansen/route/auth-routes.js index b80c136..be4efd5 100644 --- a/stefanie-hansen/route/auth-routes.js +++ b/stefanie-hansen/route/auth-routes.js @@ -2,11 +2,12 @@ const express = require('express'); const bodyParser = require('body-parser').json(); -const basicAuth = require('../lib/basic-auth.js'); +const basicAuth = require('../lib/basic-auth'); +const jwtAuth = require('../lib/jwt-auth'); const User = require('../model/user'); const router = module.exports = express.Router(); -router.get('/signin', basicAuth, (req, res, next) => { +router.get('/signin', basicAuth, jwtAuth, (req, res, next) => { let username = req.auth.username; User.findOne({username}, (err, user) => { if (err || !user) return next(new Error('Cannot find user')); @@ -17,7 +18,7 @@ router.get('/signin', basicAuth, (req, res, next) => { }); }); -router.post('signup', bodyParser, (req, res, next) => { +router.post('signup', bodyParser, jwtAuth, (req, res, next) => { let newUser = new User(req.body); newUser.password = newUser.hashPassword(); req.body.password = null; diff --git a/stefanie-hansen/server.js b/stefanie-hansen/server.js index 83bd017..ec70e44 100644 --- a/stefanie-hansen/server.js +++ b/stefanie-hansen/server.js @@ -3,19 +3,25 @@ const app = require('express')(); const bodyParser = require('body-parser'); const jsonParser = bodyParser.json(); -const plantRouter = require('./routes/plant-routes'); -const supplementRouter = require('./routes/supplement-routes'); const mongoose = require('mongoose'); const morgan = require('morgan'); +const plantRouter = require('./routes/plant-routes'); +const supplementRouter = require('./routes/supplement-routes'); +const authRouter = require('./routes/auth-routes'); const dbPort = process.env.MONGOLAB_URI || 'mongodb://localhost/dev_db'; mongoose.connect(dbPort); app.use(morgan('dev')); + app.use(jsonParser); + app.use('/plants', plantRouter); + app.use('/supplements', supplementRouter); +app.use('/', authRouter); + app.use((err, req, res, next) => { res.send('Error: ', err.message); next(err); From cf3a94ae990daa9b69802cddd40ef5e60e0929ce Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Wed, 1 Jun 2016 22:11:50 -0700 Subject: [PATCH 25/35] routes working --- stefanie-hansen/route/auth-routes.js | 2 +- stefanie-hansen/route/plant-routes.js | 2 +- stefanie-hansen/route/supplement-routes.js | 2 +- stefanie-hansen/server.js | 14 +++++++------- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/stefanie-hansen/route/auth-routes.js b/stefanie-hansen/route/auth-routes.js index be4efd5..48ab409 100644 --- a/stefanie-hansen/route/auth-routes.js +++ b/stefanie-hansen/route/auth-routes.js @@ -18,7 +18,7 @@ router.get('/signin', basicAuth, jwtAuth, (req, res, next) => { }); }); -router.post('signup', bodyParser, jwtAuth, (req, res, next) => { +router.post('/signup', bodyParser, (req, res, next) => { let newUser = new User(req.body); newUser.password = newUser.hashPassword(); req.body.password = null; diff --git a/stefanie-hansen/route/plant-routes.js b/stefanie-hansen/route/plant-routes.js index 9879bcb..0efb4af 100644 --- a/stefanie-hansen/route/plant-routes.js +++ b/stefanie-hansen/route/plant-routes.js @@ -2,7 +2,7 @@ const express = require('express'); const router = express.Router(); -const Plant = require('../schema/plant'); +const Plant = require('../model/plant'); router.all('/zones', (req, res, next) => { let minZone = 100; diff --git a/stefanie-hansen/route/supplement-routes.js b/stefanie-hansen/route/supplement-routes.js index 1e5e43c..01275b8 100644 --- a/stefanie-hansen/route/supplement-routes.js +++ b/stefanie-hansen/route/supplement-routes.js @@ -2,7 +2,7 @@ const express = require('express'); const router = express.Router(); -const Supplement = require('../schema/supplement'); +const Supplement = require('../model/supplement'); router.get('/', (req, res, next) => { Supplement.find({}, (err, data) => { diff --git a/stefanie-hansen/server.js b/stefanie-hansen/server.js index ec70e44..1aeb9cb 100644 --- a/stefanie-hansen/server.js +++ b/stefanie-hansen/server.js @@ -5,9 +5,9 @@ const bodyParser = require('body-parser'); const jsonParser = bodyParser.json(); const mongoose = require('mongoose'); const morgan = require('morgan'); -const plantRouter = require('./routes/plant-routes'); -const supplementRouter = require('./routes/supplement-routes'); -const authRouter = require('./routes/auth-routes'); +const plantRouter = require('./route/plant-routes'); +const supplementRouter = require('./route/supplement-routes'); +const authRouter = require('./route/auth-routes'); const dbPort = process.env.MONGOLAB_URI || 'mongodb://localhost/dev_db'; mongoose.connect(dbPort); @@ -22,15 +22,15 @@ app.use('/supplements', supplementRouter); app.use('/', authRouter); +app.all('*', (req, res) => { + res.sendStatus(404); +}); + app.use((err, req, res, next) => { res.send('Error: ', err.message); next(err); }); -app.all('*', (req, res) => { - res.sendStatus(404); -}); - app.listen(3000, () => { console.log('listening on 3000'); }); From 1be4a0607e4b1e35928f53ea7a03629e8c620143 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Thu, 2 Jun 2016 09:29:43 -0700 Subject: [PATCH 26/35] init auth tests page --- stefanie-hansen/test/auth-test.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 stefanie-hansen/test/auth-test.js diff --git a/stefanie-hansen/test/auth-test.js b/stefanie-hansen/test/auth-test.js new file mode 100644 index 0000000..e69de29 From f8b9b8d6d8b8be221cc9388b372282bf743ddfd5 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Thu, 2 Jun 2016 20:10:18 -0700 Subject: [PATCH 27/35] plant tests working, trying to get promises to work --- stefanie-hansen/gulpfile.js | 4 +- stefanie-hansen/lib/jwt-auth.js | 5 +- stefanie-hansen/route/auth-routes.js | 2 +- stefanie-hansen/route/plant-routes.js | 82 ++++++--- stefanie-hansen/server.js | 7 +- stefanie-hansen/test/auth-test.js | 99 +++++++++++ stefanie-hansen/test/plant-test.js | 2 +- stefanie-hansen/test/supplement-test.js | 226 ++++++++++++------------ 8 files changed, 283 insertions(+), 144 deletions(-) diff --git a/stefanie-hansen/gulpfile.js b/stefanie-hansen/gulpfile.js index 80e8ba6..8d1e7ed 100644 --- a/stefanie-hansen/gulpfile.js +++ b/stefanie-hansen/gulpfile.js @@ -44,7 +44,7 @@ const opts = { gulp.task('linter', () => { - return gulp.src(['./*.js', './schema/*.js', './route/*.js', './test/*.js', './lib/*.js']) + return gulp.src(['./*.js', './model/*.js', './route/*.js', './test/*.js', './lib/*.js']) .pipe(lint(opts)) .pipe(lint.format()); }); @@ -55,7 +55,7 @@ gulp.task('tests', () => { }); gulp.task('watch', () => { - gulp.watch(['./*.js', './schema/*.js', './route/*.js', './test/*.js', './lib/*.js'], ['linter', 'tests']); + gulp.watch(['./*.js', './model/*.js', './route/*.js', './test/*.js', './lib/*.js'], ['linter', 'tests']); }); gulp.task('default', ['linter', 'tests', 'watch'], () => { diff --git a/stefanie-hansen/lib/jwt-auth.js b/stefanie-hansen/lib/jwt-auth.js index b44b4b1..a7f7098 100644 --- a/stefanie-hansen/lib/jwt-auth.js +++ b/stefanie-hansen/lib/jwt-auth.js @@ -5,7 +5,8 @@ const User = require('../model/user'); const secret = process.env.SECRET || 'testPass'; module.exports = function(req, res, next) { - let token = req.body.token || req.headers.token; + let token = req.headers.token || req.body.token; + console.log('req from jwt', token); if (!token) return next(new Error('No token provided')); try { @@ -15,7 +16,7 @@ module.exports = function(req, res, next) { } User.findOne({_id: token}, (err, user) => { - if (err) return next(new Error('Cannot find user')); + if (err) return next(new Error('Cannot find user')); req.user = user; next(); }); diff --git a/stefanie-hansen/route/auth-routes.js b/stefanie-hansen/route/auth-routes.js index 48ab409..8e7544f 100644 --- a/stefanie-hansen/route/auth-routes.js +++ b/stefanie-hansen/route/auth-routes.js @@ -7,7 +7,7 @@ const jwtAuth = require('../lib/jwt-auth'); const User = require('../model/user'); const router = module.exports = express.Router(); -router.get('/signin', basicAuth, jwtAuth, (req, res, next) => { +router.get('/login', basicAuth, jwtAuth, (req, res, next) => { let username = req.auth.username; User.findOne({username}, (err, user) => { if (err || !user) return next(new Error('Cannot find user')); diff --git a/stefanie-hansen/route/plant-routes.js b/stefanie-hansen/route/plant-routes.js index 0efb4af..657e0fa 100644 --- a/stefanie-hansen/route/plant-routes.js +++ b/stefanie-hansen/route/plant-routes.js @@ -34,30 +34,65 @@ router.put('/', (req, res, next) => { }); router.post('/', (req, res, next) => { - if (!req.body) { - return res.sendStatus(400); - } - else { - Plant.findOne( - { - commonName: req.body.commonName, - scientificName: req.body.scientificName, - }, (err, plant) => { - if (err) return next(err); - else { - if (!plant) { - let newPlant = new Plant(req.body); - newPlant.save((err, data) => { - if (err) return next(err); - else return res.json(data); - }); - } else { - return res.sendStatus(400); - } + + let findPlant = new Promise((resolve, reject) => { + Plant.findOne({ + commonName: req.body.commonName, + scientificName: req.body.scientificName + }, (err, plant) => { + if (err || plant) { + next(new Error('Database error')); + reject(new Error('Database error')); + } + resolve(plant); + }); + }); + + let savePlant = new Promise((resolve, reject) => { + let newPlant = new Plant(req.body); + newPlant.save((err, plant) => { + if (err) { + return reject(err); } + resolve(plant); }); - } + }); + +// DON'T SAVE DUPLICATES + + if (!req.body) return res.sendStatus(400); + findPlant.then((plant) => { + return savePlant; + }).then((plant) => { + return res.json(plant); + }).catch((err) => { + console.log('error'); + return res.json(err); + }); }); +// +// +// else { +// Plant.findOne( +// { +// commonName: req.body.commonName, +// scientificName: req.body.scientificName, +// }, (err, plant) => { +// if (err) return next(err); +// else { +// if (!plant) { +// let newPlant = new Plant(req.body); +// newPlant.save((err, data) => { +// if (err) return next(err); +// else return res.json(data); +// }); +// } else { +// return res.sendStatus(400); +// } +// } +// }); +// } +// }); router.delete('/:id', (req, res, next) => { let _id = req.params.id; @@ -69,4 +104,9 @@ router.delete('/:id', (req, res, next) => { }); }); +router.use((err, req, res, next) => { + res.status(400).send('Error'); + next(err); +}); + module.exports = router; diff --git a/stefanie-hansen/server.js b/stefanie-hansen/server.js index 1aeb9cb..f40bd46 100644 --- a/stefanie-hansen/server.js +++ b/stefanie-hansen/server.js @@ -1,8 +1,7 @@ 'use strict'; const app = require('express')(); -const bodyParser = require('body-parser'); -const jsonParser = bodyParser.json(); +const bodyParser = require('body-parser').json(); const mongoose = require('mongoose'); const morgan = require('morgan'); const plantRouter = require('./route/plant-routes'); @@ -14,7 +13,7 @@ mongoose.connect(dbPort); app.use(morgan('dev')); -app.use(jsonParser); +app.use(bodyParser); app.use('/plants', plantRouter); @@ -23,7 +22,7 @@ app.use('/supplements', supplementRouter); app.use('/', authRouter); app.all('*', (req, res) => { - res.sendStatus(404); + res.status(404).json({Message:'Not Found'}); }); app.use((err, req, res, next) => { diff --git a/stefanie-hansen/test/auth-test.js b/stefanie-hansen/test/auth-test.js index e69de29..73da463 100644 --- a/stefanie-hansen/test/auth-test.js +++ b/stefanie-hansen/test/auth-test.js @@ -0,0 +1,99 @@ +// 'use strict'; +// +// const chai = require('chai'); +// const chaiHttp = require('chai-http'); +// chai.use(chaiHttp); +// const expect = chai.expect; +// const request = require('chai').request; +// const mongoose = require('mongoose'); +// const basicAuth = require('../lib/basic-auth'); +// const jwtAuth = require('../lib/jwt-auth'); +// const dbPort = process.env.MONGOLAB_URI; +// +// process.env.MONGOLAB_URI = 'mongodb://localhost/test_db'; +// require('../server'); +// +// describe('unit tests', () => { +// let authString; +// let baseString; +// let req; +// let token; +// +// baseString = new Buffer('user:pass').toString('base64'); +// authString = 'Basic ' + baseString; +// req = { +// headers: { +// authorization: authString +// } +// }; +// +// it('should decode a basic auth string into username and password', () => { +// +// basicAuth(req, {}, () => { +// expect(req.auth).to.eql({username: 'user', password: 'pass'}); +// }); +// }); +// +// describe('auth tests', () => { +// +// after((done)=> { +// process.env.MONGOLAB_URI = dbPort; +// mongoose.connection.db.dropDatabase(() => { +// done(); +// }); +// }); +// +// it('should sign up a new user', (done) => { +// request('localhost:3000') +// .post('/signup') +// .send({username:'test', password:'test'}) +// .end((err, res) => { +// token = res.body.token; +// expect(err).to.eql(null); +// expect(res).to.have.status(200); +// expect(typeof token).to.eql('string'); +// done(); +// }); +// }); +// +// // it('should find a user with a token', () => { +// // req = { +// // headers: { +// // token: token, +// // authorization: authString +// // } +// // }; +// // +// // jwtAuth(req, {}, () => { +// // expect(req.user).to.eql(null); +// // }); +// // }); +// +// it('should sign in a user with a token', (done) => { +// request('localhost:3000') +// .get('/login') +// .set('token', token) +// .auth('test', 'test') +// .end((err, res) => { +// expect(err).to.eql(null); +// expect(res).to.have.status(200); +// expect(res.body.token).to.eql(token); +// done(); +// }); +// }); +// }); +// }); +// +// describe('catch all test', () => { +// +// it('should give an error for unsupported routes', (done) => { +// request('localhost:3000') +// .get('/test') +// .end((err, res) => { +// expect(err).to.not.eql(null); +// expect(res).to.have.status(404); +// expect(res.body).to.eql({Message: 'Not Found'}); +// done(); +// }); +// }); +// }); diff --git a/stefanie-hansen/test/plant-test.js b/stefanie-hansen/test/plant-test.js index 3483320..55883e1 100644 --- a/stefanie-hansen/test/plant-test.js +++ b/stefanie-hansen/test/plant-test.js @@ -3,7 +3,7 @@ const chai = require('chai'); const chaiHTTP = require('chai-http'); chai.use(chaiHTTP); -const Plant = require('../schema/plant'); +const Plant = require('../model/plant'); const mongoose = require('mongoose'); const expect = chai.expect; diff --git a/stefanie-hansen/test/supplement-test.js b/stefanie-hansen/test/supplement-test.js index 9d54e86..daa6066 100644 --- a/stefanie-hansen/test/supplement-test.js +++ b/stefanie-hansen/test/supplement-test.js @@ -1,113 +1,113 @@ -'use strict'; - -const chai = require('chai'); -const chaiHTTP = require('chai-http'); -chai.use(chaiHTTP); -const Supplement = require('../schema/supplement'); -const mongoose = require('mongoose'); - -const expect = chai.expect; -const request = chai.request; - -const dbPort = process.env.MONGLAB_URI; -require('../server'); - -describe('Supplement router tests', () => { - - after((done) => { - process.env.MONGOLAB_URI = dbPort; - mongoose.connection.db.dropDatabase(() => { - done(); - }); - }); - - describe('tests that don\'t need data', () => { - it('should get a list of Supplements', (done) => { - request('localhost:3000') - .get('/supplements') - .end((err, res) => { - expect(err).to.eql(null); - expect(res).to.have.status(200); - expect(Array.isArray(res.body)).to.eql(true); - done(); - }); - }); - - it('should post a supplement', (done) => { - request('localhost:3000') - .post('/supplements') - .send( - { - name:'test', - medicinalEffects: ['test'], - sideEffects: ['test', 'test'] - }) - .end((err, res) => { - expect(err).to.eql(null); - expect(res).to.have.status(200); - expect(res.body.name).to.eql('test'); - expect(res.body.medicinalEffects).to.eql(['test']); - expect(res.body.sideEffects).to.eql(['test', 'test']); - done(); - }); - }); - - it('should respond with an error if an attempt is made to post a duplicate Supplement', (done) => { - request('localhost:3000') - .post('/supplements') - .send( - { - name:'test', - medicinalEffects: ['test'], - sideEffects: ['test', 'test'] - }) - .end((err, res) => { - expect(err).to.not.eql(null); - expect(res).to.have.status(400); - done(); - }); - }); - }); - - describe('tests that need data', () => { - let testSupplement; - - beforeEach((done) => { - testSupplement = new Supplement({ - name:'test', - medicinalEffects: ['test'], - sideEffects: ['test', 'test'] - }); - - testSupplement.save((err, supplement) => { - if (err) return console.log('Error: ', err); - done(); - }); - }); - - it('should update a supplement with a PUT request', (done) => { - testSupplement.name = 'updatedByTest'; - request('localhost:3000') - .put('/supplements/') - .send(testSupplement) - .end((err, res) => { - expect(err).to.eql(null); - expect(res).to.have.status(200); - expect(res.body).to.eql({Message:'Successfully updated'}); - done(); - }); - }); - - it('should delete a supplement', (done) => { - console.log(testSupplement); - request('localhost:3000') - .delete(`/supplements/${testSupplement._id}`) - .end((err, res) => { - expect(err).to.eql(null); - expect(res).to.have.status(200); - expect(res.text).to.eql(`Deleted supplement with ID of ${testSupplement._id}`); - done(); - }); - }); - }); -}); +// 'use strict'; +// +// const chai = require('chai'); +// const chaiHTTP = require('chai-http'); +// chai.use(chaiHTTP); +// const Supplement = require('../model/supplement'); +// const mongoose = require('mongoose'); +// +// const expect = chai.expect; +// const request = chai.request; +// +// const dbPort = process.env.MONGLAB_URI; +// require('../server'); +// +// describe('Supplement router tests', () => { +// +// after((done) => { +// process.env.MONGOLAB_URI = dbPort; +// mongoose.connection.db.dropDatabase(() => { +// done(); +// }); +// }); +// +// describe('tests that don\'t need data', () => { +// it('should get a list of Supplements', (done) => { +// request('localhost:3000') +// .get('/supplements') +// .end((err, res) => { +// expect(err).to.eql(null); +// expect(res).to.have.status(200); +// expect(Array.isArray(res.body)).to.eql(true); +// done(); +// }); +// }); +// +// it('should post a supplement', (done) => { +// request('localhost:3000') +// .post('/supplements') +// .send( +// { +// name:'test', +// medicinalEffects: ['test'], +// sideEffects: ['test', 'test'] +// }) +// .end((err, res) => { +// expect(err).to.eql(null); +// expect(res).to.have.status(200); +// expect(res.body.name).to.eql('test'); +// expect(res.body.medicinalEffects).to.eql(['test']); +// expect(res.body.sideEffects).to.eql(['test', 'test']); +// done(); +// }); +// }); +// +// it('should respond with an error if an attempt is made to post a duplicate Supplement', (done) => { +// request('localhost:3000') +// .post('/supplements') +// .send( +// { +// name:'test', +// medicinalEffects: ['test'], +// sideEffects: ['test', 'test'] +// }) +// .end((err, res) => { +// expect(err).to.not.eql(null); +// expect(res).to.have.status(400); +// done(); +// }); +// }); +// }); +// +// describe('tests that need data', () => { +// let testSupplement; +// +// beforeEach((done) => { +// testSupplement = new Supplement({ +// name:'test', +// medicinalEffects: ['test'], +// sideEffects: ['test', 'test'] +// }); +// +// testSupplement.save((err, supplement) => { +// if (err) return console.log('Error: ', err); +// done(); +// }); +// }); +// +// it('should update a supplement with a PUT request', (done) => { +// testSupplement.name = 'updatedByTest'; +// request('localhost:3000') +// .put('/supplements/') +// .send(testSupplement) +// .end((err, res) => { +// expect(err).to.eql(null); +// expect(res).to.have.status(200); +// expect(res.body).to.eql({Message:'Successfully updated'}); +// done(); +// }); +// }); +// +// it('should delete a supplement', (done) => { +// console.log(testSupplement); +// request('localhost:3000') +// .delete(`/supplements/${testSupplement._id}`) +// .end((err, res) => { +// expect(err).to.eql(null); +// expect(res).to.have.status(200); +// expect(res.text).to.eql(`Deleted supplement with ID of ${testSupplement._id}`); +// done(); +// }); +// }); +// }); +// }); From b434cdf8e6ed8ff0b65afdf7e55f9d7bad0e01b7 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Thu, 2 Jun 2016 20:13:06 -0700 Subject: [PATCH 28/35] all tests passing, need to figure out promises and jwt --- stefanie-hansen/route/plant-routes.js | 1 - stefanie-hansen/test/auth-test.js | 198 ++++++++++----------- stefanie-hansen/test/plant-test.js | 8 +- stefanie-hansen/test/supplement-test.js | 226 ++++++++++++------------ 4 files changed, 216 insertions(+), 217 deletions(-) diff --git a/stefanie-hansen/route/plant-routes.js b/stefanie-hansen/route/plant-routes.js index 657e0fa..8ce798a 100644 --- a/stefanie-hansen/route/plant-routes.js +++ b/stefanie-hansen/route/plant-routes.js @@ -106,7 +106,6 @@ router.delete('/:id', (req, res, next) => { router.use((err, req, res, next) => { res.status(400).send('Error'); - next(err); }); module.exports = router; diff --git a/stefanie-hansen/test/auth-test.js b/stefanie-hansen/test/auth-test.js index 73da463..a3b7ee6 100644 --- a/stefanie-hansen/test/auth-test.js +++ b/stefanie-hansen/test/auth-test.js @@ -1,99 +1,99 @@ -// 'use strict'; -// -// const chai = require('chai'); -// const chaiHttp = require('chai-http'); -// chai.use(chaiHttp); -// const expect = chai.expect; -// const request = require('chai').request; -// const mongoose = require('mongoose'); -// const basicAuth = require('../lib/basic-auth'); -// const jwtAuth = require('../lib/jwt-auth'); -// const dbPort = process.env.MONGOLAB_URI; -// -// process.env.MONGOLAB_URI = 'mongodb://localhost/test_db'; -// require('../server'); -// -// describe('unit tests', () => { -// let authString; -// let baseString; -// let req; -// let token; -// -// baseString = new Buffer('user:pass').toString('base64'); -// authString = 'Basic ' + baseString; -// req = { -// headers: { -// authorization: authString -// } -// }; -// -// it('should decode a basic auth string into username and password', () => { -// -// basicAuth(req, {}, () => { -// expect(req.auth).to.eql({username: 'user', password: 'pass'}); -// }); -// }); -// -// describe('auth tests', () => { -// -// after((done)=> { -// process.env.MONGOLAB_URI = dbPort; -// mongoose.connection.db.dropDatabase(() => { -// done(); -// }); -// }); -// -// it('should sign up a new user', (done) => { -// request('localhost:3000') -// .post('/signup') -// .send({username:'test', password:'test'}) -// .end((err, res) => { -// token = res.body.token; -// expect(err).to.eql(null); -// expect(res).to.have.status(200); -// expect(typeof token).to.eql('string'); -// done(); -// }); -// }); -// -// // it('should find a user with a token', () => { -// // req = { -// // headers: { -// // token: token, -// // authorization: authString -// // } -// // }; -// // -// // jwtAuth(req, {}, () => { -// // expect(req.user).to.eql(null); -// // }); -// // }); -// -// it('should sign in a user with a token', (done) => { -// request('localhost:3000') -// .get('/login') -// .set('token', token) -// .auth('test', 'test') -// .end((err, res) => { -// expect(err).to.eql(null); -// expect(res).to.have.status(200); -// expect(res.body.token).to.eql(token); -// done(); -// }); -// }); -// }); -// }); -// -// describe('catch all test', () => { -// -// it('should give an error for unsupported routes', (done) => { -// request('localhost:3000') -// .get('/test') -// .end((err, res) => { -// expect(err).to.not.eql(null); -// expect(res).to.have.status(404); -// expect(res.body).to.eql({Message: 'Not Found'}); -// done(); -// }); -// }); -// }); +'use strict'; + +const chai = require('chai'); +const chaiHttp = require('chai-http'); +chai.use(chaiHttp); +const expect = chai.expect; +const request = require('chai').request; +const mongoose = require('mongoose'); +const basicAuth = require('../lib/basic-auth'); +const jwtAuth = require('../lib/jwt-auth'); +const dbPort = process.env.MONGOLAB_URI; + +process.env.MONGOLAB_URI = 'mongodb://localhost/test_db'; +require('../server'); + +describe('unit tests', () => { + let authString; + let baseString; + let req; + let token; + + baseString = new Buffer('user:pass').toString('base64'); + authString = 'Basic ' + baseString; + req = { + headers: { + authorization: authString + } + }; + + it('should decode a basic auth string into username and password', () => { + + basicAuth(req, {}, () => { + expect(req.auth).to.eql({username: 'user', password: 'pass'}); + }); + }); + + describe('auth tests', () => { + + after((done)=> { + process.env.MONGOLAB_URI = dbPort; + mongoose.connection.db.dropDatabase(() => { + done(); + }); + }); + + it('should sign up a new user', (done) => { + request('localhost:3000') + .post('/signup') + .send({username:'test', password:'test'}) + .end((err, res) => { + token = res.body.token; + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(typeof token).to.eql('string'); + done(); + }); + }); + + // it('should find a user with a token', () => { + // req = { + // headers: { + // token: token, + // authorization: authString + // } + // }; + // + // jwtAuth(req, {}, () => { + // expect(req.user).to.eql(null); + // }); + // }); + + it('should sign in a user with a token', (done) => { + request('localhost:3000') + .get('/login') + .set('token', token) + .auth('test', 'test') + .end((err, res) => { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(res.body.token).to.eql(token); + done(); + }); + }); + }); +}); + +describe('catch all test', () => { + + it('should give an error for unsupported routes', (done) => { + request('localhost:3000') + .get('/test') + .end((err, res) => { + expect(err).to.not.eql(null); + expect(res).to.have.status(404); + expect(res.body).to.eql({Message: 'Not Found'}); + done(); + }); + }); +}); diff --git a/stefanie-hansen/test/plant-test.js b/stefanie-hansen/test/plant-test.js index 55883e1..3e51309 100644 --- a/stefanie-hansen/test/plant-test.js +++ b/stefanie-hansen/test/plant-test.js @@ -102,7 +102,7 @@ describe('Plant router tests', () => { zone: 2 }); - testPlant.save((err, plant) => { + testPlant.save((err) => { if (err) return console.log('Error: ', err); done(); }); @@ -151,10 +151,10 @@ describe('Plant router tests', () => { zone: 40 }); - testPlant2.save((err, plant) => { + testPlant2.save((err) => { if (err) return console.log('Error: ', err); }); - testPlant3.save((err, plant) => { + testPlant3.save((err) => { if (err) return console.log('Error: ', err); done(); }); @@ -166,7 +166,7 @@ describe('Plant router tests', () => { .end((err, res) => { expect(err).to.eql(null); expect(res).to.have.status(200); - expect(res.text).to.eql(`The range of zones represented in the database includes 2 - 100`); + expect(res.text).to.eql('The range of zones represented in the database includes 2 - 100'); done(); }); }); diff --git a/stefanie-hansen/test/supplement-test.js b/stefanie-hansen/test/supplement-test.js index daa6066..25be9c6 100644 --- a/stefanie-hansen/test/supplement-test.js +++ b/stefanie-hansen/test/supplement-test.js @@ -1,113 +1,113 @@ -// 'use strict'; -// -// const chai = require('chai'); -// const chaiHTTP = require('chai-http'); -// chai.use(chaiHTTP); -// const Supplement = require('../model/supplement'); -// const mongoose = require('mongoose'); -// -// const expect = chai.expect; -// const request = chai.request; -// -// const dbPort = process.env.MONGLAB_URI; -// require('../server'); -// -// describe('Supplement router tests', () => { -// -// after((done) => { -// process.env.MONGOLAB_URI = dbPort; -// mongoose.connection.db.dropDatabase(() => { -// done(); -// }); -// }); -// -// describe('tests that don\'t need data', () => { -// it('should get a list of Supplements', (done) => { -// request('localhost:3000') -// .get('/supplements') -// .end((err, res) => { -// expect(err).to.eql(null); -// expect(res).to.have.status(200); -// expect(Array.isArray(res.body)).to.eql(true); -// done(); -// }); -// }); -// -// it('should post a supplement', (done) => { -// request('localhost:3000') -// .post('/supplements') -// .send( -// { -// name:'test', -// medicinalEffects: ['test'], -// sideEffects: ['test', 'test'] -// }) -// .end((err, res) => { -// expect(err).to.eql(null); -// expect(res).to.have.status(200); -// expect(res.body.name).to.eql('test'); -// expect(res.body.medicinalEffects).to.eql(['test']); -// expect(res.body.sideEffects).to.eql(['test', 'test']); -// done(); -// }); -// }); -// -// it('should respond with an error if an attempt is made to post a duplicate Supplement', (done) => { -// request('localhost:3000') -// .post('/supplements') -// .send( -// { -// name:'test', -// medicinalEffects: ['test'], -// sideEffects: ['test', 'test'] -// }) -// .end((err, res) => { -// expect(err).to.not.eql(null); -// expect(res).to.have.status(400); -// done(); -// }); -// }); -// }); -// -// describe('tests that need data', () => { -// let testSupplement; -// -// beforeEach((done) => { -// testSupplement = new Supplement({ -// name:'test', -// medicinalEffects: ['test'], -// sideEffects: ['test', 'test'] -// }); -// -// testSupplement.save((err, supplement) => { -// if (err) return console.log('Error: ', err); -// done(); -// }); -// }); -// -// it('should update a supplement with a PUT request', (done) => { -// testSupplement.name = 'updatedByTest'; -// request('localhost:3000') -// .put('/supplements/') -// .send(testSupplement) -// .end((err, res) => { -// expect(err).to.eql(null); -// expect(res).to.have.status(200); -// expect(res.body).to.eql({Message:'Successfully updated'}); -// done(); -// }); -// }); -// -// it('should delete a supplement', (done) => { -// console.log(testSupplement); -// request('localhost:3000') -// .delete(`/supplements/${testSupplement._id}`) -// .end((err, res) => { -// expect(err).to.eql(null); -// expect(res).to.have.status(200); -// expect(res.text).to.eql(`Deleted supplement with ID of ${testSupplement._id}`); -// done(); -// }); -// }); -// }); -// }); +'use strict'; + +const chai = require('chai'); +const chaiHTTP = require('chai-http'); +chai.use(chaiHTTP); +const Supplement = require('../model/supplement'); +const mongoose = require('mongoose'); + +const expect = chai.expect; +const request = chai.request; + +const dbPort = process.env.MONGLAB_URI; +require('../server'); + +describe('Supplement router tests', () => { + + after((done) => { + process.env.MONGOLAB_URI = dbPort; + mongoose.connection.db.dropDatabase(() => { + done(); + }); + }); + + describe('tests that don\'t need data', () => { + it('should get a list of Supplements', (done) => { + request('localhost:3000') + .get('/supplements') + .end((err, res) => { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(Array.isArray(res.body)).to.eql(true); + done(); + }); + }); + + it('should post a supplement', (done) => { + request('localhost:3000') + .post('/supplements') + .send( + { + name:'test', + medicinalEffects: ['test'], + sideEffects: ['test', 'test'] + }) + .end((err, res) => { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(res.body.name).to.eql('test'); + expect(res.body.medicinalEffects).to.eql(['test']); + expect(res.body.sideEffects).to.eql(['test', 'test']); + done(); + }); + }); + + it('should respond with an error if an attempt is made to post a duplicate Supplement', (done) => { + request('localhost:3000') + .post('/supplements') + .send( + { + name:'test', + medicinalEffects: ['test'], + sideEffects: ['test', 'test'] + }) + .end((err, res) => { + expect(err).to.not.eql(null); + expect(res).to.have.status(400); + done(); + }); + }); + }); + + describe('tests that need data', () => { + let testSupplement; + + beforeEach((done) => { + testSupplement = new Supplement({ + name:'test', + medicinalEffects: ['test'], + sideEffects: ['test', 'test'] + }); + + testSupplement.save((err, supplement) => { + if (err) return console.log('Error: ', err); + done(); + }); + }); + + it('should update a supplement with a PUT request', (done) => { + testSupplement.name = 'updatedByTest'; + request('localhost:3000') + .put('/supplements/') + .send(testSupplement) + .end((err, res) => { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(res.body).to.eql({Message:'Successfully updated'}); + done(); + }); + }); + + it('should delete a supplement', (done) => { + console.log(testSupplement); + request('localhost:3000') + .delete(`/supplements/${testSupplement._id}`) + .end((err, res) => { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(res.text).to.eql(`Deleted supplement with ID of ${testSupplement._id}`); + done(); + }); + }); + }); +}); From c62470b77e66e7ff18a963183ed454145922b41f Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Thu, 2 Jun 2016 20:16:41 -0700 Subject: [PATCH 29/35] fixing linter errors --- stefanie-hansen/model/user.js | 2 +- stefanie-hansen/route/plant-routes.js | 8 ++++---- stefanie-hansen/route/supplement-routes.js | 8 ++++---- stefanie-hansen/test/supplement-test.js | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/stefanie-hansen/model/user.js b/stefanie-hansen/model/user.js index 152eb5b..6a811f4 100644 --- a/stefanie-hansen/model/user.js +++ b/stefanie-hansen/model/user.js @@ -20,6 +20,6 @@ User.methods.comparePassword = function(password) { User.methods.generateToken = function() { return jwt.sign({_id: this._id}, secret); -} +}; module.exports = mongoose.model('user', User); diff --git a/stefanie-hansen/route/plant-routes.js b/stefanie-hansen/route/plant-routes.js index 8ce798a..d9fd269 100644 --- a/stefanie-hansen/route/plant-routes.js +++ b/stefanie-hansen/route/plant-routes.js @@ -27,9 +27,9 @@ router.get('/', (req, res, next) => { router.put('/', (req, res, next) => { if (!req.body) return res.sendStatus(400); let _id = req.body._id; - Plant.findOneAndUpdate({_id}, req.body, (err, data) => { + Plant.findOneAndUpdate({_id}, req.body, (err) => { if (err) return next(err); - return res.json({"Message":"Successfully updated"}); + return res.json({Message:'Successfully updated'}); }); }); @@ -61,7 +61,7 @@ router.post('/', (req, res, next) => { // DON'T SAVE DUPLICATES if (!req.body) return res.sendStatus(400); - findPlant.then((plant) => { + findPlant.then(() => { return savePlant; }).then((plant) => { return res.json(plant); @@ -96,7 +96,7 @@ router.post('/', (req, res, next) => { router.delete('/:id', (req, res, next) => { let _id = req.params.id; - Plant.findOneAndRemove({_id}, null, (err, data) => { + Plant.findOneAndRemove({_id}, null, (err) => { if (err) return next(err); else { return res.send(`Deleted plant with ID of ${req.params.id}`); diff --git a/stefanie-hansen/route/supplement-routes.js b/stefanie-hansen/route/supplement-routes.js index 01275b8..5ad7958 100644 --- a/stefanie-hansen/route/supplement-routes.js +++ b/stefanie-hansen/route/supplement-routes.js @@ -14,9 +14,9 @@ router.get('/', (req, res, next) => { router.put('/', (req, res, next) => { if (!req.body) return res.sendStatus(400); let _id = req.body._id; - Supplement.findOneAndUpdate({_id}, req.body, (err, data) => { + Supplement.findOneAndUpdate({_id}, req.body, (err) => { if (err) return next(err); - return res.json({"Message":"Successfully updated"}); + return res.json({Message:'Successfully updated'}); }); }); @@ -29,7 +29,7 @@ router.post('/', (req, res, next) => { { name: req.body.name, medicinalEffects: req.body.medicinalEffects, - sideEffects: req.body.sideEffects, + sideEffects: req.body.sideEffects }, (err, supplement) => { if (err) return next(err); else { @@ -49,7 +49,7 @@ router.post('/', (req, res, next) => { router.delete('/:id', (req, res, next) => { let _id = req.params.id; - Supplement.findOneAndRemove({_id}, null, (err, data) => { + Supplement.findOneAndRemove({_id}, null, (err) => { if (err) return next(err); else { return res.send(`Deleted supplement with ID of ${req.params.id}`); diff --git a/stefanie-hansen/test/supplement-test.js b/stefanie-hansen/test/supplement-test.js index 25be9c6..9797340 100644 --- a/stefanie-hansen/test/supplement-test.js +++ b/stefanie-hansen/test/supplement-test.js @@ -79,7 +79,7 @@ describe('Supplement router tests', () => { sideEffects: ['test', 'test'] }); - testSupplement.save((err, supplement) => { + testSupplement.save((err) => { if (err) return console.log('Error: ', err); done(); }); From 55208ba7f66ef3e33c453bee8c484afdd0b375e3 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Fri, 3 Jun 2016 09:45:09 -0700 Subject: [PATCH 30/35] back to old way --- stefanie-hansen/route/plant-routes.js | 75 +++++++-------------------- 1 file changed, 19 insertions(+), 56 deletions(-) diff --git a/stefanie-hansen/route/plant-routes.js b/stefanie-hansen/route/plant-routes.js index d9fd269..07d023d 100644 --- a/stefanie-hansen/route/plant-routes.js +++ b/stefanie-hansen/route/plant-routes.js @@ -34,65 +34,28 @@ router.put('/', (req, res, next) => { }); router.post('/', (req, res, next) => { - - let findPlant = new Promise((resolve, reject) => { - Plant.findOne({ - commonName: req.body.commonName, - scientificName: req.body.scientificName - }, (err, plant) => { - if (err || plant) { - next(new Error('Database error')); - reject(new Error('Database error')); - } - resolve(plant); - }); - }); - - let savePlant = new Promise((resolve, reject) => { - let newPlant = new Plant(req.body); - newPlant.save((err, plant) => { - if (err) { - return reject(err); + if (!req.body) return res.sendStatus(400); + else { + Plant.findOne( + { + commonName: req.body.commonName, + scientificName: req.body.scientificName + }, (err, plant) => { + if (err) return next(err); + else { + if (!plant) { + let newPlant = new Plant(req.body); + newPlant.save((err, data) => { + if (err) return next(err); + else return res.json(data); + }); + } else { + return res.sendStatus(400); + } } - resolve(plant); }); - }); - -// DON'T SAVE DUPLICATES - - if (!req.body) return res.sendStatus(400); - findPlant.then(() => { - return savePlant; - }).then((plant) => { - return res.json(plant); - }).catch((err) => { - console.log('error'); - return res.json(err); - }); + } }); -// -// -// else { -// Plant.findOne( -// { -// commonName: req.body.commonName, -// scientificName: req.body.scientificName, -// }, (err, plant) => { -// if (err) return next(err); -// else { -// if (!plant) { -// let newPlant = new Plant(req.body); -// newPlant.save((err, data) => { -// if (err) return next(err); -// else return res.json(data); -// }); -// } else { -// return res.sendStatus(400); -// } -// } -// }); -// } -// }); router.delete('/:id', (req, res, next) => { let _id = req.params.id; From 2049ebb4edb2e3cf41da7732f61689f2041be269 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Fri, 3 Jun 2016 10:55:33 -0700 Subject: [PATCH 31/35] delete posts are now jwt authorized --- stefanie-hansen/route/auth-routes.js | 2 +- stefanie-hansen/route/plant-routes.js | 3 ++- stefanie-hansen/route/supplement-routes.js | 3 ++- stefanie-hansen/test/auth-test.js | 14 -------------- stefanie-hansen/test/plant-test.js | 13 +++++++++++++ stefanie-hansen/test/supplement-test.js | 12 ++++++++++++ 6 files changed, 30 insertions(+), 17 deletions(-) diff --git a/stefanie-hansen/route/auth-routes.js b/stefanie-hansen/route/auth-routes.js index 8e7544f..d6321c0 100644 --- a/stefanie-hansen/route/auth-routes.js +++ b/stefanie-hansen/route/auth-routes.js @@ -7,7 +7,7 @@ const jwtAuth = require('../lib/jwt-auth'); const User = require('../model/user'); const router = module.exports = express.Router(); -router.get('/login', basicAuth, jwtAuth, (req, res, next) => { +router.get('/login', basicAuth, (req, res, next) => { let username = req.auth.username; User.findOne({username}, (err, user) => { if (err || !user) return next(new Error('Cannot find user')); diff --git a/stefanie-hansen/route/plant-routes.js b/stefanie-hansen/route/plant-routes.js index 07d023d..36c4c7d 100644 --- a/stefanie-hansen/route/plant-routes.js +++ b/stefanie-hansen/route/plant-routes.js @@ -3,6 +3,7 @@ const express = require('express'); const router = express.Router(); const Plant = require('../model/plant'); +const jwtAuth = require('../lib/jwt-auth'); router.all('/zones', (req, res, next) => { let minZone = 100; @@ -57,7 +58,7 @@ router.post('/', (req, res, next) => { } }); -router.delete('/:id', (req, res, next) => { +router.delete('/:id', jwtAuth, (req, res, next) => { let _id = req.params.id; Plant.findOneAndRemove({_id}, null, (err) => { if (err) return next(err); diff --git a/stefanie-hansen/route/supplement-routes.js b/stefanie-hansen/route/supplement-routes.js index 5ad7958..904999f 100644 --- a/stefanie-hansen/route/supplement-routes.js +++ b/stefanie-hansen/route/supplement-routes.js @@ -3,6 +3,7 @@ const express = require('express'); const router = express.Router(); const Supplement = require('../model/supplement'); +const jwtAuth = require('../lib/jwt-auth'); router.get('/', (req, res, next) => { Supplement.find({}, (err, data) => { @@ -47,7 +48,7 @@ router.post('/', (req, res, next) => { } }); -router.delete('/:id', (req, res, next) => { +router.delete('/:id', jwtAuth, (req, res, next) => { let _id = req.params.id; Supplement.findOneAndRemove({_id}, null, (err) => { if (err) return next(err); diff --git a/stefanie-hansen/test/auth-test.js b/stefanie-hansen/test/auth-test.js index a3b7ee6..5a18ee1 100644 --- a/stefanie-hansen/test/auth-test.js +++ b/stefanie-hansen/test/auth-test.js @@ -56,23 +56,9 @@ describe('unit tests', () => { }); }); - // it('should find a user with a token', () => { - // req = { - // headers: { - // token: token, - // authorization: authString - // } - // }; - // - // jwtAuth(req, {}, () => { - // expect(req.user).to.eql(null); - // }); - // }); - it('should sign in a user with a token', (done) => { request('localhost:3000') .get('/login') - .set('token', token) .auth('test', 'test') .end((err, res) => { expect(err).to.eql(null); diff --git a/stefanie-hansen/test/plant-test.js b/stefanie-hansen/test/plant-test.js index 3e51309..6dd5067 100644 --- a/stefanie-hansen/test/plant-test.js +++ b/stefanie-hansen/test/plant-test.js @@ -5,6 +5,7 @@ const chaiHTTP = require('chai-http'); chai.use(chaiHTTP); const Plant = require('../model/plant'); const mongoose = require('mongoose'); +const User = require('../model/user'); const expect = chai.expect; const request = chai.request; @@ -92,6 +93,17 @@ describe('Plant router tests', () => { let testPlant; let testPlant2; let testPlant3; + let token; + + before((done) => { + request('localhost:3000') + .post('/signup') + .send({username:'test', password:'test'}) + .end((err, res) => { + token = res.body.token; + done(); + }); + }); beforeEach((done) => { testPlant = new Plant({ @@ -125,6 +137,7 @@ describe('Plant router tests', () => { console.log(testPlant); request('localhost:3000') .delete(`/plants/${testPlant._id}`) + .set('token', token) .end((err, res) => { expect(err).to.eql(null); expect(res).to.have.status(200); diff --git a/stefanie-hansen/test/supplement-test.js b/stefanie-hansen/test/supplement-test.js index 9797340..7362ca5 100644 --- a/stefanie-hansen/test/supplement-test.js +++ b/stefanie-hansen/test/supplement-test.js @@ -71,6 +71,17 @@ describe('Supplement router tests', () => { describe('tests that need data', () => { let testSupplement; + let token; + + before((done) => { + request('localhost:3000') + .post('/signup') + .send({username:'test', password:'test'}) + .end((err, res) => { + token = res.body.token; + done(); + }); + }); beforeEach((done) => { testSupplement = new Supplement({ @@ -102,6 +113,7 @@ describe('Supplement router tests', () => { console.log(testSupplement); request('localhost:3000') .delete(`/supplements/${testSupplement._id}`) + .set('token', token) .end((err, res) => { expect(err).to.eql(null); expect(res).to.have.status(200); From 06c21629b64d7244d99184cbe57f37d3d40039a1 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Fri, 3 Jun 2016 11:15:35 -0700 Subject: [PATCH 32/35] jwt auth test done --- stefanie-hansen/lib/jwt-auth.js | 1 - stefanie-hansen/test/auth-test.js | 46 +++++++++++++++++++------ stefanie-hansen/test/supplement-test.js | 1 + 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/stefanie-hansen/lib/jwt-auth.js b/stefanie-hansen/lib/jwt-auth.js index a7f7098..57c6871 100644 --- a/stefanie-hansen/lib/jwt-auth.js +++ b/stefanie-hansen/lib/jwt-auth.js @@ -6,7 +6,6 @@ const secret = process.env.SECRET || 'testPass'; module.exports = function(req, res, next) { let token = req.headers.token || req.body.token; - console.log('req from jwt', token); if (!token) return next(new Error('No token provided')); try { diff --git a/stefanie-hansen/test/auth-test.js b/stefanie-hansen/test/auth-test.js index 5a18ee1..c467585 100644 --- a/stefanie-hansen/test/auth-test.js +++ b/stefanie-hansen/test/auth-test.js @@ -19,22 +19,48 @@ describe('unit tests', () => { let req; let token; - baseString = new Buffer('user:pass').toString('base64'); - authString = 'Basic ' + baseString; - req = { - headers: { - authorization: authString - } - }; + before((done) => { + request('localhost:3000') + .post('/signup') + .send({username:'test', password:'test'}) + .end((err, res) => { + token = res.body.token; + done(); + }); + }); it('should decode a basic auth string into username and password', () => { + baseString = new Buffer('user:pass').toString('base64'); + authString = 'Basic ' + baseString; + req = { + headers: { + authorization: authString + } + }; + basicAuth(req, {}, () => { expect(req.auth).to.eql({username: 'user', password: 'pass'}); }); }); - describe('auth tests', () => { + it('should find a user given a token for JWT authorization', (done) => { + + req = { + headers: { + token: token + } + }; + + jwtAuth(req, {}, () => { + expect(req).to.have.property('user'); + expect(req.user.username).to.eql('test'); + done(); + }); + }); + + + describe('auth route tests', () => { after((done)=> { process.env.MONGOLAB_URI = dbPort; @@ -46,7 +72,7 @@ describe('unit tests', () => { it('should sign up a new user', (done) => { request('localhost:3000') .post('/signup') - .send({username:'test', password:'test'}) + .send({username:'test2', password:'test2'}) .end((err, res) => { token = res.body.token; expect(err).to.eql(null); @@ -59,7 +85,7 @@ describe('unit tests', () => { it('should sign in a user with a token', (done) => { request('localhost:3000') .get('/login') - .auth('test', 'test') + .auth('test2', 'test2') .end((err, res) => { expect(err).to.eql(null); expect(res).to.have.status(200); diff --git a/stefanie-hansen/test/supplement-test.js b/stefanie-hansen/test/supplement-test.js index 7362ca5..39648d9 100644 --- a/stefanie-hansen/test/supplement-test.js +++ b/stefanie-hansen/test/supplement-test.js @@ -5,6 +5,7 @@ const chaiHTTP = require('chai-http'); chai.use(chaiHTTP); const Supplement = require('../model/supplement'); const mongoose = require('mongoose'); +const jwtAuth = require('../lib/jwt-auth'); const expect = chai.expect; const request = chai.request; From a21bf44471c6f3d58701a26ce32df9aa0deeeacb Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Fri, 3 Jun 2016 12:35:02 -0700 Subject: [PATCH 33/35] can't get promises to work correctly --- stefanie-hansen/model/plant.js | 1 + stefanie-hansen/model/supplement.js | 1 + stefanie-hansen/model/user.js | 1 + stefanie-hansen/route/plant-routes.js | 1 + stefanie-hansen/route/supplement-routes.js | 4 +--- stefanie-hansen/test/plant-test.js | 1 - stefanie-hansen/test/supplement-test.js | 1 - 7 files changed, 5 insertions(+), 5 deletions(-) diff --git a/stefanie-hansen/model/plant.js b/stefanie-hansen/model/plant.js index 37f1102..8651144 100644 --- a/stefanie-hansen/model/plant.js +++ b/stefanie-hansen/model/plant.js @@ -1,6 +1,7 @@ 'use strict'; const mongoose = require('mongoose'); +mongoose.Promise = require('bluebird'); const Plant = mongoose.Schema({ commonName: {type: String, required: true}, diff --git a/stefanie-hansen/model/supplement.js b/stefanie-hansen/model/supplement.js index 4a5e055..e88ce21 100644 --- a/stefanie-hansen/model/supplement.js +++ b/stefanie-hansen/model/supplement.js @@ -1,6 +1,7 @@ 'use strict'; const mongoose = require('mongoose'); +mongoose.Promise = require('bluebird'); const Supplement = mongoose.Schema({ name: {type: String, required: true}, diff --git a/stefanie-hansen/model/user.js b/stefanie-hansen/model/user.js index 6a811f4..4279916 100644 --- a/stefanie-hansen/model/user.js +++ b/stefanie-hansen/model/user.js @@ -1,6 +1,7 @@ 'use strict'; const mongoose = require('mongoose'); +mongoose.Promise = require('bluebird'); const bcrypt = require('bcrypt'); const jwt = require('jsonwebtoken'); const secret = process.env.SECRET || 'testPass'; diff --git a/stefanie-hansen/route/plant-routes.js b/stefanie-hansen/route/plant-routes.js index 36c4c7d..8d4d683 100644 --- a/stefanie-hansen/route/plant-routes.js +++ b/stefanie-hansen/route/plant-routes.js @@ -70,6 +70,7 @@ router.delete('/:id', jwtAuth, (req, res, next) => { router.use((err, req, res, next) => { res.status(400).send('Error'); + next(); }); module.exports = router; diff --git a/stefanie-hansen/route/supplement-routes.js b/stefanie-hansen/route/supplement-routes.js index 904999f..57580dc 100644 --- a/stefanie-hansen/route/supplement-routes.js +++ b/stefanie-hansen/route/supplement-routes.js @@ -22,9 +22,7 @@ router.put('/', (req, res, next) => { }); router.post('/', (req, res, next) => { - if (!req.body) { - return res.sendStatus(400); - } + if (!req.body) return res.sendStatus(400); else { Supplement.findOne( { diff --git a/stefanie-hansen/test/plant-test.js b/stefanie-hansen/test/plant-test.js index 6dd5067..ef1e910 100644 --- a/stefanie-hansen/test/plant-test.js +++ b/stefanie-hansen/test/plant-test.js @@ -5,7 +5,6 @@ const chaiHTTP = require('chai-http'); chai.use(chaiHTTP); const Plant = require('../model/plant'); const mongoose = require('mongoose'); -const User = require('../model/user'); const expect = chai.expect; const request = chai.request; diff --git a/stefanie-hansen/test/supplement-test.js b/stefanie-hansen/test/supplement-test.js index 39648d9..7362ca5 100644 --- a/stefanie-hansen/test/supplement-test.js +++ b/stefanie-hansen/test/supplement-test.js @@ -5,7 +5,6 @@ const chaiHTTP = require('chai-http'); chai.use(chaiHTTP); const Supplement = require('../model/supplement'); const mongoose = require('mongoose'); -const jwtAuth = require('../lib/jwt-auth'); const expect = chai.expect; const request = chai.request; From 9ff766287255eda1c6c19a49a6a2930e2e4a5b95 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Fri, 3 Jun 2016 13:17:21 -0700 Subject: [PATCH 34/35] linter errors, error handling changed --- stefanie-hansen/route/auth-routes.js | 1 - stefanie-hansen/route/plant-routes.js | 5 ----- 2 files changed, 6 deletions(-) diff --git a/stefanie-hansen/route/auth-routes.js b/stefanie-hansen/route/auth-routes.js index d6321c0..0935b3c 100644 --- a/stefanie-hansen/route/auth-routes.js +++ b/stefanie-hansen/route/auth-routes.js @@ -3,7 +3,6 @@ const express = require('express'); const bodyParser = require('body-parser').json(); const basicAuth = require('../lib/basic-auth'); -const jwtAuth = require('../lib/jwt-auth'); const User = require('../model/user'); const router = module.exports = express.Router(); diff --git a/stefanie-hansen/route/plant-routes.js b/stefanie-hansen/route/plant-routes.js index 8d4d683..7b9e16d 100644 --- a/stefanie-hansen/route/plant-routes.js +++ b/stefanie-hansen/route/plant-routes.js @@ -68,9 +68,4 @@ router.delete('/:id', jwtAuth, (req, res, next) => { }); }); -router.use((err, req, res, next) => { - res.status(400).send('Error'); - next(); -}); - module.exports = router; From 44f983cf637070fc819477cad93b3a0368f17580 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Fri, 3 Jun 2016 17:47:17 -0700 Subject: [PATCH 35/35] added error checking for jwt auth function --- stefanie-hansen/test/auth-test.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/stefanie-hansen/test/auth-test.js b/stefanie-hansen/test/auth-test.js index c467585..5f355f8 100644 --- a/stefanie-hansen/test/auth-test.js +++ b/stefanie-hansen/test/auth-test.js @@ -52,13 +52,27 @@ describe('unit tests', () => { } }; - jwtAuth(req, {}, () => { + jwtAuth(req, null, () => { expect(req).to.have.property('user'); expect(req.user.username).to.eql('test'); done(); }); }); + it('should error on invalid token', (done) => { + req = { + headers: { + token: 'invalid' + } + }; + + jwtAuth(req, null, (err) => { + expect(err.message).to.eql('Invalid token'); + done(); + }); + + }); + describe('auth route tests', () => {