Skip to content

Commit 5342573

Browse files
author
BryanSchroeder
committed
simplifying jwt test by using before_request removing needs to check for async and wrapping a view.
1 parent df29ee6 commit 5342573

File tree

1 file changed

+10
-38
lines changed

1 file changed

+10
-38
lines changed

tests/integration/renderer/test_request_hooks.py

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
import asyncio
21
import json
3-
import functools
42
import flask
53
import pytest
6-
from tests.utils import test_async
74
from multiprocessing import Value
85

96
from dash import Dash, Output, Input, html, dcc
107
from dash.types import RendererHooks
11-
from werkzeug.exceptions import HTTPException
128

139

1410
def test_rdrh001_request_hooks(dash_duo):
@@ -257,40 +253,16 @@ def update_output(value):
257253
jwt_token = Value("i", 0)
258254

259255
# test with an auth layer that requires a JWT with a certain length
260-
def protect_route(func):
261-
@functools.wraps(func)
262-
def wrap(*args, **kwargs):
263-
try:
264-
265-
if flask.request.method == "OPTIONS":
266-
return func(*args, **kwargs)
267-
token = flask.request.headers.environ.get("HTTP_AUTHORIZATION")
268-
if jwt_token.value and (
269-
not token or len(token) != jwt_token.value + len("Bearer ")
270-
):
271-
# Read the data to prevent bug with base http server.
272-
flask.request.get_json(silent=True)
273-
flask.abort(expiry_code, description="JWT Expired " + str(token))
274-
except HTTPException as e:
275-
return e
276-
if asyncio.iscoroutinefunction(func):
277-
if test_async():
278-
from asgiref.sync import (
279-
async_to_sync,
280-
) # pylint: disable=unused-import, # noqa: F401
281-
282-
return async_to_sync(func)(*args, **kwargs)
283-
return func(*args, **kwargs)
284-
285-
return wrap
286-
287-
# wrap all API calls with auth.
288-
for name, method in (
289-
(x, app.server.view_functions[x])
290-
for x in app.routes
291-
if x in app.server.view_functions
292-
):
293-
app.server.view_functions[name] = protect_route(method)
256+
@app.server.before_request
257+
def add_auth():
258+
if flask.request.method != "OPTIONS":
259+
token = flask.request.headers.environ.get("HTTP_AUTHORIZATION")
260+
if jwt_token.value and (
261+
not token or len(token) != jwt_token.value + len("Bearer ")
262+
):
263+
# Read the data to prevent bug with base http server.
264+
flask.request.get_json(silent=True)
265+
flask.abort(expiry_code, description="JWT Expired " + str(token))
294266

295267
dash_duo.start_server(app)
296268

0 commit comments

Comments
 (0)