From 122bdb640aaf04075c58d6ebd684e839c261ad68 Mon Sep 17 00:00:00 2001 From: Ben West Date: Fri, 18 Jul 2014 12:14:40 -0700 Subject: [PATCH 1/2] cleanup, use strict everywhere --- env.js | 1 + lib/api/entries/index.js | 1 + lib/api/experiments/index.js | 1 + lib/api/index.js | 18 ++++++------------ lib/api/settings/index.js | 1 + lib/api/status.js | 1 + lib/middleware/index.js | 1 + lib/middleware/require-ssl.js | 1 + lib/middleware/send-json-status.js | 1 + lib/middleware/verify-token.js | 1 + lib/settings.js | 1 + lib/storage.js | 1 + server.js | 5 +++-- 13 files changed, 20 insertions(+), 14 deletions(-) diff --git a/env.js b/env.js index 18694cd1df4..d0eba0c0bd0 100644 --- a/env.js +++ b/env.js @@ -1,3 +1,4 @@ +'use strict'; var env = { }; var crypto = require('crypto'); diff --git a/lib/api/entries/index.js b/lib/api/entries/index.js index cadebecda41..69d1f3c22b6 100644 --- a/lib/api/entries/index.js +++ b/lib/api/entries/index.js @@ -1,3 +1,4 @@ +'use strict'; var consts = require('../../constants'); var es = require('event-stream'); diff --git a/lib/api/experiments/index.js b/lib/api/experiments/index.js index 5d5ea0f9b99..b0dca1ed408 100644 --- a/lib/api/experiments/index.js +++ b/lib/api/experiments/index.js @@ -1,3 +1,4 @@ +'use strict'; var consts = require('../../constants'); diff --git a/lib/api/index.js b/lib/api/index.js index b5a2ccd592b..29c69115a0f 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -1,3 +1,4 @@ +'use strict'; function create (env, entries, settings) { var express = require('express'), @@ -6,6 +7,7 @@ function create (env, entries, settings) { var wares = require('../middleware/')(env); + // set up express app with our options // Only allow access to the API if API_SECRET is set on the server. app.disable('api'); if (env.api_secret) { @@ -13,27 +15,19 @@ function create (env, entries, settings) { app.enable('api'); } - // set up express basics app.set('title', 'Nightscout API v1'); - /* - * Start setting up routes - */ - + // Start setting up routes if (app.enabled('api')) { + // experiments app.use('/experiments', require('./experiments/')(app, wares)); } - /**********\ - * Entries - \**********/ + // Entries and settings app.use('/entries', require('./entries/')(app, wares, entries)); - - /**********\ - * Settings - \**********/ app.use('/settings', require('./settings/')(app, wares, settings)); + // Status app.use('/status', require('./status')(app, wares)); return app; } diff --git a/lib/api/settings/index.js b/lib/api/settings/index.js index 4bfd7c36fbb..815fecec0f2 100644 --- a/lib/api/settings/index.js +++ b/lib/api/settings/index.js @@ -1,3 +1,4 @@ +'use strict'; var consts = require('../../constants'); diff --git a/lib/api/status.js b/lib/api/status.js index 06b662e4c68..4be93c5ca09 100644 --- a/lib/api/status.js +++ b/lib/api/status.js @@ -1,3 +1,4 @@ +'use strict'; function configure (app, wares) { var express = require('express'), diff --git a/lib/middleware/index.js b/lib/middleware/index.js index 1c9e07544b7..57736bd090e 100644 --- a/lib/middleware/index.js +++ b/lib/middleware/index.js @@ -1,3 +1,4 @@ +'use strict'; var wares = { verifyAuthorization : require('./verify-token'), diff --git a/lib/middleware/require-ssl.js b/lib/middleware/require-ssl.js index 0c69ce70f8d..6776b54bb25 100644 --- a/lib/middleware/require-ssl.js +++ b/lib/middleware/require-ssl.js @@ -1,3 +1,4 @@ +'use strict'; function requireSSL(req, res, next) { // Are we currently secure? diff --git a/lib/middleware/send-json-status.js b/lib/middleware/send-json-status.js index f7bc377934d..974fdc70af0 100644 --- a/lib/middleware/send-json-status.js +++ b/lib/middleware/send-json-status.js @@ -1,3 +1,4 @@ +'use strict'; // Craft a JSON friendly status (or error) message. function sendJSONStatus(res, status, title, description, warning) { diff --git a/lib/middleware/verify-token.js b/lib/middleware/verify-token.js index be58e39a7d2..0a390a0b5ea 100644 --- a/lib/middleware/verify-token.js +++ b/lib/middleware/verify-token.js @@ -1,3 +1,4 @@ +'use strict'; var consts = require('../constants'); function configure (env) { diff --git a/lib/settings.js b/lib/settings.js index 4b1c6a9c7a1..c094d6bf704 100644 --- a/lib/settings.js +++ b/lib/settings.js @@ -1,3 +1,4 @@ +'use strict'; var utils = require('./utils'); diff --git a/lib/storage.js b/lib/storage.js index 17ba92f4bb0..572e2b2fac3 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -1,3 +1,4 @@ +'use strict'; var mongodb = require('mongodb'); function init (env) { diff --git a/server.js b/server.js index 4948c5ecd0f..c9854fd833c 100644 --- a/server.js +++ b/server.js @@ -13,13 +13,14 @@ // Description: Basic web server to display data from Dexcom G4. Requires a database that contains // the Dexcom SGV data. +'use strict'; /////////////////////////////////////////////////// // DB Connection setup and utils /////////////////////////////////////////////////// var env = require('./env')( ); -var package = require('./package.json'); +var software = require('./package.json'); var store = require('./lib/storage')(env); @@ -41,7 +42,7 @@ var PORT = env.PORT; var THIRTY_DAYS = 2592000; var app = express(); -var appInfo = package.name + ' ' + package.version; +var appInfo = software.name + ' ' + software.version; app.set('title', appInfo); app.enable('trust proxy'); // Allows req.secure test on heroku https connections. From 174647a5948faa06313658b6ea20c0cf43dabbc4 Mon Sep 17 00:00:00 2001 From: Ben West Date: Sat, 19 Jul 2014 20:28:53 -0700 Subject: [PATCH 2/2] add some tests for the API --- .travis.yml | 2 ++ Makefile | 34 ++++++++++++++++++ package.json | 5 ++- server.js | 2 +- tests/api.entries.test.js | 70 +++++++++++++++++++++++++++++++++++++ tests/fixtures/example.json | 1 + tests/fixtures/example.tsv | 30 ++++++++++++++++ tests/fixtures/load.js | 23 ++++++++++++ 8 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 Makefile create mode 100644 tests/api.entries.test.js create mode 100644 tests/fixtures/example.json create mode 100644 tests/fixtures/example.tsv create mode 100644 tests/fixtures/load.js diff --git a/.travis.yml b/.travis.yml index 6f150205042..de83cdd87d5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,3 +7,5 @@ services: before_script: - sleep 10 - echo mongo mongo_travis +script: + - make test diff --git a/Makefile b/Makefile new file mode 100644 index 00000000000..bb605d08d82 --- /dev/null +++ b/Makefile @@ -0,0 +1,34 @@ + +TESTS=tests/*.js +MONGO_CONNECTION?=mongodb://localhost/test_db +CUSTOMCONNSTR_mongo_settings_collection?=test_settings +CUSTOMCONNSTR_mongo_collection?=test_sgvs + +BLANKET=--require blanket + +all: test + +travis-cov: + NODE_ENV=test node_modules/.bin/mocha ${BLANKET} -R 'travis-cov' ${TESTS} + +coveralls: + NODE_ENV=test \ + ./node_modules/.bin/mocha ${BLANKET} -R mocha-lcov-reporter \ + ${TESTS} | ./coverall.sh + +coverhtml: + ./node_modules/.bin/mocha ${BLANKET} -R html-cov ${TESTS} > tests/coverage.html + +test: + MONGO_CONNECTION=${MONGO_CONNECTION} \ + CUSTOMCONNSTR_mongo_collection=${CUSTOMCONNSTR_mongo_collection} \ + CUSTOMCONNSTR_mongo_settings_collection=${CUSTOMCONNSTR_mongo_settings_collection} \ + mocha --verbose -vvv -R tap ${TESTS} + +precover: + ./node_modules/.bin/mocha ${BLANKET} ${SHOULD} -R html-cov ${TESTS} | w3m -T text/html + + +travis: test travis-cov coveralls coverhtml + +.PHONY: test diff --git a/package.json b/package.json index 215fab3ea07..001fc3c2cb4 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ }, "scripts": { "start": "node server.js", + "test": "make test", "postinstall": "node node_modules/bower/bin/bower install" }, "engines": { @@ -42,6 +43,8 @@ "sgvdata": "0.0.2" }, "devDependencies": { - "supertest": "~0.13.0" + "supertest": "~0.13.0", + "should": "~4.0.4", + "mocha": "~1.20.1" } } diff --git a/server.js b/server.js index c9854fd833c..8921d5f9c93 100644 --- a/server.js +++ b/server.js @@ -19,8 +19,8 @@ // DB Connection setup and utils /////////////////////////////////////////////////// -var env = require('./env')( ); var software = require('./package.json'); +var env = require('./env')( ); var store = require('./lib/storage')(env); diff --git a/tests/api.entries.test.js b/tests/api.entries.test.js new file mode 100644 index 00000000000..c245222d635 --- /dev/null +++ b/tests/api.entries.test.js @@ -0,0 +1,70 @@ + +var request = require('supertest'); +var should = require('should'); +var load = require('./fixtures/load'); + +describe('Entries REST api', function ( ) { + var entries = require('../lib/api/entries/'); + before(function (done) { + var env = require('../env')( ); + this.wares = require('../lib/middleware/')(env); + var store = require('../lib/storage')(env); + this.archive = require('../lib/entries')(env.mongo_collection, store); + this.app = require('express')( ); + this.app.enable('api'); + var self = this; + store(function ( ) { + self.app.use('/entries/', entries(self.app, self.wares, self.archive)); + self.archive.create(load('json'), done); + }); + }); + after(function (done) { + this.archive( ).remove({ }, done); + }); + + it('should be a module', function ( ) { + entries.should.be.ok; + + }); + it('/entries.json', function (done) { + request(this.app) + .get('/entries/.json') + .expect(200) + .end(function (err, res) { + console.log('body', res.body); + res.body.length.should.equal(30); + done( ); + }); + + }); + + it('/entries/current.json', function (done) { + request(this.app) + .get('/entries/current.json') + .expect(200) + .end(function (err, res) { + res.body.length.should.equal(1); + done( ); + // console.log('err', err, 'res', res); + }); + + }); + + it('/entries/preview', function (done) { + + request(this.app) + .post('/entries/preview.json') + .send(load('json')) + .expect(201) + .end(function (err, res) { + console.log(res.body); + res.body.length.should.equal(30); + done( ); + // console.log('err', err, 'res', res); + }) + ; + + }); + +}); + diff --git a/tests/fixtures/example.json b/tests/fixtures/example.json new file mode 100644 index 00000000000..88da09c6fa8 --- /dev/null +++ b/tests/fixtures/example.json @@ -0,0 +1 @@ +[{"sgv":"5","dateString":"07/19/2014 10:49:15 AM","date":1405792155000,"device":"dexcom","direction":"NOT COMPUTABLE"},{"sgv":"5","dateString":"07/19/2014 10:44:15 AM","date":1405791855000,"device":"dexcom","direction":"NOT COMPUTABLE"},{"sgv":"5","dateString":"07/19/2014 10:39:15 AM","date":1405791555000,"device":"dexcom","direction":"NOT COMPUTABLE"},{"sgv":"5","dateString":"07/19/2014 10:34:15 AM","date":1405791255000,"device":"dexcom","direction":"NOT COMPUTABLE"},{"sgv":"5","dateString":"07/19/2014 10:29:15 AM","date":1405790955000,"device":"dexcom","direction":"NOT COMPUTABLE"},{"sgv":"5","dateString":"07/19/2014 10:24:15 AM","date":1405790655000,"device":"dexcom","direction":"NOT COMPUTABLE"},{"sgv":"5","dateString":"07/19/2014 10:19:15 AM","date":1405790355000,"device":"dexcom","direction":"NOT COMPUTABLE"},{"sgv":"5","dateString":"07/19/2014 10:14:15 AM","date":1405790055000,"device":"dexcom","direction":"NOT COMPUTABLE"},{"sgv":"5","dateString":"07/19/2014 10:09:15 AM","date":1405789755000,"device":"dexcom","direction":"NOT COMPUTABLE"},{"sgv":"5","dateString":"07/19/2014 10:04:15 AM","date":1405789455000,"device":"dexcom","direction":"NOT COMPUTABLE"},{"sgv":"5","dateString":"07/19/2014 09:59:15 AM","date":1405789155000,"device":"dexcom","direction":"NOT COMPUTABLE"},{"sgv":"5","dateString":"07/19/2014 09:54:15 AM","date":1405788855000,"device":"dexcom","direction":"NOT COMPUTABLE"},{"sgv":"178","dateString":"07/19/2014 03:59:15 AM","date":1405767555000,"device":"dexcom","direction":"Flat"},{"sgv":"179","dateString":"07/19/2014 03:54:15 AM","date":1405767255000,"device":"dexcom","direction":"Flat"},{"sgv":"178","dateString":"07/19/2014 03:49:15 AM","date":1405766955000,"device":"dexcom","direction":"Flat"},{"sgv":"177","dateString":"07/19/2014 03:44:15 AM","date":1405766655000,"device":"dexcom","direction":"Flat"},{"sgv":"176","dateString":"07/19/2014 03:39:15 AM","date":1405766355000,"device":"dexcom","direction":"Flat"},{"sgv":"176","dateString":"07/19/2014 03:34:15 AM","date":1405766055000,"device":"dexcom","direction":"Flat"},{"sgv":"175","dateString":"07/19/2014 03:29:16 AM","date":1405765756000,"device":"dexcom","direction":"Flat"},{"sgv":"174","dateString":"07/19/2014 03:24:15 AM","date":1405765455000,"device":"dexcom","direction":"Flat"},{"sgv":"174","dateString":"07/19/2014 03:19:15 AM","date":1405765155000,"device":"dexcom","direction":"Flat"},{"sgv":"175","dateString":"07/19/2014 03:14:15 AM","date":1405764855000,"device":"dexcom","direction":"Flat"},{"sgv":"176","dateString":"07/19/2014 03:09:15 AM","date":1405764555000,"device":"dexcom","direction":"Flat"},{"sgv":"176","dateString":"07/19/2014 03:04:15 AM","date":1405764255000,"device":"dexcom","direction":"Flat"},{"sgv":"173","dateString":"07/19/2014 02:59:15 AM","date":1405763955000,"device":"dexcom","direction":"Flat"},{"sgv":"171","dateString":"07/19/2014 02:54:15 AM","date":1405763655000,"device":"dexcom","direction":"Flat"},{"sgv":"170","dateString":"07/19/2014 02:49:15 AM","date":1405763355000,"device":"dexcom","direction":"Flat"},{"sgv":"171","dateString":"07/19/2014 02:44:15 AM","date":1405763055000,"device":"dexcom","direction":"Flat"},{"sgv":"169","dateString":"07/19/2014 02:39:15 AM","date":1405762755000,"device":"dexcom","direction":"Flat"},{"sgv":"169","dateString":"07/19/2014 02:34:15 AM","date":1405762455000,"device":"dexcom","direction":"Flat"}] \ No newline at end of file diff --git a/tests/fixtures/example.tsv b/tests/fixtures/example.tsv new file mode 100644 index 00000000000..da518e1003e --- /dev/null +++ b/tests/fixtures/example.tsv @@ -0,0 +1,30 @@ +07/19/2014 10:44:15 AM 1405791855000 5 NOT COMPUTABLE dexcom +07/19/2014 10:39:15 AM 1405791555000 5 NOT COMPUTABLE dexcom +07/19/2014 10:34:15 AM 1405791255000 5 NOT COMPUTABLE dexcom +07/19/2014 10:29:15 AM 1405790955000 5 NOT COMPUTABLE dexcom +07/19/2014 10:24:15 AM 1405790655000 5 NOT COMPUTABLE dexcom +07/19/2014 10:19:15 AM 1405790355000 5 NOT COMPUTABLE dexcom +07/19/2014 10:14:15 AM 1405790055000 5 NOT COMPUTABLE dexcom +07/19/2014 10:09:15 AM 1405789755000 5 NOT COMPUTABLE dexcom +07/19/2014 10:04:15 AM 1405789455000 5 NOT COMPUTABLE dexcom +07/19/2014 09:59:15 AM 1405789155000 5 NOT COMPUTABLE dexcom +07/19/2014 09:54:15 AM 1405788855000 5 NOT COMPUTABLE dexcom +07/19/2014 03:59:15 AM 1405767555000 178 Flat dexcom +07/19/2014 03:54:15 AM 1405767255000 179 Flat dexcom +07/19/2014 03:49:15 AM 1405766955000 178 Flat dexcom +07/19/2014 03:44:15 AM 1405766655000 177 Flat dexcom +07/19/2014 03:39:15 AM 1405766355000 176 Flat dexcom +07/19/2014 03:34:15 AM 1405766055000 176 Flat dexcom +07/19/2014 03:29:16 AM 1405765756000 175 Flat dexcom +07/19/2014 03:24:15 AM 1405765455000 174 Flat dexcom +07/19/2014 03:19:15 AM 1405765155000 174 Flat dexcom +07/19/2014 03:14:15 AM 1405764855000 175 Flat dexcom +07/19/2014 03:09:15 AM 1405764555000 176 Flat dexcom +07/19/2014 03:04:15 AM 1405764255000 176 Flat dexcom +07/19/2014 02:59:15 AM 1405763955000 173 Flat dexcom +07/19/2014 02:54:15 AM 1405763655000 171 Flat dexcom +07/19/2014 02:49:15 AM 1405763355000 170 Flat dexcom +07/19/2014 02:44:15 AM 1405763055000 171 Flat dexcom +07/19/2014 02:39:15 AM 1405762755000 169 Flat dexcom +07/19/2014 02:34:15 AM 1405762455000 169 Flat dexcom +07/19/2014 02:29:15 AM 1405762155000 169 Flat dexcom \ No newline at end of file diff --git a/tests/fixtures/load.js b/tests/fixtures/load.js new file mode 100644 index 00000000000..ddcd2d904e5 --- /dev/null +++ b/tests/fixtures/load.js @@ -0,0 +1,23 @@ + +var dir = __dirname; +var fs = require('fs'); + +function text ( ) { + return sync(dir + '/example.txt').toString( ); +} + +function json ( ) { + return JSON.parse(sync(dir + '/example.json')); +} + +function source (src) { + return source[src]( ); +} +source.text = text; +source.json = json; + +function sync (src) { + return fs.readFileSync(src); +} +module.exports = source; +module.exports.sync = sync;