blob: 3377604260565fff6bce891d071d527c02d61867 [file] [log] [blame]
C.J. Collier37141e42020-02-13 13:49:49 -08001# Copyright 2016 Google LLC
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -07002#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070015import io
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070016import json
17import os
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070018import subprocess
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070019
20import mock
21import pytest
22
23from google.auth import _cloud_sdk
24from google.auth import environment_vars
arithmetic1728772dac62020-03-27 14:34:13 -070025from google.auth import exceptions
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070026
27
Bu Sun Kim9eec0912019-10-21 17:04:21 -070028DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
29AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, "authorized_user.json")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070030
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070031with io.open(AUTHORIZED_USER_FILE) as fh:
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070032 AUTHORIZED_USER_FILE_DATA = json.load(fh)
33
Bu Sun Kim9eec0912019-10-21 17:04:21 -070034SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, "service_account.json")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070035
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070036with io.open(SERVICE_ACCOUNT_FILE) as fh:
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070037 SERVICE_ACCOUNT_FILE_DATA = json.load(fh)
38
Bu Sun Kim9eec0912019-10-21 17:04:21 -070039with io.open(os.path.join(DATA_DIR, "cloud_sdk_config.json"), "rb") as fh:
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070040 CLOUD_SDK_CONFIG_FILE_DATA = fh.read()
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070041
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070042
Bu Sun Kim9eec0912019-10-21 17:04:21 -070043@pytest.mark.parametrize(
44 "data, expected_project_id",
45 [
46 (CLOUD_SDK_CONFIG_FILE_DATA, "example-project"),
47 (b"I am some bad json", None),
48 (b"{}", None),
49 ],
50)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070051def test_get_project_id(data, expected_project_id):
52 check_output_patch = mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -070053 "subprocess.check_output", autospec=True, return_value=data
54 )
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070055
56 with check_output_patch as check_output:
57 project_id = _cloud_sdk.get_project_id()
58
59 assert project_id == expected_project_id
60 assert check_output.called
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070061
62
63@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -070064 "subprocess.check_output",
65 autospec=True,
66 side_effect=subprocess.CalledProcessError(-1, None),
67)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070068def test_get_project_id_call_error(check_output):
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070069 project_id = _cloud_sdk.get_project_id()
70 assert project_id is None
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070071 assert check_output.called
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070072
73
Bu Sun Kim9eec0912019-10-21 17:04:21 -070074@mock.patch("os.name", new="nt")
PicardParis4921d442017-08-01 19:05:41 +020075def test_get_project_id_windows():
76 check_output_patch = mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -070077 "subprocess.check_output",
78 autospec=True,
79 return_value=CLOUD_SDK_CONFIG_FILE_DATA,
80 )
PicardParis4921d442017-08-01 19:05:41 +020081
82 with check_output_patch as check_output:
83 project_id = _cloud_sdk.get_project_id()
84
Bu Sun Kim9eec0912019-10-21 17:04:21 -070085 assert project_id == "example-project"
PicardParis4921d442017-08-01 19:05:41 +020086 assert check_output.called
87 # Make sure the executable is `gcloud.cmd`.
88 args = check_output.call_args[0]
89 command = args[0]
90 executable = command[0]
Bu Sun Kim9eec0912019-10-21 17:04:21 -070091 assert executable == "gcloud.cmd"
PicardParis4921d442017-08-01 19:05:41 +020092
93
Bu Sun Kim9eec0912019-10-21 17:04:21 -070094@mock.patch("google.auth._cloud_sdk.get_config_path", autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070095def test_get_application_default_credentials_path(get_config_dir):
Bu Sun Kim9eec0912019-10-21 17:04:21 -070096 config_path = "config_path"
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070097 get_config_dir.return_value = config_path
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070098 credentials_path = _cloud_sdk.get_application_default_credentials_path()
99 assert credentials_path == os.path.join(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700100 config_path, _cloud_sdk._CREDENTIALS_FILENAME
101 )
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700102
103
104def test_get_config_path_env_var(monkeypatch):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700105 config_path_sentinel = "config_path"
106 monkeypatch.setenv(environment_vars.CLOUD_SDK_CONFIG_DIR, config_path_sentinel)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700107 config_path = _cloud_sdk.get_config_path()
108 assert config_path == config_path_sentinel
109
110
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700111@mock.patch("os.path.expanduser")
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700112def test_get_config_path_unix(expanduser):
113 expanduser.side_effect = lambda path: path
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700114
115 config_path = _cloud_sdk.get_config_path()
116
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700117 assert os.path.split(config_path) == ("~/.config", _cloud_sdk._CONFIG_DIRECTORY)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700118
119
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700120@mock.patch("os.name", new="nt")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700121def test_get_config_path_windows(monkeypatch):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700122 appdata = "appdata"
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700123 monkeypatch.setenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, appdata)
124
125 config_path = _cloud_sdk.get_config_path()
126
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700127 assert os.path.split(config_path) == (appdata, _cloud_sdk._CONFIG_DIRECTORY)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700128
129
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700130@mock.patch("os.name", new="nt")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700131def test_get_config_path_no_appdata(monkeypatch):
132 monkeypatch.delenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, raising=False)
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700133 monkeypatch.setenv("SystemDrive", "G:")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700134
135 config_path = _cloud_sdk.get_config_path()
136
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700137 assert os.path.split(config_path) == ("G:/\\", _cloud_sdk._CONFIG_DIRECTORY)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700138
139
arithmetic1728772dac62020-03-27 14:34:13 -0700140@mock.patch("os.name", new="nt")
141@mock.patch("subprocess.check_output", autospec=True)
142def test_get_auth_access_token_windows(check_output):
143 check_output.return_value = b"access_token\n"
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700144
arithmetic1728772dac62020-03-27 14:34:13 -0700145 token = _cloud_sdk.get_auth_access_token()
146 assert token == "access_token"
147 check_output.assert_called_with(
148 ("gcloud.cmd", "auth", "print-access-token"), stderr=subprocess.STDOUT
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700149 )
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700150
151
arithmetic1728772dac62020-03-27 14:34:13 -0700152@mock.patch("subprocess.check_output", autospec=True)
153def test_get_auth_access_token_with_account(check_output):
154 check_output.return_value = b"access_token\n"
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700155
arithmetic1728772dac62020-03-27 14:34:13 -0700156 token = _cloud_sdk.get_auth_access_token(account="account")
157 assert token == "access_token"
158 check_output.assert_called_with(
159 ("gcloud", "auth", "print-access-token", "--account=account"),
160 stderr=subprocess.STDOUT,
161 )
162
163
164@mock.patch("subprocess.check_output", autospec=True)
165def test_get_auth_access_token_with_exception(check_output):
166 check_output.side_effect = OSError()
167
168 with pytest.raises(exceptions.UserAccessTokenError):
169 _cloud_sdk.get_auth_access_token(account="account")