forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverifyauth.test.js
117 lines (95 loc) · 3.62 KB
/
verifyauth.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
'use strict';
var request = require('supertest');
var language = require('../lib/language')();
require('should');
describe('verifyauth', function ( ) {
var api = require('../lib/api/');
this.timeout(25000);
var scope = this;
function setup_app (env, fn) {
require('../lib/server/bootevent')(env, language).boot(function booted (ctx) {
ctx.app = api(env, ctx);
scope.app = ctx.app;
fn(ctx);
});
}
after(function (done) {
done();
});
it('should return defaults when called without secret', function (done) {
var known = 'b723e97aa97846eb92d5264f084b2823f57c4aa1';
var known512 = '8c8743d38cbe00debe4b3ba8d0ffbb85e4716c982a61bb9e57bab203178e3718b2965831c1a5e42b9da16f082fdf8a6cecf993b49ed67e3a8b1cd475885d8070';
delete process.env.API_SECRET;
process.env.API_SECRET = 'this is my long pass phrase';
var env = require('../lib/server/env')( );
env.enclave.isApiKey(known).should.equal(true);
env.enclave.isApiKey(known512).should.equal(true);
setup_app(env, function (ctx) {
ctx.app.enabled('api').should.equal(true);
ctx.app.api_secret = '';
ping_authorized_endpoint(ctx.app, 200, done);
});
});
it('should fail when calling with wrong secret', function (done) {
var known = 'b723e97aa97846eb92d5264f084b2823f57c4aa1';
delete process.env.API_SECRET;
process.env.API_SECRET = 'this is my long pass phrase';
var env = require('../lib/server/env')( );
env.enclave.isApiKey(known).should.equal(true);
setup_app(env, function (ctx) {
ctx.app.enabled('api').should.equal(true);
ctx.app.api_secret = 'wrong secret';
function check(res) {
res.body.message.message.should.equal('UNAUTHORIZED');
done();
}
ping_authorized_endpoint(ctx.app, 200, check, true);
});
});
it('should fail unauthorized and delay subsequent attempts', function (done) {
var known = 'b723e97aa97846eb92d5264f084b2823f57c4aa1';
delete process.env.API_SECRET;
process.env.API_SECRET = 'this is my long pass phrase';
var env = require('../lib/server/env')( );
env.enclave.isApiKey(known).should.equal(true);
setup_app(env, function (ctx) {
ctx.app.enabled('api').should.equal(true);
ctx.app.api_secret = 'wrong secret';
const time = Date.now();
function checkTimer(res) {
res.body.message.message.should.equal('UNAUTHORIZED');
const delta = Date.now() - time;
delta.should.be.greaterThan(49);
done();
}
function pingAgain (res) {
res.body.message.message.should.equal('UNAUTHORIZED');
ping_authorized_endpoint(ctx.app, 200, checkTimer, true);
}
ping_authorized_endpoint(ctx.app, 200, pingAgain, true);
});
});
it('should work fine authorized', function (done) {
var known = 'b723e97aa97846eb92d5264f084b2823f57c4aa1';
delete process.env.API_SECRET;
process.env.API_SECRET = 'this is my long pass phrase';
var env = require('../lib/server/env')( );
env.enclave.isApiKey(known).should.equal(true);
setup_app(env, function (ctx) {
ctx.app.enabled('api').should.equal(true);
ctx.app.api_secret = env.api_secret;
ping_authorized_endpoint(ctx.app, 200, done);
});
});
function ping_authorized_endpoint (app, httpResponse, fn, passres) {
request(app)
.get('/verifyauth')
.set('api-secret', app.api_secret || '')
.expect(httpResponse)
.end(function (err, res) {
res.body.status.should.equal(httpResponse);
if (passres) { fn(res); } else { fn(); }
// console.log('err', err, 'res', res);
});
}
});