Skip to content

Commit 2473546

Browse files
author
BryanSchroeder
committed
adjusting jwt test to adjust value in the MultiProcessing and removing the clearing input values to make sure that we were only triggering callbacks when we desired.
1 parent fb691c3 commit 2473546

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

tests/integration/renderer/test_request_hooks.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import asyncio
12
import json
23
import functools
34
import flask
45
import pytest
56
from tests.utils import test_async
7+
from multiprocessing import Value
68

79
from flaky import flaky
810

@@ -201,12 +203,9 @@ def update_output(value):
201203
assert dash_duo.get_logs() == []
202204

203205

204-
@flaky(max_runs=3)
206+
# @flaky(max_runs=3)
205207
@pytest.mark.parametrize("expiry_code", [401, 400])
206208
def test_rdrh003_refresh_jwt(expiry_code, dash_duo):
207-
if test_async():
208-
return # if async, bypass this test as this ends up wrapping async funcs and results in 3 failed requests
209-
210209
app = Dash(__name__)
211210

212211
app.index_string = """<!DOCTYPE html>
@@ -248,28 +247,35 @@ def test_rdrh003_refresh_jwt(expiry_code, dash_duo):
248247
]
249248
)
250249

251-
@app.callback(Output("output-1", "children"), [Input("input", "value")])
250+
@app.callback(Output("output-1", "children"), [Input("input", "value")
251+
],prevent_initial_call=True)
252252
def update_output(value):
253+
jwt_token.value = len(value) + 1
253254
return value
254255

255-
required_jwt_len = 0
256+
jwt_token = Value("i", 0)
256257

257258
# test with an auth layer that requires a JWT with a certain length
258259
def protect_route(func):
259260
@functools.wraps(func)
260261
def wrap(*args, **kwargs):
261262
try:
263+
262264
if flask.request.method == "OPTIONS":
263265
return func(*args, **kwargs)
264266
token = flask.request.headers.environ.get("HTTP_AUTHORIZATION")
265-
if required_jwt_len and (
266-
not token or len(token) != required_jwt_len + len("Bearer ")
267+
if jwt_token.value and (
268+
not token or len(token) != jwt_token.value + len("Bearer ")
267269
):
268270
# Read the data to prevent bug with base http server.
269271
flask.request.get_json(silent=True)
270272
flask.abort(expiry_code, description="JWT Expired " + str(token))
271273
except HTTPException as e:
272274
return e
275+
if asyncio.iscoroutinefunction(func):
276+
if test_async():
277+
from asgiref.sync import async_to_sync # pylint: disable=unused-import, # noqa: F401
278+
return async_to_sync(func)(*args, **kwargs)
273279
return func(*args, **kwargs)
274280

275281
return wrap
@@ -287,22 +293,21 @@ def wrap(*args, **kwargs):
287293
_in = dash_duo.find_element("#input")
288294
dash_duo.clear_input(_in)
289295

290-
required_jwt_len = 1
291-
292-
_in.send_keys("fired request")
296+
dash_duo.wait_for_text_to_equal("#output-1", "")
293297

294-
dash_duo.wait_for_text_to_equal("#output-1", "fired request")
298+
_in.send_keys(".")
299+
dash_duo.wait_for_text_to_equal("#output-1", ".")
295300
dash_duo.wait_for_text_to_equal("#output-token", ".")
296301

297-
required_jwt_len = 2
298-
299-
dash_duo.clear_input(_in)
300-
_in.send_keys("fired request again")
301-
302-
dash_duo.wait_for_text_to_equal("#output-1", "fired request again")
302+
_in.send_keys(".")
303+
dash_duo.wait_for_text_to_equal("#output-1", "..")
303304
dash_duo.wait_for_text_to_equal("#output-token", "..")
304305

305-
assert len(dash_duo.get_logs()) == 2
306+
_in.send_keys(".")
307+
dash_duo.wait_for_text_to_equal("#output-1", "...")
308+
dash_duo.wait_for_text_to_equal("#output-token", "...")
309+
310+
assert len(dash_duo.get_logs()) == 3
306311

307312

308313
def test_rdrh004_layout_hooks(dash_duo):

0 commit comments

Comments
 (0)