/
collectionController.test.js
726 lines (627 loc) · 25.1 KB
/
collectionController.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
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
const
should = require('should'),
sinon = require('sinon'),
rewire = require('rewire'),
CollectionController = rewire('../../../lib/api/controllers/collectionController'),
{
Request,
errors: {
BadRequestError,
PreconditionError,
NotFoundError,
SizeLimitError
}
} = require('kuzzle-common-objects'),
KuzzleMock = require('../../mocks/kuzzle.mock'),
BaseController = require('../../../lib/api/controllers/baseController');
describe('Test: collection controller', () => {
let
collectionController,
kuzzle,
foo = {foo: 'bar'},
index = '%text',
collection = 'unit-test-collectionController',
request,
engine;
beforeEach(() => {
const data = {
controller: 'collection',
index,
collection
};
kuzzle = new KuzzleMock();
engine = kuzzle.services.list.storageEngine;
collectionController = new CollectionController(kuzzle);
request = new Request(data);
});
describe('#constructor', () => {
it('should inherit the base constructor', () => {
should(collectionController).instanceOf(BaseController);
});
});
describe('#updateMapping', () => {
it('should throw a BadRequestError if the body is missing', () => {
return should(() => {
collectionController.updateMapping(request);
}).throw(BadRequestError);
});
it('should activate a hook on a mapping update call and add the collection to the cache', () => {
request.input.body = {foo: 'bar'};
return collectionController.updateMapping(request)
.then(response => {
should(kuzzle.services.list.storageEngine.updateMapping).be.calledOnce();
should(kuzzle.services.list.storageEngine.updateMapping).be.calledWith(request);
should(kuzzle.indexCache.add).be.calledOnce();
should(kuzzle.indexCache.add).be.calledWith(request.input.resource.index, request.input.resource.collection);
should(response).be.instanceof(Object);
should(response).match(foo);
});
});
});
describe('#getMapping', () => {
it('should fulfill with a response object', () => {
return collectionController.getMapping(request)
.then(response => {
should(kuzzle.services.list.storageEngine.getMapping).be.calledOnce();
should(kuzzle.services.list.storageEngine.getMapping).be.calledWith(request);
should(response).be.instanceof(Object);
should(response).match(foo);
});
});
});
describe('#truncate', () => {
it('should trigger the proper methods and return a valid response', () => {
return collectionController.truncate(request)
.then(response => {
const truncate = kuzzle.services.list.storageEngine.truncateCollection;
should(truncate).be.calledOnce();
should(truncate).be.calledWith(request);
should(response).be.instanceof(Object);
should(response).match({ acknowledged: true });
});
});
});
describe('#getSpecifications', () => {
it('should call internalEngine with the right id', () => {
kuzzle.internalEngine.get.resolves({_source: {foo: 'bar'}});
kuzzle.indexCache.exists.resolves(true);
return collectionController.getSpecifications(request)
.then(response => {
should(kuzzle.internalEngine.get).be.calledOnce();
should(kuzzle.internalEngine.get).be.calledWithMatch(
'validations',
`${index}#${collection}`);
should(response).match(foo);
});
});
it('should return a dedicated error if the index does not exist', () => {
kuzzle.indexCache.exists.resolves(false);
return should(collectionController.getSpecifications(request))
.be.rejectedWith(
PreconditionError,
{message: `The index ${index} does not exist.`});
});
it('should return a dedicated error if the collection does not exist', () => {
kuzzle.indexCache.exists.onFirstCall().resolves(true);
kuzzle.indexCache.exists.onSecondCall().resolves(false);
return should(collectionController.getSpecifications(request))
.be.rejectedWith(PreconditionError, {message: `The collection ${collection} does not exist.`});
});
it('should give a meaningful message if there is no specifications', () => {
kuzzle.indexCache.exists.resolves(true);
kuzzle.internalEngine.get.rejects(new NotFoundError('not found'));
return should(collectionController.getSpecifications(request))
.be.rejectedWith(
NotFoundError,
{message: `No specifications defined for index ${index} and collection ${collection}`});
});
});
describe('#searchSpecifications', () => {
it('should throw if the page size exceeds server limitations', () => {
kuzzle.config.limits.documentsFetchCount = 1;
request.input.args.from = 0;
request.input.args.size = 20;
request.input.action = 'searchSpecifications';
should(() => collectionController.searchSpecifications(request)).throw(
SizeLimitError,
{ message: 'Search page size exceeds server configured documents limit ( 1 ).' });
});
it('should call internalEngine with the right data', () => {
kuzzle.internalEngine.search.resolves({
hits: [{_id: 'bar'}],
scrollId: 'foobar',
total: 123
});
request = new Request({
body: {
query: {
match_all: {}
}
},
from: 0,
size: 20,
scroll: '15s'
});
return collectionController.searchSpecifications(request)
.then(response => {
should(kuzzle.internalEngine.search).be.calledOnce();
should(kuzzle.internalEngine.search).be.calledWithMatch('validations', request.input.body.query, {
from: request.input.args.from,
size: request.input.args.size,
scroll: request.input.args.scroll
});
should(response).match({total: 123, scrollId: 'foobar', hits: [{_id: 'bar'}]});
});
});
});
describe('#scrollSpecifications', () => {
it('should throw if no scrollId is provided', () => {
should(() => collectionController.scrollSpecifications(new Request({controller: 'collection', action: 'scrollSpecifications'})))
.throw(BadRequestError, {message: 'The request must specify a scrollId.'});
});
it('should call internalEngine with the right data', () => {
kuzzle.internalEngine.scroll.resolves({
hits: [{_id: 'bar'}],
scrollId: 'foobar',
total: 123
});
request = new Request({scrollId: 'foobar'});
return collectionController.scrollSpecifications(request)
.then(response => {
should(kuzzle.internalEngine.scroll).be.calledOnce();
should(kuzzle.internalEngine.scroll).be.calledWithMatch('validations', 'foobar', undefined);
should(response).match({total: 123, scrollId: 'foobar', hits: [{_id: 'bar'}]});
});
});
it('should handle the optional scroll argument', () => {
kuzzle.internalEngine.scroll.resolves({
hits: [{_id: 'bar'}],
scrollId: 'foobar',
total: 123
});
request = new Request({scrollId: 'foobar', scroll: 'qux'});
return collectionController.scrollSpecifications(request)
.then(response => {
should(kuzzle.internalEngine.scroll).be.calledOnce();
should(kuzzle.internalEngine.scroll).be.calledWithMatch('validations', 'foobar', 'qux');
should(response).match({total: 123, scrollId: 'foobar', hits: [{_id: 'bar'}]});
});
});
});
describe('#updateSpecifications', () => {
it('should create or replace specifications', () => {
request.input.resource.index = 'myindex';
request.input.resource.collection = 'mycollection';
request.input.body = {
myindex: {
mycollection: {
strict: true,
fields: {
myField: {
mandatory: true,
type: 'integer',
defaultValue: 42
}
}
}
}
};
kuzzle.validation.isValidSpecification.resolves({isValid: true});
kuzzle.validation.curateSpecification.resolves();
return collectionController.updateSpecifications(request)
.then(response => {
should(kuzzle.internalEngine.refresh).be.calledOnce();
should(kuzzle.validation.curateSpecification).be.called();
should(kuzzle.internalEngine.createOrReplace).be.calledOnce();
should(kuzzle.internalEngine.createOrReplace).be.calledWithMatch('validations', 'myindex#mycollection');
should(response).match(request.input.body);
});
});
it('should rejects and do not create or replace specifications if the specs are wrong', () => {
request.input.resource.index = 'myindex';
request.input.resource.collection = 'mycollection';
request.input.body = {
myindex: {
mycollection: {
strict: true,
fields: {
myField: {
mandatory: true,
type: 'bad bad',
defaultValue: 42
}
}
}
}
};
kuzzle.validation.isValidSpecification.resolves({
isValid: false,
errors: ['bad bad is a bad type !']
});
return collectionController.updateSpecifications(request)
.catch(error => {
should(kuzzle.internalEngine.refresh).not.be.called();
should(kuzzle.validation.curateSpecification).not.be.called();
should(kuzzle.internalEngine.createOrReplace).not.be.called();
should(error).be.an.instanceOf(BadRequestError);
should(error.message).be.exactly('Some errors with provided specifications.');
should(error.details).match([ 'bad bad is a bad type !' ]);
});
});
it('should create or replace specifications when requested with deprecated way', () => {
request.input.resource.index = undefined;
request.input.resource.collection = undefined;
index = 'myindex';
collection = 'mycollection';
request.input.body = {
myindex: {
mycollection: {
strict: true,
fields: {
myField: {
mandatory: true,
type: 'integer',
defaultValue: 42
}
}
}
}
};
kuzzle.validation.isValidSpecification.resolves({isValid: true});
kuzzle.validation.curateSpecification.resolves();
return collectionController.updateSpecifications(request)
.then(response => {
should(kuzzle.internalEngine.refresh).be.calledOnce();
should(kuzzle.validation.curateSpecification).be.called();
should(kuzzle.internalEngine.createOrReplace)
.be.calledOnce()
.be.calledWithMatch('validations', `${index}#${collection}`);
should(response).match(request.input.body);
});
});
it('should rejects and do not create or replace specifications if the specs are wrong when requested with deprecated way', () => {
request.input.resource.index = undefined;
request.input.resource.collection = undefined;
index = 'myindex';
collection = 'mycollection';
request.input.body = {
myindex: {
mycollection: {
strict: true,
fields: {
myField: {
mandatory: true,
type: 'bad bad',
defaultValue: 42
}
}
}
}
};
kuzzle.validation.isValidSpecification.resolves({
isValid: false,
errors: ['bad bad is a bad type !']
});
return collectionController.updateSpecifications(request)
.catch(error => {
should(kuzzle.internalEngine.refresh).not.be.called();
should(kuzzle.validation.curateSpecification).not.be.called();
should(kuzzle.internalEngine.createOrReplace).not.be.called();
should(error).be.an.instanceOf(BadRequestError);
should(error.message).be.exactly('Some errors with provided specifications.');
should(error.details).match([ 'bad bad is a bad type !' ]);
});
});
});
describe('#validateSpecifications', () => {
it('should call the right functions and respond with the right response', () => {
request.input.resource.index = 'myindex';
request.input.resource.collection = 'mycollection';
request.input.body = {
strict: true,
fields: {
myField: {
mandatory: true,
type: 'integer',
defaultValue: 42
}
}
};
const specifications = [{
_id: 'indexcollection',
_source: {
validation: 'validation',
index: 'index',
collection: 'collection'
}
}];
const createSpecificationList = sinon.stub().resolves(specifications);
const validateSpecificationList = sinon.stub().resolves({valid: true});
CollectionController.__set__({
createSpecificationList,
validateSpecificationList
});
return collectionController.validateSpecifications(request)
.then(response => {
should(response).match({valid: true});
should(validateSpecificationList)
.be.calledOnce()
.be.calledWith(kuzzle.validation, specifications);
});
});
it('should call the right functions and respond with the right response if there is an error', () => {
const errorResponse = {
valid: false,
details: ['bad bad is a bad type'],
message: 'Some error message'
};
request.input.resource.index = 'myindex';
request.input.resource.collection = 'mycollection';
request.input.body = {
strict: true,
fields: {
myField: {
mandatory: true,
type: 'bad bad',
defaultValue: 42
}
}
};
const specifications = [{
_id: 'indexcollection',
_source: {
validation: 'validation',
index: 'index',
collection: 'collection'
}
}];
const createSpecificationList = sinon.stub().resolves(specifications);
const validateSpecificationList = sinon.stub().resolves(errorResponse);
CollectionController.__set__({
createSpecificationList,
validateSpecificationList
});
return collectionController.validateSpecifications(request)
.then(response => {
should(response).match(errorResponse);
should(validateSpecificationList).be.calledOnce();
});
});
it('should call the right functions and respond with the right response when requested with deprecated way', () => {
request.input.resource.index = undefined;
request.input.resource.collection = undefined;
request.input.body = {
myindex: {
mycollection: {
strict: true,
fields: {
myField: {
mandatory: true,
type: 'integer',
defaultValue: 42
}
}
}
}
};
const specifications = [{
_id: 'indexcollection',
_source: {
validation: 'validation',
index: 'index',
collection: 'collection'
}
}];
const createSpecificationList = sinon.stub().resolves(specifications);
const validateSpecificationList = sinon.stub().resolves({valid: true});
CollectionController.__set__({
createSpecificationList,
validateSpecificationList
});
return collectionController.validateSpecifications(request)
.then(response => {
should(response).match({valid: true});
should(validateSpecificationList)
.be.calledOnce()
.be.calledWith(kuzzle.validation, specifications);
});
});
it('should call the right functions and respond with the right response if there is an error when requested with deprecated way', () => {
request.input.resource.index = undefined;
request.input.resource.collection = undefined;
const errorResponse = {
valid: false,
details: ['bad bad is a bad type'],
message: 'Some error message'
};
request.input.body = {
myindex: {
mycollection: {
strict: true,
fields: {
myField: {
mandatory: true,
type: 'bad bad',
defaultValue: 42
}
}
}
}
};
const specifications = [{
_id: 'indexcollection',
_source: {
validation: 'validation',
index: 'index',
collection: 'collection'
}
}];
const createSpecificationList = sinon.stub().resolves(specifications);
const validateSpecificationList = sinon.stub().resolves(errorResponse);
CollectionController.__set__({
createSpecificationList,
validateSpecificationList
});
return collectionController.validateSpecifications(request)
.then(response => {
should(response).match(errorResponse);
should(validateSpecificationList).be.calledOnce();
});
});
});
describe('#deleteSpecifications', () => {
it('should call the right functions and respond with the right response if the validation specification exists', () => {
kuzzle.internalEngine.delete.resolves();
kuzzle.validation.specification = {};
kuzzle.validation.specification[index] = {};
kuzzle.validation.specification[index][collection] = {};
return collectionController.deleteSpecifications(request)
.then(response => {
should(kuzzle.internalEngine.delete).be.calledOnce();
should(response).match({acknowledged: true});
});
});
it('should resolves if there is no specification set', () => {
kuzzle.validation.specification = {};
return collectionController.deleteSpecifications(request)
.then(response => {
should(kuzzle.internalEngine.delete).not.be.called();
should(response).match({acknowledged: true});
});
});
});
describe('#list', () => {
beforeEach(() => {
kuzzle.services.list.storageEngine.listCollections.resolves({collections: {stored: ['foo']}});
kuzzle.hotelClerk.getRealtimeCollections.returns(['foo', 'bar']);
});
it('should resolve to a full collections list', () => {
request = new Request({index: 'index'});
return collectionController.list(request)
.then(response => {
should(kuzzle.hotelClerk.getRealtimeCollections).be.calledOnce();
should(kuzzle.services.list.storageEngine.listCollections).be.calledOnce();
should(response).be.instanceof(Object);
should(response.type).be.exactly('all');
should(response.collections).not.be.undefined().and.be.an.Array();
should(response.collections).deepEqual([{name: 'bar', type: 'realtime'}, {name: 'foo', type: 'realtime'}, {name: 'foo', type: 'stored'}]);
});
});
it('should reject the request if an invalid "type" argument is provided', () => {
request = new Request({index: 'index', type: 'foo'});
should(() => collectionController.list(request)).throw(
BadRequestError,
{ message: 'Must specify a valid type argument; Expected: \'all\', \'stored\' or \'realtime\'; Received: foo.'});
});
it('should only return stored collections with type = stored', () => {
request = new Request({index: 'index', type: 'stored'});
return collectionController.list(request)
.then(response => {
should(response).be.instanceof(Object);
should(response.type).be.exactly('stored');
should(kuzzle.hotelClerk.getRealtimeCollections).not.be.called();
should(kuzzle.services.list.storageEngine.listCollections).be.called();
});
});
it('should only return realtime collections with type = realtime', () => {
request = new Request({index: 'index', type: 'realtime'});
return collectionController.list(request)
.then(response => {
should(response).be.instanceof(Object);
should(response.type).be.exactly('realtime');
should(kuzzle.hotelClerk.getRealtimeCollections).be.called();
should(kuzzle.services.list.storageEngine.listCollections).not.be.called();
});
});
it('should return a portion of the collection list if from and size are specified', () => {
request = new Request({index: 'index', type: 'all', from: 2, size: 3});
kuzzle.services.list.storageEngine.listCollections.resolves({collections: {stored: ['astored', 'bstored', 'cstored', 'dstored', 'estored']}});
kuzzle.hotelClerk.getRealtimeCollections.returns(['arealtime', 'brealtime', 'crealtime', 'drealtime', 'erealtime']);
return collectionController.list(request)
.then(response => {
should(response).be.instanceof(Object);
should(response.collections).be.deepEqual([
{name: 'brealtime', type: 'realtime'},
{name: 'bstored', type: 'stored'},
{name: 'crealtime', type: 'realtime'}
]);
should(response.type).be.exactly('all');
should(kuzzle.hotelClerk.getRealtimeCollections).be.called();
should(kuzzle.services.list.storageEngine.listCollections).be.called();
});
});
it('should return a portion of the collection list if from is specified', () => {
request = new Request({index: 'index', type: 'all', from: 8});
kuzzle.services.list.storageEngine.listCollections.resolves({collections: {stored: ['astored', 'bstored', 'cstored', 'dstored', 'estored']}});
kuzzle.hotelClerk.getRealtimeCollections.returns(['arealtime', 'brealtime', 'crealtime', 'drealtime', 'erealtime']);
return collectionController.list(request)
.then(response => {
should(response.type).be.exactly('all');
should(response.collections).be.deepEqual([
{name: 'erealtime', type: 'realtime'},
{name: 'estored', type: 'stored'}
]);
should(response).be.instanceof(Object);
should(kuzzle.hotelClerk.getRealtimeCollections).be.called();
should(kuzzle.services.list.storageEngine.listCollections).be.called();
});
});
it('should return a portion of the collection list if size is specified', () => {
request = new Request({index: 'index', type: 'all', size: 2});
kuzzle.services.list.storageEngine.listCollections.resolves({
collections: {stored: ['astored', 'bstored', 'cstored', 'dstored', 'estored']}
});
kuzzle.hotelClerk.getRealtimeCollections.returns(['arealtime', 'brealtime', 'crealtime', 'drealtime', 'erealtime']);
return collectionController.list(request)
.then(response => {
should(response).be.instanceof(Object);
should(response.collections).be.deepEqual([
{name: 'arealtime', type: 'realtime'},
{name: 'astored', type: 'stored'}
]);
should(response.type).be.exactly('all');
should(kuzzle.hotelClerk.getRealtimeCollections).be.called();
should(kuzzle.services.list.storageEngine.listCollections).be.called();
});
});
it('should reject an error if getting stored collections fails', () => {
kuzzle.services.list.storageEngine.listCollections.rejects(new Error('foobar'));
request = new Request({index: 'index', type: 'stored'});
return should(collectionController.list(request)).be.rejected();
});
it('should reject an error if getting all collections fails', () => {
kuzzle.services.list.storageEngine.listCollections.rejects(new Error('foobar'));
request = new Request({index: 'index', type: 'all'});
return should(collectionController.list(request)).be.rejected();
});
});
describe('#exists', () => {
it('should call the storageEngine', () => {
kuzzle.services.list.storageEngine.collectionExists.resolves(foo);
return collectionController.exists(request)
.then(response => {
should(response).match(foo);
should(kuzzle.services.list.storageEngine.collectionExists).be.calledOnce();
});
});
});
describe('#create', () => {
it('should resolve to a valid response', () => {
request.input.resource.index = '%test';
request.input.resource.collection = 'test-collection';
return collectionController.create(request)
.then(response => {
should(engine.createCollection).be.calledOnce();
should(engine.createCollection).be.calledWith(request);
should(kuzzle.indexCache.add).be.calledOnce();
should(kuzzle.indexCache.add).be.calledWith(request.input.resource.index, request.input.resource.collection);
sinon.assert.callOrder(
engine.createCollection,
kuzzle.indexCache.add
);
should(response).be.instanceof(Object);
should(response).match(foo);
});
});
});
});