|
21 | 21 | from google.auth import environment_vars
|
22 | 22 | from google.auth import exceptions
|
23 | 23 | from google.auth import transport
|
24 |
| -import google.auth.compute_engine._metadata |
25 | 24 | from google.oauth2 import id_token
|
26 | 25 | from google.oauth2 import service_account
|
27 | 26 |
|
28 | 27 | SERVICE_ACCOUNT_FILE = os.path.join(
|
29 | 28 | os.path.dirname(__file__), "../data/service_account.json"
|
30 | 29 | )
|
| 30 | +ID_TOKEN_AUDIENCE = "https://pubsub.googleapis.com" |
31 | 31 |
|
32 | 32 |
|
33 | 33 | def make_request(status, data=None):
|
@@ -201,93 +201,111 @@ def test_verify_firebase_token_clock_skew(verify_token):
|
201 | 201 | )
|
202 | 202 |
|
203 | 203 |
|
204 |
| -def test_fetch_id_token_from_metadata_server(monkeypatch): |
| 204 | +def test_fetch_id_token_credentials_optional_request(monkeypatch): |
205 | 205 | monkeypatch.delenv(environment_vars.CREDENTIALS, raising=False)
|
206 | 206 |
|
207 |
| - def mock_init(self, request, audience, use_metadata_identity_endpoint): |
208 |
| - assert use_metadata_identity_endpoint |
209 |
| - self.token = "id_token" |
210 |
| - |
| 207 | + # Test a request object is created if not provided |
211 | 208 | with mock.patch("google.auth.compute_engine._metadata.ping", return_value=True):
|
212 |
| - with mock.patch.multiple( |
213 |
| - google.auth.compute_engine.IDTokenCredentials, |
214 |
| - __init__=mock_init, |
215 |
| - refresh=mock.Mock(), |
| 209 | + with mock.patch( |
| 210 | + "google.auth.compute_engine.IDTokenCredentials.__init__", return_value=None |
216 | 211 | ):
|
217 |
| - request = mock.Mock() |
218 |
| - token = id_token.fetch_id_token(request, "https://pubsub.googleapis.com") |
219 |
| - assert token == "id_token" |
| 212 | + with mock.patch( |
| 213 | + "google.auth.transport.requests.Request.__init__", return_value=None |
| 214 | + ) as mock_request: |
| 215 | + id_token.fetch_id_token_credentials(ID_TOKEN_AUDIENCE) |
| 216 | + mock_request.assert_called() |
220 | 217 |
|
221 | 218 |
|
222 |
| -def test_fetch_id_token_from_explicit_cred_json_file(monkeypatch): |
223 |
| - monkeypatch.setenv(environment_vars.CREDENTIALS, SERVICE_ACCOUNT_FILE) |
| 219 | +def test_fetch_id_token_credentials_from_metadata_server(monkeypatch): |
| 220 | + monkeypatch.delenv(environment_vars.CREDENTIALS, raising=False) |
| 221 | + |
| 222 | + mock_req = mock.Mock() |
| 223 | + |
| 224 | + with mock.patch("google.auth.compute_engine._metadata.ping", return_value=True): |
| 225 | + with mock.patch( |
| 226 | + "google.auth.compute_engine.IDTokenCredentials.__init__", return_value=None |
| 227 | + ) as mock_init: |
| 228 | + id_token.fetch_id_token_credentials(ID_TOKEN_AUDIENCE, request=mock_req) |
| 229 | + mock_init.assert_called_once_with( |
| 230 | + mock_req, ID_TOKEN_AUDIENCE, use_metadata_identity_endpoint=True |
| 231 | + ) |
224 | 232 |
|
225 |
| - def mock_refresh(self, request): |
226 |
| - self.token = "id_token" |
227 | 233 |
|
228 |
| - with mock.patch.object(service_account.IDTokenCredentials, "refresh", mock_refresh): |
229 |
| - request = mock.Mock() |
230 |
| - token = id_token.fetch_id_token(request, "https://pubsub.googleapis.com") |
231 |
| - assert token == "id_token" |
| 234 | +def test_fetch_id_token_credentials_from_explicit_cred_json_file(monkeypatch): |
| 235 | + monkeypatch.setenv(environment_vars.CREDENTIALS, SERVICE_ACCOUNT_FILE) |
| 236 | + |
| 237 | + cred = id_token.fetch_id_token_credentials(ID_TOKEN_AUDIENCE) |
| 238 | + assert isinstance(cred, service_account.IDTokenCredentials) |
| 239 | + assert cred._target_audience == ID_TOKEN_AUDIENCE |
232 | 240 |
|
233 | 241 |
|
234 |
| -def test_fetch_id_token_no_cred_exists(monkeypatch): |
| 242 | +def test_fetch_id_token_credentials_no_cred_exists(monkeypatch): |
235 | 243 | monkeypatch.delenv(environment_vars.CREDENTIALS, raising=False)
|
236 | 244 |
|
237 | 245 | with mock.patch(
|
238 | 246 | "google.auth.compute_engine._metadata.ping",
|
239 | 247 | side_effect=exceptions.TransportError(),
|
240 | 248 | ):
|
241 | 249 | with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
|
242 |
| - request = mock.Mock() |
243 |
| - id_token.fetch_id_token(request, "https://pubsub.googleapis.com") |
| 250 | + id_token.fetch_id_token_credentials(ID_TOKEN_AUDIENCE) |
244 | 251 | assert excinfo.match(
|
245 | 252 | r"Neither metadata server or valid service account credentials are found."
|
246 | 253 | )
|
247 | 254 |
|
248 | 255 | with mock.patch("google.auth.compute_engine._metadata.ping", return_value=False):
|
249 | 256 | with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
|
250 |
| - request = mock.Mock() |
251 |
| - id_token.fetch_id_token(request, "https://pubsub.googleapis.com") |
| 257 | + id_token.fetch_id_token_credentials(ID_TOKEN_AUDIENCE) |
252 | 258 | assert excinfo.match(
|
253 | 259 | r"Neither metadata server or valid service account credentials are found."
|
254 | 260 | )
|
255 | 261 |
|
256 | 262 |
|
257 |
| -def test_fetch_id_token_invalid_cred_file_type(monkeypatch): |
| 263 | +def test_fetch_id_token_credentials_invalid_cred_file_type(monkeypatch): |
258 | 264 | user_credentials_file = os.path.join(
|
259 | 265 | os.path.dirname(__file__), "../data/authorized_user.json"
|
260 | 266 | )
|
261 | 267 | monkeypatch.setenv(environment_vars.CREDENTIALS, user_credentials_file)
|
262 | 268 |
|
263 | 269 | with mock.patch("google.auth.compute_engine._metadata.ping", return_value=False):
|
264 | 270 | with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
|
265 |
| - request = mock.Mock() |
266 |
| - id_token.fetch_id_token(request, "https://pubsub.googleapis.com") |
| 271 | + id_token.fetch_id_token_credentials(ID_TOKEN_AUDIENCE) |
267 | 272 | assert excinfo.match(
|
268 | 273 | r"Neither metadata server or valid service account credentials are found."
|
269 | 274 | )
|
270 | 275 |
|
271 | 276 |
|
272 |
| -def test_fetch_id_token_invalid_json(monkeypatch): |
| 277 | +def test_fetch_id_token_credentials_invalid_json(monkeypatch): |
273 | 278 | not_json_file = os.path.join(os.path.dirname(__file__), "../data/public_cert.pem")
|
274 | 279 | monkeypatch.setenv(environment_vars.CREDENTIALS, not_json_file)
|
275 | 280 |
|
276 | 281 | with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
|
277 |
| - request = mock.Mock() |
278 |
| - id_token.fetch_id_token(request, "https://pubsub.googleapis.com") |
| 282 | + id_token.fetch_id_token_credentials(ID_TOKEN_AUDIENCE) |
279 | 283 | assert excinfo.match(
|
280 | 284 | r"GOOGLE_APPLICATION_CREDENTIALS is not valid service account credentials."
|
281 | 285 | )
|
282 | 286 |
|
283 | 287 |
|
284 |
| -def test_fetch_id_token_invalid_cred_path(monkeypatch): |
| 288 | +def test_fetch_id_token_credentials_invalid_cred_path(monkeypatch): |
285 | 289 | not_json_file = os.path.join(os.path.dirname(__file__), "../data/not_exists.json")
|
286 | 290 | monkeypatch.setenv(environment_vars.CREDENTIALS, not_json_file)
|
287 | 291 |
|
288 | 292 | with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
|
289 |
| - request = mock.Mock() |
290 |
| - id_token.fetch_id_token(request, "https://pubsub.googleapis.com") |
| 293 | + id_token.fetch_id_token_credentials(ID_TOKEN_AUDIENCE) |
291 | 294 | assert excinfo.match(
|
292 | 295 | r"GOOGLE_APPLICATION_CREDENTIALS path is either not found or invalid."
|
293 | 296 | )
|
| 297 | + |
| 298 | + |
| 299 | +def test_fetch_id_token(monkeypatch): |
| 300 | + mock_cred = mock.MagicMock() |
| 301 | + mock_cred.token = "token" |
| 302 | + |
| 303 | + mock_req = mock.Mock() |
| 304 | + |
| 305 | + with mock.patch( |
| 306 | + "google.oauth2.id_token.fetch_id_token_credentials", return_value=mock_cred |
| 307 | + ) as mock_fetch: |
| 308 | + token = id_token.fetch_id_token(mock_req, ID_TOKEN_AUDIENCE) |
| 309 | + mock_fetch.assert_called_once_with(ID_TOKEN_AUDIENCE, request=mock_req) |
| 310 | + mock_cred.refresh.assert_called_once_with(mock_req) |
| 311 | + assert token == "token" |
0 commit comments