11from flask import Flask , Blueprint
2- from flask_jwt_extended import JWTManager , jwt_required , create_access_token
2+ from flask_jwt_extended import (
3+ JWTManager , jwt_required , fresh_jwt_required , jwt_refresh_token_required
4+ )
35import pytest
46
57@pytest .fixture (scope = 'function' )
@@ -8,27 +10,42 @@ def app():
810 app .config ['JWT_SECRET_KEY' ] = 'secret'
911 JWTManager (app )
1012
11- protected_bp = Blueprint ('protected' , __name__ )
12-
13- # This protects the entire blueprint,
14- # Also the OPTIONS method
15- @protected_bp .before_request
13+ @app .route ('/jwt_required' , methods = ["GET" , "OPTIONS" ])
1614 @jwt_required
17- def protect ():
18- pass
15+ def jwt_required_endpoint ():
16+ return b'ok'
17+
18+ @app .route ('/fresh_jwt_required' , methods = ["GET" , "OPTIONS" ])
19+ @fresh_jwt_required
20+ def fresh_jwt_required_endpoint ():
21+ return b'ok'
22+
23+ @app .route ('/jwt_refresh_token_required' , methods = ["GET" , "OPTIONS" ])
24+ @jwt_refresh_token_required
25+ def jwt_refresh_token_required_endpoint ():
26+ return b'ok'
27+
1928
20- @protected_bp .route ('/protected' , methods = ["GET" ])
21- @jwt_required
22- def protected ():
23- return 'ok'
2429
25- app .register_blueprint (protected_bp )
2630 return app
2731
28- def test_access_protected_enpoint (app ):
29- client = app .test_client ()
30- assert client .get ('/protected' ).status_code == 401 # ok
32+ def test_access_jwt_required_enpoint (app ):
33+ # Test the options method shoud not be
34+ # affected by jwt required
35+ res = app .test_client ().options ('/jwt_required' )
36+ assert res .status_code == 200
37+ assert res .data == b'ok'
38+
39+ def test_access_jwt_refresh_token_required_enpoint (app ):
40+ # Test the options method shoud not be
41+ # affected by jwt required
42+ res = app .test_client ().options ('/jwt_refresh_token_required' )
43+ assert res .status_code == 200
44+ assert res .data == b'ok'
3145
32- def test_access_protected_enpoint_options (app ):
33- client = app .test_client ()
34- assert client .options ('/protected' ).status_code == 200 # test fails
46+ def test_access_fresh_jwt_required_enpoint (app ):
47+ # Test the options method shoud not be
48+ # affected by jwt required
49+ res = app .test_client ().options ('/fresh_jwt_required' )
50+ assert res .status_code == 200
51+ assert res .data == b'ok'
0 commit comments