@strapi/plugin-documentation 4.10.0-beta.1 → 4.10.1-experimental.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,761 @@
1
+ 'use strict';
2
+
3
+ const _ = require('lodash/fp');
4
+ const buildComponentSchema = require('../helpers/build-component-schema');
5
+
6
+ const strapi = {
7
+ plugins: {
8
+ 'users-permissions': {
9
+ contentTypes: {
10
+ role: {
11
+ attributes: {
12
+ test: {
13
+ type: 'string',
14
+ },
15
+ },
16
+ },
17
+ },
18
+ routes: {
19
+ 'content-api': {
20
+ routes: [],
21
+ },
22
+ },
23
+ },
24
+ },
25
+ api: {
26
+ restaurant: {
27
+ contentTypes: {
28
+ restaurant: {
29
+ attributes: {
30
+ test: {
31
+ type: 'string',
32
+ },
33
+ },
34
+ },
35
+ },
36
+ routes: {
37
+ restaurant: { routes: [] },
38
+ },
39
+ },
40
+ },
41
+ contentType: () => ({ info: {}, attributes: { test: { type: 'string' } } }),
42
+ };
43
+
44
+ describe('Documentation plugin | Build component schema', () => {
45
+ beforeEach(() => {
46
+ // Reset the mocked strapi instance
47
+ global.strapi = _.cloneDeep(strapi);
48
+ });
49
+
50
+ afterAll(() => {
51
+ // Teardown the mocked strapi instance
52
+ global.strapi = {};
53
+ });
54
+
55
+ it('builds the Response schema', () => {
56
+ const apiMocks = [
57
+ {
58
+ name: 'users-permissions',
59
+ getter: 'plugin',
60
+ ctNames: ['role'],
61
+ },
62
+ { name: 'restaurant', getter: 'api', ctNames: ['restaurant'] },
63
+ ];
64
+
65
+ let schemas = {};
66
+ for (const mock of apiMocks) {
67
+ schemas = {
68
+ ...schemas,
69
+ ...buildComponentSchema(mock),
70
+ };
71
+ }
72
+
73
+ const expectedSchemas = {
74
+ UsersPermissionsRole: {
75
+ type: 'object',
76
+ properties: {
77
+ test: { type: 'string' },
78
+ },
79
+ },
80
+ UsersPermissionsRoleResponseDataObject: {
81
+ type: 'object',
82
+ properties: {
83
+ id: {
84
+ type: 'number',
85
+ },
86
+ attributes: {
87
+ $ref: '#/components/schemas/UsersPermissionsRole',
88
+ },
89
+ },
90
+ },
91
+ UsersPermissionsRoleResponse: {
92
+ type: 'object',
93
+ properties: {
94
+ data: {
95
+ $ref: '#/components/schemas/UsersPermissionsRoleResponseDataObject',
96
+ },
97
+ meta: {
98
+ type: 'object',
99
+ },
100
+ },
101
+ },
102
+ Restaurant: {
103
+ type: 'object',
104
+ properties: {
105
+ test: { type: 'string' },
106
+ },
107
+ },
108
+ RestaurantResponseDataObject: {
109
+ type: 'object',
110
+ properties: {
111
+ id: {
112
+ type: 'number',
113
+ },
114
+ attributes: {
115
+ $ref: '#/components/schemas/Restaurant',
116
+ },
117
+ },
118
+ },
119
+ RestaurantResponse: {
120
+ type: 'object',
121
+ properties: {
122
+ data: {
123
+ $ref: '#/components/schemas/RestaurantResponseDataObject',
124
+ },
125
+ meta: {
126
+ type: 'object',
127
+ },
128
+ },
129
+ },
130
+ };
131
+
132
+ expect(schemas).toStrictEqual(expectedSchemas);
133
+ });
134
+
135
+ it('builds the ResponseList schema', () => {
136
+ global.strapi.plugins['users-permissions'].routes['content-api'].routes = [
137
+ { method: 'GET', path: '/test', handler: 'test.find' },
138
+ ];
139
+ global.strapi.api.restaurant.routes.restaurant.routes = [
140
+ { method: 'GET', path: '/test', handler: 'test.find' },
141
+ ];
142
+
143
+ const apiMocks = [
144
+ {
145
+ name: 'users-permissions',
146
+ getter: 'plugin',
147
+ ctNames: ['role'],
148
+ },
149
+ { name: 'restaurant', getter: 'api', ctNames: ['restaurant'] },
150
+ ];
151
+
152
+ let schemas = {};
153
+ for (const mock of apiMocks) {
154
+ schemas = {
155
+ ...schemas,
156
+ ...buildComponentSchema(mock),
157
+ };
158
+ }
159
+
160
+ const expectedSchemas = {
161
+ UsersPermissionsRole: {
162
+ type: 'object',
163
+ properties: {
164
+ test: { type: 'string' },
165
+ },
166
+ },
167
+ UsersPermissionsRoleListResponseDataItem: {
168
+ type: 'object',
169
+ properties: {
170
+ id: {
171
+ type: 'number',
172
+ },
173
+ attributes: {
174
+ $ref: '#/components/schemas/UsersPermissionsRole',
175
+ },
176
+ },
177
+ },
178
+ UsersPermissionsRoleListResponse: {
179
+ type: 'object',
180
+ properties: {
181
+ data: {
182
+ type: 'array',
183
+ items: {
184
+ $ref: '#/components/schemas/UsersPermissionsRoleListResponseDataItem',
185
+ },
186
+ },
187
+ meta: {
188
+ type: 'object',
189
+ properties: {
190
+ pagination: {
191
+ type: 'object',
192
+ properties: {
193
+ page: {
194
+ type: 'integer',
195
+ },
196
+ pageSize: {
197
+ type: 'integer',
198
+ minimum: 25,
199
+ },
200
+ pageCount: {
201
+ type: 'integer',
202
+ maximum: 1,
203
+ },
204
+ total: {
205
+ type: 'integer',
206
+ },
207
+ },
208
+ },
209
+ },
210
+ },
211
+ },
212
+ },
213
+ UsersPermissionsRoleResponseDataObject: {
214
+ type: 'object',
215
+ properties: {
216
+ id: {
217
+ type: 'number',
218
+ },
219
+ attributes: {
220
+ $ref: '#/components/schemas/UsersPermissionsRole',
221
+ },
222
+ },
223
+ },
224
+ UsersPermissionsRoleResponse: {
225
+ type: 'object',
226
+ properties: {
227
+ data: {
228
+ $ref: '#/components/schemas/UsersPermissionsRoleResponseDataObject',
229
+ },
230
+ meta: {
231
+ type: 'object',
232
+ },
233
+ },
234
+ },
235
+ RestaurantListResponseDataItem: {
236
+ type: 'object',
237
+ properties: {
238
+ id: {
239
+ type: 'number',
240
+ },
241
+ attributes: {
242
+ $ref: '#/components/schemas/Restaurant',
243
+ },
244
+ },
245
+ },
246
+ RestaurantListResponse: {
247
+ type: 'object',
248
+ properties: {
249
+ data: {
250
+ type: 'array',
251
+ items: {
252
+ $ref: '#/components/schemas/RestaurantListResponseDataItem',
253
+ },
254
+ },
255
+ meta: {
256
+ type: 'object',
257
+ properties: {
258
+ pagination: {
259
+ type: 'object',
260
+ properties: {
261
+ page: {
262
+ type: 'integer',
263
+ },
264
+ pageSize: {
265
+ type: 'integer',
266
+ minimum: 25,
267
+ },
268
+ pageCount: {
269
+ type: 'integer',
270
+ maximum: 1,
271
+ },
272
+ total: {
273
+ type: 'integer',
274
+ },
275
+ },
276
+ },
277
+ },
278
+ },
279
+ },
280
+ },
281
+ RestaurantResponseDataObject: {
282
+ type: 'object',
283
+ properties: {
284
+ id: {
285
+ type: 'number',
286
+ },
287
+ attributes: {
288
+ $ref: '#/components/schemas/Restaurant',
289
+ },
290
+ },
291
+ },
292
+ RestaurantResponse: {
293
+ type: 'object',
294
+ properties: {
295
+ data: {
296
+ $ref: '#/components/schemas/RestaurantResponseDataObject',
297
+ },
298
+ meta: {
299
+ type: 'object',
300
+ },
301
+ },
302
+ },
303
+ Restaurant: {
304
+ type: 'object',
305
+ properties: {
306
+ test: { type: 'string' },
307
+ },
308
+ },
309
+ };
310
+
311
+ expect(schemas).toStrictEqual(expectedSchemas);
312
+ });
313
+
314
+ it('builds the Request schema', () => {
315
+ global.strapi.plugins['users-permissions'].routes['content-api'].routes = [
316
+ { method: 'POST', path: '/test', handler: 'test.create' },
317
+ ];
318
+ global.strapi.api.restaurant.routes.restaurant.routes = [
319
+ { method: 'POST', path: '/test', handler: 'test.create' },
320
+ ];
321
+
322
+ const apiMocks = [
323
+ {
324
+ name: 'users-permissions',
325
+ getter: 'plugin',
326
+ ctNames: ['role'],
327
+ },
328
+ { name: 'restaurant', getter: 'api', ctNames: ['restaurant'] },
329
+ ];
330
+
331
+ let schemas = {};
332
+ for (const mock of apiMocks) {
333
+ schemas = {
334
+ ...schemas,
335
+ ...buildComponentSchema(mock),
336
+ };
337
+ }
338
+ // Just get the request objects
339
+ const requestObjectsSchemas = Object.entries(schemas).reduce((acc, curr) => {
340
+ const [key, val] = curr;
341
+ if (key.endsWith('Request')) {
342
+ acc[key] = val;
343
+ }
344
+
345
+ return acc;
346
+ }, {});
347
+
348
+ const expectedSchemas = {
349
+ UsersPermissionsRoleRequest: {
350
+ type: 'object',
351
+ required: ['data'],
352
+ properties: {
353
+ data: {
354
+ type: 'object',
355
+ properties: {
356
+ test: {
357
+ type: 'string',
358
+ },
359
+ },
360
+ },
361
+ },
362
+ },
363
+ RestaurantRequest: {
364
+ type: 'object',
365
+ required: ['data'],
366
+ properties: {
367
+ data: {
368
+ type: 'object',
369
+ properties: {
370
+ test: {
371
+ type: 'string',
372
+ },
373
+ },
374
+ },
375
+ },
376
+ },
377
+ };
378
+
379
+ expect(requestObjectsSchemas).toStrictEqual(expectedSchemas);
380
+ });
381
+
382
+ it('builds the LocalizationResponse schema', () => {
383
+ global.strapi.plugins['users-permissions'].routes['content-api'].routes = [
384
+ { method: 'GET', path: '/localizations', handler: 'test' },
385
+ { method: 'GET', path: '/test', handler: 'test.find' },
386
+ ];
387
+ global.strapi.api.restaurant.routes.restaurant.routes = [
388
+ { method: 'GET', path: '/localizations', handler: 'test' },
389
+ { method: 'GET', path: '/test', handler: 'test.find' },
390
+ ];
391
+
392
+ const apiMocks = [
393
+ {
394
+ name: 'users-permissions',
395
+ getter: 'plugin',
396
+ ctNames: ['role'],
397
+ },
398
+ { name: 'restaurant', getter: 'api', ctNames: ['restaurant'] },
399
+ ];
400
+
401
+ let schemas = {};
402
+ for (const mock of apiMocks) {
403
+ schemas = {
404
+ ...schemas,
405
+ ...buildComponentSchema(mock),
406
+ };
407
+ }
408
+
409
+ const expectedSchemas = {
410
+ UsersPermissionsRole: {
411
+ type: 'object',
412
+ properties: {
413
+ test: { type: 'string' },
414
+ },
415
+ },
416
+ UsersPermissionsRoleListResponseDataItem: {
417
+ type: 'object',
418
+ properties: {
419
+ id: {
420
+ type: 'number',
421
+ },
422
+ attributes: {
423
+ $ref: '#/components/schemas/UsersPermissionsRole',
424
+ },
425
+ },
426
+ },
427
+ UsersPermissionsRoleListResponseDataItemLocalized: {
428
+ type: 'object',
429
+ properties: {
430
+ id: {
431
+ type: 'number',
432
+ },
433
+ attributes: {
434
+ $ref: '#/components/schemas/UsersPermissionsRole',
435
+ },
436
+ },
437
+ },
438
+ UsersPermissionsRoleListResponse: {
439
+ type: 'object',
440
+ properties: {
441
+ data: {
442
+ type: 'array',
443
+ items: {
444
+ $ref: '#/components/schemas/UsersPermissionsRoleListResponseDataItem',
445
+ },
446
+ },
447
+ meta: {
448
+ type: 'object',
449
+ properties: {
450
+ pagination: {
451
+ type: 'object',
452
+ properties: {
453
+ page: {
454
+ type: 'integer',
455
+ },
456
+ pageSize: {
457
+ type: 'integer',
458
+ minimum: 25,
459
+ },
460
+ pageCount: {
461
+ type: 'integer',
462
+ maximum: 1,
463
+ },
464
+ total: {
465
+ type: 'integer',
466
+ },
467
+ },
468
+ },
469
+ },
470
+ },
471
+ },
472
+ },
473
+ UsersPermissionsRoleLocalizationListResponse: {
474
+ type: 'object',
475
+ properties: {
476
+ data: {
477
+ type: 'array',
478
+ items: {
479
+ $ref: '#/components/schemas/UsersPermissionsRoleListResponseDataItemLocalized',
480
+ },
481
+ },
482
+ meta: {
483
+ type: 'object',
484
+ properties: {
485
+ pagination: {
486
+ type: 'object',
487
+ properties: {
488
+ page: {
489
+ type: 'integer',
490
+ },
491
+ pageSize: {
492
+ type: 'integer',
493
+ minimum: 25,
494
+ },
495
+ pageCount: {
496
+ type: 'integer',
497
+ maximum: 1,
498
+ },
499
+ total: {
500
+ type: 'integer',
501
+ },
502
+ },
503
+ },
504
+ },
505
+ },
506
+ },
507
+ },
508
+ UsersPermissionsRoleResponseDataObject: {
509
+ type: 'object',
510
+ properties: {
511
+ id: {
512
+ type: 'number',
513
+ },
514
+ attributes: {
515
+ $ref: '#/components/schemas/UsersPermissionsRole',
516
+ },
517
+ },
518
+ },
519
+ UsersPermissionsRoleResponseDataObjectLocalized: {
520
+ type: 'object',
521
+ properties: {
522
+ id: {
523
+ type: 'number',
524
+ },
525
+ attributes: {
526
+ $ref: '#/components/schemas/UsersPermissionsRole',
527
+ },
528
+ },
529
+ },
530
+ UsersPermissionsRoleResponse: {
531
+ type: 'object',
532
+ properties: {
533
+ data: {
534
+ $ref: '#/components/schemas/UsersPermissionsRoleResponseDataObject',
535
+ },
536
+ meta: {
537
+ type: 'object',
538
+ },
539
+ },
540
+ },
541
+ UsersPermissionsRoleLocalizationResponse: {
542
+ type: 'object',
543
+ properties: {
544
+ data: {
545
+ $ref: '#/components/schemas/UsersPermissionsRoleResponseDataObjectLocalized',
546
+ },
547
+ meta: {
548
+ type: 'object',
549
+ },
550
+ },
551
+ },
552
+ RestaurantListResponseDataItem: {
553
+ type: 'object',
554
+ properties: {
555
+ id: {
556
+ type: 'number',
557
+ },
558
+ attributes: {
559
+ $ref: '#/components/schemas/Restaurant',
560
+ },
561
+ },
562
+ },
563
+ RestaurantListResponseDataItemLocalized: {
564
+ type: 'object',
565
+ properties: {
566
+ id: {
567
+ type: 'number',
568
+ },
569
+ attributes: {
570
+ $ref: '#/components/schemas/Restaurant',
571
+ },
572
+ },
573
+ },
574
+ RestaurantListResponse: {
575
+ type: 'object',
576
+ properties: {
577
+ data: {
578
+ type: 'array',
579
+ items: {
580
+ $ref: '#/components/schemas/RestaurantListResponseDataItem',
581
+ },
582
+ },
583
+ meta: {
584
+ type: 'object',
585
+ properties: {
586
+ pagination: {
587
+ type: 'object',
588
+ properties: {
589
+ page: {
590
+ type: 'integer',
591
+ },
592
+ pageSize: {
593
+ type: 'integer',
594
+ minimum: 25,
595
+ },
596
+ pageCount: {
597
+ type: 'integer',
598
+ maximum: 1,
599
+ },
600
+ total: {
601
+ type: 'integer',
602
+ },
603
+ },
604
+ },
605
+ },
606
+ },
607
+ },
608
+ },
609
+ RestaurantLocalizationListResponse: {
610
+ type: 'object',
611
+ properties: {
612
+ data: {
613
+ type: 'array',
614
+ items: {
615
+ $ref: '#/components/schemas/RestaurantListResponseDataItemLocalized',
616
+ },
617
+ },
618
+ meta: {
619
+ type: 'object',
620
+ properties: {
621
+ pagination: {
622
+ type: 'object',
623
+ properties: {
624
+ page: {
625
+ type: 'integer',
626
+ },
627
+ pageSize: {
628
+ type: 'integer',
629
+ minimum: 25,
630
+ },
631
+ pageCount: {
632
+ type: 'integer',
633
+ maximum: 1,
634
+ },
635
+ total: {
636
+ type: 'integer',
637
+ },
638
+ },
639
+ },
640
+ },
641
+ },
642
+ },
643
+ },
644
+ RestaurantResponseDataObject: {
645
+ type: 'object',
646
+ properties: {
647
+ id: {
648
+ type: 'number',
649
+ },
650
+ attributes: {
651
+ $ref: '#/components/schemas/Restaurant',
652
+ },
653
+ },
654
+ },
655
+ RestaurantResponseDataObjectLocalized: {
656
+ type: 'object',
657
+ properties: {
658
+ id: {
659
+ type: 'number',
660
+ },
661
+ attributes: {
662
+ $ref: '#/components/schemas/Restaurant',
663
+ },
664
+ },
665
+ },
666
+ RestaurantLocalizationResponse: {
667
+ type: 'object',
668
+ properties: {
669
+ data: {
670
+ $ref: '#/components/schemas/RestaurantResponseDataObjectLocalized',
671
+ },
672
+ meta: {
673
+ type: 'object',
674
+ },
675
+ },
676
+ },
677
+ RestaurantResponse: {
678
+ type: 'object',
679
+ properties: {
680
+ data: {
681
+ $ref: '#/components/schemas/RestaurantResponseDataObject',
682
+ },
683
+ meta: {
684
+ type: 'object',
685
+ },
686
+ },
687
+ },
688
+ Restaurant: {
689
+ type: 'object',
690
+ properties: {
691
+ test: { type: 'string' },
692
+ },
693
+ },
694
+ };
695
+
696
+ expect(schemas).toStrictEqual(expectedSchemas);
697
+ });
698
+
699
+ it('builds the LocalizationRequest schema', () => {
700
+ global.strapi.plugins['users-permissions'].routes['content-api'].routes = [
701
+ { method: 'POST', path: '/localizations', handler: 'test' },
702
+ ];
703
+ global.strapi.api.restaurant.routes.restaurant.routes = [
704
+ { method: 'POST', path: '/localizations', handler: 'test' },
705
+ ];
706
+
707
+ const apiMocks = [
708
+ {
709
+ name: 'users-permissions',
710
+ getter: 'plugin',
711
+ ctNames: ['role'],
712
+ },
713
+ { name: 'restaurant', getter: 'api', ctNames: ['restaurant'] },
714
+ ];
715
+
716
+ let schemas = {};
717
+ for (const mock of apiMocks) {
718
+ schemas = {
719
+ ...schemas,
720
+ ...buildComponentSchema(mock),
721
+ };
722
+ }
723
+
724
+ const schemaNames = Object.keys(schemas);
725
+ const pluginListResponseLocalizationRequest = schemas.UsersPermissionsRoleLocalizationRequest;
726
+ const apiListResponseLocalizationRequest = schemas.RestaurantLocalizationRequest;
727
+
728
+ const expectedShape = {
729
+ type: 'object',
730
+ required: ['locale'],
731
+ properties: { test: { type: 'string' } },
732
+ };
733
+
734
+ expect(schemaNames.includes('UsersPermissionsRoleLocalizationRequest')).toBe(true);
735
+ expect(schemaNames.includes('RestaurantLocalizationRequest')).toBe(true);
736
+ expect(pluginListResponseLocalizationRequest).toStrictEqual(expectedShape);
737
+ expect(apiListResponseLocalizationRequest).toStrictEqual(expectedShape);
738
+ });
739
+
740
+ it('creates the correct name given multiple content types', () => {
741
+ const apiMock = {
742
+ name: 'users-permissions',
743
+ getter: 'plugin',
744
+ ctNames: ['permission', 'role', 'user'],
745
+ };
746
+
747
+ const schemas = buildComponentSchema(apiMock);
748
+ const schemaNames = Object.keys(schemas);
749
+ expect(schemaNames).toStrictEqual([
750
+ 'UsersPermissionsPermission',
751
+ 'UsersPermissionsPermissionResponseDataObject',
752
+ 'UsersPermissionsPermissionResponse',
753
+ 'UsersPermissionsRole',
754
+ 'UsersPermissionsRoleResponseDataObject',
755
+ 'UsersPermissionsRoleResponse',
756
+ 'UsersPermissionsUser',
757
+ 'UsersPermissionsUserResponseDataObject',
758
+ 'UsersPermissionsUserResponse',
759
+ ]);
760
+ });
761
+ });