-
-
Notifications
You must be signed in to change notification settings - Fork 753
/
__init__.py
608 lines (367 loc) · 11.3 KB
/
__init__.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
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
#!/usr/bin/env python3
import datetime
import uuid
from flask import jsonify, redirect
from connexion import NoContent, ProblemException, context, request
class DummyClass(object):
@classmethod
def test_classmethod(cls):
return cls.__name__
def test_method(self):
return self.__class__.__name__
class_instance = DummyClass() # noqa
def get():
return ''
def search():
return ''
def api_list():
return 'a'
def post():
return ''
def post_greeting(name, **kwargs):
data = {'greeting': 'Hello {name}'.format(name=name)}
return data
def post_greeting3(body, **kwargs):
data = {'greeting': 'Hello {name}'.format(name=body["name"])}
return data
def post_greeting_url(name, remainder, **kwargs):
data = {'greeting': 'Hello {name} thanks for {remainder}'.format(name=name,remainder=remainder)}
return data
def post_goodday(name):
data = {'greeting': 'Hello {name}'.format(name=name)}
headers = {"Location": "/my/uri"}
return data, 201, headers
def post_goodday_no_header():
return {'greeting': 'Hello.'}, 201
def post_goodevening(name):
data = 'Good evening {name}'.format(name=name)
headers = {"Location": "/my/uri"}
return data, 201, headers
def get_list(name):
data = ['hello', name]
return data
def get_bye(name):
return 'Goodbye {name}'.format(name=name)
def get_flask_response_tuple():
return jsonify({'foo': 'bar'}), 201
def get_bye_secure(name, user, token_info):
return 'Goodbye {name} (Secure: {user})'.format(name=name, user=user)
def get_bye_secure_from_flask():
return 'Goodbye {user} (Secure!)'.format(user=context['user'])
def get_bye_secure_from_connexion(req_context):
return 'Goodbye {user} (Secure!)'.format(user=req_context['user'])
def get_bye_secure_ignoring_context(name):
return 'Goodbye {name} (Secure!)'.format(name=name)
def get_bye_secure_jwt(name, user, token_info):
return 'Goodbye {name} (Secure: {user})'.format(name=name, user=user)
def with_problem():
raise ProblemException(type='http://www.example.com/error',
title='Some Error',
detail='Something went wrong somewhere',
status=418,
instance='instance1',
headers={'x-Test-Header': 'In Test'})
def with_problem_txt():
raise ProblemException(title='Some Error',
detail='Something went wrong somewhere',
status=418,
instance='instance1')
def internal_error():
return 42 / 0
def get_greetings(name):
"""
Used to test custom mimetypes
"""
data = {'greetings': 'Hello {name}'.format(name=name)}
return data
def multimime():
return 'Goodbye'
def empty():
return None, 204
def schema(new_stack):
return new_stack
def forward(body):
"""Return a response with the same payload as in the request body."""
return body
def schema_response_object(valid):
if valid == "invalid_requirements":
return {"docker_version": 1.0}
elif valid == "invalid_type":
return {"image_version": 1.0}
else:
return {"image_version": "1.0"} # valid
def schema_response_string(valid):
if valid == "valid":
return "Image version 2.0"
else:
return 2.0
def schema_response_integer(valid):
if valid == "valid":
return 3
else:
return 3.0
def schema_response_number(valid):
if valid == "valid":
return 4.0
else:
return "Four"
def schema_response_boolean(valid):
if valid == "valid":
return True
else:
return "yes"
def schema_response_array(valid):
if valid == "invalid_dict":
return {
{"image_version": "1.0"}:
{"image_version": "2.0"}
}
elif valid == "invalid_string":
return "Not an array."
else:
return [
{"image_version": "1.0"},
{"image_version": "2.0"}
]
def schema_query(image_version=None):
return {'image_version': image_version}
def schema_list():
return ''
def schema_map():
return ''
def schema_recursive():
return ''
def schema_format():
return ''
def test_parameter_validation():
return ''
def test_required_query_param():
return ''
def test_apikey_query_parameter_validation():
return ''
def test_array_csv_query_param(items):
return items
def test_array_pipes_form_param3(items):
return items['items']
def test_array_csv_form_param3(items):
return items['items']
def test_array_pipes_form_param(items):
return items
def test_array_csv_form_param(items):
return items
def test_array_multi_query_param(items):
return items
def test_array_pipes_query_param(items):
return items
def test_array_unsupported_query_param(items):
return items
def test_no_content_response():
return NoContent, 204
def test_schema_array(test_array):
return test_array
def test_schema_int(test_int):
return test_int
def test_get_someint(someint):
return type(someint).__name__
def test_get_somefloat(somefloat):
return type(somefloat).__name__
def test_default_param(name):
return {"app_name": name}
def test_default_object_body(stack):
return {"stack": stack}
def test_nested_additional_properties(body):
return body
def test_default_integer_body(stack_version):
return stack_version
def test_empty_object_body(stack):
return {"stack": stack}
def test_falsy_param(falsy):
return falsy
def test_formdata_param3(body):
return body["formData"]
def test_formdata_param(formData):
return formData
def test_formdata_missing_param():
return ''
def test_formdata_file_upload(formData, **kwargs):
filename = formData.filename
contents = formData.read().decode('utf-8', 'replace')
return {filename: contents}
def test_formdata_file_upload_missing_param():
return ''
def test_bool_default_param(thruthiness):
return thruthiness
def test_bool_array_param(thruthiness=None):
if thruthiness is None:
thruthiness = []
return all(thruthiness)
def test_required_param(simple):
return simple
def test_exploded_deep_object_param(id):
return id
def test_nested_exploded_deep_object_param(id):
return id
def test_exploded_deep_object_param_additional_properties(id):
return id
def test_redirect_endpoint():
headers = {'Location': 'http://www.google.com/'}
return '', 302, headers
def test_redirect_response_endpoint():
return redirect('http://www.google.com/')
def test_204_with_headers():
headers = {'X-Something': 'test'}
return '', 204, headers
def test_nocontent_obj_with_headers():
headers = {'X-Something': 'test'}
return NoContent, 204, headers
def path_parameters_in_get_method(title):
return [title], 200, {}
def test_default_missmatch_definition(age):
return 'OK'
def test_array_in_path(names):
return names, 200
def test_global_response_definition():
return ['general', 'list'], 200
def test_nullable_parameters(time_start):
if time_start is None:
return 'it was None'
return time_start
def test_nullable_param_post(post_param):
if post_param is None:
return 'it was None'
return post_param
def test_nullable_param_post3(body):
if body is None:
return 'it was None'
if body["post_param"] is None:
return 'it was None'
return body["post_param"]
def test_nullable_param_put(contents):
if contents is None:
return 'it was None'
return contents
def test_custom_json_response():
return {'theResult': DummyClass()}, 200
def get_blob_data():
return b'cool\x00\x08'
def get_data_as_binary():
return get_blob_data(), 200, {'Content-Type': 'application/octet-stream'}
def get_data_as_text(post_param):
return ''
def get_invalid_response():
return {"simple": object()}
def get_empty_dict():
return {}
def get_custom_problem_response():
raise ProblemException(403, "You need to pay", "Missing amount",
ext={'amount': 23.0})
def throw_problem_exception():
raise ProblemException(
title="As Exception",
detail="Something wrong or not!",
ext={'age': 30}
)
def unordered_params_response(first, path_param, second):
return dict(first=int(first), path_param=str(path_param), second=int(second))
def more_than_one_scope_defined(**kwargs):
return "OK"
def optional_auth(**kwargs):
key = apikey_info(request.headers.get('X-AUTH'))
if key is None:
return "Unauthenticated"
else:
return "Authenticated"
def test_args_kwargs(*args, **kwargs):
return kwargs
def test_param_sanitization(query=None, form=None):
result = {}
if query:
result['query'] = query
if form:
result['form'] = form
return result
def test_param_sanitization3(query=None, body=None):
result = {}
if query:
result['query'] = query
if body:
result['form'] = body["form"]
return result
def test_body_sanitization(body=None):
return body
def test_body_sanitization_additional_properties(body):
return body
def test_body_sanitization_additional_properties_defined(body):
return body
def test_body_not_allowed_additional_properties(body):
return body
def post_wrong_content_type():
return "NOT OK"
def get_unicode_query(price=None):
return {'price': price}
def get_unicode_data():
jsonResponse = {u'currency': u'\xa3', u'key': u'leena'}
return jsonResponse
def get_enum_response():
try:
from enum import Enum
class HTTPStatus(Enum):
OK = 200
except ImportError:
return {}, 200
else:
return {}, HTTPStatus.OK
def get_httpstatus_response():
try:
from http import HTTPStatus
except ImportError:
return {}, 200
else:
return {}, HTTPStatus.OK
def get_bad_default_response(response_code):
return {}, response_code
def get_user():
return {'user_id': 7, 'name': 'max'}
def get_user_with_password():
return {'user_id': 7, 'name': 'max', 'password': '5678'}
def post_user(body):
body['user_id'] = 8
body.pop('password', None)
return body
def post_multipart_form(body):
x = body['x']
x['name'] += "-reply"
x['age'] += 10
return x
def apikey_info(apikey, required_scopes=None):
if apikey == 'mykey':
return {'sub': 'admin'}
return None
def jwt_info(token):
if token == '100':
return {'sub': '100'}
return None
def get_add_operation_on_http_methods_only():
return ""
def put_add_operation_on_http_methods_only():
return ""
def post_add_operation_on_http_methods_only():
return ""
def delete_add_operation_on_http_methods_only():
return ""
def options_add_operation_on_http_methods_only():
return ""
def head_add_operation_on_http_methods_only():
return ""
def patch_add_operation_on_http_methods_only():
return ""
def trace_add_operation_on_http_methods_only():
return ""
def get_datetime():
return {'value': datetime.datetime(2000, 1, 2, 3, 4, 5, 6)}
def get_date():
return {'value': datetime.date(2000, 1, 2)}
def get_uuid():
return {'value': uuid.UUID(hex='e7ff66d0-3ec2-4c4e-bed0-6e4723c24c51')}
def test_optional_headers():
return {}, 200