forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.treatments.test.js
247 lines (226 loc) · 8.95 KB
/
api.treatments.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
'use strict';
var _ = require('lodash');
var request = require('supertest');
var should = require('should');
var language = require('../lib/language')();
var _moment = require('moment');
describe('Treatment API', function ( ) {
this.timeout(10000);
var self = this;
var api_secret_hash = 'b723e97aa97846eb92d5264f084b2823f57c4aa1';
var api = require('../lib/api/');
beforeEach(function (done) {
process.env.API_SECRET = 'this is my long pass phrase';
self.env = require('../lib/server/env')();
self.env.settings.authDefaultRoles = 'readable';
self.env.settings.enable = ['careportal', 'api'];
this.wares = require('../lib/middleware/')(self.env);
self.app = require('express')();
self.app.enable('api');
require('../lib/server/bootevent')(self.env, language).boot(function booted(ctx) {
self.ctx = ctx;
self.ctx.ddata = require('../lib/data/ddata')();
self.app.use('/api', api(self.env, ctx));
done();
});
});
after(function () {
// delete process.env.API_SECRET;
});
it('post single treatments', function (done) {
self.ctx.treatments().remove({ }, function ( ) {
var now = (new Date()).toISOString();
request(self.app)
.post('/api/treatments/')
.set('api-secret', api_secret_hash || '')
.send({eventType: 'Meal Bolus', created_at: now, carbs: '30', insulin: '2.00', preBolus: '15', glucose: 100, glucoseType: 'Finger', units: 'mg/dl', notes: '<IMG SRC="javascript:alert(\'XSS\');">'})
.expect(200)
.end(function (err) {
if (err) {
done(err);
} else {
self.ctx.treatments.list({}, function (err, list) {
var sorted = _.sortBy(list, function (treatment) {
return treatment.created_at;
});
sorted.length.should.equal(2);
sorted[0].glucose.should.equal(100);
sorted[0].notes.should.equal('<img>');
should.not.exist(sorted[0].eventTime);
sorted[0].insulin.should.equal(2);
sorted[1].carbs.should.equal(30);
done();
});
}
});
});
});
/*
it('saving entry without created_at should fail', function (done) {
self.ctx.treatments().remove({ }, function ( ) {
request(self.app)
.post('/api/treatments/')
.set('api-secret', self.env.api_secret || '')
.send({eventType: 'Meal Bolus', carbs: '30', insulin: '2.00', preBolus: '15', glucose: 100, glucoseType: 'Finger', units: 'mg/dl'})
.expect(422)
.end(function (err) {
if (err) {
done(err);
} else {
done();
}
});
});
});
*/
it('post single treatments in zoned time format', function (done) {
var current_time = Date.now();
console.log('Testing date with local format: ', _moment(current_time).format("YYYY-MM-DDTHH:mm:ss.SSSZZ"));
self.ctx.treatments().remove({ }, function ( ) {
request(self.app)
.post('/api/treatments/')
.set('api-secret', api_secret_hash || '')
.send({eventType: 'Meal Bolus', created_at: _moment(current_time).format("YYYY-MM-DDTHH:mm:ss.SSSZZ"), carbs: '30', insulin: '2.00', glucose: 100, glucoseType: 'Finger', units: 'mg/dl'})
.expect(200)
.end(function (err) {
if (err) {
done(err);
} else {
self.ctx.treatments.list({}, function (err, list) {
var sorted = _.sortBy(list, function (treatment) {
return treatment.created_at;
});
console.log(sorted);
sorted.length.should.equal(1);
sorted[0].glucose.should.equal(100);
should.not.exist(sorted[0].eventTime);
sorted[0].insulin.should.equal(2);
sorted[0].carbs.should.equal(30);
var zonedTime = _moment(current_time).utc().format("YYYY-MM-DDTHH:mm:ss.SSS") + "Z";
sorted[0].created_at.should.equal(zonedTime);
sorted[0].utcOffset.should.equal(-1* new Date().getTimezoneOffset());
done();
});
}
});
});
});
it('post a treatment array', function (done) {
self.ctx.treatments().remove({ }, function ( ) {
var now = (new Date()).toISOString();
request(self.app)
.post('/api/treatments/')
.set('api-secret', api_secret_hash || '')
.send([
{eventType: 'BG Check', created_at: now, glucose: 100, preBolus: '0', glucoseType: 'Finger', units: 'mg/dl', notes: ''}
, {eventType: 'Meal Bolus', created_at: now, carbs: '30', insulin: '2.00', preBolus: '15', glucose: 100, glucoseType: 'Finger', units: 'mg/dl'}
])
.expect(200)
.end(function (err) {
if (err) {
done(err);
} else {
self.ctx.treatments.list({}, function (err, list) {
list.length.should.equal(3);
should.not.exist(list[0].eventTime);
should.not.exist(list[1].eventTime);
done();
});
}
});
});
});
it('post a treatment array and dedupe', function (done) {
self.ctx.treatments().remove({ }, function ( ) {
var now = (new Date()).toISOString();
request(self.app)
.post('/api/treatments/')
.set('api-secret', api_secret_hash || '')
.send([
{eventType: 'BG Check', glucose: 100, units: 'mg/dl', created_at: now}
, {eventType: 'BG Check', glucose: 100, units: 'mg/dl', created_at: now}
, {eventType: 'BG Check', glucose: 100, units: 'mg/dl', created_at: now}
, {eventType: 'BG Check', glucose: 100, units: 'mg/dl', created_at: now}
, {eventType: 'BG Check', glucose: 100, units: 'mg/dl', created_at: now}
, {eventType: 'BG Check', glucose: 100, units: 'mg/dl', created_at: now}
, {eventType: 'BG Check', glucose: 100, units: 'mg/dl', created_at: now}
, {eventType: 'BG Check', glucose: 100, units: 'mg/dl', created_at: now}
, {eventType: 'Meal Bolus', created_at: now, carbs: '30', insulin: '2.00', preBolus: '15', glucose: 100, glucoseType: 'Finger', units: 'mg/dl'}
])
.expect(200)
.end(function (err) {
if (err) {
done(err);
} else {
self.ctx.treatments.list({}, function (err, list) {
var sorted = _.sortBy(list, function (treatment) {
return treatment.created_at;
});
if (sorted.length !== 3) {
console.info('unexpected result length, sorted treatments:', sorted);
}
sorted.length.should.equal(3);
sorted[0].glucose.should.equal(100);
done();
});
}
});
});
});
it('post a treatment, query, delete, verify gone', function (done) {
// insert a treatment - needs to be unique from example data
console.log('Inserting treatment entry');
var now = (new Date()).toISOString();
request(self.app)
.post('/api/treatments/')
.set('api-secret', api_secret_hash || '')
.send({eventType: 'Meal Bolus', created_at: now, carbs: '99', insulin: '2.00', preBolus: '15', glucose: 100, glucoseType: 'Finger', units: 'mg/dl'})
.expect(200)
.end(function (err) {
if (err) {
done(err);
} else {
// make sure treatment was inserted successfully
console.log('Ensuring treatment entry was inserted successfully');
request(self.app)
.get('/api/treatments/')
.query('find[carbs]=99')
.set('api-secret', api_secret_hash || '')
.expect(200)
.expect(function (response) {
response.body[0].carbs.should.equal(99);
})
.end(function (err) {
if (err) {
done(err);
} else {
// delete the treatment
console.log('Deleting test treatment entry');
request(self.app)
.delete('/api/treatments/')
.query('find[carbs]=99')
.set('api-secret', api_secret_hash || '')
.expect(200)
.end(function (err) {
if (err) {
done(err);
} else {
// make sure it was deleted
console.log('Testing if entry was deleted');
request(self.app)
.get('/api/treatments/')
.query('find[carbs]=99')
.set('api-secret', api_secret_hash || '')
.expect(200)
.expect(function (response) {
response.body.length.should.equal(0);
})
.end(done);
}
});
}
});
}
});
});
});