forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurity.test.js
178 lines (142 loc) · 4.32 KB
/
security.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
174
175
176
177
178
'use strict';
const request = require('supertest');
const should = require('should');
const language = require('../lib/language')();
//const io = require('socket.io-client');
describe('API_SECRET', function() {
var api;
var scope = this;
var websocket;
var app;
var server;
var listener;
this.timeout(7000);
afterEach(function(done) {
if (listener) {
listener.close(done);
}
done();
});
after(function(done) {
if (listener) {
listener.close(done);
}
done();
});
function setup_app (env, fn) {
api = require('../lib/api/');
require('../lib/server/bootevent')(env, language).boot(function booted (ctx) {
ctx.app = api(env, ctx);
scope.app = ctx.app;
scope.entries = ctx.entries;
fn(ctx);
});
}
function setup_big_app (env, fn) {
api = require('../lib/api/');
require('../lib/server/bootevent')(env, language).boot(function booted (ctx) {
ctx.app = api(env, ctx);
scope.app = ctx.app;
scope.entries = ctx.entries;
app = require('../lib/server/app')(env, ctx);
server = require('http').createServer(app);
listener = server.listen(1337, 'localhost');
websocket = require('../lib/server/websocket')(env, ctx, server);
fn(ctx);
});
}
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';
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);
ping_status(ctx.app, again);
function again () {
ctx.app.api_secret = '';
ping_authorized_endpoint(ctx.app, 401, done);
}
});
});
it('should work fine set', 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);
ping_status(ctx.app, again);
function again () {
ctx.app.api_secret = known;
ping_authorized_endpoint(ctx.app, 200, done);
}
});
});
it('should not work short', function() {
delete process.env.API_SECRET;
process.env.API_SECRET = 'tooshort';
var env = require('../lib/server/env')();
should.not.exist(env.api_secret);
env.err[0].desc.should.startWith('API_SECRET should be at least');
});
function ping_status (app, fn) {
request(app)
.get('/status.json')
.expect(200)
.end(function(err, res) {
res.body.status.should.equal('ok');
fn();
});
}
function ping_authorized_endpoint (app, fails, fn) {
request(app)
.get('/experiments/test')
.set('api-secret', app.api_secret || '')
.expect(fails)
.end(function(err, res) {
if (fails < 400) {
res.body.status.should.equal('ok');
}
fn();
});
}
/*
it('socket IO should connect', function(done) {
var known = 'b723e97aa97846eb92d5264f084b2823f57c4aa1';
process.env.API_SECRET = 'this is my long pass phrase';
var env = require('../lib/server/env')();
setup_big_app(env, function(ctx) {
const socket2 = io.connect('ws://localhost:1337/');
socket2.on('connect', function() {
console.log('Socket 2 authorizing');
socket2.emit("authorize", {
secret: known
});
});
socket2.on('disconnect', function() {
//socket.emit("authorize");
console.log('Client 2 disconnected');
done();
});
socket2.on('connected', function(msg) {
console.log('Connected');
// Disconnect both client connections
socket2.disconnect();
const socket = io.connect('ws://localhost:1337/');
socket.on('connect', function() {
console.log('Socket 1 authorizing');
socket.emit("authorize");
});
socket.on('disconnect', function() {
//socket.emit("authorize");
console.log('Client 1 disconnected');
done();
});
});
});
});
*/
});