1
+ import asyncio
1
2
import json
2
3
import functools
3
4
import flask
4
5
import pytest
5
6
from tests .utils import test_async
7
+ from multiprocessing import Value
6
8
7
9
from flaky import flaky
8
10
@@ -201,12 +203,9 @@ def update_output(value):
201
203
assert dash_duo .get_logs () == []
202
204
203
205
204
- @flaky (max_runs = 3 )
206
+ # @flaky(max_runs=3)
205
207
@pytest .mark .parametrize ("expiry_code" , [401 , 400 ])
206
208
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
-
210
209
app = Dash (__name__ )
211
210
212
211
app .index_string = """<!DOCTYPE html>
@@ -248,28 +247,35 @@ def test_rdrh003_refresh_jwt(expiry_code, dash_duo):
248
247
]
249
248
)
250
249
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 )
252
252
def update_output (value ):
253
+ jwt_token .value = len (value ) + 1
253
254
return value
254
255
255
- required_jwt_len = 0
256
+ jwt_token = Value ( "i" , 0 )
256
257
257
258
# test with an auth layer that requires a JWT with a certain length
258
259
def protect_route (func ):
259
260
@functools .wraps (func )
260
261
def wrap (* args , ** kwargs ):
261
262
try :
263
+
262
264
if flask .request .method == "OPTIONS" :
263
265
return func (* args , ** kwargs )
264
266
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 " )
267
269
):
268
270
# Read the data to prevent bug with base http server.
269
271
flask .request .get_json (silent = True )
270
272
flask .abort (expiry_code , description = "JWT Expired " + str (token ))
271
273
except HTTPException as e :
272
274
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 )
273
279
return func (* args , ** kwargs )
274
280
275
281
return wrap
@@ -287,22 +293,21 @@ def wrap(*args, **kwargs):
287
293
_in = dash_duo .find_element ("#input" )
288
294
dash_duo .clear_input (_in )
289
295
290
- required_jwt_len = 1
291
-
292
- _in .send_keys ("fired request" )
296
+ dash_duo .wait_for_text_to_equal ("#output-1" , "" )
293
297
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" , "." )
295
300
dash_duo .wait_for_text_to_equal ("#output-token" , "." )
296
301
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" , ".." )
303
304
dash_duo .wait_for_text_to_equal ("#output-token" , ".." )
304
305
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
306
311
307
312
308
313
def test_rdrh004_layout_hooks (dash_duo ):
0 commit comments