forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashauth.test.js
173 lines (134 loc) · 4.84 KB
/
hashauth.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict';
require('should');
var benv = require('benv');
var read = require('fs').readFileSync;
var serverSettings = require('./fixtures/default-server-settings');
describe('hashauth', function ( ) {
this.timeout(50000); // TODO: see why this test takes longer on Travis to complete
var self = this;
var headless = require('./fixtures/headless')(benv, this);
before(function (done) {
done( );
});
after(function (done) {
// cleanup js-storage as it evaluates if the test is running in the window or not when first required
delete require.cache[require.resolve('js-storage')];
done( );
});
beforeEach(function (done) {
headless.setup({mockAjax: true}, done);
});
afterEach(function (done) {
headless.teardown( );
done( );
});
/*
before(function (done) {
benv.setup(function() {
self.$ = require('jquery');
self.$.localStorage = require('./fixtures/localstorage');
self.$.fn.tooltip = function mockTooltip ( ) { };
var indexHtml = read(__dirname + '/../static/index.html', 'utf8');
self.$('body').html(indexHtml);
var d3 = require('d3');
//disable all d3 transitions so most of the other code can run with jsdom
d3.timer = function mockTimer() { };
benv.expose({
$: self.$
, jQuery: self.$
, d3: d3
, io: {
connect: function mockConnect ( ) {
return {
on: function mockOn ( ) { }
};
}
}
});
done();
});
});
after(function (done) {
benv.teardown();
done();
});
*/
it ('should make module unauthorized', function () {
var client = require('../lib/client');
var hashauth = require('../lib/client/hashauth');
hashauth.init(client,$);
hashauth.verifyAuthentication = function mockVerifyAuthentication(next) {
hashauth.authenticated = false;
next(true);
};
client.init();
hashauth.inlineCode().indexOf('Unauthorized').should.be.greaterThan(0);
hashauth.isAuthenticated().should.equal(false);
var testnull = (hashauth.hash()===null);
testnull.should.equal(true);
});
it ('should make module authorized', function () {
var client = require('../lib/client');
var hashauth = require('../lib/client/hashauth');
hashauth.init(client,$);
hashauth.verifyAuthentication = function mockVerifyAuthentication(next) {
hashauth.authenticated = true;
next(true);
};
client.init();
hashauth.inlineCode().indexOf('Admin authorized').should.be.greaterThan(0);
hashauth.isAuthenticated().should.equal(true);
});
it ('should store hash and the remove authentication', function () {
var client = require('../lib/client');
var hashauth = require('../lib/client/hashauth');
var localStorage = require('./fixtures/localstorage');
localStorage.remove('apisecrethash');
hashauth.init(client,$);
hashauth.verifyAuthentication = function mockVerifyAuthentication(next) {
hashauth.authenticated = true;
next(true);
};
hashauth.updateSocketAuth = function mockUpdateSocketAuth() {};
client.init();
hashauth.processSecret('this is my long pass phrase',true);
hashauth.hash().should.equal('b723e97aa97846eb92d5264f084b2823f57c4aa1');
localStorage.get('apisecrethash').should.equal('b723e97aa97846eb92d5264f084b2823f57c4aa1');
hashauth.isAuthenticated().should.equal(true);
hashauth.removeAuthentication();
hashauth.isAuthenticated().should.equal(false);
});
it ('should not store hash', function () {
var client = require('../lib/client');
var hashauth = require('../lib/client/hashauth');
var localStorage = require('./fixtures/localstorage');
localStorage.remove('apisecrethash');
hashauth.init(client,$);
hashauth.verifyAuthentication = function mockVerifyAuthentication(next) {
hashauth.authenticated = true;
next(true);
};
client.init();
hashauth.processSecret('this is my long pass phrase',false);
hashauth.hash().should.equal('b723e97aa97846eb92d5264f084b2823f57c4aa1');
var testnull = (localStorage.get('apisecrethash')===null);
testnull.should.equal(true);
hashauth.isAuthenticated().should.equal(true);
});
it ('should report secret too short', function () {
var client = require('../lib/client');
var hashauth = require('../lib/client/hashauth');
var localStorage = require('./fixtures/localstorage');
localStorage.remove('apisecrethash');
hashauth.init(client, self.$);
client.init();
window.alert = function mockConfirm (message) {
function containsLine (line) {
message.indexOf(line).should.be.greaterThan(-1);
}
containsLine('Too short API secret');
return true;
};
hashauth.processSecret('short passp',false);
});
});