forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi3.search.test.js
279 lines (203 loc) · 7.97 KB
/
api3.search.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
/* eslint require-atomic-updates: 0 */
/* global should */
'use strict';
require('should');
describe('API3 SEARCH', function() {
const self = this
, testConst = require('./fixtures/api3/const.json')
, instance = require('./fixtures/api3/instance')
, authSubject = require('./fixtures/api3/authSubject')
, opTools = require('../lib/api3/shared/operationTools')
;
self.docs = testConst.SAMPLE_ENTRIES;
self.timeout(15000);
/**
* Get document detail for futher processing
*/
self.get = function get (identifier, done) {
self.instance.get(`${self.url}/${identifier}?token=${self.token.read}`)
.expect(200)
.end((err, res) => {
should.not.exist(err);
done(res.body);
});
};
/**
* Create given document in a promise
*/
self.create = (doc) => new Promise((resolve) => {
doc.identifier = opTools.calculateIdentifier(doc);
self.instance.post(`${self.url}?token=${self.token.all}`)
.send(doc)
.end((err) => {
should.not.exist(err);
self.get(doc.identifier, resolve);
});
});
before(async () => {
self.testStarted = new Date();
self.instance = await instance.create({});
self.app = self.instance.app;
self.env = self.instance.env;
self.url = '/api/v3/entries';
let authResult = await authSubject(self.instance.ctx.authorization.storage);
self.subject = authResult.subject;
self.token = authResult.token;
self.urlToken = `${self.url}?token=${self.token.read}`;
self.urlTest = `${self.urlToken}&srvModified$gte=${self.testStarted.getTime()}`;
const promises = testConst.SAMPLE_ENTRIES.map(doc => self.create(doc));
self.docs = await Promise.all(promises);
});
after(() => {
self.instance.ctx.bus.teardown();
});
it('should require authentication', async () => {
let res = await self.instance.get(self.url)
.expect(401);
res.body.status.should.equal(401);
res.body.message.should.equal('Missing or bad access token or JWT');
should.not.exist(res.body.result);
});
it('should not found not existing collection', async () => {
let res = await self.instance.get(`/api/v3/NOT_EXIST?token=${self.url}`)
.send(self.validDoc)
.expect(404);
res.body.status.should.equal(404);
should.not.exist(res.body.result);
});
it('should found at least 10 documents', async () => {
let res = await self.instance.get(self.urlToken)
.expect(200);
res.body.status.should.equal(200);
res.body.result.length.should.be.aboveOrEqual(self.docs.length);
});
it('should found at least 10 documents from test start', async () => {
let res = await self.instance.get(self.urlTest)
.expect(200);
res.body.status.should.equal(200);
res.body.result.length.should.be.aboveOrEqual(self.docs.length);
});
it('should reject invalid limit - not a number', async () => {
let res = await self.instance.get(`${self.urlToken}&limit=INVALID`)
.expect(400);
res.body.status.should.equal(400);
res.body.message.should.equal('Parameter limit out of tolerance');
should.not.exist(res.body.result);
});
it('should reject invalid limit - negative number', async () => {
let res = await self.instance.get(`${self.urlToken}&limit=-1`)
.expect(400);
res.body.status.should.equal(400);
res.body.message.should.equal('Parameter limit out of tolerance');
should.not.exist(res.body.result);
});
it('should reject invalid limit - zero', async () => {
let res = await self.instance.get(`${self.urlToken}&limit=0`)
.expect(400);
res.body.status.should.equal(400);
res.body.message.should.equal('Parameter limit out of tolerance');
should.not.exist(res.body.result);
});
it('should accept valid limit', async () => {
let res = await self.instance.get(`${self.urlToken}&limit=3`)
.expect(200);
res.body.status.should.equal(200);
res.body.result.length.should.equal(3);
});
it('should reject invalid skip - not a number', async () => {
let res = await self.instance.get(`${self.urlToken}&skip=INVALID`)
.expect(400);
res.body.status.should.equal(400);
res.body.message.should.equal('Parameter skip out of tolerance');
should.not.exist(res.body.result);
});
it('should reject invalid skip - negative number', async () => {
let res = await self.instance.get(`${self.urlToken}&skip=-5`)
.expect(400);
res.body.status.should.equal(400);
res.body.message.should.equal('Parameter skip out of tolerance');
should.not.exist(res.body.result);
});
it('should reject both sort and sort$desc', async () => {
let res = await self.instance.get(`${self.urlToken}&sort=date&sort$desc=created_at`)
.expect(400);
res.body.status.should.equal(400);
res.body.message.should.equal('Parameters sort and sort_desc cannot be combined');
should.not.exist(res.body.result);
});
it('should sort well by date field', async () => {
let res = await self.instance.get(`${self.urlTest}&sort=date`)
.expect(200);
res.body.status.should.equal(200);
const ascending = res.body.result;
const length = ascending.length;
length.should.be.aboveOrEqual(self.docs.length);
res = await self.instance.get(`${self.urlTest}&sort$desc=date`)
.expect(200);
res.body.status.should.equal(200);
const descending = res.body.result;
descending.length.should.equal(length);
for (let i in ascending) {
ascending[i].should.eql(descending[length - i - 1]);
if (i > 0) {
ascending[i - 1].date.should.be.lessThanOrEqual(ascending[i].date);
}
}
});
it('should skip documents', async () => {
let res = await self.instance.get(`${self.urlToken}&sort=date&limit=8`)
.expect(200);
res.body.status.should.equal(200);
const fullDocs = res.body.result;
fullDocs.length.should.equal(8);
res = await self.instance.get(`${self.urlToken}&sort=date&skip=3&limit=5`)
.expect(200);
res.body.status.should.equal(200);
const skipDocs = res.body.result;
skipDocs.length.should.equal(5);
for (let i = 0; i < 3; i++) {
skipDocs[i].should.be.eql(fullDocs[i + 3]);
}
});
it('should project selected fields', async () => {
let res = await self.instance.get(`${self.urlToken}&fields=date,app,subject`)
.expect(200);
res.body.status.should.equal(200);
res.body.result.forEach(doc => {
const docFields = Object.getOwnPropertyNames(doc);
docFields.sort().should.be.eql(['app', 'date', 'subject']);
});
});
it('should project all fields', async () => {
let res = await self.instance.get(`${self.urlToken}&fields=_all`)
.expect(200);
res.body.status.should.equal(200);
res.body.result.forEach(doc => {
Object.getOwnPropertyNames(doc).length.should.be.aboveOrEqual(10);
Object.prototype.hasOwnProperty.call(doc, '_id').should.not.be.true();
Object.prototype.hasOwnProperty.call(doc, 'identifier').should.be.true();
Object.prototype.hasOwnProperty.call(doc, 'srvModified').should.be.true();
Object.prototype.hasOwnProperty.call(doc, 'srvCreated').should.be.true();
});
});
it('should not exceed the limit of docs count', async () => {
const apiApp = self.instance.ctx.apiApp
, limitBackup = apiApp.get('API3_MAX_LIMIT');
apiApp.set('API3_MAX_LIMIT', 5);
let res = await self.instance.get(`${self.urlToken}&limit=10`)
.expect(400);
res.body.status.should.equal(400);
res.body.message.should.equal('Parameter limit out of tolerance');
apiApp.set('API3_MAX_LIMIT', limitBackup);
});
it('should respect the ceiling (hard) limit of docs', async () => {
const apiApp = self.instance.ctx.apiApp
, limitBackup = apiApp.get('API3_MAX_LIMIT');
apiApp.set('API3_MAX_LIMIT', 5);
let res = await self.instance.get(`${self.urlToken}`)
.expect(200);
res.body.status.should.equal(200);
res.body.result.length.should.equal(5);
apiApp.set('API3_MAX_LIMIT', limitBackup);
});
});