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
|
import json
import pytest
from flask import Flask, jsonify
from flask_jwt_simple import JWTManager
@pytest.fixture(scope='function')
def app():
app = Flask(__name__)
return app
def _parse_callback(result):
response = result[0]
status_code = result[1]
data = json.loads(response.get_data(as_text=True))
return status_code, data
def test_manual_init_app(app):
jwt_manager = JWTManager()
jwt_manager.init_app(app)
assert jwt_manager == app.extensions['flask-jwt-simple']
def test_class_init_app(app):
jwt_manager = JWTManager(app)
assert jwt_manager == app.extensions['flask-jwt-simple']
def test_default_expired_token_callback(app):
jwt_manager = JWTManager(app)
with app.test_request_context():
result = jwt_manager._expired_token_callback()
status_code, data = _parse_callback(result)
assert status_code == 401
assert data == {'msg': 'Token has expired'}
def test_custom_expired_token_callback(app):
jwt_manager = JWTManager(app)
@jwt_manager.expired_token_loader
def custom():
return jsonify({"foo": "bar"}), 200
with app.test_request_context():
result = jwt_manager._expired_token_callback()
status_code, data = _parse_callback(result)
assert status_code == 200
assert data == {'foo': 'bar'}
def test_default_invalid_token_callback(app):
jwt_manager = JWTManager(app)
with app.test_request_context():
err = "Test error"
result = jwt_manager._invalid_token_callback(err)
status_code, data = _parse_callback(result)
assert status_code == 422
assert data == {'msg': err}
def test_custom_invalid_token_callback(app):
jwt_manager = JWTManager(app)
@jwt_manager.invalid_token_loader
def custom(err):
return jsonify({"foo": "bar"}), 200
with app.test_request_context():
err = "Test error"
result = jwt_manager._invalid_token_callback(err)
status_code, data = _parse_callback(result)
assert status_code == 200
assert data == {'foo': 'bar'}
def test_default_unauthorized_callback(app):
jwt_manager = JWTManager(app)
with app.test_request_context():
err = "Test error"
result = jwt_manager._unauthorized_callback(err)
status_code, data = _parse_callback(result)
assert status_code == 401
assert data == {'msg': err}
def test_custom_unauthorized_callback(app):
jwt_manager = JWTManager(app)
@jwt_manager.unauthorized_loader
def custom(err):
return jsonify({"foo": "bar"}), 200
with app.test_request_context():
err = "Test error"
result = jwt_manager._unauthorized_callback(err)
status_code, data = _parse_callback(result)
assert status_code == 200
assert data == {'foo': 'bar'}
def test_default_get_jwt_data_callback(app):
jwt_manager = JWTManager(app)
with app.test_request_context():
result = jwt_manager._get_jwt_data(identity='foo')
assert 'exp' in result
assert 'iat' in result
assert 'nbf' in result
assert result['sub'] == 'foo'
def test_custom_get_jwt_data_callback(app):
jwt_manager = JWTManager(app)
@jwt_manager.jwt_data_loader
def custom(identity):
return {"foo": "bar"}
with app.test_request_context():
result = jwt_manager._get_jwt_data(identity='foo')
assert result == {"foo": "bar"}
|