forked from apigee-127/swagger-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-v1_2.js
More file actions
649 lines (574 loc) · 22.5 KB
/
Copy pathtest-v1_2.js
File metadata and controls
649 lines (574 loc) · 22.5 KB
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
/* global describe, it */
/*
* Copyright 2014 Apigee Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// Module requirements
var _ = require('lodash');
var assert = require('assert');
var fs = require('fs');
var path = require('path');
var spec = require('../').v1_2; // jshint ignore:line
var allSchemaFiles = [
'apiDeclaration.json',
'authorizationObject.json',
'dataType.json',
'dataTypeBase.json',
'infoObject.json',
'modelsObject.json',
'oauth2GrantType.json',
'operationObject.json',
'parameterObject.json',
'resourceListing.json',
'resourceObject.json'
];
var allSampleFiles = {};
var invalidApiResourceListingJson = require('./v1_2-invalid-api-resource-listing.json');
var invalidApiResource1Json = require('./v1_2-invalid-api-resource1.json');
var invalidApiResource2Json = require('./v1_2-invalid-api-resource2.json');
var invalidApiResource3Json = require('./v1_2-invalid-api-resource3.json');
var invalidModelMiscJson = require('./v1_2-invalid-model-misc.json');
var invalidModelRefsJson = require('./v1_2-invalid-model-refs.json');
var invalidModelInheritanceJson = require('./v1_2-invalid-model-inheritance.json');
var invalidOperationMiscJson = require('./v1_2-invalid-operation-misc.json');
var findAllErrorsOrWarnings = function (type, code, results) {
var arr = [];
var finder = function (result) {
if (result.code === code) {
arr.push(result);
}
};
if (_.isArray(results)) {
results.forEach(function (resource) {
resource[type].forEach(finder);
});
} else {
results[type].forEach(finder);
}
return arr;
};
// Load the sample files from disk
fs.readdirSync(path.join(__dirname, '..', 'samples', '1.2'))
.filter(function (name) {
return name.match(/^(.*)\.json$/);
})
.forEach(function (name) {
allSampleFiles[name] = require('../samples/1.2/' + name);
});
describe('Specification v1.2', function () {
describe('metadata', function () {
it('should have proper docsUrl, primitives, options, schemasUrl and verison properties', function () {
assert.deepEqual(spec.options, {
validator: {
useDefault: false,
useCoerce: false,
checkRequired: true,
removeAdditional: false
}
});
assert.strictEqual(spec.docsUrl, 'https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md');
assert.deepEqual(spec.primitives, [
'integer',
'long',
'float',
'double',
'number',
'string',
'byte',
'boolean',
'date',
'dateTime'
]);
assert.strictEqual(spec.schemasUrl, 'https://github.com/wordnik/swagger-spec/tree/master/schemas/v1.2');
assert.strictEqual(spec.version, '1.2');
});
});
describe('schemas', function () {
it('should contain all schema files', function () {
assert.deepEqual(Object.keys(spec.schemas), allSchemaFiles);
});
it('should contain the proper content for each schema file', function () {
Object.keys(spec.schemas).forEach(function (schemaName) {
var schema = spec.schemas[schemaName];
assert.ok(schema.id.substring(schema.id.lastIndexOf('/') + 1), schemaName + '#');
});
});
});
// Test validators
describe('validators', function () {
it('should contain all validators', function () {
assert.deepEqual(Object.keys(spec.validators), allSchemaFiles);
});
});
describe('#validate', function () {
it('should return true for valid JSON files', function () {
Object.keys(allSampleFiles).forEach(function (name) {
var result;
switch (name) {
case 'pet.json':
case 'store.json':
case 'user.json':
result = spec.validate(allSampleFiles[name]);
break;
case 'resource-listing.json':
result = spec.validate(allSampleFiles[name], 'resourceListing.json');
break;
default:
throw new Error('Unexpected sample file: ' + name);
}
assert.ok(_.isUndefined(result));
});
});
it('should return errors for structurally invalid JSON files', function () {
var petJson = _.cloneDeep(allSampleFiles['pet.json']);
var petErrors = [
{
code: 'VALIDATION_FAILED',
message: 'Validation error: enum',
data: 'body',
path: '$.apis[1].operations[0].parameters[1].paramType'
}
];
var rlJson = _.cloneDeep(allSampleFiles['resource-listing.json']);
var rlErrors = [
{
code: 'VALIDATION_OBJECT_REQUIRED',
message: 'Missing required property: apis',
path: '$.apis'
}
];
var storeJson = _.cloneDeep(allSampleFiles['store.json']);
var storeErrors = [
{
code: 'VALIDATION_INVALID_TYPE',
message: 'Invalid type: boolean should be string',
data: false,
path: '$.models.Order.description'
}
];
var userJson = _.cloneDeep(allSampleFiles['user.json']);
var userErrors = [
{
code: 'VALIDATION_ADDITIONAL_PROPERTIES',
message: 'Additional properties not allowed: extra',
data: 'value',
path: '$.apis[0].operations[0].authorizations.oauth2[0].extra'
}
];
// Wrong enum value
petJson.apis[1].operations[0].parameters[1].paramType = 'body';
// Missing required
delete rlJson.apis;
// Wrong type
storeJson.models.Order.description = false;
// Extra property
userJson.apis[0].operations[0].authorizations.oauth2[0].extra = 'value';
assert.deepEqual(spec.validate(petJson).errors, petErrors);
assert.equal(spec.validate(petJson).warnings, 0);
assert.deepEqual(spec.validate(rlJson, 'resourceListing.json').errors, rlErrors);
assert.equal(spec.validate(rlJson, 'resourceListing.json').warnings, 0);
assert.deepEqual(spec.validate(storeJson).errors, storeErrors);
assert.equal(spec.validate(storeJson).warnings, 0);
assert.deepEqual(spec.validate(userJson).errors, userErrors);
assert.equal(spec.validate(userJson).warnings, 0);
});
it('should return errors for missing model references in apiDeclaration files', function () {
var result = spec.validate(invalidModelRefsJson);
var expectedMissingModelRefs = {
'MissingParamRef': '$.apis[0].operations[0].parameters[0].type',
'MissingParamItemsRef': '$.apis[0].operations[0].parameters[1].items.$ref',
'MissingResponseMessageRef': '$.apis[0].operations[0].responseMessages[0].responseModel',
'MissingTypeRef': '$.apis[0].operations[0].type',
'MissingTypeItemsRef': '$.apis[1].operations[0].items.$ref',
'MissingPropertyItemsRef': '$.models[\'Animal\'].properties[\'breeds\'].items.$ref',
'MissingSubTypeRef': '$.models[\'Animal\'].subTypes[1]',
'MissingPropertyRef': '$.models[\'Cat\'].properties[\'address\'].$ref'
};
assert.equal(result.errors.length, Object.keys(expectedMissingModelRefs).length);
result.errors.forEach(function (error) {
assert.equal(error.code, 'UNRESOLVABLE_MODEL_REFERENCE');
assert.equal(error.message, 'Model reference could not be resolved: ' + error.data);
assert.equal(error.path, expectedMissingModelRefs[error.data]);
});
});
it('should return warnings for unused models in apiDeclaration files', function () {
var result = spec.validate(invalidModelRefsJson);
assert.equal(1, result.warnings.length);
assert.deepEqual(result.warnings[0], {
code: 'UNUSED_MODEL',
message: 'Model is defined but is not used: Animal',
data: 'Animal',
path: '$.models[\'Animal\']'
});
});
it('should return errors for duplicate model ids in apiDeclaration files', function () {
var result = spec.validate(invalidModelInheritanceJson);
var errors = findAllErrorsOrWarnings('errors', 'DUPLICATE_MODEL_DEFINITION', result);
assert.deepEqual(errors, [
{
code: 'DUPLICATE_MODEL_DEFINITION',
message: 'Model already defined: A',
data: 'A',
path: '$.models[\'J\'].id'
}
]);
});
it('should return errors for cyclical model subTypes in apiDeclaration files', function () {
var result = spec.validate(invalidModelInheritanceJson);
var errors = findAllErrorsOrWarnings('errors', 'CYCLICAL_MODEL_INHERITANCE', result);
assert.deepEqual(errors, [
{
code: 'CYCLICAL_MODEL_INHERITANCE',
message: 'Model has a circular inheritance: C -> A -> D -> C',
data: ['D'],
path: '$.models[\'C\'].subTypes'
},
{
code: 'CYCLICAL_MODEL_INHERITANCE',
message: 'Model has a circular inheritance: H -> I -> H',
data: ['I', 'I'],
path: '$.models[\'H\'].subTypes'
}
]);
});
it('should return errors for model multiple inheritance in apiDeclaration files', function () {
var result = spec.validate(invalidModelInheritanceJson);
var errors = findAllErrorsOrWarnings('errors', 'MULTIPLE_MODEL_INHERITANCE', result);
assert.deepEqual(errors, [
{
code: 'MULTIPLE_MODEL_INHERITANCE',
message: 'Child model is sub type of multiple models: A && E',
data: invalidModelInheritanceJson.models.B,
path: '$.models[\'B\']'
}
]);
});
it('should return errors for model subTypes redeclaring ancestor properties in apiDeclaration files', function () {
var result = spec.validate(invalidModelInheritanceJson);
var errors = findAllErrorsOrWarnings('errors', 'CHILD_MODEL_REDECLARES_PROPERTY', result);
assert.deepEqual(errors, [
{
code: 'CHILD_MODEL_REDECLARES_PROPERTY',
message: 'Child model declares property already declared by ancestor: fId',
data: invalidModelInheritanceJson.models.G.properties.fId,
path: '$.models[\'G\'].properties[\'fId\']'
}
]);
});
it('should return warning for model subTypes with duplicate entries in apiDeclaration files', function () {
var result = spec.validate(invalidModelInheritanceJson);
var warnings = findAllErrorsOrWarnings('warnings', 'DUPLICATE_MODEL_SUBTYPE_DEFINITION', result);
assert.deepEqual(warnings, [
{
code: 'DUPLICATE_MODEL_SUBTYPE_DEFINITION',
message: 'Model already has subType defined: I',
data: 'I',
path: '$.models[\'H\'].subTypes[1]'
}
]);
});
it('should return errors for model with invalid discriminator in apiDeclaration files', function () {
var result = spec.validate(invalidModelMiscJson);
var errors = findAllErrorsOrWarnings('errors', 'INVALID_MODEL_DISCRIMINATOR', result);
assert.deepEqual(errors, [
{
code: 'INVALID_MODEL_DISCRIMINATOR',
message: 'Model cannot have discriminator without subTypes: aId',
data: 'aId',
path: '$.models[\'A\'].discriminator'
}
]);
});
it('should return errors for model with missing required property in apiDeclaration files', function () {
var result = spec.validate(invalidModelMiscJson);
var errors = findAllErrorsOrWarnings('errors', 'MISSING_REQUIRED_MODEL_PROPERTY', result);
assert.deepEqual(errors, [
{
'code': 'MISSING_REQUIRED_MODEL_PROPERTY',
'message': 'Model requires property but it is not defined: bId',
'data': 'bId',
'path': '$.models[\'A\'].required[1]'
}
]);
});
it('should return warning for operations with duplicate method apiDeclaration files', function () {
var result = spec.validate(invalidOperationMiscJson);
var errors = findAllErrorsOrWarnings('errors', 'DUPLICATE_OPERATION_METHOD', result);
assert.deepEqual(errors, [
{
code: 'DUPLICATE_OPERATION_METHOD',
message: 'Operation method already defined: GET',
data: 'GET',
path: '$.apis[0].operations[1].method'
}
]);
});
it('should return warning for operations with duplicate nickname apiDeclaration files', function () {
var result = spec.validate(invalidOperationMiscJson);
var errors = findAllErrorsOrWarnings('errors', 'DUPLICATE_OPERATION_NICKNAME', result);
assert.deepEqual(errors, [
{
code: 'DUPLICATE_OPERATION_NICKNAME',
message: 'Operation method already defined: getGreeting',
data: 'getGreeting',
path: '$.apis[0].operations[1].nickname'
}
]);
});
it('should return warning for operations with responseMessage codes nickname apiDeclaration files', function () {
var result = spec.validate(invalidOperationMiscJson);
var errors = findAllErrorsOrWarnings('errors', 'DUPLICATE_OPERATION_RESPONSEMESSAGE_CODE', result);
assert.deepEqual(errors, [
{
code: 'DUPLICATE_OPERATION_RESPONSEMESSAGE_CODE',
message: 'Operation responseMessage code already defined: 400',
data: 400,
path: '$.apis[0].operations[0].responseMessages[1].code'
}
]);
});
it('should return warning for operation with 121+ character summary length in apiDeclaration files', function () {
var json = _.cloneDeep(invalidOperationMiscJson);
var summary = new Array(122).join('.');
var warnings = [];
var result;
json.apis[0].operations[1].summary = summary;
result = spec.validate(json);
warnings = findAllErrorsOrWarnings('warnings', 'OPERATION_SUMMARY_LONG', result);
assert.deepEqual(warnings, [
{
code: 'OPERATION_SUMMARY_LONG',
message: 'Operation summary is greater than 120 characters: 121',
data: summary,
path: '$.apis[0].operations[1].summary'
}
]);
});
it('should return errors for defaultValue related properties in apiDeclaration files', function () {
var result = spec.validate(require('./v1_2-invalid-defaultValues.json'));
var expectedErrors = [
{
code: 'ENUM_MISMATCH',
message: 'Default value is not within enum values (A, B): C',
data: 'C',
path: '$.apis[0].operations[0].parameters[0].defaultValue'
},
{
code: 'INVALID_TYPE',
message: 'Invalid type (expected parseable number): NaN',
data: 'NaN',
path: '$.apis[0].operations[0].parameters[1].defaultValue'
},
{
code: 'INVALID_TYPE',
message: 'Invalid type (expected parseable number): NaN',
data: 'NaN',
path: '$.apis[0].operations[0].parameters[2].maximum'
},
{
code: 'MAXIMUM',
message: 'Default value is greater than maximum (1): 2',
data: '2',
path: '$.apis[0].operations[0].parameters[3].defaultValue'
},
{
code: 'INVALID_TYPE',
message: 'Invalid type (expected parseable number): NaN',
data: 'NaN',
path: '$.apis[0].operations[0].parameters[4].minimum'
},
{
code: 'MINIMUM',
message: 'Default value is less than minimum (2): 1',
data: '1',
path: '$.apis[0].operations[0].parameters[5].defaultValue'
},
{
code: 'INVALID_TYPE',
message: 'Invalid type (expected parseable boolean): NaN',
data: 'NaN',
path: '$.apis[0].operations[0].parameters[6].defaultValue'
}
];
assert.equal(result.errors.length, Object.keys(expectedErrors).length);
assert.equal(result.warnings.length, 0);
assert.deepEqual(result.errors, expectedErrors);
});
});
describe('#validateApi', function () {
it('should return errors for duplicate resource paths in resource listing JSON files', function () {
var result = spec.validateApi(invalidApiResourceListingJson, [
invalidApiResource1Json,
invalidApiResource2Json,
invalidApiResource3Json
]);
var errors = findAllErrorsOrWarnings('errors', 'DUPLICATE_RESOURCE_PATH', result);
assert.deepEqual(errors, [
{
code: 'DUPLICATE_RESOURCE_PATH',
message: 'Resource path already defined: /resource1',
data: '/resource1',
path: '$.apis[2].path'
}
]);
});
it('should return errors for defined but unused resource paths in resource listing JSON files', function () {
var result = spec.validateApi(invalidApiResourceListingJson, [
invalidApiResource1Json,
invalidApiResource2Json,
invalidApiResource3Json
]);
var errors = findAllErrorsOrWarnings('errors', 'UNUSED_RESOURCE', result);
assert.deepEqual(errors, [
{
code: 'UNUSED_RESOURCE',
message: 'Resource is defined but is not used: /resource2',
data: {
description: 'Operations about resource2',
path: '/resource2'
},
path: '$.apis[1]'
},
{
code: 'UNUSED_RESOURCE',
message: 'Resource is defined but is not used: /resource4',
data: {
description: 'Operations about resource4',
path: '/resource4'
},
path: '$.apis[3]'
}
]);
});
it('should return errors for defined but unused authorizations in resource listing JSON files', function () {
var result = spec.validateApi(invalidApiResourceListingJson, [
invalidApiResource1Json,
invalidApiResource2Json,
invalidApiResource3Json
]);
var errors = findAllErrorsOrWarnings('errors', 'UNUSED_AUTHORIZATION', result);
assert.deepEqual(errors, [
{
code: 'UNUSED_AUTHORIZATION',
message: 'Authorization is defined but is not used: unusedBasicAuth',
data: {
type: 'basicAuth'
},
path: '$.authorizations[\'unusedBasicAuth\']'
}
]);
});
it('should return errors for defined but unused authorization scopes in resource listing JSON files', function () {
var result = spec.validateApi(invalidApiResourceListingJson, [
invalidApiResource1Json,
invalidApiResource2Json,
invalidApiResource3Json
]);
var errors = findAllErrorsOrWarnings('errors', 'UNUSED_AUTHORIZATION_SCOPE', result);
assert.deepEqual(errors, [
{
code: 'UNUSED_AUTHORIZATION_SCOPE',
message: 'Authorization scope is defined but is not used: scope2',
data: {
description: 'Scope 2',
scope: 'scope2'
},
path: '$.authorizations[\'oauth2\'].scopes[1]'
}
]);
});
it('should return errors for missing authorization references in apiDeclaration JSON files', function () {
var result = spec.validateApi(invalidApiResourceListingJson, [
invalidApiResource1Json,
invalidApiResource2Json,
invalidApiResource3Json
]);
var errors = findAllErrorsOrWarnings('errors', 'UNRESOLVABLE_AUTHORIZATION_REFERENCE', result.resources);
assert.deepEqual(errors, [
{
code: 'UNRESOLVABLE_AUTHORIZATION_REFERENCE',
message: 'Authorization reference could not be resolved: missingAuth',
data: [],
path: '$.apis[0].operations[0].authorizations[\'missingAuth\']'
}
]);
});
it('should return errors for missing authorization scope reference in apiDeclaration JSON files', function () {
var result = spec.validateApi(invalidApiResourceListingJson, [
invalidApiResource1Json,
invalidApiResource2Json,
invalidApiResource3Json
]);
var errors = findAllErrorsOrWarnings('errors', 'UNRESOLVABLE_AUTHORIZATION_SCOPE_REFERENCE', result.resources);
assert.deepEqual(errors, [
{
code: 'UNRESOLVABLE_AUTHORIZATION_SCOPE_REFERENCE',
message: 'Authorization scope reference could not be resolved: missingScope',
data: 'missingScope',
path: '$.apis[1].operations[0].authorizations[\'oauth2\'].scopes[1]'
}
]);
});
it('should return errors for duplicate resource path in apiDeclaration JSON files', function () {
var result = spec.validateApi(invalidApiResourceListingJson, [
invalidApiResource1Json,
invalidApiResource2Json,
invalidApiResource3Json
]);
var errors = findAllErrorsOrWarnings('errors', 'DUPLICATE_RESOURCE_PATH', result.resources);
assert.deepEqual(errors, [
{
code: 'DUPLICATE_RESOURCE_PATH',
message: 'Resource path already defined: /resource1',
data: '/resource1',
path: '$.resourcePath'
}
]);
});
it('should return errors for missing resource listing for resource path in apiDeclaration JSON files', function () {
var result = spec.validateApi(invalidApiResourceListingJson, [
invalidApiResource1Json,
invalidApiResource2Json,
invalidApiResource3Json
]);
var errors = findAllErrorsOrWarnings('errors', 'UNRESOLVABLE_RESOURCEPATH_REFERENCE', result.resources);
assert.deepEqual(errors, [
{
code: 'UNRESOLVABLE_RESOURCEPATH_REFERENCE',
message: 'Resource defined but not declared in resource listing: /resource3',
data: '/resource3',
path: '$.resourcePath'
}
]);
});
it('should return warning for Swagger version mismatch in apiDeclaration JSON files', function () {
var result = spec.validateApi(invalidApiResourceListingJson, [
invalidApiResource1Json,
invalidApiResource2Json,
invalidApiResource3Json
]);
var warnings = findAllErrorsOrWarnings('warnings', 'SWAGGER_VERSION_MISMATCH', result.resources);
assert.deepEqual(warnings, [
{
code: 'SWAGGER_VERSION_MISMATCH',
message: 'Swagger version differs from resource listing (1.2): 1.1',
data: '1.1',
path: '$.swaggerVersion'
}
]);
});
});
});
// TODO: Add test for calling 'validate' with invalid schema name