1- import os
21from unittest .mock import patch
32
43import requests
54from dash import Dash , Input , Output , dcc , html
65from flask import redirect
76
8- from dash_auth import (
9- protected_callback ,
10- OIDCAuth ,
11- )
7+ from dash_auth import OIDCAuth , protected_callback
128
139
1410def valid_authorize_redirect (_ , redirect_uri , * args , ** kwargs ):
@@ -17,7 +13,9 @@ def valid_authorize_redirect(_, redirect_uri, *args, **kwargs):
1713
1814def invalid_authorize_redirect (_ , redirect_uri , * args , ** kwargs ):
1915 base_url = "/" + redirect_uri .split ("/" , maxsplit = 3 )[- 1 ]
20- return redirect (f"{ base_url } ?error=Unauthorized&error_description=something went wrong" )
16+ return redirect (
17+ f"{ base_url } ?error=Unauthorized&error_description=something went wrong"
18+ )
2119
2220
2321def valid_authorize_access_token (* args , ** kwargs ):
@@ -27,18 +25,26 @@ def valid_authorize_access_token(*args, **kwargs):
2725 }
2826
2927
30- @patch ("authlib.integrations.flask_client.apps.FlaskOAuth2App.authorize_redirect" , valid_authorize_redirect )
31- @patch ("authlib.integrations.flask_client.apps.FlaskOAuth2App.authorize_access_token" , valid_authorize_access_token )
28+ @patch (
29+ "authlib.integrations.flask_client.apps.FlaskOAuth2App.authorize_redirect" ,
30+ valid_authorize_redirect ,
31+ )
32+ @patch (
33+ "authlib.integrations.flask_client.apps.FlaskOAuth2App.authorize_access_token" ,
34+ valid_authorize_access_token ,
35+ )
3236def test_oa001_oidc_auth_login_flow_success (dash_br , dash_thread_server ):
3337 app = Dash (__name__ )
34- app .layout = html .Div ([
35- dcc .Input (id = "input" , value = "initial value" ),
36- html .Div (id = "output1" ),
37- html .Div (id = "output2" ),
38- html .Div ("static" , id = "output3" ),
39- html .Div ("static" , id = "output4" ),
40- html .Div ("not static" , id = "output5" ),
41- ])
38+ app .layout = html .Div (
39+ [
40+ dcc .Input (id = "input" , value = "initial value" ),
41+ html .Div (id = "output1" ),
42+ html .Div (id = "output2" ),
43+ html .Div ("static" , id = "output3" ),
44+ html .Div ("static" , id = "output4" ),
45+ html .Div ("not static" , id = "output5" ),
46+ ]
47+ )
4248
4349 @app .callback (Output ("output1" , "children" ), Input ("input" , "value" ))
4450 def update_output1 (new_value ):
@@ -101,13 +107,15 @@ def update_output5(new_value):
101107 dash_br .wait_for_text_to_equal ("#output5" , "initial value" )
102108
103109
104- @patch ("authlib.integrations.flask_client.apps.FlaskOAuth2App.authorize_redirect" , invalid_authorize_redirect )
110+ @patch (
111+ "authlib.integrations.flask_client.apps.FlaskOAuth2App.authorize_redirect" ,
112+ invalid_authorize_redirect ,
113+ )
105114def test_oa002_oidc_auth_login_fail (dash_thread_server ):
106115 app = Dash (__name__ )
107- app .layout = html .Div ([
108- dcc .Input (id = "input" , value = "initial value" ),
109- html .Div (id = "output" )
110- ])
116+ app .layout = html .Div (
117+ [dcc .Input (id = "input" , value = "initial value" ), html .Div (id = "output" )]
118+ )
111119
112120 @app .callback (Output ("output" , "children" ), Input ("input" , "value" ))
113121 def update_output (new_value ):
@@ -122,7 +130,7 @@ def update_output(new_value):
122130 server_metadata_url = "https://idp.com/oidc/2/.well-known/openid-configuration" ,
123131 )
124132 dash_thread_server (app )
125- base_url = dash_thread_server .url
133+ base_url = dash_thread_server .url . rstrip ( "/" )
126134
127135 def test_unauthorized (url ):
128136 r = requests .get (url )
@@ -133,17 +141,25 @@ def test_authorized(url):
133141 assert requests .get (url ).status_code == 200
134142
135143 test_unauthorized (base_url )
136- test_authorized (os . path . join ( base_url , " public") )
144+ test_authorized (base_url + "/ public" )
137145
138146
139- @patch ("authlib.integrations.flask_client.apps.FlaskOAuth2App.authorize_redirect" , valid_authorize_redirect )
140- @patch ("authlib.integrations.flask_client.apps.FlaskOAuth2App.authorize_access_token" , valid_authorize_access_token )
147+ @patch (
148+ "authlib.integrations.flask_client.apps.FlaskOAuth2App.authorize_redirect" ,
149+ valid_authorize_redirect ,
150+ )
151+ @patch (
152+ "authlib.integrations.flask_client.apps.FlaskOAuth2App.authorize_access_token" ,
153+ valid_authorize_access_token ,
154+ )
141155def test_oa003_oidc_auth_login_several_idp (dash_br , dash_thread_server ):
142156 app = Dash (__name__ )
143- app .layout = html .Div ([
144- dcc .Input (id = "input" , value = "initial value" ),
145- html .Div (id = "output1" ),
146- ])
157+ app .layout = html .Div (
158+ [
159+ dcc .Input (id = "input" , value = "initial value" ),
160+ html .Div (id = "output1" ),
161+ ]
162+ )
147163
148164 @app .callback (Output ("output1" , "children" ), Input ("input" , "value" ))
149165 def update_output1 (new_value ):
@@ -168,21 +184,21 @@ def update_output1(new_value):
168184 )
169185
170186 dash_thread_server (app )
171- base_url = dash_thread_server .url
187+ base_url = dash_thread_server .url . rstrip ( "/" )
172188
173189 assert requests .get (base_url ).status_code == 400
174190
175191 # Login with IDP1
176- assert requests .get (os . path . join ( base_url , " oidc/idp1/login") ).status_code == 200
192+ assert requests .get (base_url + "/ oidc/idp1/login" ).status_code == 200
177193
178194 # Logout
179- assert requests .get (os . path . join ( base_url , " oidc/logout") ).status_code == 200
195+ assert requests .get (base_url + "/ oidc/logout" ).status_code == 200
180196
181197 assert requests .get (base_url ).status_code == 400
182198
183199 # Login with IDP2
184- assert requests .get (os . path . join ( base_url , " oidc/idp2/login") ).status_code == 200
200+ assert requests .get (base_url + "/ oidc/idp2/login" ).status_code == 200
185201
186- dash_br .driver .get (os . path . join ( base_url , " oidc/idp2/login") )
202+ dash_br .driver .get (base_url + "/ oidc/idp2/login" )
187203 dash_br .driver .get (base_url )
188- dash_br .wait_for_text_to_equal ("#output1" , "initial value" )
204+ dash_br .wait_for_text_to_equal ("#output1" , "initial value" )
0 commit comments