serverless-openapi-documenter 0.0.119 → 0.0.121

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.
@@ -1,980 +0,0 @@
1
- "use strict";
2
-
3
- const fs = require("fs").promises;
4
- const path = require("path");
5
- const expect = require("chai").expect;
6
-
7
- const serverlessMock = require("../helpers/serverless");
8
- const modelsDocument = require("../models/models/models.json");
9
- const DefinitionGenerator = require("../../src/definitionGenerator");
10
-
11
- describe("DefinitionGenerator", () => {
12
- let mockServerless;
13
- const logger = {
14
- verbose: (str) => {
15
- console.log(str);
16
- },
17
- warn: (str) => {
18
- console.log(str);
19
- },
20
- };
21
- const v4 = new RegExp(
22
- /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
23
- );
24
-
25
- beforeEach(function () {
26
- mockServerless = structuredClone(serverlessMock);
27
- Object.assign(mockServerless.service.custom.documentation, modelsDocument);
28
- });
29
-
30
- after(function () {
31
- delete require
32
- .cache[require.resolve(`${path.resolve("options")}/redocly.json`)];
33
- });
34
-
35
- describe("constructor", () => {
36
- it("should return a definitionGenerator", function () {
37
- const expected = new DefinitionGenerator(mockServerless, logger);
38
- expect(expected).to.be.an.instanceOf(DefinitionGenerator);
39
- });
40
-
41
- it("should default to version 3.0.0 of openAPI when openAPI version is not passed in", function () {
42
- const serverlessWithoutOpenAPIVersion = structuredClone(mockServerless);
43
- delete serverlessWithoutOpenAPIVersion.processedInput;
44
- let expected = new DefinitionGenerator(
45
- serverlessWithoutOpenAPIVersion,
46
- logger
47
- );
48
- expect(expected.version).to.be.equal("3.0.0");
49
-
50
- Object.assign(serverlessWithoutOpenAPIVersion, { processedInput: {} });
51
- expected = new DefinitionGenerator(serverlessWithoutOpenAPIVersion, {
52
- verbose: (str) => {
53
- console.log(str);
54
- },
55
- });
56
- expect(expected.version).to.be.equal("3.0.0");
57
-
58
- serverlessWithoutOpenAPIVersion.processedInput = {
59
- options: {},
60
- };
61
- expected = new DefinitionGenerator(
62
- serverlessWithoutOpenAPIVersion,
63
- logger
64
- );
65
- expect(expected.version).to.be.equal("3.0.0");
66
-
67
- serverlessWithoutOpenAPIVersion.processedInput.options = {
68
- test: "abc",
69
- };
70
-
71
- expected = new DefinitionGenerator(
72
- serverlessWithoutOpenAPIVersion,
73
- logger
74
- );
75
- expect(expected.version).to.be.equal("3.0.0");
76
-
77
- serverlessWithoutOpenAPIVersion.processedInput.options = {
78
- openApiVersion: null,
79
- };
80
-
81
- expected = new DefinitionGenerator(
82
- serverlessWithoutOpenAPIVersion,
83
- logger
84
- );
85
- expect(expected.version).to.be.equal("3.0.0");
86
-
87
- serverlessWithoutOpenAPIVersion.processedInput.options = {
88
- openApiVersion: undefined,
89
- };
90
-
91
- expected = new DefinitionGenerator(
92
- serverlessWithoutOpenAPIVersion,
93
- logger
94
- );
95
- expect(expected.version).to.be.equal("3.0.0");
96
-
97
- serverlessWithoutOpenAPIVersion.processedInput.options = {
98
- openapiVersion: undefined,
99
- };
100
-
101
- expected = new DefinitionGenerator(
102
- serverlessWithoutOpenAPIVersion,
103
- logger
104
- );
105
- expect(expected.version).to.be.equal("3.0.0");
106
- });
107
-
108
- it("should respect the version of openAPI when passed in", function () {
109
- const serverlessWithOpenAPIVersion = structuredClone(mockServerless);
110
- serverlessWithOpenAPIVersion.processedInput.options.openApiVersion =
111
- "3.0.2";
112
- let expected = new DefinitionGenerator(
113
- serverlessWithOpenAPIVersion,
114
- logger
115
- );
116
- expect(expected.version).to.be.equal("3.0.2");
117
-
118
- serverlessWithOpenAPIVersion.processedInput.options.openApiVersion =
119
- "3.0.1";
120
- expected = new DefinitionGenerator(serverlessWithOpenAPIVersion, logger);
121
- expect(expected.version).to.be.equal("3.0.1");
122
- });
123
-
124
- it(`correctly resolves external redocly rules`, async function () {
125
- await fs.mkdir(path.resolve("options")).catch((err) => {
126
- console.error(err);
127
- throw err;
128
- });
129
-
130
- await fs
131
- .copyFile(
132
- path.resolve("test/helpers/redocly.json"),
133
- path.resolve("options/redocly.json")
134
- )
135
- .catch((err) => {
136
- console.error(err);
137
- throw err;
138
- });
139
-
140
- const expected = new DefinitionGenerator(mockServerless, logger);
141
-
142
- expect(expected.REDOCLY_RULES).to.have.property(
143
- "operation-2xx-response",
144
- "warn"
145
- );
146
-
147
- await fs.rm(path.resolve("options/redocly.json")).catch((err) => {
148
- console.error(err);
149
- throw err;
150
- });
151
-
152
- await fs.rmdir(path.resolve("options")).catch((err) => {
153
- console.error(err);
154
- throw err;
155
- });
156
- });
157
- });
158
-
159
- describe("createInfo", () => {
160
- it("should create openAPI info object correctly", function () {
161
- const definitionGenerator = new DefinitionGenerator(
162
- mockServerless,
163
- logger
164
- );
165
- definitionGenerator.createInfo();
166
-
167
- expect(definitionGenerator.openAPI).to.be.an("object");
168
- expect(definitionGenerator.openAPI.info).to.be.an("object");
169
- // expect(definitionGenerator.openAPI.info).to.deep.equal(mockServerless.service.custom.documentation)
170
- });
171
-
172
- it("should use the service name when documentation title has not been supplied", function () {
173
- delete mockServerless.service.custom.documentation.title;
174
- const definitionGenerator = new DefinitionGenerator(
175
- mockServerless,
176
- logger
177
- );
178
- definitionGenerator.createInfo();
179
-
180
- expect(definitionGenerator.openAPI).to.be.an("object");
181
- expect(definitionGenerator.openAPI.info).to.be.an("object");
182
- expect(definitionGenerator.openAPI.info.title).to.be.equal(
183
- mockServerless.service.service
184
- );
185
- });
186
-
187
- it("should use the service name when documentation description has not been supplied", function () {
188
- delete mockServerless.service.custom.documentation.description;
189
- const definitionGenerator = new DefinitionGenerator(
190
- mockServerless,
191
- logger
192
- );
193
- definitionGenerator.createInfo();
194
-
195
- expect(definitionGenerator.openAPI).to.be.an("object");
196
- expect(definitionGenerator.openAPI.info).to.be.an("object");
197
- expect(definitionGenerator.openAPI.info.description).to.be.equal("");
198
- });
199
-
200
- it("should use an empty string when documentation description has not been supplied", function () {
201
- delete mockServerless.service.custom.documentation.description;
202
- const definitionGenerator = new DefinitionGenerator(
203
- mockServerless,
204
- logger
205
- );
206
- definitionGenerator.createInfo();
207
-
208
- expect(definitionGenerator.openAPI).to.be.an("object");
209
- expect(definitionGenerator.openAPI.info).to.be.an("object");
210
- expect(definitionGenerator.openAPI.info.description).to.be.equal("");
211
- });
212
-
213
- it("should generate a uuid for version when documentation version has not been supplied", function () {
214
- delete mockServerless.service.custom.documentation.version;
215
-
216
- const definitionGenerator = new DefinitionGenerator(
217
- mockServerless,
218
- logger
219
- );
220
- definitionGenerator.createInfo();
221
-
222
- expect(definitionGenerator.openAPI).to.be.an("object");
223
- expect(definitionGenerator.openAPI.info).to.be.an("object");
224
- expect(v4.test(definitionGenerator.openAPI.info.version)).to.be.true;
225
- });
226
-
227
- it("should assign a contact Object when a contact object is included", function () {
228
- mockServerless.service.custom.documentation.contact = {
229
- name: "John",
230
- url: "http://example.com",
231
- email: "john@example.com",
232
- };
233
- const definitionGenerator = new DefinitionGenerator(
234
- mockServerless,
235
- logger
236
- );
237
- definitionGenerator.createInfo();
238
-
239
- expect(definitionGenerator.openAPI).to.be.an("object");
240
- expect(definitionGenerator.openAPI.info).to.be.an("object");
241
- expect(definitionGenerator.openAPI.info).to.have.property("contact");
242
- expect(definitionGenerator.openAPI.info.contact).to.be.an("object");
243
- expect(definitionGenerator.openAPI.info.contact.name).to.be.an("string");
244
- });
245
-
246
- it("should only assign a contact url if one is provided", function () {
247
- mockServerless.service.custom.documentation.contact = {
248
- name: "John",
249
- email: "john@example.com",
250
- };
251
- const definitionGenerator = new DefinitionGenerator(
252
- mockServerless,
253
- logger
254
- );
255
- definitionGenerator.createInfo();
256
-
257
- expect(definitionGenerator.openAPI).to.be.an("object");
258
- expect(definitionGenerator.openAPI.info).to.be.an("object");
259
- expect(definitionGenerator.openAPI.info).to.have.property("contact");
260
- expect(definitionGenerator.openAPI.info.contact).to.be.an("object");
261
- expect(definitionGenerator.openAPI.info.contact.name).to.be.an("string");
262
- expect(definitionGenerator.openAPI.info.contact).to.not.have.property(
263
- "url"
264
- );
265
- });
266
-
267
- it("should assign a license Object when a license object is included with a name", function () {
268
- mockServerless.service.custom.documentation.license = {
269
- name: "Apache 2.0",
270
- url: "https://www.apache.org/licenses/LICENSE-2.0.html",
271
- };
272
- const definitionGenerator = new DefinitionGenerator(
273
- mockServerless,
274
- logger
275
- );
276
- definitionGenerator.createInfo();
277
-
278
- expect(definitionGenerator.openAPI).to.be.an("object");
279
- expect(definitionGenerator.openAPI.info).to.be.an("object");
280
- expect(definitionGenerator.openAPI.info).to.have.property("license");
281
- expect(definitionGenerator.openAPI.info.license).to.be.an("object");
282
- expect(definitionGenerator.openAPI.info.license.name).to.be.an("string");
283
- });
284
-
285
- it("should not assign a license Object when a license object is included without a name", function () {
286
- mockServerless.service.custom.documentation.license = {
287
- url: "https://www.apache.org/licenses/LICENSE-2.0.html",
288
- };
289
- const definitionGenerator = new DefinitionGenerator(
290
- mockServerless,
291
- logger
292
- );
293
- definitionGenerator.createInfo();
294
-
295
- expect(definitionGenerator.openAPI).to.be.an("object");
296
- expect(definitionGenerator.openAPI.info).to.be.an("object");
297
- expect(definitionGenerator.openAPI.info).to.not.have.property("license");
298
- });
299
-
300
- it("should only assign a contact url if one is provided", function () {
301
- mockServerless.service.custom.documentation.license = {
302
- name: "John",
303
- };
304
- const definitionGenerator = new DefinitionGenerator(
305
- mockServerless,
306
- logger
307
- );
308
- definitionGenerator.createInfo();
309
-
310
- expect(definitionGenerator.openAPI).to.be.an("object");
311
- expect(definitionGenerator.openAPI.info).to.be.an("object");
312
- expect(definitionGenerator.openAPI.info).to.have.property("license");
313
- expect(definitionGenerator.openAPI.info.license).to.be.an("object");
314
- expect(definitionGenerator.openAPI.info.license.name).to.be.an("string");
315
- expect(definitionGenerator.openAPI.info.license).to.not.have.property(
316
- "url"
317
- );
318
- });
319
-
320
- it("should assign specification extension fields when included", function () {
321
- mockServerless.service.custom.documentation["x-field"] = "john";
322
- const definitionGenerator = new DefinitionGenerator(
323
- mockServerless,
324
- logger
325
- );
326
- definitionGenerator.createInfo();
327
-
328
- expect(definitionGenerator.openAPI).to.be.an("object");
329
- expect(definitionGenerator.openAPI.info).to.be.an("object");
330
- expect(definitionGenerator.openAPI.info).to.have.property("x-field");
331
- expect(definitionGenerator.openAPI.info["x-field"]).to.be.equal("john");
332
- });
333
-
334
- it("should ignore fields that do not conform to specifiction extension", function () {
335
- mockServerless.service.custom.documentation.otherField = "john";
336
- const definitionGenerator = new DefinitionGenerator(
337
- mockServerless,
338
- logger
339
- );
340
- definitionGenerator.createInfo();
341
-
342
- expect(definitionGenerator.openAPI).to.be.an("object");
343
- expect(definitionGenerator.openAPI.info).to.be.an("object");
344
- expect(definitionGenerator.openAPI.info).to.not.have.property(
345
- "otherField"
346
- );
347
- });
348
- });
349
-
350
- describe("createSecuritySchemes", () => {
351
- describe("API Keys", () => {
352
- it("should add an API Key security scheme to components", function () {
353
- mockServerless.service.custom.documentation.securitySchemes = {
354
- api_key: {
355
- type: "apiKey",
356
- name: "Authorization",
357
- in: "header",
358
- },
359
- };
360
-
361
- const definitionGenerator = new DefinitionGenerator(
362
- mockServerless,
363
- logger
364
- );
365
- definitionGenerator.createSecuritySchemes(
366
- mockServerless.service.custom.documentation.securitySchemes
367
- );
368
-
369
- expect(definitionGenerator.openAPI).to.be.an("object");
370
- expect(definitionGenerator.openAPI.components).to.be.an("object");
371
- expect(definitionGenerator.openAPI.components).to.have.property(
372
- "securitySchemes"
373
- );
374
- expect(definitionGenerator.openAPI.components.securitySchemes).to.be.an(
375
- "object"
376
- );
377
- expect(
378
- definitionGenerator.openAPI.components.securitySchemes
379
- ).to.have.property("api_key");
380
- expect(
381
- definitionGenerator.openAPI.components.securitySchemes.api_key
382
- ).to.have.property("type");
383
- expect(
384
- definitionGenerator.openAPI.components.securitySchemes.api_key.type
385
- ).to.be.equal("apiKey");
386
- });
387
-
388
- it("should throw an error when name is missing from an API Key scheme", function () {
389
- mockServerless.service.custom.documentation.securitySchemes = {
390
- api_key: {
391
- type: "apiKey",
392
- in: "header",
393
- },
394
- };
395
-
396
- const definitionGenerator = new DefinitionGenerator(
397
- mockServerless,
398
- logger
399
- );
400
- expect(() => {
401
- definitionGenerator.createSecuritySchemes(
402
- mockServerless.service.custom.documentation.securitySchemes
403
- );
404
- }).to.throw(
405
- 'Security Scheme for "apiKey" requires the name of the header, query or cookie parameter to be used'
406
- );
407
- });
408
-
409
- it("should throw an error when in is missing from an API Key scheme", function () {
410
- mockServerless.service.custom.documentation.securitySchemes = {
411
- api_key: {
412
- type: "apiKey",
413
- name: "Authorization",
414
- },
415
- };
416
-
417
- const definitionGenerator = new DefinitionGenerator(
418
- mockServerless,
419
- logger
420
- );
421
- expect(() => {
422
- definitionGenerator.createSecuritySchemes(
423
- mockServerless.service.custom.documentation.securitySchemes
424
- );
425
- }).to.throw(
426
- 'Security Scheme for "apiKey" requires the location of the API key: header, query or cookie parameter'
427
- );
428
- });
429
- });
430
-
431
- describe("HTTP", () => {
432
- it("should add an HTTP security scheme to components", function () {
433
- mockServerless.service.custom.documentation.securitySchemes = {
434
- http_key: {
435
- type: "http",
436
- scheme: "basic",
437
- },
438
- };
439
-
440
- const definitionGenerator = new DefinitionGenerator(
441
- mockServerless,
442
- logger
443
- );
444
- definitionGenerator.createSecuritySchemes(
445
- mockServerless.service.custom.documentation.securitySchemes
446
- );
447
-
448
- expect(definitionGenerator.openAPI).to.be.an("object");
449
- expect(definitionGenerator.openAPI.components).to.be.an("object");
450
- expect(definitionGenerator.openAPI.components).to.have.property(
451
- "securitySchemes"
452
- );
453
- expect(definitionGenerator.openAPI.components.securitySchemes).to.be.an(
454
- "object"
455
- );
456
- expect(
457
- definitionGenerator.openAPI.components.securitySchemes
458
- ).to.have.property("http_key");
459
- });
460
-
461
- it("should throw an error when scheme is missing from an HTTP scheme", function () {
462
- mockServerless.service.custom.documentation.securitySchemes = {
463
- http_key: {
464
- type: "http",
465
- },
466
- };
467
-
468
- const definitionGenerator = new DefinitionGenerator(
469
- mockServerless,
470
- logger
471
- );
472
- expect(() => {
473
- definitionGenerator.createSecuritySchemes(
474
- mockServerless.service.custom.documentation.securitySchemes
475
- );
476
- }).to.throw('Security Scheme for "http" requires scheme');
477
- });
478
- });
479
-
480
- describe("openIdConnect", () => {
481
- it("should add an openIdConnect security scheme to components", function () {
482
- mockServerless.service.custom.documentation.securitySchemes = {
483
- openIdConnect_key: {
484
- type: "openIdConnect",
485
- openIdConnectUrl: "http://example.com",
486
- },
487
- };
488
-
489
- const definitionGenerator = new DefinitionGenerator(
490
- mockServerless,
491
- logger
492
- );
493
- definitionGenerator.createSecuritySchemes(
494
- mockServerless.service.custom.documentation.securitySchemes
495
- );
496
-
497
- expect(definitionGenerator.openAPI).to.be.an("object");
498
- expect(definitionGenerator.openAPI.components).to.be.an("object");
499
- expect(definitionGenerator.openAPI.components).to.have.property(
500
- "securitySchemes"
501
- );
502
- expect(definitionGenerator.openAPI.components.securitySchemes).to.be.an(
503
- "object"
504
- );
505
- expect(
506
- definitionGenerator.openAPI.components.securitySchemes
507
- ).to.have.property("openIdConnect_key");
508
- });
509
-
510
- it("should throw an error when openIdConnectUrl is missing from an openIdConnect scheme", function () {
511
- mockServerless.service.custom.documentation.securitySchemes = {
512
- openIdConnect_key: {
513
- type: "openIdConnect",
514
- },
515
- };
516
-
517
- const definitionGenerator = new DefinitionGenerator(
518
- mockServerless,
519
- logger
520
- );
521
- expect(() => {
522
- definitionGenerator.createSecuritySchemes(
523
- mockServerless.service.custom.documentation.securitySchemes
524
- );
525
- }).to.throw(
526
- 'Security Scheme for "openIdConnect" requires openIdConnectUrl'
527
- );
528
- });
529
- });
530
-
531
- describe("oauth2", () => {
532
- it("should add an oauth2 security scheme to components", function () {
533
- mockServerless.service.custom.documentation.securitySchemes = {
534
- oAuth2_key: {
535
- type: "oauth2",
536
- flows: {
537
- implicit: {
538
- authorizationUrl: "http://example.org/api/oauth/dialog",
539
- scopes: {
540
- "write:pets": "modify pets in your account",
541
- "read:pets": "read your pets",
542
- },
543
- },
544
- },
545
- },
546
- };
547
-
548
- const definitionGenerator = new DefinitionGenerator(
549
- mockServerless,
550
- logger
551
- );
552
- definitionGenerator.createSecuritySchemes(
553
- mockServerless.service.custom.documentation.securitySchemes
554
- );
555
-
556
- expect(definitionGenerator.openAPI).to.be.an("object");
557
- expect(definitionGenerator.openAPI.components).to.be.an("object");
558
- expect(definitionGenerator.openAPI.components).to.have.property(
559
- "securitySchemes"
560
- );
561
- expect(definitionGenerator.openAPI.components.securitySchemes).to.be.an(
562
- "object"
563
- );
564
- expect(
565
- definitionGenerator.openAPI.components.securitySchemes
566
- ).to.have.property("oAuth2_key");
567
- expect(
568
- definitionGenerator.openAPI.components.securitySchemes.oAuth2_key
569
- ).to.be.an("object");
570
- expect(
571
- definitionGenerator.openAPI.components.securitySchemes.oAuth2_key
572
- ).to.have.property("type");
573
- expect(
574
- definitionGenerator.openAPI.components.securitySchemes.oAuth2_key
575
- ).to.have.property("flows");
576
- expect(
577
- definitionGenerator.openAPI.components.securitySchemes.oAuth2_key
578
- .flows
579
- ).to.be.an("object");
580
- expect(
581
- definitionGenerator.openAPI.components.securitySchemes.oAuth2_key
582
- .flows
583
- ).to.have.property("implicit");
584
- expect(
585
- definitionGenerator.openAPI.components.securitySchemes.oAuth2_key
586
- .flows.implicit
587
- ).to.be.an("object");
588
- expect(
589
- definitionGenerator.openAPI.components.securitySchemes.oAuth2_key
590
- .flows.implicit
591
- ).to.have.property("scopes");
592
- expect(
593
- definitionGenerator.openAPI.components.securitySchemes.oAuth2_key
594
- .flows.implicit.scopes
595
- ).to.be.an("object");
596
- });
597
-
598
- it("should throw an error when flows is missing from an oauth2 scheme", function () {
599
- mockServerless.service.custom.documentation.securitySchemes = {
600
- oAuth2_key: {
601
- type: "oauth2",
602
- },
603
- };
604
-
605
- const definitionGenerator = new DefinitionGenerator(
606
- mockServerless,
607
- logger
608
- );
609
- expect(() => {
610
- definitionGenerator.createSecuritySchemes(
611
- mockServerless.service.custom.documentation.securitySchemes
612
- );
613
- }).to.throw('Security Scheme for "oauth2" requires flows');
614
- });
615
-
616
- it("should throw an error when authorizationUrl is missing from an oauth2 implicit flow scheme", function () {
617
- mockServerless.service.custom.documentation.securitySchemes = {
618
- oAuth2_key: {
619
- type: "oauth2",
620
- flows: {
621
- implicit: {
622
- scopes: {
623
- "write:pets": "modify pets in your account",
624
- "read:pets": "read your pets",
625
- },
626
- },
627
- },
628
- },
629
- };
630
-
631
- const definitionGenerator = new DefinitionGenerator(
632
- mockServerless,
633
- logger
634
- );
635
- expect(() => {
636
- definitionGenerator.createSecuritySchemes(
637
- mockServerless.service.custom.documentation.securitySchemes
638
- );
639
- }).to.throw("oAuth2 implicit flow requires an authorizationUrl");
640
- });
641
-
642
- it("should throw an error when authorizationUrl is missing from an oauth2 authorizationCode flow scheme", function () {
643
- mockServerless.service.custom.documentation.securitySchemes = {
644
- oAuth2_key: {
645
- type: "oauth2",
646
- flows: {
647
- authorizationCode: {
648
- tokenUrl: "http://example.com",
649
- scopes: {
650
- "write:pets": "modify pets in your account",
651
- "read:pets": "read your pets",
652
- },
653
- },
654
- },
655
- },
656
- };
657
-
658
- const definitionGenerator = new DefinitionGenerator(
659
- mockServerless,
660
- logger
661
- );
662
- expect(() => {
663
- definitionGenerator.createSecuritySchemes(
664
- mockServerless.service.custom.documentation.securitySchemes
665
- );
666
- }).to.throw(
667
- "oAuth2 authorizationCode flow requires an authorizationUrl"
668
- );
669
- });
670
-
671
- it("should throw an error when tokenUrl is missing from an oauth2 authorizationCode flow scheme", function () {
672
- mockServerless.service.custom.documentation.securitySchemes = {
673
- oAuth2_key: {
674
- type: "oauth2",
675
- flows: {
676
- authorizationCode: {
677
- authorizationUrl: "http://example.org/api/oauth/dialog",
678
- scopes: {
679
- "write:pets": "modify pets in your account",
680
- "read:pets": "read your pets",
681
- },
682
- },
683
- },
684
- },
685
- };
686
-
687
- const definitionGenerator = new DefinitionGenerator(
688
- mockServerless,
689
- logger
690
- );
691
- expect(() => {
692
- definitionGenerator.createSecuritySchemes(
693
- mockServerless.service.custom.documentation.securitySchemes
694
- );
695
- }).to.throw("oAuth2 authorizationCode flow requires a tokenUrl");
696
- });
697
-
698
- it("should throw an error when tokenUrl is missing from an oauth2 password flow scheme", function () {
699
- mockServerless.service.custom.documentation.securitySchemes = {
700
- oAuth2_key: {
701
- type: "oauth2",
702
- flows: {
703
- password: {
704
- scopes: {
705
- "write:pets": "modify pets in your account",
706
- "read:pets": "read your pets",
707
- },
708
- },
709
- },
710
- },
711
- };
712
-
713
- const definitionGenerator = new DefinitionGenerator(
714
- mockServerless,
715
- logger
716
- );
717
- expect(() => {
718
- definitionGenerator.createSecuritySchemes(
719
- mockServerless.service.custom.documentation.securitySchemes
720
- );
721
- }).to.throw("oAuth2 password flow requires a tokenUrl");
722
- });
723
-
724
- it("should throw an error when tokenUrl is missing from an oauth2 clientCredentials flow scheme", function () {
725
- mockServerless.service.custom.documentation.securitySchemes = {
726
- oAuth2_key: {
727
- type: "oauth2",
728
- flows: {
729
- clientCredentials: {
730
- scopes: {
731
- "write:pets": "modify pets in your account",
732
- "read:pets": "read your pets",
733
- },
734
- },
735
- },
736
- },
737
- };
738
-
739
- const definitionGenerator = new DefinitionGenerator(
740
- mockServerless,
741
- logger
742
- );
743
- expect(() => {
744
- definitionGenerator.createSecuritySchemes(
745
- mockServerless.service.custom.documentation.securitySchemes
746
- );
747
- }).to.throw("oAuth2 clientCredentials flow requires a tokenUrl");
748
- });
749
-
750
- it("should throw an error when scopes is missing from an oauth2 clientCredentials flow scheme", function () {
751
- mockServerless.service.custom.documentation.securitySchemes = {
752
- oAuth2_key: {
753
- type: "oauth2",
754
- flows: {
755
- clientCredentials: {
756
- tokenUrl: "http://example.com",
757
- },
758
- },
759
- },
760
- };
761
-
762
- const definitionGenerator = new DefinitionGenerator(
763
- mockServerless,
764
- logger
765
- );
766
- expect(() => {
767
- definitionGenerator.createSecuritySchemes(
768
- mockServerless.service.custom.documentation.securitySchemes
769
- );
770
- }).to.throw("oAuth2 clientCredentials flow requires scopes");
771
- });
772
-
773
- it("should throw an error when scopes is missing from an oauth2 authorizationCode flow scheme", function () {
774
- mockServerless.service.custom.documentation.securitySchemes = {
775
- oAuth2_key: {
776
- type: "oauth2",
777
- flows: {
778
- authorizationCode: {
779
- tokenUrl: "http://example.com",
780
- authorizationUrl: "http://example.org/api/oauth/dialog",
781
- },
782
- },
783
- },
784
- };
785
-
786
- const definitionGenerator = new DefinitionGenerator(
787
- mockServerless,
788
- logger
789
- );
790
- expect(() => {
791
- definitionGenerator.createSecuritySchemes(
792
- mockServerless.service.custom.documentation.securitySchemes
793
- );
794
- }).to.throw("oAuth2 authorizationCode flow requires scopes");
795
- });
796
-
797
- it("should throw an error when scopes is missing from an oauth2 password flow scheme", function () {
798
- mockServerless.service.custom.documentation.securitySchemes = {
799
- oAuth2_key: {
800
- type: "oauth2",
801
- flows: {
802
- password: {
803
- tokenUrl: "http://example.com",
804
- },
805
- },
806
- },
807
- };
808
-
809
- const definitionGenerator = new DefinitionGenerator(
810
- mockServerless,
811
- logger
812
- );
813
- expect(() => {
814
- definitionGenerator.createSecuritySchemes(
815
- mockServerless.service.custom.documentation.securitySchemes
816
- );
817
- }).to.throw("oAuth2 password flow requires scopes");
818
- });
819
-
820
- it("should throw an error when scopes is missing from an oauth2 implicit flow scheme", function () {
821
- mockServerless.service.custom.documentation.securitySchemes = {
822
- oAuth2_key: {
823
- type: "oauth2",
824
- flows: {
825
- implicit: {
826
- authorizationUrl: "http://example.org/api/oauth/dialog",
827
- },
828
- },
829
- },
830
- };
831
-
832
- const definitionGenerator = new DefinitionGenerator(
833
- mockServerless,
834
- logger
835
- );
836
- expect(() => {
837
- definitionGenerator.createSecuritySchemes(
838
- mockServerless.service.custom.documentation.securitySchemes
839
- );
840
- }).to.throw("oAuth2 implicit flow requires scopes");
841
- });
842
- });
843
-
844
- describe("Multiple Schemes", () => {
845
- it("should add an oauth2 and an apiKey security scheme to components", function () {
846
- mockServerless.service.custom.documentation.securitySchemes = {
847
- oAuth2_key: {
848
- type: "oauth2",
849
- flows: {
850
- implicit: {
851
- authorizationUrl: "http://example.org/api/oauth/dialog",
852
- scopes: {
853
- "write:pets": "modify pets in your account",
854
- "read:pets": "read your pets",
855
- },
856
- },
857
- },
858
- },
859
- api_key: {
860
- type: "apiKey",
861
- name: "Authorization",
862
- in: "header",
863
- },
864
- };
865
-
866
- const definitionGenerator = new DefinitionGenerator(
867
- mockServerless,
868
- logger
869
- );
870
- definitionGenerator.createSecuritySchemes(
871
- mockServerless.service.custom.documentation.securitySchemes
872
- );
873
- expect(definitionGenerator.openAPI).to.be.an("object");
874
- expect(definitionGenerator.openAPI.components).to.be.an("object");
875
- expect(definitionGenerator.openAPI.components).to.have.property(
876
- "securitySchemes"
877
- );
878
- expect(definitionGenerator.openAPI.components.securitySchemes).to.be.an(
879
- "object"
880
- );
881
- expect(
882
- definitionGenerator.openAPI.components.securitySchemes
883
- ).to.have.property("oAuth2_key");
884
- expect(
885
- definitionGenerator.openAPI.components.securitySchemes
886
- ).to.have.property("api_key");
887
- });
888
- });
889
-
890
- describe("x-amazon-* extensions", () => {
891
- it("should add an x-amazon-* security scheme to components", function () {
892
- mockServerless.service.custom.documentation.securitySchemes = {
893
- x_amazon_api_key: {
894
- type: "apiKey",
895
- name: "x-amz-security-token",
896
- in: "header",
897
- "x-amazon-apigateway-authtype": "awsSigv4",
898
- },
899
- };
900
-
901
- const definitionGenerator = new DefinitionGenerator(
902
- mockServerless,
903
- logger
904
- );
905
- definitionGenerator.createSecuritySchemes(
906
- mockServerless.service.custom.documentation.securitySchemes
907
- );
908
-
909
- expect(definitionGenerator.openAPI).to.be.an("object");
910
- expect(definitionGenerator.openAPI.components).to.be.an("object");
911
- expect(definitionGenerator.openAPI.components).to.have.property(
912
- "securitySchemes"
913
- );
914
- expect(definitionGenerator.openAPI.components.securitySchemes).to.be.an(
915
- "object"
916
- );
917
- expect(
918
- definitionGenerator.openAPI.components.securitySchemes
919
- ).to.have.property("x_amazon_api_key");
920
- expect(
921
- definitionGenerator.openAPI.components.securitySchemes.x_amazon_api_key
922
- ).to.have.property("x-amazon-apigateway-authtype");
923
- });
924
- });
925
- });
926
-
927
- describe("createTags", () => {
928
- it("should add tags to the openAPI object correctly", function () {
929
- mockServerless.service.custom.documentation.tags = [{ name: "tag1" }];
930
-
931
- const definitionGenerator = new DefinitionGenerator(
932
- mockServerless,
933
- logger
934
- );
935
- definitionGenerator.createTags();
936
-
937
- expect(definitionGenerator.openAPI).to.be.an("object");
938
- expect(definitionGenerator.openAPI.tags).to.be.an("array");
939
- expect(definitionGenerator.openAPI.tags[0].name).to.be.equal("tag1");
940
- });
941
-
942
- it("should not add tags when they are not defined", function () {
943
- const definitionGenerator = new DefinitionGenerator(
944
- mockServerless,
945
- logger
946
- );
947
- expect(() => {
948
- definitionGenerator.createTags();
949
- }).to.throw();
950
- });
951
- });
952
-
953
- describe(`createResponses`, async function () {
954
- it(`handles creating headers with pragma as a default`, async function () {
955
- const description = "this is a description";
956
- const responseMock = {
957
- methodResponses: [
958
- {
959
- responseBody: { description: description },
960
- statusCode: 200,
961
- owasp: { pragma: true },
962
- },
963
- ],
964
- };
965
-
966
- const definitionGenerator = new DefinitionGenerator(
967
- mockServerless,
968
- logger
969
- );
970
-
971
- const response = await definitionGenerator.createResponses(responseMock);
972
-
973
- expect(response).to.be.an("object");
974
- expect(response).to.have.property("200");
975
- expect(response["200"]).to.have.property("description", description);
976
- expect(response["200"].headers).to.be.an("object");
977
- expect(response["200"].headers).to.have.property("Pragma");
978
- });
979
- });
980
- });