forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.entries.test.js
310 lines (280 loc) · 9.77 KB
/
api.entries.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
'use strict';
var request = require('supertest');
var load = require('./fixtures/load');
var bootevent = require('../lib/server/bootevent');
var language = require('../lib/language')();
require('should');
describe('Entries REST api', function ( ) {
var entries = require('../lib/api/entries/');
var self = this;
this.timeout(10000);
before(function (done) {
self.env = require('../env')( );
self.env.settings.authDefaultRoles = 'readable';
self.wares = require('../lib/middleware/')(self.env);
self.archive = null;
self.app = require('express')( );
self.app.enable('api');
bootevent(self.env, language).boot(function booted (ctx) {
self.app.use('/', entries(self.app, self.wares, ctx));
self.archive = require('../lib/server/entries')(self.env, ctx);
var creating = load('json');
creating.push({type: 'sgv', sgv: 100, date: Date.now()});
self.archive.create(creating, done);
});
});
beforeEach(function (done) {
var creating = load('json');
creating.push({type: 'sgv', sgv: 100, date: Date.now()});
self.archive.create(creating, done);
});
afterEach(function (done) {
self.archive( ).remove({ }, done);
});
after(function (done) {
self.archive( ).remove({ }, done);
});
// keep this test pinned at or near the top in order to validate all
// entries successfully uploaded. if res.body.length is short of the
// expected value, it may indicate a regression in the create
// function callback logic in entries.js.
it('gets requested number of entries', function (done) {
var count = 30;
request(self.app)
.get('/entries.json?find[dateString][$gte]=2014-07-19&count=' + count)
.expect(200)
.end(function (err, res) {
res.body.should.be.instanceof(Array).and.have.lengthOf(count);
done();
});
});
it('gets default number of entries', function (done) {
var defaultCount = 10;
request(self.app)
.get('/entries/sgv.json?find[dateString][$gte]=2014-07-19&find[dateString][$lte]=2014-07-20')
.expect(200)
.end(function (err, res) {
res.body.should.be.instanceof(Array).and.have.lengthOf(defaultCount);
done( );
});
});
it('gets entries in right order', function (done) {
var defaultCount = 10;
request(self.app)
.get('/entries/sgv.json?find[dateString][$gte]=2014-07-19&find[dateString][$lte]=2014-07-20')
.expect(200)
.end(function (err, res) {
res.body.should.be.instanceof(Array).and.have.lengthOf(defaultCount);
var array = res.body;
var firstEntry = array[0];
var secondEntry = array[1];
firstEntry.date.should.be.above(secondEntry.date);
done( );
});
});
it('/echo/ api shows query', function (done) {
request(self.app)
.get('/echo/entries/sgv.json?find[dateString][$gte]=2014-07-19&find[dateString][$lte]=2014-07-20')
.expect(200)
.end(function (err, res) {
res.body.should.be.instanceof(Object);
res.body.query.should.be.instanceof(Object);
res.body.input.should.be.instanceof(Object);
res.body.input.find.should.be.instanceof(Object);
res.body.storage.should.equal('entries');
done( );
});
});
it('/slice/ can slice time', function (done) {
var app = self.app;
request(app)
.get('/slice/entries/dateString/sgv/2014-07.json?count=20')
.expect(200)
.end(function (err, res) {
res.body.should.be.instanceof(Array).and.have.lengthOf(20);
done( );
});
});
it('/times/echo can describe query', function (done) {
var app = self.app;
request(app)
.get('/times/echo/2014-07/.*T{00..05}:.json?count=20&find[sgv][$gte]=160')
.expect(200)
.end(function (err, res) {
res.body.should.be.instanceof(Object);
res.body.req.should.have.property('query');
res.body.should.have.property('pattern').with.lengthOf(6);
done( );
});
});
it('/slice/ can slice with multiple prefix', function (done) {
var app = self.app;
request(app)
.get('/slice/entries/dateString/sgv/2014-07-{17..20}.json?count=20')
.expect(200)
.end(function (err, res) {
res.body.should.be.instanceof(Array).and.have.lengthOf(20);
done( );
});
});
it('/slice/ can slice time with prefix and no results', function (done) {
var app = self.app;
request(app)
.get('/slice/entries/dateString/sgv/1999-07.json?count=20&find[sgv][$lte]=401')
.expect(200)
.end(function (err, res) {
res.body.should.be.instanceof(Array).and.have.lengthOf(0);
done( );
});
});
it('/times/ can get modal times', function (done) {
var app = self.app;
request(app)
.get('/times/2014-07-/{0..30}T.json?')
.expect(200)
.end(function (err, res) {
res.body.should.be.instanceof(Array).and.have.lengthOf(10);
done( );
});
});
it('/times/ can get modal minutes and times', function (done) {
var app = self.app;
request(app)
.get('/times/20{14..15}-07/T{09..10}.json?')
.expect(200)
.end(function (err, res) {
res.body.should.be.instanceof(Array).and.have.lengthOf(10);
done( );
});
});
it('/times/ can get multiple prefixen and modal minutes and times', function (done) {
var app = self.app;
request(app)
.get('/times/20{14..15}/T.*:{00..60}.json?')
.expect(200)
.end(function (err, res) {
res.body.should.be.instanceof(Array).and.have.lengthOf(10);
done( );
});
});
it('/entries/current.json', function (done) {
request(self.app)
.get('/entries/current.json')
.expect(200)
.end(function (err, res) {
res.body.should.be.instanceof(Array).and.have.lengthOf(1);
res.body[0].sgv.should.equal(100);
done();
});
});
it('/entries/:id', function (done) {
var app = self.app;
self.archive.list({count: 1}, function(err, records) {
var currentId = records.pop()._id.toString();
request(app)
.get('/entries/'+currentId+'.json')
.expect(200)
.end(function (err, res) {
res.body.should.be.instanceof(Array).and.have.lengthOf(1);
res.body[0]._id.should.equal(currentId);
done( );
});
});
});
it('/entries/:model', function (done) {
var app = self.app;
request(app)
.get('/entries/sgv/.json?count=10&find[dateString][$gte]=2014')
.expect(200)
.end(function (err, res) {
res.body.should.be.instanceof(Array).and.have.lengthOf(10);
done( );
});
});
it('disallow POST by readable /entries/preview', function (done) {
request(self.app)
.post('/entries/preview.json')
.send(load('json'))
.expect(401)
.end(function (err, res) {
// res.body.should.be.instanceof(Array).and.have.lengthOf(30);
done();
});
});
it('disallow deletes unauthorized', function (done) {
var app = self.app;
request(app)
.delete('/entries/sgv?find[dateString][$gte]=2014-07-19&find[dateString][$lte]=2014-07-20')
.expect(401)
.end(function (err) {
if (err) {
done(err);
} else {
request(app)
.get('/entries/sgv.json?find[dateString][$gte]=2014-07-19&find[dateString][$lte]=2014-07-20')
.expect(200)
.end(function (err, res) {
res.body.should.be.instanceof(Array).and.have.lengthOf(10);
done();
});
}
});
});
it('post an entry, query, delete, verify gone', function (done) {
// insert a glucose entry - needs to be unique from example data
console.log('Inserting glucose entry')
request(self.app)
.post('/entries/')
.set('api-secret', self.env.api_secret || '')
.send({
"type": "sgv", "sgv": "199", "dateString": "2014-07-20T00:44:15.000-07:00"
, "date": 1405791855000, "device": "dexcom", "direction": "NOT COMPUTABLE"
})
.expect(200)
.end(function (err) {
if (err) {
done(err);
} else {
// make sure treatment was inserted successfully
console.log('Ensuring glucose entry was inserted successfully');
request(self.app)
.get('/entries.json?find[dateString][$gte]=2014-07-20&count=100')
.set('api-secret', self.env.api_secret || '')
.expect(200)
.expect(function (response) {
var entry = response.body[0];
entry.sgv.should.equal('199');
entry.utcOffset.should.equal(-420);
})
.end(function (err) {
if (err) {
done(err);
} else {
// delete the glucose entry
console.log('Deleting test glucose entry');
request(self.app)
.delete('/entries.json?find[dateString][$gte]=2014-07-20&count=100')
.set('api-secret', self.env.api_secret || '')
.expect(200)
.end(function (err) {
if (err) {
done(err);
} else {
// make sure it was deleted
console.log('Testing if glucose entry was deleted');
request(self.app)
.get('/entries.json?find[dateString][$gte]=2014-07-20&count=100')
.set('api-secret', self.env.api_secret || '')
.expect(200)
.expect(function (response) {
response.body.length.should.equal(0);
})
.end(done);
}
});
}
});
}
});
});
});