forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi3.patch.test.js
239 lines (169 loc) · 6.99 KB
/
api3.patch.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
/* eslint require-atomic-updates: 0 */
'use strict';
require('should');
describe('API3 PATCH', 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(),
utcOffset: -180,
app: testConst.TEST_APP,
device: testConst.TEST_DEVICE + ' API3 PATCH',
eventType: 'Correction Bolus',
insulin: 0.3
};
self.validDoc.identifier = opTools.calculateIdentifier(self.validDoc);
self.timeout(15000);
/**
* Get document detail for futher processing
*/
self.get = async function get (identifier) {
let res = await self.instance.get(`${self.url}/${identifier}`, self.jwt.read)
.expect(200);
res.body.status.should.equal(200);
return res.body.result;
};
before(async () => {
self.instance = await instance.create({});
self.app = self.instance.app;
self.env = self.instance.env;
self.col = 'treatments';
self.url = `/api/v3/${self.col}`;
let authResult = await authSubject(self.instance.ctx.authorization.storage, [
'create',
'update',
'read'
], self.instance.app);
self.subject = authResult.subject;
self.jwt = authResult.jwt;
self.urlIdent = `${self.url}/${self.validDoc.identifier}`;
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.patch(`${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.patch(`/api/v3/NOT_EXIST`, self.jwt.update)
.send(self.validDoc)
.expect(404);
res.body.status.should.equal(404);
});
it('should not found not existing document', async () => {
let res = await self.instance.patch(self.urlIdent, self.jwt.update)
.send(self.validDoc)
.expect(404);
res.body.status.should.equal(404);
// now let's insert the document for further patching
res = await self.instance.post(`${self.url}`, self.jwt.create)
.send(self.validDoc)
.expect(201);
res.body.status.should.equal(201);
self.cache.nextShouldEql(self.col, self.validDoc)
});
it('should reject identifier alteration', async () => {
let res = await self.instance.patch(self.urlIdent, self.jwt.update)
.send(Object.assign({}, self.validDoc, { identifier: 'MODIFIED'}))
.expect(400);
res.body.status.should.equal(400);
res.body.message.should.equal('Field identifier cannot be modified by the client');
});
it('should reject date alteration', async () => {
let res = await self.instance.patch(self.urlIdent, self.jwt.update)
.send(Object.assign({}, self.validDoc, { date: self.validDoc.date + 10000 }))
.expect(400);
res.body.status.should.equal(400);
res.body.message.should.equal('Field date cannot be modified by the client');
});
it('should reject utcOffset alteration', async () => {
let res = await self.instance.patch(self.urlIdent, self.jwt.update)
.send(Object.assign({}, self.validDoc, { utcOffset: self.utcOffset - 120 }))
.expect(400);
res.body.status.should.equal(400);
res.body.message.should.equal('Field utcOffset cannot be modified by the client');
});
it('should reject eventType alteration', async () => {
let res = await self.instance.patch(self.urlIdent, self.jwt.update)
.send(Object.assign({}, self.validDoc, { eventType: 'MODIFIED' }))
.expect(400);
res.body.status.should.equal(400);
res.body.message.should.equal('Field eventType cannot be modified by the client');
});
it('should reject device alteration', async () => {
let res = await self.instance.patch(self.urlIdent, self.jwt.update)
.send(Object.assign({}, self.validDoc, { device: 'MODIFIED' }))
.expect(400);
res.body.status.should.equal(400);
res.body.message.should.equal('Field device cannot be modified by the client');
});
it('should reject app alteration', async () => {
let res = await self.instance.patch(self.urlIdent, self.jwt.update)
.send(Object.assign({}, self.validDoc, { app: 'MODIFIED' }))
.expect(400);
res.body.status.should.equal(400);
res.body.message.should.equal('Field app cannot be modified by the client');
});
it('should reject srvCreated alteration', async () => {
let res = await self.instance.patch(self.urlIdent, self.jwt.update)
.send(Object.assign({}, self.validDoc, { srvCreated: self.validDoc.date - 10000 }))
.expect(400);
res.body.status.should.equal(400);
res.body.message.should.equal('Field srvCreated cannot be modified by the client');
});
it('should reject subject alteration', async () => {
let res = await self.instance.patch(self.urlIdent, self.jwt.update)
.send(Object.assign({}, self.validDoc, { subject: 'MODIFIED' }))
.expect(400);
res.body.status.should.equal(400);
res.body.message.should.equal('Field subject cannot be modified by the client');
});
it('should reject srvModified alteration', async () => {
let res = await self.instance.patch(self.urlIdent, self.jwt.update)
.send(Object.assign({}, self.validDoc, { srvModified: self.validDoc.date - 100000 }))
.expect(400);
res.body.status.should.equal(400);
res.body.message.should.equal('Field srvModified cannot be modified by the client');
});
it('should reject modifiedBy alteration', async () => {
let res = await self.instance.patch(self.urlIdent, self.jwt.update)
.send(Object.assign({}, self.validDoc, { modifiedBy: 'MODIFIED' }))
.expect(400);
res.body.status.should.equal(400);
res.body.message.should.equal('Field modifiedBy cannot be modified by the client');
});
it('should reject isValid alteration', async () => {
let res = await self.instance.patch(self.urlIdent, self.jwt.update)
.send(Object.assign({}, self.validDoc, { isValid: false }))
.expect(400);
res.body.status.should.equal(400);
res.body.message.should.equal('Field isValid cannot be modified by the client');
});
it('should patch document', async () => {
self.validDoc.carbs = 10;
let res = await self.instance.patch(self.urlIdent, self.jwt.update)
.send(self.validDoc)
.expect(200);
res.body.status.should.equal(200);
let body = await self.get(self.validDoc.identifier);
body.carbs.should.equal(10);
body.insulin.should.equal(0.3);
body.subject.should.equal(self.subject.apiCreate.name);
body.modifiedBy.should.equal(self.subject.apiUpdate.name);
self.cache.nextShouldEql(self.col, body)
});
});