forked from flask-restful/flask-restful
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_api_with_blueprint.py
192 lines (168 loc) · 7.8 KB
/
test_api_with_blueprint.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
import unittest
from flask import Flask, Blueprint, request
try:
from mock import Mock
except:
# python3
from unittest.mock import Mock
import flask
import flask_restful
import flask_restful.fields
#noinspection PyUnresolvedReferences
from nose.tools import assert_true, assert_false # you need it for tests in form of continuations
# Add a dummy Resource to verify that the app is properly set.
class HelloWorld(flask_restful.Resource):
def get(self):
return {}
class GoodbyeWorld(flask_restful.Resource):
def __init__(self, err):
self.err = err
def get(self):
flask.abort(self.err)
class APIWithBlueprintTestCase(unittest.TestCase):
def test_api_base(self):
blueprint = Blueprint('test', __name__)
api = flask_restful.Api(blueprint)
app = Flask(__name__)
app.register_blueprint(blueprint)
self.assertEquals(api.urls, {})
self.assertEquals(api.prefix, '')
self.assertEquals(api.default_mediatype, 'application/json')
def test_api_delayed_initialization(self):
blueprint = Blueprint('test', __name__)
api = flask_restful.Api()
api.init_app(blueprint)
app = Flask(__name__)
app.register_blueprint(blueprint)
api.add_resource(HelloWorld, '/', endpoint="hello")
def test_add_resource_endpoint(self):
blueprint = Blueprint('test', __name__)
api = flask_restful.Api(blueprint)
view = Mock(**{'as_view.return_value': Mock(__name__='test_view')})
api.add_resource(view, '/foo', endpoint='bar')
app = Flask(__name__)
app.register_blueprint(blueprint)
view.as_view.assert_called_with('bar')
def test_add_resource_endpoint_after_registration(self):
blueprint = Blueprint('test', __name__)
api = flask_restful.Api(blueprint)
app = Flask(__name__)
app.register_blueprint(blueprint)
view = Mock(**{'as_view.return_value': Mock(__name__='test_view')})
api.add_resource(view, '/foo', endpoint='bar')
view.as_view.assert_called_with('bar')
def test_url_with_api_prefix(self):
blueprint = Blueprint('test', __name__)
api = flask_restful.Api(blueprint, prefix='/api')
api.add_resource(HelloWorld, '/hi', endpoint='hello')
app = Flask(__name__)
app.register_blueprint(blueprint)
with app.test_request_context('/api/hi'):
self.assertEquals(request.endpoint, 'test.hello')
def test_url_with_blueprint_prefix(self):
blueprint = Blueprint('test', __name__, url_prefix='/bp')
api = flask_restful.Api(blueprint)
api.add_resource(HelloWorld, '/hi', endpoint='hello')
app = Flask(__name__)
app.register_blueprint(blueprint)
with app.test_request_context('/bp/hi'):
self.assertEquals(request.endpoint, 'test.hello')
def test_url_with_registration_prefix(self):
blueprint = Blueprint('test', __name__)
api = flask_restful.Api(blueprint)
api.add_resource(HelloWorld, '/hi', endpoint='hello')
app = Flask(__name__)
app.register_blueprint(blueprint, url_prefix='/reg')
with app.test_request_context('/reg/hi'):
self.assertEquals(request.endpoint, 'test.hello')
def test_registration_prefix_overrides_blueprint_prefix(self):
blueprint = Blueprint('test', __name__, url_prefix='/bp')
api = flask_restful.Api(blueprint)
api.add_resource(HelloWorld, '/hi', endpoint='hello')
app = Flask(__name__)
app.register_blueprint(blueprint, url_prefix='/reg')
with app.test_request_context('/reg/hi'):
self.assertEquals(request.endpoint, 'test.hello')
def test_url_with_api_and_blueprint_prefix(self):
blueprint = Blueprint('test', __name__, url_prefix='/bp')
api = flask_restful.Api(blueprint, prefix='/api')
api.add_resource(HelloWorld, '/hi', endpoint='hello')
app = Flask(__name__)
app.register_blueprint(blueprint)
with app.test_request_context('/bp/api/hi'):
self.assertEquals(request.endpoint, 'test.hello')
def test_url_part_order_aeb(self):
blueprint = Blueprint('test', __name__, url_prefix='/bp')
api = flask_restful.Api(blueprint, prefix='/api', url_part_order='aeb')
api.add_resource(HelloWorld, '/hi', endpoint='hello')
app = Flask(__name__)
app.register_blueprint(blueprint)
with app.test_request_context('/api/hi/bp'):
self.assertEquals(request.endpoint, 'test.hello')
def test_error_routing(self):
blueprint = Blueprint('test', __name__)
api = flask_restful.Api(blueprint)
api.add_resource(HelloWorld(), '/hi', endpoint="hello")
api.add_resource(GoodbyeWorld(404), '/bye', endpoint="bye")
app = Flask(__name__)
app.register_blueprint(blueprint)
with app.test_request_context('/hi', method='POST'):
assert_true(api._should_use_fr_error_handler())
assert_true(api._has_fr_route())
with app.test_request_context('/bye'):
api._should_use_fr_error_handler = Mock(return_value=False)
assert_true(api._has_fr_route())
def test_non_blueprint_rest_error_routing(self):
blueprint = Blueprint('test', __name__)
api = flask_restful.Api(blueprint)
api.add_resource(HelloWorld(), '/hi', endpoint="hello")
api.add_resource(GoodbyeWorld(404), '/bye', endpoint="bye")
app = Flask(__name__)
app.register_blueprint(blueprint, url_prefix='/blueprint')
api2 = flask_restful.Api(app)
api2.add_resource(HelloWorld(), '/hi', endpoint="hello")
api2.add_resource(GoodbyeWorld(404), '/bye', endpoint="bye")
with app.test_request_context('/hi', method='POST'):
assert_false(api._should_use_fr_error_handler())
assert_true(api2._should_use_fr_error_handler())
assert_false(api._has_fr_route())
assert_true(api2._has_fr_route())
with app.test_request_context('/blueprint/hi', method='POST'):
assert_true(api._should_use_fr_error_handler())
assert_false(api2._should_use_fr_error_handler())
assert_true(api._has_fr_route())
assert_false(api2._has_fr_route())
api._should_use_fr_error_handler = Mock(return_value=False)
api2._should_use_fr_error_handler = Mock(return_value=False)
with app.test_request_context('/bye'):
assert_false(api._has_fr_route())
assert_true(api2._has_fr_route())
with app.test_request_context('/blueprint/bye'):
assert_true(api._has_fr_route())
assert_false(api2._has_fr_route())
def test_non_blueprint_non_rest_error_routing(self):
blueprint = Blueprint('test', __name__)
api = flask_restful.Api(blueprint)
api.add_resource(HelloWorld(), '/hi', endpoint="hello")
api.add_resource(GoodbyeWorld(404), '/bye', endpoint="bye")
app = Flask(__name__)
app.register_blueprint(blueprint, url_prefix='/blueprint')
@app.route('/hi')
def hi():
return 'hi'
@app.route('/bye')
def bye():
flask.abort(404)
with app.test_request_context('/hi', method='POST'):
assert_false(api._should_use_fr_error_handler())
assert_false(api._has_fr_route())
with app.test_request_context('/blueprint/hi', method='POST'):
assert_true(api._should_use_fr_error_handler())
assert_true(api._has_fr_route())
api._should_use_fr_error_handler = Mock(return_value=False)
with app.test_request_context('/bye'):
assert_false(api._has_fr_route())
with app.test_request_context('/blueprint/bye'):
assert_true(api._has_fr_route())
if __name__ == '__main__':
unittest.main()