forked from apiflask/apiflask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_route.py
214 lines (161 loc) · 5.41 KB
/
test_route.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
import pytest
from flask.views import MethodView
from openapi_spec_validator import validate_spec
from .schemas import Foo
from apiflask import APIBlueprint
from apiflask import HTTPTokenAuth
@pytest.mark.parametrize('method', ['get', 'post', 'put', 'patch', 'delete'])
def test_route_shortcuts(app, client, method):
route_method = getattr(app, method)
client_method = getattr(client, method)
@route_method('/pet')
@app.output(Foo)
def test_shortcuts():
return {'name': method}
assert client_method('/pet').json['name'] == method
rv = client.get('/openapi.json')
assert rv.status_code == 200
validate_spec(rv.json)
assert rv.json['paths']['/pet'][method]
def test_route_on_method_view(app, client):
@app.route('/')
class Foo(MethodView):
def get(self):
return 'get'
def post(self):
return 'post'
def delete(self):
return 'delete'
def put(self):
return 'put'
def patch(self):
return 'patch'
rv = client.get('/')
assert rv.data == b'get'
rv = client.post('/')
assert rv.data == b'post'
rv = client.delete('/')
assert rv.data == b'delete'
rv = client.put('/')
assert rv.data == b'put'
rv = client.patch('/')
assert rv.data == b'patch'
def test_blueprint_route_on_method_view(app, client):
bp = APIBlueprint('test', __name__, tag='foo')
@bp.route('/')
class Foo(MethodView):
def get(self):
return 'get'
def post(self):
return 'post'
def delete(self):
return 'delete'
def put(self):
return 'put'
def patch(self):
return 'patch'
app.register_blueprint(bp)
rv = client.get('/')
assert rv.data == b'get'
rv = client.post('/')
assert rv.data == b'post'
rv = client.delete('/')
assert rv.data == b'delete'
rv = client.put('/')
assert rv.data == b'put'
rv = client.patch('/')
assert rv.data == b'patch'
def test_bad_route_decorator_usages(app):
with pytest.raises(RuntimeError):
@app.get('/foo', methods=['GET'])
def foo(self):
pass
with pytest.raises(RuntimeError):
@app.get('/baz')
class Baz(MethodView):
def get(self):
pass
def test_class_attribute_decorators(app, client):
auth = HTTPTokenAuth()
@app.route('/')
class Foo(MethodView):
decorators = [app.auth_required(auth), app.doc(responses=[404])]
def get(self):
pass
def post(self):
pass
rv = client.get('/')
assert rv.status_code == 401
rv = client.post('/')
assert rv.status_code == 401
rv = client.get('/openapi.json')
assert rv.status_code == 200
validate_spec(rv.json)
assert '404' in rv.json['paths']['/']['get']['responses']
assert '404' in rv.json['paths']['/']['post']['responses']
assert 'BearerAuth' in rv.json['paths']['/']['get']['security'][0]
assert 'BearerAuth' in rv.json['paths']['/']['post']['security'][0]
def test_overwrite_class_attribute_decorators(app, client):
@app.route('/')
class Foo(MethodView):
decorators = [app.doc(deprecated=True, tags=['foo'])]
def get(self):
pass
@app.doc(deprecated=False)
def post(self):
pass
@app.doc(tags=['bar'])
def delete(self):
pass
rv = client.get('/openapi.json')
assert rv.status_code == 200
validate_spec(rv.json)
assert rv.json['paths']['/']['get']['deprecated']
assert rv.json['paths']['/']['get']['tags'] == ['foo']
assert rv.json['paths']['/']['post']['tags'] == ['foo']
assert rv.json['paths']['/']['delete']['tags'] == ['bar']
assert 'deprecated' not in rv.json['paths']['/']['post']
def test_add_url_rule_with_method_view(app, client):
class Foo(MethodView):
def get(self):
return 'get'
def post(self):
"""Create foo"""
return 'post'
app.add_url_rule('/', view_func=Foo, methods=['GET', 'POST'])
rv = client.get('/')
assert rv.data == b'get'
rv = client.post('/')
assert rv.data == b'post'
rv = client.get('/openapi.json')
assert rv.status_code == 200
validate_spec(rv.json)
assert rv.json['paths']['/']['get']['summary'] == 'Get Foo'
assert rv.json['paths']['/']['post']['summary'] == 'Create foo'
def test_add_url_rule_with_method_view_as_view(app, client):
class Foo(MethodView):
def get(self):
return 'get'
def post(self):
"""Create foo"""
return 'post'
foo_view = Foo.as_view('foo')
app.add_url_rule('/foo/get', view_func=foo_view, methods=['GET'])
app.add_url_rule('/foo/post', view_func=foo_view, methods=['POST'])
rv = client.get('/foo/get')
assert rv.data == b'get'
rv = client.post('/foo/post')
assert rv.data == b'post'
rv = client.get('/openapi.json')
assert rv.status_code == 200
validate_spec(rv.json)
assert rv.json['paths']['/foo/get']['get']['summary'] == 'Get Foo'
assert rv.json['paths']['/foo/post']['post']['summary'] == 'Create foo'
def test_view_endpoint_contains_dot(app, client):
@app.route('/', endpoint='hello.world')
def foo():
pass
rv = client.get('/openapi.json')
assert rv.status_code == 200
validate_spec(rv.json)
assert rv.json['paths']['/']['get']