-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathcustom-components.spec.ts
142 lines (123 loc) · 3.62 KB
/
custom-components.spec.ts
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
import { OpenAPIRegistry } from '../src/openapi-registry';
import { z } from 'zod';
import { extendZodWithOpenApi } from '../src/zod-extensions';
import { OpenApiGeneratorV3 } from '../src/v3.0/openapi-generator';
import { testDocConfig } from './lib/helpers';
extendZodWithOpenApi(z);
// TODO: Tests with both generators
describe('Custom components', () => {
it('can register and generate security schemes', () => {
const registry = new OpenAPIRegistry();
const bearerAuth = registry.registerComponent(
'securitySchemes',
'bearerAuth',
{
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
}
);
registry.registerPath({
path: '/units',
method: 'get',
security: [{ [bearerAuth.name]: [] }],
responses: {
200: {
description: 'Sample response',
content: {
'application/json': {
schema: z.string(),
},
},
},
},
});
const builder = new OpenApiGeneratorV3(registry.definitions);
const document = builder.generateDocument(testDocConfig);
expect(document.paths['/units']?.get?.security).toEqual([
{ bearerAuth: [] },
]);
expect(document.components!.securitySchemes).toEqual({
bearerAuth: {
bearerFormat: 'JWT',
scheme: 'bearer',
type: 'http',
},
});
});
it('can register and generate headers', () => {
const registry = new OpenAPIRegistry();
const apiKeyHeader = registry.registerComponent('headers', 'api-key', {
example: '1234',
required: true,
description: 'The API Key you were given in the developer portal',
});
registry.registerPath({
path: '/units',
method: 'get',
responses: {
200: {
description: 'Sample response',
headers: { 'x-api-key': apiKeyHeader.ref },
content: {
'application/json': {
schema: z.string(),
},
},
},
},
});
const builder = new OpenApiGeneratorV3(registry.definitions);
const document = builder.generateDocument(testDocConfig);
expect(document.paths['/units']?.get?.responses['200'].headers).toEqual({
'x-api-key': { $ref: '#/components/headers/api-key' },
});
expect(document.components!.headers).toEqual({
'api-key': {
example: '1234',
required: true,
description: 'The API Key you were given in the developer portal',
},
});
});
it('can generate responses', () => {
const registry = new OpenAPIRegistry();
const response = registry.registerComponent('responses', 'BadRequest', {
description: 'BadRequest',
content: {
'application/json': {
schema: {
type: 'object',
properties: { name: { type: 'string' } },
},
},
},
});
registry.registerPath({
summary: 'Get user of an organization',
method: 'get',
path: '/test',
responses: {
'400': response.ref,
},
});
const builder = new OpenApiGeneratorV3(registry.definitions);
const document = builder.generateDocument(testDocConfig);
expect(document.paths['/test']?.get?.responses['400']).toEqual({
$ref: '#/components/responses/BadRequest',
});
expect(document.components!.responses).toEqual({
BadRequest: {
description: 'BadRequest',
content: {
'application/json': {
schema: {
type: 'object',
properties: { name: { type: 'string' } },
},
},
},
},
});
});
});