From 3a1d9be89d81036a7bee26d32d2407aaaab13601 Mon Sep 17 00:00:00 2001 From: Jakob Date: Tue, 14 Jan 2020 07:33:57 -0800 Subject: [PATCH] Fix eslint errors and add npm script for eslint (#5427) * re-enable auth check for device status routes * Resolve eslint errors * Add npm script for eslint * Correct regex for express extension middleware and add tests for expected behaviour * Resolve lint error in virtual assistant base * Update index.js * Update index.js --- lib/api/activity/index.js | 8 ++--- lib/api/alexa/index.js | 2 +- lib/api/devicestatus/index.js | 2 +- lib/api/googlehome/index.js | 2 +- lib/api/status.js | 2 +- lib/api/treatments/index.js | 6 ++-- lib/authorization/index.js | 2 +- lib/authorization/storage.js | 6 ++-- lib/client/chart.js | 2 -- lib/data/calcdelta.js | 12 +++---- lib/data/dataloader.js | 1 - lib/middleware/express-extension-to-accept.js | 2 +- lib/middleware/index.js | 2 +- lib/plugins/alexa.js | 2 +- lib/plugins/googlehome.js | 2 +- lib/plugins/virtAsstBase.js | 12 +++---- lib/server/websocket.js | 6 ---- package.json | 3 +- tests/expressextensions.test.js | 33 +++++++++++++++++++ 19 files changed, 66 insertions(+), 41 deletions(-) create mode 100644 tests/expressextensions.test.js diff --git a/lib/api/activity/index.js b/lib/api/activity/index.js index d88019ab..c42e7357 100644 --- a/lib/api/activity/index.js +++ b/lib/api/activity/index.js @@ -43,17 +43,17 @@ function configure(app, wares, ctx) { var d2 = null; - if (t.hasOwnProperty('created_at')) { + if (Object.prototype.hasOwnProperty.call(t, 'created_at')) { d2 = new Date(t.created_at); } else { - if (t.hasOwnProperty('timestamp')) { + if (Object.prototype.hasOwnProperty.call(t, 'timestamp')) { d2 = new Date(t.timestamp); } } if (d2 == null) { return; } - if (d1 == null || d2.getTime() > d1.getTime()) { + if (d1 == null || d2.getTime() > d1.getTime()) { d1 = d2; } }); @@ -80,7 +80,7 @@ function configure(app, wares, ctx) { if (!_isArray(activity)) { activity = [activity]; - }; + } ctx.activity.create(activity, function(err, created) { if (err) { diff --git a/lib/api/alexa/index.js b/lib/api/alexa/index.js index 560de7c4..2a5fd4ef 100644 --- a/lib/api/alexa/index.js +++ b/lib/api/alexa/index.js @@ -118,4 +118,4 @@ function configure (app, wares, ctx, env) { return api; } -module.exports = configure; \ No newline at end of file +module.exports = configure; diff --git a/lib/api/devicestatus/index.js b/lib/api/devicestatus/index.js index 97489646..25e226ef 100644 --- a/lib/api/devicestatus/index.js +++ b/lib/api/devicestatus/index.js @@ -105,7 +105,7 @@ function configure (app, wares, ctx, env) { api.delete('/devicestatus/', ctx.authorization.isPermitted('api:devicestatus:delete'), delete_records); } - if (app.enabled('api') || true /*TODO: auth disabled for quick UI testing...*/ ) { + if (app.enabled('api')) { config_authed(app, api, wares, ctx); } diff --git a/lib/api/googlehome/index.js b/lib/api/googlehome/index.js index 8c99eea6..b44715b2 100644 --- a/lib/api/googlehome/index.js +++ b/lib/api/googlehome/index.js @@ -54,4 +54,4 @@ function configure (app, wares, ctx, env) { return api; } -module.exports = configure; \ No newline at end of file +module.exports = configure; diff --git a/lib/api/status.js b/lib/api/status.js index a6ab2e82..dc8d97bc 100644 --- a/lib/api/status.js +++ b/lib/api/status.js @@ -15,7 +15,7 @@ function configure (app, wares, env, ctx) { // Status badge/text/json api.get('/status', function (req, res) { - var authToken = req.query.token || req.query.secret || ''; + var authToken = req.query.token || req.query.secret || ''; var date = new Date(); var info = { status: 'ok' diff --git a/lib/api/treatments/index.js b/lib/api/treatments/index.js index e62e6a35..b30c2971 100644 --- a/lib/api/treatments/index.js +++ b/lib/api/treatments/index.js @@ -53,10 +53,10 @@ function configure (app, wares, ctx, env) { var d2 = null; - if (t.hasOwnProperty('created_at')) { + if (Object.prototype.hasOwnProperty.call(t, 'created_at')) { d2 = new Date(t.created_at); } else { - if (t.hasOwnProperty('timestamp')) { + if (Object.prototype.hasOwnProperty.call(t, 'timestamp')) { d2 = new Date(t.timestamp); } } @@ -90,7 +90,7 @@ function configure (app, wares, ctx, env) { if (!_isArray(treatments)) { treatments = [treatments]; - }; + } ctx.treatments.create(treatments, function(err, created) { if (err) { diff --git a/lib/authorization/index.js b/lib/authorization/index.js index feaed739..b81578e0 100644 --- a/lib/authorization/index.js +++ b/lib/authorization/index.js @@ -186,7 +186,7 @@ function init (env, ctx) { authorization.isPermitted = function isPermitted (permission, opts) { - opts = mkopts(opts); + mkopts(opts); authorization.seenPermissions = _.chain(authorization.seenPermissions) .push(permission) .sort() diff --git a/lib/authorization/storage.js b/lib/authorization/storage.js index 3a4c4490..c032018d 100644 --- a/lib/authorization/storage.js +++ b/lib/authorization/storage.js @@ -24,7 +24,7 @@ function init (env, ctx) { function create (collection) { function doCreate(obj, fn) { - if (!obj.hasOwnProperty('created_at')) { + if (!Object.prototype.hasOwnProperty.call(obj, 'created_at')) { obj.created_at = (new Date()).toISOString(); } collection.insert(obj, function (err, doc) { @@ -211,14 +211,14 @@ function init (env, ctx) { if (!accessToken) return null; var split_token = accessToken.split('-'); - var prefix = split_token ? _.last(split_token) : ''; + var prefix = split_token ? _.last(split_token) : ''; if (prefix.length < 16) { return null; } return _.find(storage.subjects, function matches (subject) { - return subject.accessTokenDigest.indexOf(accessToken) === 0 || subject.digest.indexOf(prefix) === 0; + return subject.accessTokenDigest.indexOf(accessToken) === 0 || subject.digest.indexOf(prefix) === 0; }); }; diff --git a/lib/client/chart.js b/lib/client/chart.js index ffd513cd..71d68bbc 100644 --- a/lib/client/chart.js +++ b/lib/client/chart.js @@ -730,8 +730,6 @@ function init (client, d3, $) { // on the number of hours the user has selected to show var forecastMills = Math.min(availForecastMills, maxForecastMills); - var lastSGVMills = client.sbx.lastSGVMills(); - // Don't allow the forecast time to go below the minimum forecast time client.forecastTime = Math.max(forecastMills, minForecastMills); } diff --git a/lib/data/calcdelta.js b/lib/data/calcdelta.js index e3e0fde7..c991bd8d 100644 --- a/lib/data/calcdelta.js +++ b/lib/data/calcdelta.js @@ -75,7 +75,7 @@ module.exports = function calcDelta (oldData, newData) { var result = []; l = newArray.length; for (var j = 0; j < l; j++) { - if (!seen.hasOwnProperty(newArray[j].mills)) { + if (!Object.prototype.hasOwnProperty.call(seen, newArray[j].mills)) { result.push(newArray[j]); } } @@ -94,12 +94,12 @@ module.exports = function calcDelta (oldData, newData) { var changesFound = false; for (var array in compressibleArrays) { - if (compressibleArrays.hasOwnProperty(array)) { + if (Object.prototype.hasOwnProperty.call(compressibleArrays, array)) { var a = compressibleArrays[array]; - if (newData.hasOwnProperty(a)) { + if (Object.prototype.hasOwnProperty.call(newData, a)) { // if previous data doesn't have the property (first time delta?), just assign data over - if (!oldData.hasOwnProperty(a)) { + if (!Object.prototype.hasOwnProperty.call(oldData, a)) { delta[a] = newData[a]; changesFound = true; continue; @@ -125,9 +125,9 @@ module.exports = function calcDelta (oldData, newData) { var changesFound = false; for (var object in skippableObjects) { - if (skippableObjects.hasOwnProperty(object)) { + if (Object.prototype.hasOwnProperty.call(skippableObjects, object)) { var o = skippableObjects[object]; - if (newData.hasOwnProperty(o)) { + if (Object.prototype.hasOwnProperty.call(newData, o)) { if (JSON.stringify(newData[o]) !== JSON.stringify(oldData[o])) { //console.log('delta changes found on', o); changesFound = true; diff --git a/lib/data/dataloader.js b/lib/data/dataloader.js index 31121c2c..1c00f998 100644 --- a/lib/data/dataloader.js +++ b/lib/data/dataloader.js @@ -192,7 +192,6 @@ function loadActivity(ddata, ctx, callback) { } }; - var activity = []; ctx.activity.list(q, function(err, results) { if (err) { diff --git a/lib/middleware/express-extension-to-accept.js b/lib/middleware/express-extension-to-accept.js index 8f357e86..cdfe9a26 100644 --- a/lib/middleware/express-extension-to-accept.js +++ b/lib/middleware/express-extension-to-accept.js @@ -16,7 +16,7 @@ module.exports = function (formats) { throw new Error('Invalid format.') }) - var regexp = new RegExp('\.(' + formats.join('|') + ')$', 'i') + var regexp = new RegExp('\\.(' + formats.join('|') + ')$', 'i') return function (req, res, next) { var match = req.path.match(regexp) diff --git a/lib/middleware/index.js b/lib/middleware/index.js index c20eede6..deb4fd5f 100644 --- a/lib/middleware/index.js +++ b/lib/middleware/index.js @@ -10,7 +10,7 @@ function extensions (list) { return require('./express-extension-to-accept')(list); } -function configure (env) { +function configure () { return { sendJSONStatus: wares.sendJSONStatus( ), bodyParser: wares.bodyParser, diff --git a/lib/plugins/alexa.js b/lib/plugins/alexa.js index da7bbdca..25b13d05 100644 --- a/lib/plugins/alexa.js +++ b/lib/plugins/alexa.js @@ -1,7 +1,7 @@ var _ = require('lodash'); var async = require('async'); -function init (env, ctx) { +function init () { console.log('Configuring Alexa...'); function alexa() { return alexa; diff --git a/lib/plugins/googlehome.js b/lib/plugins/googlehome.js index 8e818151..6b6d7b09 100644 --- a/lib/plugins/googlehome.js +++ b/lib/plugins/googlehome.js @@ -1,7 +1,7 @@ var _ = require('lodash'); var async = require('async'); -function init (env, ctx) { +function init () { console.log('Configuring Google Home...'); function googleHome() { return googleHome; diff --git a/lib/plugins/virtAsstBase.js b/lib/plugins/virtAsstBase.js index 79864f5b..e0d10367 100644 --- a/lib/plugins/virtAsstBase.js +++ b/lib/plugins/virtAsstBase.js @@ -28,7 +28,7 @@ function init(env, ctx) { moment(records[0].date).from(moment(sbx.time)) ] }); - + callback(null, {results: status, priority: -1}); }); }, 'BG Status'); @@ -40,7 +40,7 @@ function init(env, ctx) { }); // blood sugar and direction - configuredPlugin.configureIntentHandler('MetricNow', function (callback, slots, sbx, locale) { + configuredPlugin.configureIntentHandler('MetricNow', function (callback, slots, sbx) { entries.list({count: 1}, function(err, records) { var direction; if(translate(records[0].direction)){ @@ -54,13 +54,13 @@ function init(env, ctx) { direction, moment(records[0].date).from(moment(sbx.time))] }); - + callback(translate('virtAsstTitleCurrentBG'), status); }); }, ['bg', 'blood glucose', 'number']); - + // blood sugar delta - configuredPlugin.configureIntentHandler('MetricNow', function (callback, slots, sbx, locale) { + configuredPlugin.configureIntentHandler('MetricNow', function (callback, slots, sbx) { if (sbx.properties.delta && sbx.properties.delta.display) { entries.list({count: 2}, function(err, records) { callback( @@ -108,4 +108,4 @@ function init(env, ctx) { return virtAsstBase; } -module.exports = init; \ No newline at end of file +module.exports = init; diff --git a/lib/server/websocket.js b/lib/server/websocket.js index 0288da5f..899d9326 100644 --- a/lib/server/websocket.js +++ b/lib/server/websocket.js @@ -443,15 +443,9 @@ function init (env, ctx, server) { socketAuthorization = authorization; clientType = message.client; history = message.history || 48; //default history is 48 hours - var from = message.from; if (socketAuthorization.read) { socket.join('DataReceivers'); - var msecHistory = times.hours(history).msecs; - // if `from` is received, it's a reconnection and full data is not needed - if (from && from > 0) { - msecHistory = Math.min(new Date().getTime() - from, msecHistory); - } if (lastData && lastData.dataWithRecentStatuses) { let data = lastData.dataWithRecentStatuses(); diff --git a/package.json b/package.json index 8018bc64..0abbae89 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,8 @@ "update-buster": "node bin/generateCacheBuster.js >tmp/cacheBusterToken", "coverage": "cat ./coverage/lcov.info | env-cmd ./ci.test.env codacy-coverage", "dev": "env-cmd ./my.env nodemon server.js 0.0.0.0", - "prod": "env-cmd ./my.prod.env node server.js 0.0.0.0" + "prod": "env-cmd ./my.prod.env node server.js 0.0.0.0", + "lint": "eslint lib" }, "main": "server.js", "config": { diff --git a/tests/expressextensions.test.js b/tests/expressextensions.test.js new file mode 100644 index 00000000..b65ebd89 --- /dev/null +++ b/tests/expressextensions.test.js @@ -0,0 +1,33 @@ +'use strict'; + +require('should'); + +var extensionsMiddleware = require('../lib/middleware/express-extension-to-accept.js'); + +var acceptJsonRequests = extensionsMiddleware(['json']); + +describe('Express extension middleware', function ( ) { + + it('Valid json request should be given accept header for application/json', function () { + var entriesRequest = { + path: '/api/v1/entries.json', + url: '/api/v1/entries.json', + headers: {} + }; + + acceptJsonRequests(entriesRequest, {}, () => {}); + entriesRequest.headers.accept.should.equal('application/json'); + }); + + it('Invalid json request should NOT be given accept header', function () { + var invalidEntriesRequest = { + path: '/api/v1/entriesXjson', + url: '/api/v1/entriesXjson', + headers: {} + }; + + acceptJsonRequests(invalidEntriesRequest, {}, () => {}); + should(invalidEntriesRequest.headers.accept).not.be.ok; + }); + +});