forked from apiflask/apiflask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_blueprint.py
89 lines (67 loc) · 2.09 KB
/
test_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
from flask.views import MethodView
from openapi_spec_validator import validate_spec
from apiflask import APIBlueprint
from apiflask.security import HTTPBasicAuth
from apiflask.security import HTTPTokenAuth
def test_blueprint_object():
bp = APIBlueprint('test', __name__)
assert bp.name == 'test'
assert hasattr(bp, 'tag')
assert bp.tag is None
def test_blueprint_tag():
bp = APIBlueprint('test', __name__, tag='foo')
assert bp.name == 'test'
assert bp.tag == 'foo'
def test_blueprint_enable_openapi(app, client):
auth = HTTPBasicAuth()
@app.get('/hello')
@app.auth_required(auth)
def hello():
pass
bp = APIBlueprint('foo', __name__, tag='test', enable_openapi=False)
auth = HTTPTokenAuth()
@bp.before_request
@bp.auth_required(auth)
def before():
pass
@bp.get('/foo')
def foo():
pass
app.register_blueprint(bp)
rv = client.get('/foo')
assert rv.status_code == 401
rv = client.get('/openapi.json')
assert rv.status_code == 200
validate_spec(rv.json)
assert rv.json['tags'] == []
assert '/hello' in rv.json['paths']
assert '/foo' not in rv.json['paths']
assert 'BearerAuth' not in rv.json['components']['securitySchemes']
def test_blueprint_enable_openapi_with_methodview(app, client):
auth = HTTPBasicAuth()
@app.get('/hello')
@app.auth_required(auth)
def hello():
pass
bp = APIBlueprint('foo', __name__, tag='test', enable_openapi=False)
auth = HTTPTokenAuth()
@bp.before_request
@bp.auth_required(auth)
def before():
pass
@bp.route('/foo')
class Foo(MethodView):
def get(self):
pass
def post(self):
pass
app.register_blueprint(bp)
rv = client.get('/foo')
assert rv.status_code == 401
rv = client.get('/openapi.json')
assert rv.status_code == 200
validate_spec(rv.json)
assert rv.json['tags'] == []
assert '/hello' in rv.json['paths']
assert '/foo' not in rv.json['paths']
assert 'BearerAuth' not in rv.json['components']['securitySchemes']