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

Unit test teardown improvements #6821

Merged
merged 12 commits into from
Feb 1, 2021
6 changes: 3 additions & 3 deletions lib/client/hashauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ hashauth.init = function init (client, $) {
return;
}

if (message.message === 'OK') {
if (response.message === 'OK' || message.message === 'OK') {
hashauth.authenticated = true;
console.log('Authentication passed.');
next(true);
return;
}

console.log('Authentication failed.', response);
console.log('Authentication failed!', response);
hashauth.removeAuthentication();
next(false);
return;

}).fail(function verifyfail (err) {
console.log('Authentication failed.', err);
console.log('Authentication failure', err);
hashauth.removeAuthentication();
next(false);
});
Expand Down
6 changes: 4 additions & 2 deletions lib/profilefunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ var times = require('./times');

var cacheTTL = 5000;
var prevBasalTreatment = null;
var cache = new c.Cache();

function init (profileData) {

var cache = new c.Cache();
var profile = {};

profile.clear = function clear() {
Expand All @@ -19,6 +19,8 @@ function init (profileData) {
prevBasalTreatment = null;
}

profile.clear();

profile.loadData = function loadData (profileData) {
if (profileData && profileData.length) {
profile.data = profile.convertToProfileStore(profileData);
Expand All @@ -36,7 +38,7 @@ function init (profileData) {
var newObject = {};
newObject.defaultProfile = 'Default';
newObject.store = {};
newObject.startDate = profile.startDate;
newObject.startDate = profile.startDate ? profile.startDate : '1980-01-01';
newObject._id = profile._id;
newObject.convertedOnTheFly = true;
delete profile.startDate;
Expand Down
3 changes: 2 additions & 1 deletion my.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ ALARM_TYPES="predict"
LANGUAGE=en
INSECURE_USE_HTTP=true
PORT=1337
NODE_ENV=development
NODE_ENV=development
AUTH_FAIL_DELAY=50
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
},
"scripts": {
"start": "node server.js",
"test": "env-cmd -f ./my.test.env mocha --exit tests/*.test.js",
"test-single": "env-cmd -f ./my.test.env mocha --exit tests/$TEST.test.js",
"test-ci": "env-cmd -f ./ci.test.env nyc --reporter=lcov --reporter=text-summary mocha --exit tests/*.test.js",
"test": "env-cmd -f ./my.test.env mocha --require ./tests/hooks.js -exit ./tests/*.test.js",
"test-single": "env-cmd -f ./my.test.env mocha --require ./tests/hooks.js --exit ./tests/$TEST.test.js",
"test-ci": "env-cmd -f ./ci.test.env nyc --reporter=lcov --reporter=text-summary mocha --require ./tests/hooks.js --exit ./tests/*.test.js",
"env": "env",
"postinstall": "webpack --mode production --config webpack.config.js && npm run-script update-buster",
"bundle": "webpack --mode production --config webpack.config.js && npm run-script update-buster",
Expand Down
2 changes: 1 addition & 1 deletion tests/adminnotifies.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

'use strict';

const should = require('should');
require('should');

const ctx = {};

Expand Down
4 changes: 0 additions & 4 deletions tests/api.devicestatus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ describe('Devicestatus API', function ( ) {
});
});

after(function () {
// delete process.env.API_SECRET;
});

it('post a devicestatus, query, delete, verify gone', function (done) {
// insert a devicestatus - needs to be unique from example data
console.log('Inserting devicestatus entry');
Expand Down
36 changes: 20 additions & 16 deletions tests/careportal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

require('should');
var benv = require('benv');
var read = require('fs').readFileSync;
var serverSettings = require('./fixtures/default-server-settings');

var nowData = {
sgvs: [
Expand All @@ -12,33 +10,35 @@ var nowData = {
, treatments: []
};

describe('client', function ( ) {
this.timeout(40000); // TODO: see why this test takes longer on Travis to complete

var self = this;
describe('careportal', function ( ) {
this.timeout(60000); // TODO: see why this test takes longer on Travis to complete

var headless = require('./fixtures/headless')(benv, this);

before(function (done) {
done( );
});

after(function (done) {
done( );
});
const t = Date.now();
console.log('Starting headless setup for Careportal test');

function d () {
console.log('Done called by headless', Date.now() - t );
done();
}

beforeEach(function (done) {
headless.setup({mockAjax: true}, done);
headless.setup({mockAjax: true}, d);
console.log('Headless setup for Careportal test done');
});

afterEach(function (done) {
after(function (done) {
headless.teardown( );
done( );
});

it ('open careportal, and enter a treatment', function (done) {

var client = window.Nightscout.client;
console.log('Careportal test client start');

var client = window.Nightscout.client;

var hashauth = require('../lib/client/hashauth');
hashauth.init(client,$);
Expand All @@ -47,7 +47,9 @@ describe('client', function ( ) {
next(true);
};

console.log('Careportal test client init');
client.init();
console.log('Careportal test client data update');
client.dataUpdate(nowData, true);

client.careportal.prepareEvents();
Expand Down Expand Up @@ -81,8 +83,10 @@ describe('client', function ( ) {
return true;
};

window.alert = function mockAlert() {};
window.alert = function mockAlert(messages) { messages.should.equal(''); };

console.log('Careportal test saving');

client.careportal.save();

done();
Expand Down
3 changes: 2 additions & 1 deletion tests/cob.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ describe('COB', function ( ) {
var cob = require('../lib/plugins/cob')(ctx);

var profileData = {
sens: 95
startDate: '2015-06-21'
, sens: 95
, carbratio: 18
, carbs_hr: 30
};
Expand Down
21 changes: 16 additions & 5 deletions tests/fixtures/headless.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,36 @@ function headless (benv, binding) {
}

function init (opts, callback) {
var localStorage = opts.localStorage || './localstorage';

const t = Date.now();

console.log('Headless init');

var htmlFile = opts.htmlFile || __dirname + '/../../views/index.html';
var serverSettings = opts.serverSettings || require('./default-server-settings');
var someData = opts.mockAjax || { };

console.log('Entering setup', Date.now() - t);

benv.setup(function() {

console.log('Setting up benv', Date.now() - t);

benv.require(__dirname + '/../../tmp/js/bundle.report.js');


console.log('Bundle loaded', Date.now() - t);

self.$ = $;

self.localCookieStorage = self.localStorage = self.$.localStorage = require('./localstorage');

//self.$ = require('jquery');
//self.$.localStorage = require(localStorage);

self.$.fn.tooltip = function mockTooltip ( ) { };

var indexHtml = read(htmlFile, 'utf8');
self.$('body').html(indexHtml);

console.log('HTML set', Date.now() - t);

var d3 = require('d3');
//disable all d3 transitions so most of the other code can run with jsdom
d3.timer = function mockTimer() { };
Expand Down Expand Up @@ -124,6 +134,7 @@ function headless (benv, binding) {
};
}

console.log('Benv expose', Date.now() - t);

benv.expose({
$: self.$
Expand Down
14 changes: 14 additions & 0 deletions tests/hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict;'

function clearRequireCache () {
Object.keys(require.cache).forEach(function(key) {
delete require.cache[key];
});
}

exports.mochaHooks = {
afterEach (done) {
clearRequireCache();
done();
}
};
3 changes: 2 additions & 1 deletion tests/security.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var language = require('../lib/language')();
describe('API_SECRET', function ( ) {
var api;
var scope = this;
this.timeout(5000);

function setup_app (env, fn) {
api = require('../lib/api/');
Expand All @@ -18,7 +19,7 @@ describe('API_SECRET', function ( ) {
});
}

it('should work fail set unauthorized', function (done) {
it('should fail when unauthorized', function (done) {
var known = 'b723e97aa97846eb92d5264f084b2823f57c4aa1';
delete process.env.API_SECRET;
process.env.API_SECRET = 'this is my long pass phrase';
Expand Down