feat: add access token credentials (#476)
feat: add access token credentials
diff --git a/tests/oauth2/test_credentials.py b/tests/oauth2/test_credentials.py
index bdb63e9..76aa463 100644
--- a/tests/oauth2/test_credentials.py
+++ b/tests/oauth2/test_credentials.py
@@ -421,3 +421,31 @@
) as f:
credentials = pickle.load(f)
assert credentials.quota_project_id is None
+
+
+class TestUserAccessTokenCredentials(object):
+ def test_instance(self):
+ cred = credentials.UserAccessTokenCredentials()
+ assert cred._account is None
+
+ 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"
+
+ @mock.patch(
+ "google.oauth2.credentials.UserAccessTokenCredentials.apply", autospec=True
+ )
+ @mock.patch(
+ "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()
diff --git a/tests/test__cloud_sdk.py b/tests/test__cloud_sdk.py
index 049ed99..3377604 100644
--- a/tests/test__cloud_sdk.py
+++ b/tests/test__cloud_sdk.py
@@ -22,7 +22,7 @@
from google.auth import _cloud_sdk
from google.auth import environment_vars
-import google.oauth2.credentials
+from google.auth import exceptions
DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
@@ -137,23 +137,33 @@
assert os.path.split(config_path) == ("G:/\\", _cloud_sdk._CONFIG_DIRECTORY)
-def test_load_authorized_user_credentials():
- credentials = _cloud_sdk.load_authorized_user_credentials(AUTHORIZED_USER_FILE_DATA)
+@mock.patch("os.name", new="nt")
+@mock.patch("subprocess.check_output", autospec=True)
+def test_get_auth_access_token_windows(check_output):
+ check_output.return_value = b"access_token\n"
- assert isinstance(credentials, google.oauth2.credentials.Credentials)
-
- assert credentials.token is None
- assert credentials._refresh_token == AUTHORIZED_USER_FILE_DATA["refresh_token"]
- assert credentials._client_id == AUTHORIZED_USER_FILE_DATA["client_id"]
- assert credentials._client_secret == AUTHORIZED_USER_FILE_DATA["client_secret"]
- assert (
- credentials._token_uri
- == google.oauth2.credentials._GOOGLE_OAUTH2_TOKEN_ENDPOINT
+ token = _cloud_sdk.get_auth_access_token()
+ assert token == "access_token"
+ check_output.assert_called_with(
+ ("gcloud.cmd", "auth", "print-access-token"), stderr=subprocess.STDOUT
)
-def test_load_authorized_user_credentials_bad_format():
- with pytest.raises(ValueError) as excinfo:
- _cloud_sdk.load_authorized_user_credentials({})
+@mock.patch("subprocess.check_output", autospec=True)
+def test_get_auth_access_token_with_account(check_output):
+ check_output.return_value = b"access_token\n"
- assert excinfo.match(r"missing fields")
+ token = _cloud_sdk.get_auth_access_token(account="account")
+ assert token == "access_token"
+ check_output.assert_called_with(
+ ("gcloud", "auth", "print-access-token", "--account=account"),
+ stderr=subprocess.STDOUT,
+ )
+
+
+@mock.patch("subprocess.check_output", autospec=True)
+def test_get_auth_access_token_with_exception(check_output):
+ check_output.side_effect = OSError()
+
+ with pytest.raises(exceptions.UserAccessTokenError):
+ _cloud_sdk.get_auth_access_token(account="account")