@@ -38,7 +38,7 @@ def _perform_refresh_token(self, request):
3838 def with_quota_project (self , quota_project_id ):
3939 raise NotImplementedError ()
4040
41- def _build_regional_access_boundary_lookup_url (self ):
41+ def _build_regional_access_boundary_lookup_url (self , request = None ):
4242 # Using self.token here to make the URL dynamic for testing purposes
4343 return "http://mock.url/lookup_for_{}" .format (self .token )
4444
@@ -107,7 +107,7 @@ def test_before_request():
107107 headers = {}
108108
109109 # First call should call refresh, setting the token.
110- credentials .before_request (request , "http://example.com" , "GET " , headers )
110+ credentials .before_request (request , "GET" , " http://example.com" , headers )
111111 assert credentials .valid
112112 assert credentials .token == "refreshed-token"
113113 assert headers ["authorization" ] == "Bearer refreshed-token"
@@ -117,7 +117,7 @@ def test_before_request():
117117 headers = {}
118118
119119 # Second call shouldn't call refresh.
120- credentials .before_request (request , "http://example.com" , "GET " , headers )
120+ credentials .before_request (request , "GET" , " http://example.com" , headers )
121121 assert credentials .valid
122122 assert credentials .token == "refreshed-token"
123123 assert headers ["authorization" ] == "Bearer refreshed-token"
@@ -137,7 +137,7 @@ def test_before_request_with_regional_access_boundary():
137137 headers = {}
138138
139139 # First call should call refresh, setting the token.
140- creds .before_request (request , "http://example.com" , "GET " , headers )
140+ creds .before_request (request , "GET" , " http://example.com" , headers )
141141 assert creds .valid
142142 assert creds .token == "refreshed-token"
143143 assert headers ["authorization" ] == "Bearer refreshed-token"
@@ -147,7 +147,7 @@ def test_before_request_with_regional_access_boundary():
147147 headers = {}
148148
149149 # Second call shouldn't call refresh.
150- creds .before_request (request , "http://example.com" , "GET " , headers )
150+ creds .before_request (request , "GET" , " http://example.com" , headers )
151151 assert creds .valid
152152 assert creds .token == "refreshed-token"
153153 assert headers ["authorization" ] == "Bearer refreshed-token"
@@ -159,7 +159,7 @@ def test_before_request_metrics():
159159 request = "token"
160160 headers = {}
161161
162- credentials .before_request (request , "http://example.com" , "GET " , headers )
162+ credentials .before_request (request , "GET" , " http://example.com" , headers )
163163 assert headers ["x-goog-api-client" ] == "foo"
164164
165165
@@ -282,7 +282,7 @@ def test_nonblocking_refresh_fresh_credentials():
282282 assert c .token_state == credentials .TokenState .FRESH
283283
284284 c .with_non_blocking_refresh ()
285- c .before_request (request , "http://example.com" , "GET " , {})
285+ c .before_request (request , "GET" , " http://example.com" , {})
286286
287287
288288def test_nonblocking_refresh_invalid_credentials ():
@@ -294,7 +294,7 @@ def test_nonblocking_refresh_invalid_credentials():
294294
295295 assert c .token_state == credentials .TokenState .INVALID
296296
297- c .before_request (request , "http://example.com" , "GET " , headers )
297+ c .before_request (request , "GET" , " http://example.com" , headers )
298298 assert c .token_state == credentials .TokenState .FRESH
299299 assert c .valid
300300 assert c .token == "refreshed-token"
@@ -310,7 +310,7 @@ def test_nonblocking_refresh_stale_credentials():
310310 headers = {}
311311
312312 # Invalid credentials MUST require a blocking refresh.
313- c .before_request (request , "http://example.com" , "GET " , headers )
313+ c .before_request (request , "GET" , " http://example.com" , headers )
314314 assert c .token_state == credentials .TokenState .FRESH
315315 assert not c ._refresh_worker ._worker
316316
@@ -322,7 +322,7 @@ def test_nonblocking_refresh_stale_credentials():
322322
323323 # STALE credentials SHOULD spawn a non-blocking worker
324324 assert c .token_state == credentials .TokenState .STALE
325- c .before_request (request , "http://example.com" , "GET " , headers )
325+ c .before_request (request , "GET" , " http://example.com" , headers )
326326 assert c ._refresh_worker ._worker is not None
327327
328328 assert c .token_state == credentials .TokenState .FRESH
@@ -340,7 +340,7 @@ def test_nonblocking_refresh_failed_credentials():
340340 headers = {}
341341
342342 # Invalid credentials MUST require a blocking refresh.
343- c .before_request (request , "http://example.com" , "GET " , headers )
343+ c .before_request (request , "GET" , " http://example.com" , headers )
344344 assert c .token_state == credentials .TokenState .FRESH
345345 assert not c ._refresh_worker ._worker
346346
@@ -354,7 +354,7 @@ def test_nonblocking_refresh_failed_credentials():
354354 assert c .token_state == credentials .TokenState .STALE
355355 c ._refresh_worker ._worker = mock .MagicMock ()
356356 c ._refresh_worker ._worker ._error_info = "Some Error"
357- c .before_request (request , "http://example.com" , "GET " , headers )
357+ c .before_request (request , "GET" , " http://example.com" , headers )
358358 assert c ._refresh_worker ._worker is not None
359359
360360 assert c .token_state == credentials .TokenState .FRESH
@@ -373,7 +373,7 @@ def test_token_state_no_expiry():
373373 c .expiry = None
374374 assert c .token_state == credentials .TokenState .FRESH
375375
376- c .before_request (request , "http://example.com" , "GET " , {})
376+ c .before_request (request , "GET" , " http://example.com" , {})
377377
378378
379379def test_credentials_with_trust_boundary_bridge ():
@@ -389,3 +389,34 @@ def _build_trust_boundary_lookup_url(self):
389389 # Verify that calling the new method delegates to the old method
390390 with pytest .warns (DeprecationWarning ):
391391 assert creds ._build_regional_access_boundary_lookup_url () == "http://legacy.url"
392+
393+ def test_before_request_triggers_rab_refresh ():
394+ with mock .patch (
395+ "google.auth._regional_access_boundary_utils.is_regional_access_boundary_enabled" ,
396+ return_value = True ,
397+ ):
398+ with mock .patch ("google.oauth2._client._lookup_regional_access_boundary" ) as lookup :
399+ lookup .return_value = {"encodedLocations" : "0xA30" }
400+
401+ creds = CredentialsImpl ()
402+ creds = creds ._with_blocking_regional_access_boundary_lookup ()
403+
404+ request = mock .Mock ()
405+ headers = {}
406+
407+ # Initial state: no token
408+ assert creds .token is None
409+
410+ # before_request should trigger token refresh and THEN RAB refresh.
411+ # We verify this by checking that the RAB lookup was called with
412+ # the URL containing the refreshed token.
413+ creds .before_request (request , "GET" , "http://example.com" , headers )
414+
415+ assert creds .token == "refreshed-token"
416+ assert headers ["authorization" ] == "Bearer refreshed-token"
417+ assert headers ["x-allowed-locations" ] == "0xA30"
418+
419+ # Verify lookup was called with the refreshed token's URL
420+ lookup .assert_called_once ()
421+ args , kwargs = lookup .call_args
422+ assert args [1 ] == "http://mock.url/lookup_for_refreshed-token"
0 commit comments