mirror of
https://github.com/bckelley/cgm-remote-monitor.git
synced 2026-08-24 03:14:12 -05:00
ctx everywhere; fixed security.test.js
This commit is contained in:
@@ -6,13 +6,8 @@ function create (env, ctx) {
|
||||
// api and json object variables
|
||||
///////////////////////////////////////////////////
|
||||
var api = require('./lib/api/')(env, ctx);
|
||||
var pebble = ctx.pebble;
|
||||
|
||||
var app = express();
|
||||
app.entries = ctx.entries;
|
||||
app.treatments = ctx.treatments;
|
||||
app.profiles = ctx.profiles;
|
||||
app.devicestatus = ctx.devicestatus;
|
||||
var appInfo = env.name + ' ' + env.version;
|
||||
app.set('title', appInfo);
|
||||
app.enable('trust proxy'); // Allows req.secure test on heroku https connections.
|
||||
@@ -32,7 +27,7 @@ function create (env, ctx) {
|
||||
|
||||
|
||||
// pebble data
|
||||
app.get('/pebble', pebble(ctx.entries, ctx.treatments, ctx.profiles, ctx.devicestatus, env));
|
||||
app.get('/pebble', ctx.pebble);
|
||||
|
||||
//app.get('/package.json', software);
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
var consts = require('../../constants');
|
||||
|
||||
function configure (app, wares, devicestatus) {
|
||||
function configure (app, wares, ctx) {
|
||||
var express = require('express'),
|
||||
api = express.Router( );
|
||||
|
||||
@@ -21,16 +21,16 @@ function configure (app, wares, devicestatus) {
|
||||
if (!q.count) {
|
||||
q.count = 10;
|
||||
}
|
||||
devicestatus.list(q, function (err, results) {
|
||||
ctx.devicestatus.list(q, function (err, results) {
|
||||
return res.json(results);
|
||||
});
|
||||
});
|
||||
|
||||
function config_authed (app, api, wares, devicestatus) {
|
||||
function config_authed (app, api, wares, ctx) {
|
||||
|
||||
api.post('/devicestatus/', /*TODO: auth disabled for quick UI testing... wares.verifyAuthorization, */ function(req, res) {
|
||||
var obj = req.body;
|
||||
devicestatus.create(obj, function (err, created) {
|
||||
ctx.devicestatus.create(obj, function (err, created) {
|
||||
if (err)
|
||||
res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err);
|
||||
else
|
||||
@@ -41,7 +41,7 @@ function configure (app, wares, devicestatus) {
|
||||
}
|
||||
|
||||
if (app.enabled('api') || true /*TODO: auth disabled for quick UI testing...*/) {
|
||||
config_authed(app, api, wares, devicestatus);
|
||||
config_authed(app, api, wares, ctx);
|
||||
}
|
||||
|
||||
return api;
|
||||
|
||||
+11
-11
@@ -7,8 +7,8 @@ var sgvdata = require('sgvdata');
|
||||
/**********\
|
||||
* Entries
|
||||
\**********/
|
||||
function configure (app, wares, core) {
|
||||
var entries = core.entries;
|
||||
function configure (app, wares, ctx) {
|
||||
var entries = ctx.entries;
|
||||
var express = require('express'),
|
||||
api = express.Router( )
|
||||
;
|
||||
@@ -128,30 +128,30 @@ function configure (app, wares, core) {
|
||||
switch (model) {
|
||||
case 'treatments':
|
||||
case 'devicestatus':
|
||||
//TODO: profiles not working now, maybe profiles are special
|
||||
//case 'profiles':
|
||||
req.model = core[model];
|
||||
//TODO: profile not working now, maybe profiles are special
|
||||
//case 'profile':
|
||||
req.model = ctx[model];
|
||||
break;
|
||||
|
||||
case 'meter':
|
||||
case 'mbg':
|
||||
find.type = 'mbg';
|
||||
req.model = core.entries;
|
||||
req.model = ctx.entries;
|
||||
break;
|
||||
case 'cal':
|
||||
find.type = 'cal';
|
||||
req.model = core.entries;
|
||||
req.model = ctx.entries;
|
||||
break;
|
||||
case 'sensor':
|
||||
find.type = 'sensor';
|
||||
req.model = core.entries;
|
||||
req.model = ctx.entries;
|
||||
break;
|
||||
case 'sgv':
|
||||
find.type = 'sgv';
|
||||
req.model = core.entries;
|
||||
req.model = ctx.entries;
|
||||
break;
|
||||
default:
|
||||
req.model = core.entries;
|
||||
req.model = ctx.entries;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -203,7 +203,7 @@ function configure (app, wares, core) {
|
||||
function query_models (req, res, next) {
|
||||
// If "?count=" is present, use that number to decided how many to return.
|
||||
if (!req.model) {
|
||||
req.model = core.entries;
|
||||
req.model = ctx.entries;
|
||||
}
|
||||
var query = req.query;
|
||||
if (!query.count) { query.count = 10 }
|
||||
|
||||
+5
-5
@@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
function create (env, core) {
|
||||
function create (env, ctx) {
|
||||
var express = require('express'),
|
||||
app = express( )
|
||||
;
|
||||
@@ -46,10 +46,10 @@ function create (env, core) {
|
||||
}
|
||||
|
||||
// Entries and settings
|
||||
app.use('/', require('./entries/')(app, wares, core));
|
||||
app.use('/', require('./treatments/')(app, wares, core.treatments));
|
||||
app.use('/', require('./profile/')(app, wares, core.profiles));
|
||||
app.use('/', require('./devicestatus/')(app, wares, core.devicestatus));
|
||||
app.use('/', require('./entries/')(app, wares, ctx));
|
||||
app.use('/', require('./treatments/')(app, wares, ctx));
|
||||
app.use('/', require('./profile/')(app, wares, ctx));
|
||||
app.use('/', require('./devicestatus/')(app, wares, ctx));
|
||||
|
||||
// Status
|
||||
app.use('/', require('./status')(app, wares));
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
var consts = require('../../constants');
|
||||
|
||||
function configure (app, wares, profile) {
|
||||
function configure (app, wares, ctx) {
|
||||
var express = require('express'),
|
||||
api = express.Router( );
|
||||
|
||||
@@ -17,24 +17,24 @@ function configure (app, wares, profile) {
|
||||
|
||||
// List profiles available
|
||||
api.get('/profile/', function(req, res) {
|
||||
profile.list(function (err, attribute) {
|
||||
ctx.profile.list(function (err, attribute) {
|
||||
return res.json(attribute);
|
||||
});
|
||||
});
|
||||
|
||||
// List current active record (in current state LAST record is current active)
|
||||
api.get('/profile/current', function(req, res) {
|
||||
profile.last( function(err, records) {
|
||||
ctx.profile.last( function(err, records) {
|
||||
return res.json(records.length > 0 ? records[0] : null);
|
||||
});
|
||||
});
|
||||
|
||||
function config_authed (app, api, wares, profile) {
|
||||
function config_authed (app, api, wares, ctx) {
|
||||
|
||||
// create new record
|
||||
api.post('/profile/', wares.verifyAuthorization, function(req, res) {
|
||||
var data = req.body;
|
||||
profile.create(data, function (err, created) {
|
||||
ctx.profile.create(data, function (err, created) {
|
||||
if (err) {
|
||||
res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err);
|
||||
console.log('Error creating profile');
|
||||
@@ -50,7 +50,7 @@ function configure (app, wares, profile) {
|
||||
// update record
|
||||
api.put('/profile/', wares.verifyAuthorization, function(req, res) {
|
||||
var data = req.body;
|
||||
profile.save(data, function (err, created) {
|
||||
ctx.profile.save(data, function (err, created) {
|
||||
if (err) {
|
||||
res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err);
|
||||
console.log('Error saving profile');
|
||||
@@ -65,7 +65,7 @@ function configure (app, wares, profile) {
|
||||
}
|
||||
|
||||
if (app.enabled('api')) {
|
||||
config_authed(app, api, wares, profile);
|
||||
config_authed(app, api, wares, ctx);
|
||||
}
|
||||
|
||||
return api;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
var consts = require('../../constants');
|
||||
|
||||
function configure (app, wares, treatments) {
|
||||
function configure (app, wares, ctx) {
|
||||
var express = require('express'),
|
||||
api = express.Router( );
|
||||
|
||||
@@ -17,16 +17,16 @@ function configure (app, wares, treatments) {
|
||||
|
||||
// List treatments available
|
||||
api.get('/treatments/', function(req, res) {
|
||||
treatments.list({find: req.params}, function (err, results) {
|
||||
ctx.treatments.list({find: req.params}, function (err, results) {
|
||||
return res.json(results);
|
||||
});
|
||||
});
|
||||
|
||||
function config_authed (app, api, wares, treatments) {
|
||||
function config_authed (app, api, wares, ctx) {
|
||||
|
||||
api.post('/treatments/', /*TODO: auth disabled for now, need to get login figured out... wares.verifyAuthorization, */ function(req, res) {
|
||||
var treatment = req.body;
|
||||
treatments.create(treatment, function (err, created) {
|
||||
ctx.treatments.create(treatment, function (err, created) {
|
||||
if (err)
|
||||
res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err);
|
||||
else
|
||||
@@ -37,7 +37,7 @@ function configure (app, wares, treatments) {
|
||||
}
|
||||
|
||||
if (app.enabled('api') && app.enabled('careportal')) {
|
||||
config_authed(app, api, wares, treatments);
|
||||
config_authed(app, api, wares, ctx);
|
||||
}
|
||||
|
||||
return api;
|
||||
|
||||
+4
-4
@@ -20,15 +20,15 @@ function boot (env) {
|
||||
ctx.pushover = require('./pushover')(env);
|
||||
ctx.entries = require('./entries')(env, ctx);
|
||||
ctx.treatments = require('./treatments')(env, ctx);
|
||||
ctx.devicestatus = require('./devicestatus')(env.devicestatus_collection, ctx.store);
|
||||
ctx.profiles = require('./profile')(env.profile_collection, ctx.store);
|
||||
ctx.pebble = require('./pebble');
|
||||
ctx.devicestatus = require('./devicestatus')(env.devicestatus_collection, ctx);
|
||||
ctx.profile = require('./profile')(env.profile_collection, ctx);
|
||||
ctx.pebble = require('./pebble')(env, ctx);
|
||||
|
||||
console.info("Ensuring indexes");
|
||||
store.ensureIndexes(ctx.entries( ), ctx.entries.indexedFields);
|
||||
store.ensureIndexes(ctx.treatments( ), ctx.treatments.indexedFields);
|
||||
store.ensureIndexes(ctx.devicestatus( ), ctx.devicestatus.indexedFields);
|
||||
store.ensureIndexes(ctx.profiles( ), ctx.profiles.indexedFields);
|
||||
store.ensureIndexes(ctx.profile( ), ctx.profile.indexedFields);
|
||||
|
||||
next( );
|
||||
})
|
||||
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
function storage (collection, storage) {
|
||||
function storage (collection, ctx) {
|
||||
|
||||
function create(obj, fn) {
|
||||
if (! obj.hasOwnProperty("created_at")){
|
||||
@@ -29,11 +29,11 @@ function storage (collection, storage) {
|
||||
|
||||
function list(opts, fn) {
|
||||
var q = opts && opts.find ? opts.find : { };
|
||||
return storage.limit.call(api().find(q).sort({created_at: -1}), opts).toArray(fn);
|
||||
return ctx.store.limit.call(api().find(q).sort({created_at: -1}), opts).toArray(fn);
|
||||
}
|
||||
|
||||
function api() {
|
||||
return storage.pool.db.collection(collection);
|
||||
return ctx.store.pool.db.collection(collection);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+8
-8
@@ -168,7 +168,7 @@ function iter_mqtt_record_stream (packet, prop, sync) {
|
||||
return stream.pipe(es.map(map));
|
||||
}
|
||||
|
||||
function configure(env, core, devicestatus) {
|
||||
function configure(env, ctx) {
|
||||
var uri = env['MQTT_MONITOR'];
|
||||
var opts = {
|
||||
encoding: 'binary',
|
||||
@@ -212,37 +212,37 @@ function configure(env, core, devicestatus) {
|
||||
console.log("WRITE TO MONGO");
|
||||
var download_timestamp = moment(packet.download_timestamp);
|
||||
if (packet.download_status === 0) {
|
||||
es.readArray(sgvSensorMerge(packet)).pipe(core.persist(function empty(err, result) {
|
||||
es.readArray(sgvSensorMerge(packet)).pipe(ctx.entries.persist(function empty(err, result) {
|
||||
console.log("DONE WRITING MERGED SGV TO MONGO", err, result);
|
||||
}));
|
||||
|
||||
iter_mqtt_record_stream(packet, 'cal', toCal)
|
||||
.pipe(core.persist(function empty(err, result) {
|
||||
.pipe(ctx.entries.persist(function empty(err, result) {
|
||||
console.log("DONE WRITING Cal TO MONGO", err, result.length);
|
||||
}));
|
||||
iter_mqtt_record_stream(packet, 'meter', toMeter)
|
||||
.pipe(core.persist(function empty(err, result) {
|
||||
.pipe(ctx.entries.persist(function empty(err, result) {
|
||||
console.log("DONE WRITING Meter TO MONGO", err, result.length);
|
||||
}));
|
||||
}
|
||||
packet.type = "download";
|
||||
devicestatus.create({
|
||||
ctx.devicestatus.create({
|
||||
uploaderBattery: packet.uploader_battery,
|
||||
created_at: download_timestamp.toISOString()
|
||||
}, function empty(err, result) {
|
||||
console.log("DONE WRITING TO MONGO devicestatus ", result, err);
|
||||
});
|
||||
|
||||
core.create([ packet ], function empty(err, res) {
|
||||
ctx.entries.create([ packet ], function empty(err, res) {
|
||||
console.log("Download written to mongo: ", packet)
|
||||
});
|
||||
|
||||
|
||||
// core.write(packet);
|
||||
// ctx.entries.write(packet);
|
||||
break;
|
||||
default:
|
||||
console.log(topic, 'on message', 'msg', msg);
|
||||
// core.write(msg);
|
||||
// ctx.entries.write(msg);
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
+7
-10
@@ -62,7 +62,7 @@ function pebble (req, res) {
|
||||
|
||||
async.parallel({
|
||||
devicestatus: function (callback) {
|
||||
req.devicestatus.last(function (err, value) {
|
||||
req.ctx.devicestatus.last(function (err, value) {
|
||||
if (!err && value) {
|
||||
uploaderBattery = value.uploaderBattery;
|
||||
} else {
|
||||
@@ -96,7 +96,7 @@ function pebble (req, res) {
|
||||
, cal: function(callback) {
|
||||
if (req.rawbg) {
|
||||
var cq = { count: req.count, find: {type: 'cal'} };
|
||||
req.entries.list(cq, function (err, results) {
|
||||
req.ctx.entries.list(cq, function (err, results) {
|
||||
if (!err && results) {
|
||||
results.forEach(function (element) {
|
||||
if (element) {
|
||||
@@ -119,7 +119,7 @@ function pebble (req, res) {
|
||||
, entries: function(callback) {
|
||||
var q = { count: req.count + 1, find: { "sgv": { $exists: true }} };
|
||||
|
||||
req.entries.list(q, function(err, results) {
|
||||
req.ctx.entries.list(q, function(err, results) {
|
||||
if (!err && results) {
|
||||
results.forEach(function(element, index) {
|
||||
if (element) {
|
||||
@@ -162,7 +162,7 @@ function pebble (req, res) {
|
||||
function loadTreatments(req, earliest_data, fn) {
|
||||
if (req.iob) {
|
||||
var q = { find: {"created_at": {"$gte": new Date(earliest_data).toISOString()}} };
|
||||
req.treatments.list(q, fn);
|
||||
req.ctx.treatments.list(q, fn);
|
||||
} else {
|
||||
fn(null, []);
|
||||
}
|
||||
@@ -170,18 +170,15 @@ function loadTreatments(req, earliest_data, fn) {
|
||||
|
||||
function loadProfile(req, fn) {
|
||||
if (req.iob) {
|
||||
req.profile.list(fn);
|
||||
req.ctx.profile.list(fn);
|
||||
} else {
|
||||
fn(null, []);
|
||||
}
|
||||
}
|
||||
|
||||
function configure (entries, treatments, profile, devicestatus, env) {
|
||||
function configure (env, ctx) {
|
||||
function middle (req, res, next) {
|
||||
req.entries = entries;
|
||||
req.treatments = treatments;
|
||||
req.profile = profile;
|
||||
req.devicestatus = devicestatus;
|
||||
req.ctx = ctx;
|
||||
req.rawbg = env.enable && env.enable.indexOf('rawbg') > -1;
|
||||
req.iob = env.enable && env.enable.indexOf('iob') > -1;
|
||||
req.mmol = (req.query.units || env.DISPLAY_UNITS) === 'mmol';
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
|
||||
function storage (collection, storage) {
|
||||
function storage (collection, ctx) {
|
||||
var ObjectID = require('mongodb').ObjectID;
|
||||
|
||||
function create (obj, fn) {
|
||||
@@ -28,7 +28,7 @@ function storage (collection, storage) {
|
||||
}
|
||||
|
||||
function api () {
|
||||
return storage.pool.db.collection(collection);
|
||||
return ctx.store.pool.db.collection(collection);
|
||||
}
|
||||
|
||||
api.list = list;
|
||||
|
||||
+6
-6
@@ -1,6 +1,6 @@
|
||||
var async = require('async');
|
||||
|
||||
function websocket (env, server, entries, treatments, profiles, devicestatus) {
|
||||
function websocket (env, ctx, server) {
|
||||
"use strict";
|
||||
// CONSTANTS
|
||||
var ONE_HOUR = 3600000,
|
||||
@@ -168,7 +168,7 @@ function update() {
|
||||
async.parallel({
|
||||
entries: function(callback) {
|
||||
var q = { find: {"date": {"$gte": earliest_data}} };
|
||||
entries.list(q, function (err, results) {
|
||||
ctx.entries.list(q, function (err, results) {
|
||||
if (!err && results) {
|
||||
results.forEach(function (element) {
|
||||
if (element) {
|
||||
@@ -200,7 +200,7 @@ function update() {
|
||||
}
|
||||
, cal: function(callback) {
|
||||
var cq = { count: 1, find: {"type": "cal"} };
|
||||
entries.list(cq, function (err, results) {
|
||||
ctx.entries.list(cq, function (err, results) {
|
||||
if (!err && results) {
|
||||
results.forEach(function (element) {
|
||||
if (element) {
|
||||
@@ -219,7 +219,7 @@ function update() {
|
||||
}
|
||||
, treatments: function(callback) {
|
||||
var tq = { find: {"created_at": {"$gte": new Date(treatment_earliest_data).toISOString()}} };
|
||||
treatments.list(tq, function (err, results) {
|
||||
ctx.treatments.list(tq, function (err, results) {
|
||||
treatmentData = results.map(function (treatment) {
|
||||
var timestamp = new Date(treatment.timestamp || treatment.created_at);
|
||||
treatment.x = timestamp.getTime();
|
||||
@@ -229,7 +229,7 @@ function update() {
|
||||
});
|
||||
}
|
||||
, profile: function(callback) {
|
||||
profiles.list(function (err, results) {
|
||||
ctx.profile.list(function (err, results) {
|
||||
if (!err && results) {
|
||||
// There should be only one document in the profile collection with a DIA. If there are multiple, use the last one.
|
||||
results.forEach(function (element, index, array) {
|
||||
@@ -244,7 +244,7 @@ function update() {
|
||||
});
|
||||
}
|
||||
, devicestatus: function(callback) {
|
||||
devicestatus.last(function (err, result) {
|
||||
ctx.devicestatus.last(function (err, result) {
|
||||
if (!err && result) {
|
||||
devicestatusData = {
|
||||
uploaderBattery: result.uploaderBattery
|
||||
|
||||
@@ -45,22 +45,21 @@ function create (app) {
|
||||
|
||||
var bootevent = require('./lib/bootevent');
|
||||
bootevent(env).boot(function booted (ctx) {
|
||||
env.store = ctx.store;
|
||||
var app = require('./app')(env, ctx);
|
||||
var server = create(app).listen(PORT);
|
||||
console.log('listening', PORT);
|
||||
|
||||
if (env.MQTT_MONITOR) {
|
||||
var mqtt = require('./lib/mqtt')(env, app.entries, app.devicestatus);
|
||||
var mqtt = require('./lib/mqtt')(env, ctx);
|
||||
var es = require('event-stream');
|
||||
es.pipeline(mqtt.entries, app.entries.map( ), mqtt.every(app.entries));
|
||||
es.pipeline(mqtt.entries, ctx.entries.map( ), mqtt.every(ctx.entries));
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////
|
||||
// setup socket io for data and message transmission
|
||||
///////////////////////////////////////////////////
|
||||
var websocket = require('./lib/websocket');
|
||||
var io = websocket(env, server, app.entries, app.treatments, app.profiles, app.devicestatus);
|
||||
var io = websocket(env, ctx, server);
|
||||
})
|
||||
;
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@ describe('Entries REST api', function ( ) {
|
||||
var self = this;
|
||||
var bootevent = require('../lib/bootevent');
|
||||
bootevent(env).boot(function booted (ctx) {
|
||||
env.store = ctx.store;
|
||||
self.app.use('/', entries(self.app, self.wares, ctx));
|
||||
self.archive = require('../lib/entries')(env, ctx);
|
||||
self.archive.create(load('json'), done);
|
||||
|
||||
+94
-99
@@ -2,106 +2,101 @@
|
||||
var request = require('supertest');
|
||||
var should = require('should');
|
||||
|
||||
//Mock entries
|
||||
var entries = {
|
||||
list: function(opts, callback) {
|
||||
var sgvs = [
|
||||
{ device: 'dexcom',
|
||||
date: 1422727301000,
|
||||
dateString: 'Sat Jan 31 10:01:41 PST 2015',
|
||||
sgv: 82,
|
||||
direction: 'Flat',
|
||||
type: 'sgv',
|
||||
filtered: 113984,
|
||||
unfiltered: 111920,
|
||||
rssi: 179,
|
||||
noise: 1
|
||||
},
|
||||
{ device: 'dexcom',
|
||||
date: 1422727001000,
|
||||
dateString: 'Sat Jan 31 09:56:41 PST 2015',
|
||||
sgv: 84,
|
||||
direction: 'Flat',
|
||||
type: 'sgv',
|
||||
filtered: 115680,
|
||||
unfiltered: 113552,
|
||||
rssi: 179,
|
||||
noise: 1
|
||||
},
|
||||
{ device: 'dexcom',
|
||||
date: 1422726701000,
|
||||
dateString: 'Sat Jan 31 09:51:41 PST 2015',
|
||||
sgv: 86,
|
||||
direction: 'Flat',
|
||||
type: 'sgv',
|
||||
filtered: 117808,
|
||||
unfiltered: 114640,
|
||||
rssi: 169,
|
||||
noise: 1
|
||||
},
|
||||
{ device: 'dexcom',
|
||||
date: 1422726401000,
|
||||
dateString: 'Sat Jan 31 09:46:41 PST 2015',
|
||||
sgv: 88,
|
||||
direction: 'Flat',
|
||||
type: 'sgv',
|
||||
filtered: 120464,
|
||||
unfiltered: 116608,
|
||||
rssi: 175,
|
||||
noise: 1
|
||||
},
|
||||
{ device: 'dexcom',
|
||||
date: 1422726101000,
|
||||
dateString: 'Sat Jan 31 09:41:41 PST 2015',
|
||||
sgv: 91,
|
||||
direction: 'Flat',
|
||||
type: 'sgv',
|
||||
filtered: 124048,
|
||||
unfiltered: 118880,
|
||||
rssi: 174,
|
||||
noise: 1
|
||||
//Mocked ctx
|
||||
var ctx = {
|
||||
entries: {
|
||||
list: function (opts, callback) {
|
||||
var sgvs = [
|
||||
{ device: 'dexcom',
|
||||
date: 1422727301000,
|
||||
dateString: 'Sat Jan 31 10:01:41 PST 2015',
|
||||
sgv: 82,
|
||||
direction: 'Flat',
|
||||
type: 'sgv',
|
||||
filtered: 113984,
|
||||
unfiltered: 111920,
|
||||
rssi: 179,
|
||||
noise: 1
|
||||
},
|
||||
{ device: 'dexcom',
|
||||
date: 1422727001000,
|
||||
dateString: 'Sat Jan 31 09:56:41 PST 2015',
|
||||
sgv: 84,
|
||||
direction: 'Flat',
|
||||
type: 'sgv',
|
||||
filtered: 115680,
|
||||
unfiltered: 113552,
|
||||
rssi: 179,
|
||||
noise: 1
|
||||
},
|
||||
{ device: 'dexcom',
|
||||
date: 1422726701000,
|
||||
dateString: 'Sat Jan 31 09:51:41 PST 2015',
|
||||
sgv: 86,
|
||||
direction: 'Flat',
|
||||
type: 'sgv',
|
||||
filtered: 117808,
|
||||
unfiltered: 114640,
|
||||
rssi: 169,
|
||||
noise: 1
|
||||
},
|
||||
{ device: 'dexcom',
|
||||
date: 1422726401000,
|
||||
dateString: 'Sat Jan 31 09:46:41 PST 2015',
|
||||
sgv: 88,
|
||||
direction: 'Flat',
|
||||
type: 'sgv',
|
||||
filtered: 120464,
|
||||
unfiltered: 116608,
|
||||
rssi: 175,
|
||||
noise: 1
|
||||
},
|
||||
{ device: 'dexcom',
|
||||
date: 1422726101000,
|
||||
dateString: 'Sat Jan 31 09:41:41 PST 2015',
|
||||
sgv: 91,
|
||||
direction: 'Flat',
|
||||
type: 'sgv',
|
||||
filtered: 124048,
|
||||
unfiltered: 118880,
|
||||
rssi: 174,
|
||||
noise: 1
|
||||
}
|
||||
];
|
||||
|
||||
var cals = [
|
||||
{ device: 'dexcom',
|
||||
date: 1422647711000,
|
||||
dateString: 'Fri Jan 30 11:55:11 PST 2015',
|
||||
slope: 895.8571693029189,
|
||||
intercept: 34281.06876195567,
|
||||
scale: 1,
|
||||
type: 'cal'
|
||||
}
|
||||
];
|
||||
|
||||
var count = (opts && opts.count) || 1;
|
||||
|
||||
if (opts && opts.find && opts.find.sgv) {
|
||||
callback(null, sgvs.slice(0, count));
|
||||
} else if (opts && opts.find && opts.find.type == 'cal') {
|
||||
callback(null, cals.slice(0, count));
|
||||
}
|
||||
];
|
||||
|
||||
var cals = [
|
||||
{ device: 'dexcom',
|
||||
date: 1422647711000,
|
||||
dateString: 'Fri Jan 30 11:55:11 PST 2015',
|
||||
slope: 895.8571693029189,
|
||||
intercept: 34281.06876195567,
|
||||
scale: 1,
|
||||
type: 'cal'
|
||||
}
|
||||
];
|
||||
|
||||
var count = (opts && opts.count) || 1;
|
||||
|
||||
if (opts && opts.find && opts.find.sgv) {
|
||||
callback(null, sgvs.slice(0, count));
|
||||
} else if (opts && opts.find && opts.find.type == 'cal') {
|
||||
callback(null, cals.slice(0, count));
|
||||
}
|
||||
}, treatments: {
|
||||
list: function (callback) {
|
||||
callback(null, []);
|
||||
}
|
||||
}, profile: {
|
||||
list: function (callback) {
|
||||
callback(null, []);
|
||||
}
|
||||
}, devicestatus: {
|
||||
last: function (callback) {
|
||||
callback(null, {uploaderBattery: 100});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//Mock devicestatus
|
||||
var treatments = {
|
||||
list: function(callback) {
|
||||
callback(null, []);
|
||||
}
|
||||
};
|
||||
|
||||
var profile = {
|
||||
list: function(callback) {
|
||||
callback(null, []);
|
||||
}
|
||||
};
|
||||
|
||||
var devicestatus = {
|
||||
last: function(callback) {
|
||||
callback(null, {uploaderBattery: 100});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
describe('Pebble Endpoint without Raw', function ( ) {
|
||||
var pebble = require('../lib/pebble');
|
||||
@@ -109,7 +104,7 @@ describe('Pebble Endpoint without Raw', function ( ) {
|
||||
var env = require('../env')( );
|
||||
this.app = require('express')( );
|
||||
this.app.enable('api');
|
||||
this.app.use('/pebble', pebble(entries, treatments, profile, devicestatus, env));
|
||||
this.app.use('/pebble', pebble(env, ctx));
|
||||
done();
|
||||
});
|
||||
|
||||
@@ -174,7 +169,7 @@ describe('Pebble Endpoint with Raw', function ( ) {
|
||||
envRaw.enable = "rawbg";
|
||||
this.appRaw = require('express')( );
|
||||
this.appRaw.enable('api');
|
||||
this.appRaw.use('/pebble', pebbleRaw(entries, treatments, profile, devicestatus, envRaw));
|
||||
this.appRaw.use('/pebble', pebbleRaw(envRaw, ctx));
|
||||
done();
|
||||
});
|
||||
|
||||
|
||||
+12
-15
@@ -10,19 +10,16 @@ describe('API_SECRET', function ( ) {
|
||||
|
||||
var scope = this;
|
||||
function setup_app (env, fn) {
|
||||
var ctx = { };
|
||||
ctx.wares = require('../lib/middleware/')(env);
|
||||
ctx.store = require('../lib/storage')(env);
|
||||
ctx.archive = require('../lib/entries').storage(env.mongo_collection, ctx.store);
|
||||
|
||||
ctx.store(function ( ) {
|
||||
ctx.app = api(env, ctx.wares, ctx.archive);
|
||||
var bootevent = require('../lib/bootevent');
|
||||
bootevent(env).boot(function booted (ctx) {
|
||||
var wares = require('../lib/middleware/')(env);
|
||||
ctx.app = api(env, wares, ctx);
|
||||
scope.app = ctx.app;
|
||||
ctx.archive.create(load('json'), fn);
|
||||
scope.archive = ctx.archive;
|
||||
scope.entries = ctx.entries;
|
||||
ctx.entries.create(load('json'), function () {
|
||||
fn(ctx);
|
||||
});
|
||||
});
|
||||
|
||||
return ctx;
|
||||
}
|
||||
/*
|
||||
before(function (done) {
|
||||
@@ -30,14 +27,14 @@ describe('API_SECRET', function ( ) {
|
||||
});
|
||||
*/
|
||||
after(function (done) {
|
||||
scope.archive( ).remove({ }, done);
|
||||
scope.entries( ).remove({ }, done);
|
||||
});
|
||||
|
||||
it('should work fine absent', function (done) {
|
||||
delete process.env.API_SECRET;
|
||||
var env = require('../env')( );
|
||||
should.not.exist(env.api_secret);
|
||||
var ctx = setup_app(env, function ( ) {
|
||||
setup_app(env, function (ctx) {
|
||||
ctx.app.enabled('api').should.be.false;
|
||||
ping_status(ctx.app, again);
|
||||
function again ( ) {
|
||||
@@ -53,7 +50,7 @@ describe('API_SECRET', function ( ) {
|
||||
process.env.API_SECRET = 'this is my long pass phrase';
|
||||
var env = require('../env')( );
|
||||
env.api_secret.should.equal(known);
|
||||
var ctx = setup_app(env, function ( ) {
|
||||
setup_app(env, function (ctx) {
|
||||
// console.log(this.app.enabled('api'));
|
||||
ctx.app.enabled('api').should.be.true;
|
||||
// ping_status(ctx.app, done);
|
||||
@@ -74,7 +71,7 @@ describe('API_SECRET', function ( ) {
|
||||
process.env.API_SECRET = 'this is my long pass phrase';
|
||||
var env = require('../env')( );
|
||||
env.api_secret.should.equal(known);
|
||||
var ctx = setup_app(env, function ( ) {
|
||||
setup_app(env, function (ctx) {
|
||||
// console.log(this.app.enabled('api'));
|
||||
ctx.app.enabled('api').should.be.true;
|
||||
// ping_status(ctx.app, done);
|
||||
|
||||
Reference in New Issue
Block a user