blob: 31cb6c22c4279efa08bdd3eab6bc2d0f56d281f8 [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
arithmetic17283f2f3ea2021-02-19 10:26:48 -080074def test__run_subprocess_ignore_stderr():
75 command = [
76 "python",
77 "-c",
78 "from __future__ import print_function;"
79 + "import sys;"
80 + "print('error', file=sys.stderr);"
81 + "print('output', file=sys.stdout)",
82 ]
83
84 # If we ignore stderr, then the output only has stdout
85 output = _cloud_sdk._run_subprocess_ignore_stderr(command)
86 assert output == b"output\n"
87
88 # If we pipe stderr to stdout, then the output is mixed with stdout and stderr.
89 output = subprocess.check_output(command, stderr=subprocess.STDOUT)
90 assert output == b"output\nerror\n" or output == b"error\noutput\n"
91
92
Bu Sun Kim9eec0912019-10-21 17:04:21 -070093@mock.patch("os.name", new="nt")
PicardParis4921d442017-08-01 19:05:41 +020094def test_get_project_id_windows():
95 check_output_patch = mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -070096 "subprocess.check_output",
97 autospec=True,
98 return_value=CLOUD_SDK_CONFIG_FILE_DATA,
99 )
PicardParis4921d442017-08-01 19:05:41 +0200100
101 with check_output_patch as check_output:
102 project_id = _cloud_sdk.get_project_id()
103
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700104 assert project_id == "example-project"
PicardParis4921d442017-08-01 19:05:41 +0200105 assert check_output.called
106 # Make sure the executable is `gcloud.cmd`.
107 args = check_output.call_args[0]
108 command = args[0]
109 executable = command[0]
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700110 assert executable == "gcloud.cmd"
PicardParis4921d442017-08-01 19:05:41 +0200111
112
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700113@mock.patch("google.auth._cloud_sdk.get_config_path", autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700114def test_get_application_default_credentials_path(get_config_dir):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700115 config_path = "config_path"
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700116 get_config_dir.return_value = config_path
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700117 credentials_path = _cloud_sdk.get_application_default_credentials_path()
118 assert credentials_path == os.path.join(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700119 config_path, _cloud_sdk._CREDENTIALS_FILENAME
120 )
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700121
122
123def test_get_config_path_env_var(monkeypatch):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700124 config_path_sentinel = "config_path"
125 monkeypatch.setenv(environment_vars.CLOUD_SDK_CONFIG_DIR, config_path_sentinel)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700126 config_path = _cloud_sdk.get_config_path()
127 assert config_path == config_path_sentinel
128
129
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700130@mock.patch("os.path.expanduser")
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700131def test_get_config_path_unix(expanduser):
132 expanduser.side_effect = lambda path: path
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700133
134 config_path = _cloud_sdk.get_config_path()
135
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700136 assert os.path.split(config_path) == ("~/.config", _cloud_sdk._CONFIG_DIRECTORY)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700137
138
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700139@mock.patch("os.name", new="nt")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700140def test_get_config_path_windows(monkeypatch):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700141 appdata = "appdata"
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700142 monkeypatch.setenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, appdata)
143
144 config_path = _cloud_sdk.get_config_path()
145
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700146 assert os.path.split(config_path) == (appdata, _cloud_sdk._CONFIG_DIRECTORY)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700147
148
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700149@mock.patch("os.name", new="nt")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700150def test_get_config_path_no_appdata(monkeypatch):
151 monkeypatch.delenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, raising=False)
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700152 monkeypatch.setenv("SystemDrive", "G:")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700153
154 config_path = _cloud_sdk.get_config_path()
155
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700156 assert os.path.split(config_path) == ("G:/\\", _cloud_sdk._CONFIG_DIRECTORY)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700157
158
arithmetic1728772dac62020-03-27 14:34:13 -0700159@mock.patch("os.name", new="nt")
160@mock.patch("subprocess.check_output", autospec=True)
161def test_get_auth_access_token_windows(check_output):
162 check_output.return_value = b"access_token\n"
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700163
arithmetic1728772dac62020-03-27 14:34:13 -0700164 token = _cloud_sdk.get_auth_access_token()
165 assert token == "access_token"
166 check_output.assert_called_with(
167 ("gcloud.cmd", "auth", "print-access-token"), stderr=subprocess.STDOUT
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700168 )
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700169
170
arithmetic1728772dac62020-03-27 14:34:13 -0700171@mock.patch("subprocess.check_output", autospec=True)
172def test_get_auth_access_token_with_account(check_output):
173 check_output.return_value = b"access_token\n"
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700174
arithmetic1728772dac62020-03-27 14:34:13 -0700175 token = _cloud_sdk.get_auth_access_token(account="account")
176 assert token == "access_token"
177 check_output.assert_called_with(
178 ("gcloud", "auth", "print-access-token", "--account=account"),
179 stderr=subprocess.STDOUT,
180 )
181
182
183@mock.patch("subprocess.check_output", autospec=True)
184def test_get_auth_access_token_with_exception(check_output):
185 check_output.side_effect = OSError()
186
187 with pytest.raises(exceptions.UserAccessTokenError):
188 _cloud_sdk.get_auth_access_token(account="account")