@nikhil-qeas/api-core 0.6.0 → 0.8.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.
- package/CHANGELOG.md +67 -0
- package/README.md +216 -5
- package/bin/qe-api.js +3 -0
- package/dist/contracts/assertions.d.ts +11 -0
- package/dist/contracts/assertions.js +31 -0
- package/dist/contracts/openapi.d.ts +28 -1
- package/dist/contracts/openapi.js +113 -0
- package/dist/graphql/graphql.d.ts +86 -0
- package/dist/graphql/graphql.js +312 -0
- package/dist/http/api-client.d.ts +20 -0
- package/dist/http/api-client.js +70 -9
- package/dist/http/cookies.d.ts +32 -0
- package/dist/http/cookies.js +116 -0
- package/dist/http/multipart.d.ts +20 -0
- package/dist/http/multipart.js +48 -0
- package/dist/http/trace.d.ts +26 -0
- package/dist/http/trace.js +43 -0
- package/dist/http/types.d.ts +22 -0
- package/dist/index.d.ts +31 -5
- package/dist/index.js +16 -3
- package/dist/integrations/mocha.d.ts +32 -0
- package/dist/integrations/mocha.js +81 -0
- package/dist/reporting/html-report.d.ts +23 -0
- package/dist/reporting/html-report.js +118 -0
- package/dist/reporting/junit.d.ts +11 -2
- package/dist/reporting/junit.js +9 -3
- package/dist/reporting/redaction.d.ts +10 -0
- package/dist/reporting/redaction.js +36 -2
- package/dist/security/authorization-matrix.d.ts +55 -0
- package/dist/security/authorization-matrix.js +102 -0
- package/dist/security/headers.d.ts +21 -0
- package/dist/security/headers.js +45 -0
- package/dist/security/policy.js +3 -2
- package/dist/security/sensitive-data.d.ts +22 -0
- package/dist/security/sensitive-data.js +76 -0
- package/dist/testing/json-path.d.ts +6 -0
- package/dist/testing/json-path.js +97 -0
- package/dist/testing/load.d.ts +85 -0
- package/dist/testing/load.js +134 -0
- package/dist/testing/matchers.d.ts +63 -0
- package/dist/testing/matchers.js +164 -0
- package/dist/testing/spec-cases.d.ts +136 -0
- package/dist/testing/spec-cases.js +1065 -0
- package/dist/testing/wait-for.d.ts +40 -0
- package/dist/testing/wait-for.js +82 -0
- package/package.json +1 -1
- package/templates/project/README.md +80 -5
- package/templates/project/__dot__env.example +4 -0
- package/templates/project/__dot__gitlab-ci.yml +1 -0
- package/templates/project/openapi/openapi.json +476 -19
- package/templates/project/src/clients/api.ts +4 -2
- package/templates/project/src/environment.ts +8 -0
- package/templates/project/src/observability.ts +15 -1
- package/templates/project/tests/negative/spec-cases.spec.ts +48 -0
- package/templates/project/tests/performance/health.spec.ts +22 -0
- package/templates/project/tests/security/authentication.spec.ts +3 -1
- package/templates/project/tests/security/authorization.spec.ts +29 -0
- package/templates/project/tests/security/hygiene.spec.ts +37 -0
- package/templates/project/tests/security/spec-cases.spec.ts +61 -0
- package/templates/project/tests/security/write-protection.spec.ts +2 -2
- package/templates/project/tests/setup/hooks.ts +32 -5
- package/templates/project/tests/smoke/health.spec.ts +3 -3
- package/templates/project/tests/smoke/tracing.spec.ts +48 -0
- package/templates/project/tests/support/approved-writes.ts +27 -0
- package/templates/project/tests/support/identities.ts +24 -0
- package/templates/project/tests/support/mock-api.ts +166 -3
|
@@ -8,11 +8,69 @@
|
|
|
8
8
|
"/health": {
|
|
9
9
|
"get": {
|
|
10
10
|
"operationId": "health",
|
|
11
|
-
"tags": [
|
|
11
|
+
"tags": [
|
|
12
|
+
"ops"
|
|
13
|
+
],
|
|
12
14
|
"responses": {
|
|
13
15
|
"200": {
|
|
14
16
|
"description": "The service is up",
|
|
15
|
-
"content": {
|
|
17
|
+
"content": {
|
|
18
|
+
"application/json": {
|
|
19
|
+
"schema": {
|
|
20
|
+
"$ref": "#/components/schemas/Health"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"/accounts": {
|
|
29
|
+
"post": {
|
|
30
|
+
"operationId": "createAccount",
|
|
31
|
+
"tags": [
|
|
32
|
+
"accounts"
|
|
33
|
+
],
|
|
34
|
+
"requestBody": {
|
|
35
|
+
"required": true,
|
|
36
|
+
"content": {
|
|
37
|
+
"application/json": {
|
|
38
|
+
"schema": {
|
|
39
|
+
"$ref": "#/components/schemas/NewAccount"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"responses": {
|
|
45
|
+
"201": {
|
|
46
|
+
"description": "The account was created",
|
|
47
|
+
"content": {
|
|
48
|
+
"application/json": {
|
|
49
|
+
"schema": {
|
|
50
|
+
"$ref": "#/components/schemas/Account"
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"400": {
|
|
56
|
+
"description": "The request was invalid",
|
|
57
|
+
"content": {
|
|
58
|
+
"application/json": {
|
|
59
|
+
"schema": {
|
|
60
|
+
"$ref": "#/components/schemas/Error"
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
"415": {
|
|
66
|
+
"description": "The content type is not supported",
|
|
67
|
+
"content": {
|
|
68
|
+
"application/json": {
|
|
69
|
+
"schema": {
|
|
70
|
+
"$ref": "#/components/schemas/Error"
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
16
74
|
}
|
|
17
75
|
}
|
|
18
76
|
}
|
|
@@ -20,16 +78,39 @@
|
|
|
20
78
|
"/accounts/{id}": {
|
|
21
79
|
"get": {
|
|
22
80
|
"operationId": "getAccount",
|
|
23
|
-
"tags": [
|
|
24
|
-
|
|
81
|
+
"tags": [
|
|
82
|
+
"accounts"
|
|
83
|
+
],
|
|
84
|
+
"parameters": [
|
|
85
|
+
{
|
|
86
|
+
"name": "id",
|
|
87
|
+
"in": "path",
|
|
88
|
+
"required": true,
|
|
89
|
+
"schema": {
|
|
90
|
+
"type": "string"
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
],
|
|
25
94
|
"responses": {
|
|
26
95
|
"200": {
|
|
27
96
|
"description": "The account",
|
|
28
|
-
"content": {
|
|
97
|
+
"content": {
|
|
98
|
+
"application/json": {
|
|
99
|
+
"schema": {
|
|
100
|
+
"$ref": "#/components/schemas/Account"
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
29
104
|
},
|
|
30
105
|
"404": {
|
|
31
106
|
"description": "No such account",
|
|
32
|
-
"content": {
|
|
107
|
+
"content": {
|
|
108
|
+
"application/json": {
|
|
109
|
+
"schema": {
|
|
110
|
+
"$ref": "#/components/schemas/Error"
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
33
114
|
}
|
|
34
115
|
}
|
|
35
116
|
}
|
|
@@ -37,15 +118,204 @@
|
|
|
37
118
|
"/secure/profile": {
|
|
38
119
|
"get": {
|
|
39
120
|
"operationId": "getProfile",
|
|
40
|
-
"tags": [
|
|
121
|
+
"tags": [
|
|
122
|
+
"security"
|
|
123
|
+
],
|
|
124
|
+
"security": [
|
|
125
|
+
{
|
|
126
|
+
"bearerAuth": []
|
|
127
|
+
}
|
|
128
|
+
],
|
|
41
129
|
"responses": {
|
|
42
130
|
"200": {
|
|
43
131
|
"description": "The authenticated caller",
|
|
44
|
-
"content": {
|
|
132
|
+
"content": {
|
|
133
|
+
"application/json": {
|
|
134
|
+
"schema": {
|
|
135
|
+
"$ref": "#/components/schemas/Profile"
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
"401": {
|
|
141
|
+
"description": "Missing or invalid access token",
|
|
142
|
+
"content": {
|
|
143
|
+
"application/json": {
|
|
144
|
+
"schema": {
|
|
145
|
+
"$ref": "#/components/schemas/Error"
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
"/admin/stats": {
|
|
154
|
+
"get": {
|
|
155
|
+
"operationId": "getAdminStats",
|
|
156
|
+
"tags": [
|
|
157
|
+
"security"
|
|
158
|
+
],
|
|
159
|
+
"security": [
|
|
160
|
+
{
|
|
161
|
+
"bearerAuth": []
|
|
162
|
+
}
|
|
163
|
+
],
|
|
164
|
+
"responses": {
|
|
165
|
+
"200": {
|
|
166
|
+
"description": "Statistics only an administrator may read",
|
|
167
|
+
"content": {
|
|
168
|
+
"application/json": {
|
|
169
|
+
"schema": {
|
|
170
|
+
"$ref": "#/components/schemas/Stats"
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
45
174
|
},
|
|
46
175
|
"401": {
|
|
47
176
|
"description": "Missing or invalid access token",
|
|
48
|
-
"content": {
|
|
177
|
+
"content": {
|
|
178
|
+
"application/json": {
|
|
179
|
+
"schema": {
|
|
180
|
+
"$ref": "#/components/schemas/Error"
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
"403": {
|
|
186
|
+
"description": "The caller is not an administrator",
|
|
187
|
+
"content": {
|
|
188
|
+
"application/json": {
|
|
189
|
+
"schema": {
|
|
190
|
+
"$ref": "#/components/schemas/Error"
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
"/oauth/token": {
|
|
199
|
+
"post": {
|
|
200
|
+
"operationId": "issueToken",
|
|
201
|
+
"tags": [
|
|
202
|
+
"security"
|
|
203
|
+
],
|
|
204
|
+
"requestBody": {
|
|
205
|
+
"required": true,
|
|
206
|
+
"content": {
|
|
207
|
+
"application/x-www-form-urlencoded": {
|
|
208
|
+
"schema": {
|
|
209
|
+
"$ref": "#/components/schemas/TokenRequest"
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
"responses": {
|
|
215
|
+
"200": {
|
|
216
|
+
"description": "A new access token",
|
|
217
|
+
"content": {
|
|
218
|
+
"application/json": {
|
|
219
|
+
"schema": {
|
|
220
|
+
"$ref": "#/components/schemas/TokenResponse"
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
},
|
|
225
|
+
"400": {
|
|
226
|
+
"description": "The request was invalid",
|
|
227
|
+
"content": {
|
|
228
|
+
"application/json": {
|
|
229
|
+
"schema": {
|
|
230
|
+
"$ref": "#/components/schemas/Error"
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
"415": {
|
|
236
|
+
"description": "The content type is not supported",
|
|
237
|
+
"content": {
|
|
238
|
+
"application/json": {
|
|
239
|
+
"schema": {
|
|
240
|
+
"$ref": "#/components/schemas/Error"
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
"/accounts/{id}/documents": {
|
|
249
|
+
"post": {
|
|
250
|
+
"operationId": "uploadDocument",
|
|
251
|
+
"tags": [
|
|
252
|
+
"accounts"
|
|
253
|
+
],
|
|
254
|
+
"parameters": [
|
|
255
|
+
{
|
|
256
|
+
"name": "id",
|
|
257
|
+
"in": "path",
|
|
258
|
+
"required": true,
|
|
259
|
+
"schema": {
|
|
260
|
+
"type": "string"
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
],
|
|
264
|
+
"requestBody": {
|
|
265
|
+
"required": true,
|
|
266
|
+
"content": {
|
|
267
|
+
"multipart/form-data": {
|
|
268
|
+
"schema": {
|
|
269
|
+
"$ref": "#/components/schemas/DocumentUpload"
|
|
270
|
+
},
|
|
271
|
+
"encoding": {
|
|
272
|
+
"file": {
|
|
273
|
+
"contentType": "application/pdf, image/png"
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
},
|
|
279
|
+
"responses": {
|
|
280
|
+
"201": {
|
|
281
|
+
"description": "The document was stored",
|
|
282
|
+
"content": {
|
|
283
|
+
"application/json": {
|
|
284
|
+
"schema": {
|
|
285
|
+
"$ref": "#/components/schemas/Document"
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
},
|
|
290
|
+
"400": {
|
|
291
|
+
"description": "The request was invalid",
|
|
292
|
+
"content": {
|
|
293
|
+
"application/json": {
|
|
294
|
+
"schema": {
|
|
295
|
+
"$ref": "#/components/schemas/Error"
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
},
|
|
300
|
+
"404": {
|
|
301
|
+
"description": "No such account",
|
|
302
|
+
"content": {
|
|
303
|
+
"application/json": {
|
|
304
|
+
"schema": {
|
|
305
|
+
"$ref": "#/components/schemas/Error"
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
},
|
|
310
|
+
"415": {
|
|
311
|
+
"description": "The content type is not supported",
|
|
312
|
+
"content": {
|
|
313
|
+
"application/json": {
|
|
314
|
+
"schema": {
|
|
315
|
+
"$ref": "#/components/schemas/Error"
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
49
319
|
}
|
|
50
320
|
}
|
|
51
321
|
}
|
|
@@ -55,28 +325,215 @@
|
|
|
55
325
|
"schemas": {
|
|
56
326
|
"Health": {
|
|
57
327
|
"type": "object",
|
|
58
|
-
"required": [
|
|
59
|
-
|
|
328
|
+
"required": [
|
|
329
|
+
"status"
|
|
330
|
+
],
|
|
331
|
+
"properties": {
|
|
332
|
+
"status": {
|
|
333
|
+
"type": "string",
|
|
334
|
+
"enum": [
|
|
335
|
+
"ok"
|
|
336
|
+
]
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
},
|
|
340
|
+
"NewAccount": {
|
|
341
|
+
"type": "object",
|
|
342
|
+
"required": [
|
|
343
|
+
"name",
|
|
344
|
+
"currency"
|
|
345
|
+
],
|
|
346
|
+
"additionalProperties": false,
|
|
347
|
+
"properties": {
|
|
348
|
+
"name": {
|
|
349
|
+
"type": "string",
|
|
350
|
+
"minLength": 1,
|
|
351
|
+
"maxLength": 50
|
|
352
|
+
},
|
|
353
|
+
"currency": {
|
|
354
|
+
"type": "string",
|
|
355
|
+
"enum": [
|
|
356
|
+
"USD",
|
|
357
|
+
"EUR",
|
|
358
|
+
"GBP"
|
|
359
|
+
]
|
|
360
|
+
},
|
|
361
|
+
"initialDeposit": {
|
|
362
|
+
"type": "number",
|
|
363
|
+
"minimum": 0
|
|
364
|
+
},
|
|
365
|
+
"webhookUrl": {
|
|
366
|
+
"type": "string",
|
|
367
|
+
"format": "uri",
|
|
368
|
+
"description": "Where the API reports changes to this account. Must be a public http or https address."
|
|
369
|
+
},
|
|
370
|
+
"accountNumber": {
|
|
371
|
+
"type": "string",
|
|
372
|
+
"readOnly": true,
|
|
373
|
+
"description": "Assigned by the server. Sending it has no effect."
|
|
374
|
+
}
|
|
375
|
+
}
|
|
60
376
|
},
|
|
61
377
|
"Account": {
|
|
62
378
|
"type": "object",
|
|
63
|
-
"required": [
|
|
379
|
+
"required": [
|
|
380
|
+
"id",
|
|
381
|
+
"status"
|
|
382
|
+
],
|
|
64
383
|
"additionalProperties": false,
|
|
65
384
|
"properties": {
|
|
66
|
-
"id": {
|
|
67
|
-
|
|
68
|
-
|
|
385
|
+
"id": {
|
|
386
|
+
"type": "string"
|
|
387
|
+
},
|
|
388
|
+
"status": {
|
|
389
|
+
"type": "string",
|
|
390
|
+
"enum": [
|
|
391
|
+
"open",
|
|
392
|
+
"closed"
|
|
393
|
+
]
|
|
394
|
+
},
|
|
395
|
+
"balance": {
|
|
396
|
+
"type": "number"
|
|
397
|
+
}
|
|
69
398
|
}
|
|
70
399
|
},
|
|
71
400
|
"Profile": {
|
|
72
401
|
"type": "object",
|
|
73
|
-
"required": [
|
|
74
|
-
|
|
402
|
+
"required": [
|
|
403
|
+
"user"
|
|
404
|
+
],
|
|
405
|
+
"properties": {
|
|
406
|
+
"user": {
|
|
407
|
+
"type": "string"
|
|
408
|
+
}
|
|
409
|
+
}
|
|
75
410
|
},
|
|
76
411
|
"Error": {
|
|
77
412
|
"type": "object",
|
|
78
|
-
"required": [
|
|
79
|
-
|
|
413
|
+
"required": [
|
|
414
|
+
"error"
|
|
415
|
+
],
|
|
416
|
+
"properties": {
|
|
417
|
+
"error": {
|
|
418
|
+
"type": "string"
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
},
|
|
422
|
+
"Stats": {
|
|
423
|
+
"type": "object",
|
|
424
|
+
"required": [
|
|
425
|
+
"accounts"
|
|
426
|
+
],
|
|
427
|
+
"properties": {
|
|
428
|
+
"accounts": {
|
|
429
|
+
"type": "integer",
|
|
430
|
+
"minimum": 0
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
},
|
|
434
|
+
"TokenRequest": {
|
|
435
|
+
"type": "object",
|
|
436
|
+
"required": [
|
|
437
|
+
"grant_type",
|
|
438
|
+
"client_id",
|
|
439
|
+
"client_secret"
|
|
440
|
+
],
|
|
441
|
+
"additionalProperties": false,
|
|
442
|
+
"properties": {
|
|
443
|
+
"grant_type": {
|
|
444
|
+
"type": "string",
|
|
445
|
+
"enum": [
|
|
446
|
+
"client_credentials"
|
|
447
|
+
]
|
|
448
|
+
},
|
|
449
|
+
"client_id": {
|
|
450
|
+
"type": "string",
|
|
451
|
+
"minLength": 1,
|
|
452
|
+
"maxLength": 100
|
|
453
|
+
},
|
|
454
|
+
"client_secret": {
|
|
455
|
+
"type": "string",
|
|
456
|
+
"minLength": 1,
|
|
457
|
+
"maxLength": 200
|
|
458
|
+
},
|
|
459
|
+
"scope": {
|
|
460
|
+
"type": "string",
|
|
461
|
+
"maxLength": 200
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
},
|
|
465
|
+
"TokenResponse": {
|
|
466
|
+
"type": "object",
|
|
467
|
+
"required": [
|
|
468
|
+
"access_token",
|
|
469
|
+
"token_type"
|
|
470
|
+
],
|
|
471
|
+
"properties": {
|
|
472
|
+
"access_token": {
|
|
473
|
+
"type": "string"
|
|
474
|
+
},
|
|
475
|
+
"token_type": {
|
|
476
|
+
"type": "string",
|
|
477
|
+
"enum": [
|
|
478
|
+
"Bearer"
|
|
479
|
+
]
|
|
480
|
+
},
|
|
481
|
+
"expires_in": {
|
|
482
|
+
"type": "integer"
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
},
|
|
486
|
+
"DocumentUpload": {
|
|
487
|
+
"type": "object",
|
|
488
|
+
"required": [
|
|
489
|
+
"file",
|
|
490
|
+
"kind"
|
|
491
|
+
],
|
|
492
|
+
"additionalProperties": false,
|
|
493
|
+
"properties": {
|
|
494
|
+
"file": {
|
|
495
|
+
"type": "string",
|
|
496
|
+
"format": "binary"
|
|
497
|
+
},
|
|
498
|
+
"kind": {
|
|
499
|
+
"type": "string",
|
|
500
|
+
"enum": [
|
|
501
|
+
"statement",
|
|
502
|
+
"identity",
|
|
503
|
+
"other"
|
|
504
|
+
]
|
|
505
|
+
},
|
|
506
|
+
"note": {
|
|
507
|
+
"type": "string",
|
|
508
|
+
"maxLength": 100
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
},
|
|
512
|
+
"Document": {
|
|
513
|
+
"type": "object",
|
|
514
|
+
"required": [
|
|
515
|
+
"id",
|
|
516
|
+
"kind",
|
|
517
|
+
"size"
|
|
518
|
+
],
|
|
519
|
+
"properties": {
|
|
520
|
+
"id": {
|
|
521
|
+
"type": "string"
|
|
522
|
+
},
|
|
523
|
+
"kind": {
|
|
524
|
+
"type": "string"
|
|
525
|
+
},
|
|
526
|
+
"size": {
|
|
527
|
+
"type": "integer",
|
|
528
|
+
"minimum": 0
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
},
|
|
533
|
+
"securitySchemes": {
|
|
534
|
+
"bearerAuth": {
|
|
535
|
+
"type": "http",
|
|
536
|
+
"scheme": "bearer"
|
|
80
537
|
}
|
|
81
538
|
}
|
|
82
539
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ApiClient, TokenManager, assertRequestPolicy, clientCredentialsProvider } from '@nikhil-qeas/api-core';
|
|
2
2
|
import type { ApiClientConfig, ApiRequest, ApiResult, SecurityPolicy } from '@nikhil-qeas/api-core';
|
|
3
|
-
import { oauthSettings, requestPolicy, targetBaseUrl } from '../environment.js';
|
|
4
|
-
import { recordEvent } from '../observability.js';
|
|
3
|
+
import { oauthSettings, requestPolicy, targetBaseUrl, traceEnabled } from '../environment.js';
|
|
4
|
+
import { currentTrace, recordEvent } from '../observability.js';
|
|
5
5
|
|
|
6
6
|
let sharedTokens: TokenManager | undefined;
|
|
7
7
|
/** One token cache per process, so every client and test shares the same access token. */
|
|
@@ -37,6 +37,7 @@ class PolicyEnforcingClient extends ApiClient {
|
|
|
37
37
|
* - Read-only unless QE_ALLOW_MUTATIONS=true and QE_APPROVAL=approved (never in production): see requestPolicy().
|
|
38
38
|
* - Retries safe requests with backoff and honors Retry-After; mutations are never retried after an ambiguous failure.
|
|
39
39
|
* - Authenticates with OAuth2 client credentials when OAUTH_* is configured, and renews the token once on a 401.
|
|
40
|
+
* - Sends a W3C traceparent when QE_TRACE=true, one trace per test, so a failed test can be found in the API's tracing backend.
|
|
40
41
|
* - Feeds every request to the metrics and OpenAPI coverage collectors.
|
|
41
42
|
*/
|
|
42
43
|
export function createApiClient(baseUrl: string = targetBaseUrl(), overrides: Partial<ApiClientConfig> = {}): ApiClient {
|
|
@@ -48,6 +49,7 @@ export function createApiClient(baseUrl: string = targetBaseUrl(), overrides: Pa
|
|
|
48
49
|
maxSafeRetries: 1,
|
|
49
50
|
onEvent: recordEvent,
|
|
50
51
|
...(tokens ? { auth: { tokens } } : {}),
|
|
52
|
+
...(traceEnabled() ? { trace: { traceId: currentTrace } } : {}),
|
|
51
53
|
...overrides
|
|
52
54
|
}, policy);
|
|
53
55
|
}
|
|
@@ -29,6 +29,14 @@ export function oauthSettings(env: NodeJS.ProcessEnv = process.env): OAuthSettin
|
|
|
29
29
|
};
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Whether every request carries a W3C `traceparent` header (QE_TRACE=true), one trace per test, so a failed test can be found in the
|
|
34
|
+
* API's tracing backend. Off by default, because it adds a header to every request.
|
|
35
|
+
*/
|
|
36
|
+
export function traceEnabled(env: NodeJS.ProcessEnv = process.env): boolean {
|
|
37
|
+
return env.QE_TRACE === 'true';
|
|
38
|
+
}
|
|
39
|
+
|
|
32
40
|
/**
|
|
33
41
|
* What the run may send (QE_ENV, QE_ALLOW_MUTATIONS, QE_APPROVAL).
|
|
34
42
|
* Runs are read-only: POST, PUT, PATCH and DELETE are refused unless QE_ALLOW_MUTATIONS=true and QE_APPROVAL=approved,
|
|
@@ -1,13 +1,27 @@
|
|
|
1
|
-
import { MetricsCollector, OpenApiCoverage } from '@nikhil-qeas/api-core';
|
|
1
|
+
import { MetricsCollector, MochaResultCollector, OpenApiCoverage, newTraceId } from '@nikhil-qeas/api-core';
|
|
2
2
|
import type { HttpEvent } from '@nikhil-qeas/api-core';
|
|
3
3
|
import { loadSpec } from './openapi.js';
|
|
4
4
|
|
|
5
5
|
/** Request timings, errors and retry counts for the whole run. */
|
|
6
6
|
export const metrics = new MetricsCollector();
|
|
7
7
|
|
|
8
|
+
/** The final outcome of every test, recorded by the root hooks for the HTML report. */
|
|
9
|
+
export const results = new MochaResultCollector();
|
|
10
|
+
|
|
8
11
|
/** Which operations and documented responses of the OpenAPI spec the run exercised. Exclude what you do not test, e.g. ['GET /health']. */
|
|
9
12
|
export const coverage = new OpenApiCoverage(loadSpec(), { exclude: [] });
|
|
10
13
|
|
|
14
|
+
let currentTraceId: string | undefined;
|
|
15
|
+
/** Starts a new trace. The root hook calls it before every test, so all requests of one test join one trace (see QE_TRACE). */
|
|
16
|
+
export function startTrace(): string {
|
|
17
|
+
currentTraceId = newTraceId();
|
|
18
|
+
return currentTraceId;
|
|
19
|
+
}
|
|
20
|
+
/** The trace id of the running test, or undefined before the first test. */
|
|
21
|
+
export function currentTrace(): string | undefined {
|
|
22
|
+
return currentTraceId;
|
|
23
|
+
}
|
|
24
|
+
|
|
11
25
|
/** Event sink for ApiClient: every request and retry is recorded for the end-of-run report. */
|
|
12
26
|
export function recordEvent(event: HttpEvent): void {
|
|
13
27
|
metrics.record(event);
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { OpenApiCases, assertCaseOutcome } from '@nikhil-qeas/api-core';
|
|
2
|
+
import { createApiClient } from '../../src/clients/api.js';
|
|
3
|
+
import { loadSpec } from '../../src/openapi.js';
|
|
4
|
+
import { useMockWithApprovedWrites } from '../support/approved-writes.js';
|
|
5
|
+
|
|
6
|
+
// Test cases generated from openapi/openapi.json, so they follow the spec when you replace it:
|
|
7
|
+
// @negative requests that each break one rule of the spec (missing field, wrong type, too long, bad format, ...) must be rejected with a 4xx.
|
|
8
|
+
// @fuzz hostile and extreme values (injection strings, huge or odd numbers, deep nesting) must not make the API fail with a 5xx.
|
|
9
|
+
// If an API needs values the generator cannot invent (business rules, complex patterns), give it known-good ones:
|
|
10
|
+
// new OpenApiCases(loadSpec(), { baselines: { 'POST /accounts': { json: { name: 'Savings', currency: 'EUR' } } } })
|
|
11
|
+
const cases = new OpenApiCases(loadSpec());
|
|
12
|
+
|
|
13
|
+
// These cases send POST, PUT, PATCH and DELETE, so the suite approves writes for itself while it runs against the local mock
|
|
14
|
+
// (see tests/support/approved-writes.ts). Against a real environment, approve writes in the shell or the CI job, never in code.
|
|
15
|
+
|
|
16
|
+
describe('@negative requests the spec forbids are rejected', () => {
|
|
17
|
+
const mock = useMockWithApprovedWrites();
|
|
18
|
+
for (const operation of cases.operations()) {
|
|
19
|
+
const negative = cases.negative(operation);
|
|
20
|
+
if (!negative.length) continue;
|
|
21
|
+
describe(`${operation.method} ${operation.path}`, () => {
|
|
22
|
+
it('accepts a valid request', async () => {
|
|
23
|
+
const valid = cases.valid(operation);
|
|
24
|
+
assertCaseOutcome(await createApiClient(mock().baseUrl).request(valid.request), valid);
|
|
25
|
+
});
|
|
26
|
+
for (const testCase of negative) {
|
|
27
|
+
it(testCase.description, async () => {
|
|
28
|
+
assertCaseOutcome(await createApiClient(mock().baseUrl).request(testCase.request), testCase);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe('@fuzz hostile input does not crash the API', () => {
|
|
36
|
+
const mock = useMockWithApprovedWrites();
|
|
37
|
+
for (const operation of cases.operations()) {
|
|
38
|
+
const fuzz = cases.fuzz(operation);
|
|
39
|
+
if (!fuzz.length) continue;
|
|
40
|
+
describe(`${operation.method} ${operation.path}`, () => {
|
|
41
|
+
for (const testCase of fuzz) {
|
|
42
|
+
it(testCase.description, async () => {
|
|
43
|
+
assertCaseOutcome(await createApiClient(mock().baseUrl).request(testCase.request), testCase);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { expectLoad, runLoad, summarizeLoad } from '@nikhil-qeas/api-core';
|
|
2
|
+
import { createApiClient } from '../../src/clients/api.js';
|
|
3
|
+
import { startMockApi } from '../support/mock-api.js';
|
|
4
|
+
import type { MockApi } from '../support/mock-api.js';
|
|
5
|
+
|
|
6
|
+
// A performance smoke test: a few dozen requests, a few at a time, with limits on latency and on failures. It catches an endpoint that
|
|
7
|
+
// became slow or started failing under a little concurrency. It is not a load test (it is capped at 1000 requests and 50 at a time):
|
|
8
|
+
// use k6 or Gatling for that. The limits below are generous so the sample passes anywhere; set yours from what the API promises.
|
|
9
|
+
// The first requests (warm-up) are sent before the measured ones and left out of the figures. The run stops early after repeated
|
|
10
|
+
// failures, and because it goes through createApiClient() a read-only run cannot turn it into a write load.
|
|
11
|
+
describe('@performance health check', () => {
|
|
12
|
+
let mock: MockApi;
|
|
13
|
+
before(async () => { mock = await startMockApi(); });
|
|
14
|
+
after(async () => { await mock.close(); });
|
|
15
|
+
|
|
16
|
+
it('answers 50 requests, 5 at a time, without failures and within the latency budget', async () => {
|
|
17
|
+
const api = createApiClient(mock.baseUrl);
|
|
18
|
+
const report = await runLoad(() => api.request({ method: 'GET', path: '/health' }), { requests: 50, concurrency: 5 });
|
|
19
|
+
console.log(` ${summarizeLoad(report)}`);
|
|
20
|
+
expectLoad(report, { p95Ms: 1000, maxMs: 5000 }); // no failures are allowed unless you pass maxErrorRate
|
|
21
|
+
});
|
|
22
|
+
});
|