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
167 lines (146 loc) · 5.08 KB
/
Copy pathtest-v1_2.js
File metadata and controls
167 lines (146 loc) · 5.08 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
/* 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 = {};
// 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('swagger-tools v1.2 Specification', function () {
describe('metadata', function () {
it('should have proper docsUrl, options, schemasUrl and verison properties', function () {
assert.deepEqual(spec.options, {
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.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 false for 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), petErrors);
assert.deepEqual(spec.validate(rlJson, 'resourceListing.json'), rlErrors);
assert.deepEqual(spec.validate(storeJson), storeErrors);
assert.deepEqual(spec.validate(userJson), userErrors);
});
});
});