-
Notifications
You must be signed in to change notification settings - Fork 9
/
api.feeds.delete.js
579 lines (527 loc) · 27.8 KB
/
api.feeds.delete.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
const should = require('should');
const flow = require('nimble');
const httpStatus = require('http-status');
const superagent = require('superagent-ls');
const requireNew = require('require-new');
const wipe = require('./fixture-helpers/wipe');
const setup = require('./fixture-helpers/setup');
const createAuthorizationHeader = require('./fixture-helpers/test-utils').createAuthorizationHeader;
const fs = require('fs');
const path = require('path');
const config = require('../config');
const ESDR_API_ROOT_URL = config.get("esdr:apiRootUrl");
const ESDR_FEEDS_API_URL = ESDR_API_ROOT_URL + "/feeds";
describe("REST API", function() {
const client1 = requireNew('./fixtures/client1.json');
const client2 = requireNew('./fixtures/client2.json');
const user1 = requireNew('./fixtures/user1.json');
const user2 = requireNew('./fixtures/user2.json');
let user1Client2 = null;
const product1 = requireNew('./fixtures/product1.json');
const product2 = requireNew('./fixtures/product2.json');
const device1 = requireNew('./fixtures/device1.json');
const device2 = requireNew('./fixtures/device2.json');
const device3 = requireNew('./fixtures/device3.json');
const feed1 = requireNew('./fixtures/feed1.json'); // public, user 1, product 1, device 1
const feed2 = requireNew('./fixtures/feed2.json'); // private, user 1, product 1, device 1
const feed3 = requireNew('./fixtures/feed3.json'); // public, user 1, product 2, device 2
const feed4 = requireNew('./fixtures/feed4.json'); // private, user 1, product 2, device 2
const feed5 = requireNew('./fixtures/feed5.json'); // public, user 2, product 1, device 3
const feed6 = requireNew('./fixtures/feed6.json'); // private, user 2, product 1, device 3
const feed7 = requireNew('./fixtures/feed-custom-channelSpecs.json'); // private, user 1, product 1, device 1
const feed8 = requireNew('./fixtures/feed-null-channelSpecs.json'); // private, user 1, product 1, device 1
const feedUpload1 = {
request : requireNew('./fixtures/feed-upload1-request.json'),
response : requireNew('./fixtures/feed-upload1-response.json')
};
const feedUpload5 = {
request : requireNew('./fixtures/feed-upload5-request.json'),
response : requireNew('./fixtures/feed-upload5-response.json')
};
before(function(initDone) {
const doUpload = function(upload, done) {
superagent
.put(ESDR_FEEDS_API_URL + "/" + upload.feed.id)
.set(createAuthorizationHeader(upload.user.accessToken))
.send(upload.uploadRequestResponse.request)
.end(function(err, res) {
should.not.exist(err);
should.exist(res);
res.should.have.property('status', httpStatus.OK);
res.should.have.property('body');
res.body.should.have.properties({
code : httpStatus.OK,
status : 'success'
});
res.body.should.have.property('data');
res.body.data.should.have.properties(upload.uploadRequestResponse.response.data);
done();
});
};
flow.series(
[
wipe.wipeAllData,
function(done) {
setup.createClient(client1, done);
},
function(done) {
setup.createClient(client2, done);
},
function(done) {
setup.createUser(user1, done);
},
function(done) {
setup.verifyUser(user1, done);
},
function(done) {
setup.authenticateUserWithClient(user1, client1, done);
},
function(done) {
// authenticate the same user with a different client
user1Client2 = JSON.parse(JSON.stringify(user1));
setup.authenticateUserWithClient(user1Client2, client2, done);
},
function(done) {
setup.createUser(user2, done);
},
function(done) {
setup.verifyUser(user2, done);
},
function(done) {
setup.authenticateUser(user2, done);
},
function(done) {
product1.creatorUserId = user1.id;
setup.createProduct(product1, done);
},
function(done) {
product2.creatorUserId = user1.id;
setup.createProduct(product2, done);
},
function(done) {
device1.userId = user1.id;
device1.productId = product1.id;
setup.createDevice(device1, done);
},
function(done) {
device2.userId = user1.id;
device2.productId = product2.id;
setup.createDevice(device2, done);
},
function(done) {
device3.userId = user2.id;
device3.productId = product1.id;
setup.createDevice(device3, done);
},
function(done) {
feed1.userId = user1.id;
feed1.deviceId = device1.id;
feed1.productId = product1.id;
feed1.channelSpecs = product1.defaultChannelSpecs;
setup.createFeed(feed1, done);
},
function(done) {
feed2.userId = user1.id;
feed2.deviceId = device1.id;
feed2.productId = product1.id;
feed2.channelSpecs = product1.defaultChannelSpecs;
setup.createFeed(feed2, done);
},
function(done) {
feed3.userId = user1.id;
feed3.deviceId = device2.id;
feed3.productId = product2.id;
feed3.channelSpecs = product2.defaultChannelSpecs;
setup.createFeed(feed3, done);
},
function(done) {
feed4.userId = user1.id;
feed4.deviceId = device2.id;
feed4.productId = product2.id;
feed4.channelSpecs = product2.defaultChannelSpecs;
setup.createFeed(feed4, done);
},
function(done) {
feed5.userId = user2.id;
feed5.deviceId = device3.id;
feed5.productId = product1.id;
feed5.channelSpecs = product1.defaultChannelSpecs;
setup.createFeed(feed5, done);
},
function(done) {
feed6.userId = user2.id;
feed6.deviceId = device3.id;
feed6.productId = product1.id;
feed6.channelSpecs = product1.defaultChannelSpecs;
setup.createFeed(feed6, done);
},
function(done) {
feed7.userId = user1.id;
feed7.deviceId = device1.id;
feed7.productId = product1.id;
feed7.channelSpecs = JSON.stringify(feed7.channelSpecs);
setup.createFeed(feed7, done);
},
function(done) {
feed8.userId = user1.id;
feed8.deviceId = device1.id;
feed8.productId = product1.id;
feed8.channelSpecs = product1.defaultChannelSpecs;
setup.createFeed(feed8, done);
},
function(done) {
doUpload({
feed : feed1,
user : user1,
uploadRequestResponse : feedUpload1
}, done);
},
function(done) {
doUpload({
feed : feed2,
user : user1,
uploadRequestResponse : feedUpload5
}, done);
}
],
initDone
);
});
describe("Feeds", function() {
describe("Delete", function() {
const executeDelete = function(test, done) {
superagent
.del(test.url)
.set(typeof test.headers === 'undefined' ? {} : test.headers)
.end(function(err, res) {
should.not.exist(err);
should.exist(res);
if (test.willDebug) {
console.log(JSON.stringify(res.body, null, 3));
}
res.should.have.property('status', test.expectedHttpStatus);
res.should.have.property('body');
if (test.hasEmptyBody) {
res.body.should.be.empty();
}
else {
res.body.should.have.properties({
code : test.expectedHttpStatus,
status : test.expectedStatusText
});
if (typeof test.expectedResponseData !== 'undefined') {
if (test.expectedResponseData == null) {
res.body.should.have.property('data', null);
}
else {
res.body.should.have.property('data');
res.body.data.should.have.properties(test.expectedResponseData);
}
}
}
if (typeof test.additionalTests === 'function') {
test.additionalTests(done);
}
else {
done();
}
});
};
const verifyFeedIsDeleted = function(feedId, user, done) {
superagent
.get(ESDR_FEEDS_API_URL + "/" + feedId)
.set(createAuthorizationHeader(user.accessToken))
.end(function(err, res) {
should.not.exist(err);
should.exist(res);
res.should.have.property('status', httpStatus.NOT_FOUND);
res.should.have.property('body');
res.body.should.have.properties({
code : httpStatus.NOT_FOUND,
status : 'error'
});
// make sure the feed directory doesn't exist anymore
const dirPath = path.join(config.get("datastore:dataDirectory"), String(user.id), "feed_" + feedId);
(function() {
fs.statSync(dirPath)
}).should.throw(Error);
done();
});
};
describe("No Authentication", function() {
it("Shouldn't be able to delete a public feed without authentication", function(done) {
executeDelete({
url : ESDR_FEEDS_API_URL + "/" + feed1.id,
expectedHttpStatus : httpStatus.UNAUTHORIZED,
expectedStatusText : 'error',
hasEmptyBody : true
}, done);
});
it("Shouldn't be able to delete a private feed without authentication", function(done) {
executeDelete({
url : ESDR_FEEDS_API_URL + "/" + feed2.id,
expectedHttpStatus : httpStatus.UNAUTHORIZED,
expectedStatusText : 'error',
hasEmptyBody : true
}, done);
});
}); // End No Authentication
describe("API Key Authentication", function() {
describe("Feed API Key in the request header", function() {
it("Shouldn't be able to delete a feed referencing it by read-write API key", function(done) {
executeDelete({
url : ESDR_FEEDS_API_URL + "/" + feed1.id,
headers : { FeedApiKey : feed1.apiKey },
expectedHttpStatus : httpStatus.UNAUTHORIZED,
expectedStatusText : 'error',
hasEmptyBody : true
}, done);
});
it("Shouldn't be able to delete a feed referencing it by read-only API key", function(done) {
executeDelete({
url : ESDR_FEEDS_API_URL + "/" + feed1.id,
headers : { FeedApiKey : feed1.apiKeyReadOnly },
expectedHttpStatus : httpStatus.UNAUTHORIZED,
expectedStatusText : 'error',
hasEmptyBody : true
}, done);
});
}); // End Feed API Key in the request header
describe("Feed API Key in the URL", function() {
it("Shouldn't be able to delete a feed referencing it by read-write API key", function(done) {
executeDelete({
url : ESDR_FEEDS_API_URL + "/" + feed1.apiKey,
expectedHttpStatus : httpStatus.UNAUTHORIZED,
expectedStatusText : 'error',
hasEmptyBody : true
}, done);
});
it("Shouldn't be able to delete a feed referencing it by read-only API key", function(done) {
executeDelete({
url : ESDR_FEEDS_API_URL + "/" + feed1.apiKeyReadOnly,
expectedHttpStatus : httpStatus.UNAUTHORIZED,
expectedStatusText : 'error',
hasEmptyBody : true
}, done);
});
}); // End Feed API Key in the URL
}); // End API Key Authentication
describe("OAuth2 authentication", function() {
it("Should be able to delete a public feed with authentication by the owning user", function(done) {
executeDelete({
url : ESDR_FEEDS_API_URL + "/" + feed1.id,
headers : createAuthorizationHeader(user1.accessToken),
expectedHttpStatus : httpStatus.OK,
expectedStatusText : 'success',
expectedResponseData : { id : feed1.id },
additionalTests : function(done) {
// make sure the feed no longer exists
verifyFeedIsDeleted(feed1.id, user1, done);
}
}, done);
});
it("Shouldn't be able to delete the same public feed again", function(done) {
executeDelete({
url : ESDR_FEEDS_API_URL + "/" + feed1.id,
headers : createAuthorizationHeader(user1.accessToken),
expectedHttpStatus : httpStatus.NOT_FOUND,
expectedStatusText : 'error',
expectedResponseData : { id : feed1.id }
}, done);
});
it("Should be able to delete a private feed with authentication by the owning user", function(done) {
executeDelete({
url : ESDR_FEEDS_API_URL + "/" + feed2.id,
headers : createAuthorizationHeader(user1.accessToken),
expectedHttpStatus : httpStatus.OK,
expectedStatusText : 'success',
expectedResponseData : { id : feed2.id },
additionalTests : function(done) {
// make sure the feed no longer exists
verifyFeedIsDeleted(feed2.id, user1, done);
}
}, done);
});
it("Shouldn't be able to delete the same private feed again", function(done) {
executeDelete({
url : ESDR_FEEDS_API_URL + "/" + feed2.id,
headers : createAuthorizationHeader(user1.accessToken),
expectedHttpStatus : httpStatus.NOT_FOUND,
expectedStatusText : 'error',
expectedResponseData : { id : feed2.id }
}, done);
});
it("Shouldn't be able to delete a public feed owned by a different user", function(done) {
executeDelete({
url : ESDR_FEEDS_API_URL + "/" + feed5.id,
headers : createAuthorizationHeader(user1.accessToken),
expectedHttpStatus : httpStatus.FORBIDDEN,
expectedStatusText : 'error',
expectedResponseData : { id : feed5.id }
}, done);
});
it("Shouldn't be able to delete a private feed owned by a different user", function(done) {
executeDelete({
url : ESDR_FEEDS_API_URL + "/" + feed5.id,
headers : createAuthorizationHeader(user1.accessToken),
expectedHttpStatus : httpStatus.FORBIDDEN,
expectedStatusText : 'error',
expectedResponseData : { id : feed5.id }
}, done);
});
it("Shouldn't be able to delete a public feed with an invalid access token", function(done) {
executeDelete({
url : ESDR_FEEDS_API_URL + "/" + feed5.id,
headers : createAuthorizationHeader("bogus"),
expectedHttpStatus : httpStatus.UNAUTHORIZED,
expectedStatusText : 'error',
hasEmptyBody : true
}, done);
});
it("Shouldn't be able to delete a private feed with an invalid access token", function(done) {
executeDelete({
url : ESDR_FEEDS_API_URL + "/" + feed5.id,
headers : createAuthorizationHeader("bogus"),
expectedHttpStatus : httpStatus.UNAUTHORIZED,
expectedStatusText : 'error',
hasEmptyBody : true
}, done);
});
it("Shouldn't be able to delete a feed with an invalid ID (int)", function(done) {
executeDelete({
url : ESDR_FEEDS_API_URL + "/" + 0,
headers : createAuthorizationHeader(user1.accessToken),
expectedHttpStatus : httpStatus.NOT_FOUND,
expectedStatusText : 'error',
expectedResponseData : null
}, done);
});
it("Shouldn't be able to delete a feed with an invalid ID (negative int)", function(done) {
executeDelete({
url : ESDR_FEEDS_API_URL + "/" + -30,
headers : createAuthorizationHeader(user1.accessToken),
expectedHttpStatus : httpStatus.NOT_FOUND,
expectedStatusText : 'error',
expectedResponseData : null
}, done);
});
it("Shouldn't be able to delete a feed with an invalid ID (string)", function(done) {
executeDelete({
url : ESDR_FEEDS_API_URL + "/" + "bogus",
headers : createAuthorizationHeader(user1.accessToken),
expectedHttpStatus : httpStatus.NOT_FOUND,
expectedStatusText : 'error',
expectedResponseData : null
}, done);
});
describe("Cascading Delete of Feed Properties", function() {
const setProperty = function(feedId, accessToken, propertyKey, propertyValue, callback, willDebug) {
superagent
.put(ESDR_FEEDS_API_URL + "/" + feedId + "/properties/" + propertyKey)
.set(createAuthorizationHeader(accessToken))
.send(propertyValue)
.end(function(err, res) {
should.not.exist(err);
should.exist(res);
if (willDebug) {
console.log(JSON.stringify(res.body, null, 3));
}
res.should.have.property('status', httpStatus.OK);
res.should.have.property('body');
res.body.should.have.properties({
code : httpStatus.OK,
status : 'success'
});
res.body.should.have.property('data');
const expectedResponse = {};
expectedResponse[propertyKey] = propertyValue.value;
res.body.data.should.have.properties(expectedResponse);
callback();
});
};
const getProperty = function(feedId, accessToken, propertyKey, callback, willDebug, expectedValue) {
superagent
.get(ESDR_FEEDS_API_URL + "/" + feedId + "/properties/" + propertyKey)
.set(createAuthorizationHeader(accessToken))
.end(function(err, res) {
should.not.exist(err);
should.exist(res);
if (willDebug) {
console.log(JSON.stringify(res.body, null, 3));
}
res.should.have.property('status', httpStatus.OK);
res.should.have.property('body');
res.body.should.have.properties({
code : httpStatus.OK,
status : 'success'
});
res.body.should.have.property('data');
if (typeof expectedValue !== 'undefined') {
const expectedResponse = {};
expectedResponse[propertyKey] = expectedValue;
res.body.data.should.have.properties(expectedResponse);
}
callback();
});
};
before(function(initDone) {
flow.series([
// set a property on feed 3 with client 1
function(done) {
setProperty(feed3.id,
user1.accessToken,
'foo',
{ type : 'int', value : 42 },
done);
},
// verify the property is set
function(done) {
getProperty(feed3.id, user1.accessToken, 'foo', done, false, 42);
},
// set a property on feed 3 with client 2
function(done) {
setProperty(feed3.id,
user1Client2.accessToken,
'bar',
{ type : 'string', value : 'forty-two' },
done);
},
// verify the property is set
function(done) {
getProperty(feed3.id, user1Client2.accessToken, 'bar', done, false, 'forty-two');
},
// set a property on feed 4 with client 1
function(done) {
setProperty(feed4.id,
user1.accessToken,
'baz',
{ type : 'double', value : 42.42 },
done);
},
// verify the property is set
function(done) {
getProperty(feed4.id, user1.accessToken, 'baz', done, false, 42.42);
}
], initDone);
});
it("Should be able to delete a feed having feed properties, and the cascading delete will delete the feed's properties too", function(done) {
executeDelete({
url : ESDR_FEEDS_API_URL + "/" + feed3.id,
headers : createAuthorizationHeader(user1.accessToken),
expectedHttpStatus : httpStatus.OK,
expectedStatusText : 'success',
expectedResponseData : { id : feed3.id },
additionalTests : function(done) {
// make sure the feed no longer exists
verifyFeedIsDeleted(feed3.id, user1, function() {
// make sure the property for feed4 didn't get deleted
getProperty(feed4.id, user1.accessToken, 'baz', done, false, 42.42);
});
}
}, done);
});
}); // Cascading Delete of Feed Properties
}); // End OAuth2 authentication
}); // End Delete
}); // End Feeds
}); // End REST API