8000 Fixed exception propagation which, when `DEBUG==False`, prevented a t… · KBrummage/testing-python-apps@5fbc910 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5fbc910

Browse files
committed
Fixed exception propagation which, when DEBUG==False, prevented a test from returning 401. Instead, it was just crashing with a JWTError exception.
1 parent 40fd309 commit 5fbc910

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

section4/video_code/app.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import os
22

3-
from flask import Flask
3+
from flask import Flask, jsonify
44
from flask_restful import Api
5-
from flask_jwt import JWT
5+
import flask_jwt
66

77
from security import authenticate, identity
88
from resources.user import UserRegister
@@ -15,10 +15,11 @@
1515

1616
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'sqlite:///data.db')
1717
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
18+
app.config['PROPAGATE_EXCEPTIONS'] = True
1819
app.secret_key = 'jose'
1920
api = Api(app)
2021

21-
jwt = JWT(app, authenticate, identity) # /auth
22+
jwt = flask_jwt.JWT(app, authenticate, identity) # /auth
2223

2324
api.add_resource(Store, '/store/<string:name>')
2425
api.add_resource(Item, '/item/<string:name>')
@@ -27,6 +28,11 @@
2728

2829
api.add_resource(UserRegister, '/register')
2930

31+
32+
@app.errorhandler(flask_jwt.JWTError)
33+
def auth_error(err):
34+
return jsonify({'message': 'Could not authorize. Did you include a valid Authorization header?'}), 401
35+
3036
if __name__ == '__main__':
3137
from db import db
3238

section4/video_code/tests/base_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class BaseTest(TestCase):
1717
@classmetho 58C4 d
1818
def setUpClass(cls):
1919
app.config['SQLALCHEMY_DATABASE_URI'] = BaseTest.SQLALCHEMY_DATABASE_URI
20+
app.config['DEBUG'] = False
2021
with app.app_context():
2122
db.init_app(app)
2223

0 commit comments

Comments
 (0)
0