Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: deprecate UserAccessTokenCredentials #1344

Merged
merged 1 commit into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions google/oauth2/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import io
import json
import logging
import warnings

import six

Expand Down Expand Up @@ -498,6 +499,13 @@ class UserAccessTokenCredentials(credentials.CredentialsWithQuotaProject):
"""

def __init__(self, account=None, quota_project_id=None):
warnings.warn(
"UserAccessTokenCredentials is deprecated, please use "
"google.oauth2.credentials.Credentials instead. To use "
"that credential type, simply run "
"`gcloud auth application-default login` and let the "
"client libraries pick up the application default credentials."
)
super(UserAccessTokenCredentials, self).__init__()
self._account = account
self._quota_project_id = quota_project_id
Expand Down
Binary file modified system_tests/secrets.tar.enc
Binary file not shown.
44 changes: 28 additions & 16 deletions tests/oauth2/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,25 +951,34 @@ def test_unpickle_old_credentials_pickle(self):

class TestUserAccessTokenCredentials(object):
def test_instance(self):
cred = credentials.UserAccessTokenCredentials()
assert cred._account is None
with pytest.warns(
UserWarning, match="UserAccessTokenCredentials is deprecated"
):
cred = credentials.UserAccessTokenCredentials()
assert cred._account is None

cred = cred.with_account("account")
assert cred._account == "account"
cred = cred.with_account("account")
assert cred._account == "account"

@mock.patch("google.auth._cloud_sdk.get_auth_access_token", autospec=True)
def test_refresh(self, get_auth_access_token):
get_auth_access_token.return_value = "access_token"
cred = credentials.UserAccessTokenCredentials()
cred.refresh(None)
assert cred.token == "access_token"
with pytest.warns(
UserWarning, match="UserAccessTokenCredentials is deprecated"
):
get_auth_access_token.return_value = "access_token"
cred = credentials.UserAccessTokenCredentials()
cred.refresh(None)
assert cred.token == "access_token"

def test_with_quota_project(self):
cred = credentials.UserAccessTokenCredentials()
quota_project_cred = cred.with_quota_project("project-foo")
with pytest.warns(
UserWarning, match="UserAccessTokenCredentials is deprecated"
):
cred = credentials.UserAccessTokenCredentials()
quota_project_cred = cred.with_quota_project("project-foo")

assert quota_project_cred._quota_project_id == "project-foo"
assert quota_project_cred._account == cred._account
assert quota_project_cred._quota_project_id == "project-foo"
assert quota_project_cred._account == cred._account

@mock.patch(
"google.oauth2.credentials.UserAccessTokenCredentials.apply", autospec=True
Expand All @@ -978,7 +987,10 @@ def test_with_quota_project(self):
"google.oauth2.credentials.UserAccessTokenCredentials.refresh", autospec=True
)
def test_before_request(self, refresh, apply):
cred = credentials.UserAccessTokenCredentials()
cred.before_request(mock.Mock(), "GET", "https://example.com", {})
refresh.assert_called()
apply.assert_called()
with pytest.warns(
UserWarning, match="UserAccessTokenCredentials is deprecated"
):
cred = credentials.UserAccessTokenCredentials()
cred.before_request(mock.Mock(), "GET", "https://example.com", {})
refresh.assert_called()
apply.assert_called()