forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi3.read.test.js
224 lines (158 loc) · 6.28 KB
/
api3.read.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
/* eslint require-atomic-updates: 0 */
/* global should */
'use strict';
require('should');
describe('API3 READ', 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.validDoc = {
date: (new Date()).getTime(),
app: testConst.TEST_APP,
device: testConst.TEST_DEVICE + ' API3 READ',
uploaderBattery: 58
};
self.validDoc.identifier = opTools.calculateIdentifier(self.validDoc);
self.timeout(15000);
before(async () => {
self.instance = await instance.create({});
self.app = self.instance.app;
self.env = self.instance.env;
self.col = 'devicestatus';
self.url = `/api/v3/${self.col}`;
let authResult = await authSubject(self.instance.ctx.authorization.storage, [
'create',
'read',
'delete'
], self.instance.app);
self.subject = authResult.subject;
self.jwt = authResult.jwt;
self.cache = self.instance.cacheMonitor;
});
after(() => {
self.instance.ctx.bus.teardown();
});
beforeEach(() => {
self.cache.clear();
});
afterEach(() => {
self.cache.shouldBeEmpty();
});
it('should require authentication', async () => {
let res = await self.instance.get(`${self.url}/FAKE_IDENTIFIER`)
.expect(401);
res.body.status.should.equal(401);
res.body.message.should.equal('Missing or bad access token or JWT');
});
it('should not found not existing collection', async () => {
let res = await self.instance.get(`/api/v3/NOT_EXIST/NOT_EXIST`, self.jwt.read)
.send(self.validDoc)
.expect(404);
res.body.status.should.equal(404);
should.not.exist(res.body.result);
self.cache.shouldBeEmpty()
});
it('should not found not existing document', async () => {
let res = await self.instance.get(`${self.url}/${self.validDoc.identifier}`, self.jwt.read)
.expect(404);
res.body.status.should.equal(404);
should.not.exist(res.body.result);
self.cache.shouldBeEmpty()
});
it('should read just created document', async () => {
let res = await self.instance.post(`${self.url}`, self.jwt.create)
.send(self.validDoc)
.expect(201);
res.body.status.should.equal(201);
res = await self.instance.get(`${self.url}/${self.validDoc.identifier}`, self.jwt.read)
.expect(200);
res.body.status.should.equal(200);
const result = res.body.result;
result.should.containEql(self.validDoc);
result.should.have.property('srvCreated').which.is.a.Number();
result.should.have.property('srvModified').which.is.a.Number();
result.should.have.property('subject');
self.validDoc.subject = result.subject; // let's store subject for later tests
self.cache.nextShouldEql(self.col, self.validDoc)
});
it('should contain only selected fields', async () => {
let res = await self.instance.get(`${self.url}/${self.validDoc.identifier}?fields=date,device,subject`, self.jwt.read)
.expect(200);
res.body.status.should.equal(200);
const correct = {
date: self.validDoc.date,
device: self.validDoc.device,
subject: self.validDoc.subject
};
res.body.result.should.eql(correct);
});
it('should contain all fields', async () => {
let res = await self.instance.get(`${self.url}/${self.validDoc.identifier}?fields=_all`, self.jwt.read)
.expect(200);
res.body.status.should.equal(200);
for (let fieldName of ['app', 'date', 'device', 'identifier', 'srvModified', 'uploaderBattery', 'subject']) {
res.body.result.should.have.property(fieldName);
}
});
it('should not send unmodified document since', async () => {
let res = await self.instance.get(`${self.url}/${self.validDoc.identifier}`, self.jwt.read)
.set('If-Modified-Since', new Date(new Date().getTime() + 1000).toUTCString())
.expect(304);
res.body.should.be.empty();
});
it('should send modified document since', async () => {
let res = await self.instance.get(`${self.url}/${self.validDoc.identifier}`, self.jwt.read)
.set('If-Modified-Since', new Date(new Date(self.validDoc.date).getTime() - 1000).toUTCString())
.expect(200);
res.body.status.should.equal(200);
res.body.result.should.containEql(self.validDoc);
});
it('should recognize softly deleted document', async () => {
let res = await self.instance.delete(`${self.url}/${self.validDoc.identifier}`, self.jwt.delete)
.expect(200);
res.body.status.should.equal(200);
self.cache.nextShouldDeleteLast(self.col)
res = await self.instance.get(`${self.url}/${self.validDoc.identifier}`, self.jwt.read)
.expect(410);
res.body.status.should.equal(410);
should.not.exist(res.body.result);
});
it('should not find permanently deleted document', async () => {
let res = await self.instance.delete(`${self.url}/${self.validDoc.identifier}?permanent=true`, self.jwt.delete)
.expect(200);
res.body.status.should.equal(200);
self.cache.nextShouldDeleteLast(self.col)
res = await self.instance.get(`${self.url}/${self.validDoc.identifier}`, self.jwt.read)
.expect(404);
res.body.status.should.equal(404);
should.not.exist(res.body.result);
});
it('should find document created by APIv1', async () => {
const doc = Object.assign({}, self.validDoc, {
created_at: new Date(self.validDoc.date).toISOString()
});
delete doc.identifier;
await new Promise((resolve, reject) => {
self.instance.ctx.devicestatus.create([doc], async (err) => { // let's insert the document in APIv1's way
should.not.exist(err);
doc._id = doc._id.toString();
self.cache.nextShouldEql(self.col, doc)
err ? reject(err) : resolve(doc);
});
});
const identifier = doc._id.toString();
delete doc._id;
let res = await self.instance.get(`${self.url}/${identifier}`, self.jwt.read)
.expect(200);
res.body.status.should.equal(200);
res.body.result.should.containEql(doc);
res = await self.instance.delete(`${self.url}/${identifier}?permanent=true`, self.jwt.delete)
.expect(200);
res.body.status.should.equal(200);
self.cache.nextShouldDeleteLast(self.col)
});
})
;