forked from Cornices/cornice.ext.swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_swagger.py
295 lines (221 loc) · 9.97 KB
/
test_swagger.py
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
import unittest
from cornice.validators import colander_validator, colander_body_validator
from cornice.service import Service
from flex.core import validate
from cornice_swagger.swagger import CorniceSwagger, CorniceSwaggerException
from .support import GetRequestSchema, PutRequestSchema, response_schemas, BodySchema, HeaderSchema
class TestCorniceSwaggerGenerator(unittest.TestCase):
def setUp(self):
service = Service("IceCream", "/icecream/{flavour}")
class IceCream(object):
@service.get(validators=(colander_validator, ),
schema=GetRequestSchema(),
response_schemas=response_schemas)
def view_get(self, request):
"""Serve ice cream"""
return self.request.validated
@service.put(validators=(colander_validator, ), schema=PutRequestSchema())
def view_put(self, request):
"""Add flavour"""
return self.request.validated
self.service = service
self.swagger = CorniceSwagger([self.service])
self.spec = self.swagger('IceCreamAPI', '4.2')
validate(self.spec)
def test_path(self):
self.assertIn('/icecream/{flavour}', self.spec['paths'])
def test_path_methods(self):
path = self.spec['paths']['/icecream/{flavour}']
self.assertIn('get', path)
self.assertIn('put', path)
def test_path_parameters(self):
parameters = self.spec['paths']['/icecream/{flavour}']['parameters']
self.assertEquals(len(parameters), 1)
self.assertEquals(parameters[0]['name'], 'flavour')
def test_summary_docstrings(self):
self.spec = self.swagger('IceCreamAPI', '4.2', summary_docstrings=True)
validate(self.spec)
summary = self.spec['paths']['/icecream/{flavour}']['get']['summary']
self.assertEquals(summary, 'Serve ice cream')
def test_with_schema_ref(self):
swagger = CorniceSwagger([self.service], def_ref_depth=1)
spec = swagger('IceCreamAPI', '4.2')
self.assertIn('definitions', spec)
def test_with_param_ref(self):
swagger = CorniceSwagger([self.service], param_ref=True)
spec = swagger('IceCreamAPI', '4.2')
self.assertIn('parameters', spec)
def test_with_resp_ref(self):
swagger = CorniceSwagger([self.service], resp_ref=True)
spec = swagger('IceCreamAPI', '4.2')
self.assertIn('responses', spec)
class TestExtractContentTypes(unittest.TestCase):
def test_json_renderer(self):
service = Service("IceCream", "/icecream/{flavour}")
class IceCream(object):
@service.get(renderer='json')
def view_get(self, request):
return self.request.validated
swagger = CorniceSwagger([service])
spec = swagger('IceCreamAPI', '4.2')
self.assertEquals(spec['paths']['/icecream/{flavour}']['get']['produces'],
['application/json'])
def test_xml_renderer(self):
service = Service("IceCream", "/icecream/{flavour}")
class IceCream(object):
@service.get(renderer='xml')
def view_get(self, request):
return self.request.validated
swagger = CorniceSwagger([service])
spec = swagger('IceCreamAPI', '4.2')
self.assertEquals(spec['paths']['/icecream/{flavour}']['get']['produces'],
['text/xml'])
def test_unkown_renderer(self):
service = Service("IceCream", "/icecream/{flavour}")
class IceCream(object):
@service.get(renderer='')
def view_get(self, request):
return self.request.validated
swagger = CorniceSwagger([service])
spec = swagger('IceCreamAPI', '4.2')
self.assertNotIn('produces', spec['paths']['/icecream/{flavour}']['get'])
def test_single_ctype(self):
service = Service("IceCream", "/icecream/{flavour}")
class IceCream(object):
@service.put(content_type='application/json')
def view_put(self, request):
return self.request.validated
swagger = CorniceSwagger([service])
spec = swagger('IceCreamAPI', '4.2')
self.assertEquals(spec['paths']['/icecream/{flavour}']['put']['consumes'],
['application/json'])
def test_multiple_ctypes(self):
service = Service("IceCream", "/icecream/{flavour}")
class IceCream(object):
@service.put(content_type=('application/json', 'text/xml'))
def view_put(self, request):
return self.request.validated
swagger = CorniceSwagger([service])
spec = swagger('IceCreamAPI', '4.2')
self.assertEquals(spec['paths']['/icecream/{flavour}']['put']['consumes'],
['application/json', 'text/xml'])
def test_multiple_views_with_different_ctypes(self):
service = Service("IceCream", "/icecream/{flavour}")
class IceCream(object):
def view_put(self, request):
return "red"
service.add_view(
"put",
IceCream.view_put,
validators=(colander_validator, ),
schema=PutRequestSchema(),
content_type='application/json',
)
service.add_view(
"put",
IceCream.view_put,
validators=(colander_validator, ),
schema=PutRequestSchema(),
content_type='text/xml',
)
swagger = CorniceSwagger([service])
spec = swagger('IceCreamAPI', '4.2')
self.assertEquals(sorted(spec['paths']['/icecream/{flavour}']['put']['consumes']),
['application/json', 'text/xml'])
class TestExtractTransformSchema(unittest.TestCase):
def setUp(self):
self.service = Service("IceCream", "/icecream/{flavour}")
def test_colander_body_validator_to_full_schema(self):
swagger = CorniceSwagger([self.service])
service_args = dict(
schema=BodySchema(),
validators=(colander_body_validator,)
)
full_schema = swagger._extract_transform_schema(service_args)
# ensure schema is cloned
self.assertNotEqual(service_args['schema'], full_schema['body'])
# ensure schema is transformed
self.assertEqual(service_args['schema'].typ, full_schema['body'].typ)
self.assertEqual(len(service_args['schema'].children), len(full_schema['body'].children))
def test_schema_transform(self):
swagger = CorniceSwagger([self.service])
header_schema = HeaderSchema()
service_args = dict(
schema=GetRequestSchema()
)
def add_headers_transform(schema, args):
schema['header'] = header_schema
return schema
swagger.schema_transformers.append(add_headers_transform)
full_schema = swagger._extract_transform_schema(service_args)
self.assertEqual(header_schema, full_schema['header'])
# ensure service schema is left untouched
self.assertNotIn('header', service_args['schema'])
class TestExtractTags(unittest.TestCase):
def test_user_defined_tags(self):
service = Service("IceCream", "/icecream/{flavour}")
class IceCream(object):
@service.get(tags=['cold', 'foo'])
def view_get(self, request):
return service
swagger = CorniceSwagger([service])
spec = swagger('IceCreamAPI', '4.2')
validate(spec)
tags = spec['paths']['/icecream/{flavour}']['get']['tags']
self.assertEquals(tags, ['cold', 'foo'])
def test_listed_default_tags(self):
service = Service("IceCream", "/icecream/{flavour}")
class IceCream(object):
@service.get()
def view_get(self, request):
return service
swagger = CorniceSwagger([service])
spec = swagger('IceCreamAPI', '4.2',
default_tags=['cold'])
validate(spec)
tags = spec['paths']['/icecream/{flavour}']['get']['tags']
self.assertEquals(tags, ['cold'])
def test_callable_default_tags(self):
service = Service("IceCream", "/icecream/{flavour}")
class IceCream(object):
@service.get()
def view_get(self, request):
return service
def default_tag_callable(service):
return ['cold']
swagger = CorniceSwagger([service])
spec = swagger('IceCreamAPI', '4.2',
default_tags=default_tag_callable)
validate(spec)
tags = spec['paths']['/icecream/{flavour}']['get']['tags']
self.assertEquals(tags, ['cold'])
def test_invalid_tag_raises_exception(self):
service = Service("IceCream", "/icecream/{flavour}")
class IceCream(object):
@service.get(tags='cold')
def view_get(self, request):
return service
swagger = CorniceSwagger([service])
self.assertRaises(CorniceSwaggerException,
swagger, 'IceCreamAPI', '4.2')
class NotInstantiatedSchemaTest(unittest.TestCase):
def test_not_instantiated(self):
service = Service("IceCream", "/icecream/{flavour}")
class IceCream(object):
"""
Ice cream service
"""
# Use GetRequestSchema and ResponseSchemas classes instead of objects
@service.get(validators=(colander_validator, ),
schema=GetRequestSchema)
def view_get(self, request):
"""Serve icecream"""
return self.request.validated
@service.put(validators=(colander_validator, ), schema=PutRequestSchema())
def view_put(self, request):
"""Add flavour"""
return self.request.validated
self.service = service
self.swagger = CorniceSwagger([self.service])
self.spec = self.swagger('IceCreamAPI', '4.2')
validate(self.spec)