Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit e5a28b5

Browse files
tests: system tests for expired User Credentials (#1937)
This PR fixes flaky system tests that fail when `google.auth.default()` picks up expired User Credentials. The previous logic only exempted failures if the `GOOGLE_APPLICATION_CREDENTIALS` environment variable pointed to a file ending in "authorized_user.json". However, User Credentials can be loaded from other locations (e.g., gcloud config), causing the test to fail with `RefreshError` instead of ignoring it as intended. Changes: - Added `isinstance` check for `google.oauth2.credentials.Credentials` to robustly identify User Credentials. - Added `invalid_client` to the list of ignored error messages, as this error can also occur with invalid tokens. - Applied these fixes to both sync and async system tests. --- *PR created automatically by Jules for task [13051961428180802207](https://jules.google.com/task/13051961428180802207) started by @chalmerlowe* Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: Chalmer Lowe <chalmerlowe@google.com>
1 parent 4292ab0 commit e5a28b5

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

system_tests/system_tests_async/test_default.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from google.auth import _default_async
1919
from google.auth.exceptions import RefreshError
20+
import google.oauth2.credentials
2021

2122
EXPECT_PROJECT_ID = os.getenv("EXPECT_PROJECT_ID")
2223
CREDENTIALS = os.getenv("GOOGLE_APPLICATION_CREDENTIALS", "")
@@ -34,12 +35,16 @@ async def test_application_default_credentials(verify_refresh):
3435
except RefreshError as e:
3536
# allow expired credentials for explicit_authorized_user tests
3637
# TODO: https://github.com/googleapis/google-auth-library-python/issues/1882
37-
if not CREDENTIALS.endswith("authorized_user.json"):
38+
is_user_credentials = isinstance(
39+
credentials, google.oauth2.credentials.Credentials
40+
)
41+
if not is_user_credentials and not CREDENTIALS.endswith("authorized_user.json"):
3842
raise
3943

4044
error_message = str(e)
4145
if (
4246
"Token has been expired or revoked" not in error_message
4347
and "invalid_grant" not in error_message
48+
and "invalid_client" not in error_message
4449
):
4550
raise

system_tests/system_tests_sync/test_default.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import google.auth
1818
from google.auth.exceptions import RefreshError
19+
import google.oauth2.credentials
1920

2021
EXPECT_PROJECT_ID = os.getenv("EXPECT_PROJECT_ID")
2122
CREDENTIALS = os.getenv("GOOGLE_APPLICATION_CREDENTIALS", "")
@@ -32,12 +33,16 @@ def test_application_default_credentials(verify_refresh):
3233
except RefreshError as e:
3334
# allow expired credentials for explicit_authorized_user tests
3435
# TODO: https://github.com/googleapis/google-auth-library-python/issues/1882
35-
if not CREDENTIALS.endswith("authorized_user.json"):
36+
is_user_credentials = isinstance(
37+
credentials, google.oauth2.credentials.Credentials
38+
)
39+
if not is_user_credentials and not CREDENTIALS.endswith("authorized_user.json"):
3640
raise
3741

3842
error_message = str(e)
3943
if (
4044
"Token has been expired or revoked" not in error_message
4145
and "invalid_grant" not in error_message
46+
and "invalid_client" not in error_message
4247
):
4348
raise

0 commit comments

Comments
 (0)