Skip to content

Commit

Permalink
fix: deprecate UserAccessTokenCredentials (#1344)
Browse files Browse the repository at this point in the history
  • Loading branch information
arithmetic1728 committed Jul 6, 2023
1 parent 908c8d1 commit 5f97fe9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
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()

0 comments on commit 5f97fe9

Please sign in to comment.