1616# under the License.
1717
1818import base64
19+ import json
1920import pickle
21+ from pathlib import Path
2022from unittest .mock import MagicMock , patch
2123
2224import pytest
@@ -57,6 +59,23 @@ def google_mock(requests_mock: Mocker) -> Mocker:
5759 return requests_mock
5860
5961
62+ @pytest .fixture
63+ def google_default_credentials (tmp_path : Path , monkeypatch : pytest .MonkeyPatch ) -> None :
64+ credentials_path = tmp_path / "credentials.json"
65+ credentials_path .write_text (
66+ json .dumps (
67+ {
68+ "type" : "authorized_user" ,
69+ "client_id" : "client" ,
70+ "client_secret" : "secret" ,
71+ "refresh_token" : "refresh" ,
72+ "token_uri" : GOOGLE_CREDS_URI ,
73+ }
74+ )
75+ )
76+ monkeypatch .setenv ("GOOGLE_APPLICATION_CREDENTIALS" , str (credentials_path ))
77+
78+
6079def test_noop_auth_header (rest_mock : Mocker ) -> None :
6180 auth_manager = NoopAuthManager ()
6281 session = requests .Session ()
@@ -93,6 +112,7 @@ def test_google_auth_manager_default_credentials(
93112) -> None :
94113 """Test GoogleAuthManager with default application credentials."""
95114 mock_credentials = MagicMock ()
115+ mock_credentials .valid = False
96116 mock_credentials .token = "test_token"
97117 mock_google_auth_default .return_value = (mock_credentials , "test_project" )
98118
@@ -116,6 +136,7 @@ def test_google_auth_manager_with_credentials_file(
116136) -> None :
117137 """Test GoogleAuthManager with a credentials file path."""
118138 mock_credentials = MagicMock ()
139+ mock_credentials .valid = False
119140 mock_credentials .token = "file_token"
120141 mock_load_creds .return_value = (mock_credentials , "test_project_file" )
121142
@@ -139,6 +160,7 @@ def test_google_auth_manager_with_credentials_file_and_scopes(
139160) -> None :
140161 """Test GoogleAuthManager with a credentials file path and scopes."""
141162 mock_credentials = MagicMock ()
163+ mock_credentials .valid = False
142164 mock_credentials .token = "scoped_token"
143165 mock_load_creds .return_value = (mock_credentials , "test_project_scoped" )
144166 scopes = ["https://www.googleapis.com/auth/bigquery" ]
@@ -156,6 +178,28 @@ def test_google_auth_manager_with_credentials_file_and_scopes(
156178 assert actual_headers ["Authorization" ] == "Bearer scoped_token"
157179
158180
181+ def test_google_auth_manager_reuses_valid_token (google_mock : Mocker , google_default_credentials : None ) -> None :
182+ session = requests .Session ()
183+ session .auth = AuthManagerAdapter (GoogleAuthManager ())
184+ for _ in range (3 ):
185+ session .get (TEST_URI )
186+
187+ history = google_mock .request_history
188+ assert [request .url for request in history ].count (GOOGLE_CREDS_URI ) == 1
189+ assert [request .headers ["Authorization" ] for request in history if request .url == TEST_URI ] == ["Bearer aaaabbb" ] * 3
190+
191+
192+ def test_google_auth_manager_refreshes_expiring_token (google_mock : Mocker , google_default_credentials : None ) -> None :
193+ # A token that expires within google-auth's refresh threshold is not valid
194+ google_mock .post (GOOGLE_CREDS_URI , json = {"access_token" : "aaaabbb" , "expires_in" : 60 }, status_code = 200 )
195+ session = requests .Session ()
196+ session .auth = AuthManagerAdapter (GoogleAuthManager ())
197+ for _ in range (3 ):
198+ session .get (TEST_URI )
199+
200+ assert [request .url for request in google_mock .request_history ].count (GOOGLE_CREDS_URI ) == 3
201+
202+
159203def test_google_auth_manager_import_error () -> None :
160204 """Test GoogleAuthManager raises ImportError if google-auth is not installed."""
161205 with patch .dict ("sys.modules" , {"google.auth" : None , "google.auth.transport.requests" : None }):
0 commit comments