Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merging wip/api-modules #45

Merged
merged 3 commits into from
Jul 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ services:
before_script:
- sleep 10
- echo mongo mongo_travis
script:
- make test
34 changes: 34 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions env.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';

var env = { };
var crypto = require('crypto');
Expand Down
1 change: 1 addition & 0 deletions lib/api/entries/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';

var consts = require('../../constants');
var es = require('event-stream');
Expand Down
1 change: 1 addition & 0 deletions lib/api/experiments/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';

var consts = require('../../constants');

Expand Down
18 changes: 6 additions & 12 deletions lib/api/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';

function create (env, entries, settings) {
var express = require('express'),
Expand All @@ -6,34 +7,27 @@ 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) {
console.log("API_SECRET", env.api_secret);
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;
}
Expand Down
1 change: 1 addition & 0 deletions lib/api/settings/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';

var consts = require('../../constants');

Expand Down
1 change: 1 addition & 0 deletions lib/api/status.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';

function configure (app, wares) {
var express = require('express'),
Expand Down
1 change: 1 addition & 0 deletions lib/middleware/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';

var wares = {
verifyAuthorization : require('./verify-token'),
Expand Down
1 change: 1 addition & 0 deletions lib/middleware/require-ssl.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';

function requireSSL(req, res, next) {
// Are we currently secure?
Expand Down
1 change: 1 addition & 0 deletions lib/middleware/send-json-status.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';

// Craft a JSON friendly status (or error) message.
function sendJSONStatus(res, status, title, description, warning) {
Expand Down
1 change: 1 addition & 0 deletions lib/middleware/verify-token.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';

var consts = require('../constants');
function configure (env) {
Expand Down
1 change: 1 addition & 0 deletions lib/settings.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';


var utils = require('./utils');
Expand Down
1 change: 1 addition & 0 deletions lib/storage.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';
var mongodb = require('mongodb');

function init (env) {
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"scripts": {
"start": "node server.js",
"test": "make test",
"postinstall": "node node_modules/bower/bin/bower install"
},
"engines": {
Expand All @@ -42,6 +43,8 @@
"socket.io": "^0.9.17"
},
"devDependencies": {
"supertest": "~0.13.0"
"supertest": "~0.13.0",
"should": "~4.0.4",
"mocha": "~1.20.1"
}
}
5 changes: 3 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 software = require('./package.json');
var env = require('./env')( );
var package = require('./package.json');
var store = require('./lib/storage')(env);


Expand All @@ -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.

Expand Down
70 changes: 70 additions & 0 deletions tests/api.entries.test.js
Original file line number Diff line number Diff line change
@@ -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);
})
;

});

});

1 change: 1 addition & 0 deletions tests/fixtures/example.json
Original file line number Diff line number Diff line change
@@ -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"}]
30 changes: 30 additions & 0 deletions tests/fixtures/example.tsv
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions tests/fixtures/load.js
Original file line number Diff line number Diff line change
@@ -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;