blob: 2c86f3ffbf564b2d517243cf217d8204a9b7ad60 [file] [log] [blame]
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -07001# Copyright 2016 Google Inc.
2#
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
15import json
16import os
17
18import mock
19import pytest
20
21from google.auth import _default
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -070022from google.auth import app_engine
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070023from google.auth import compute_engine
24from google.auth import environment_vars
25from google.auth import exceptions
26from google.oauth2 import service_account
27import google.oauth2.credentials
28
29
Bu Sun Kim9eec0912019-10-21 17:04:21 -070030DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
31AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, "authorized_user.json")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070032
33with open(AUTHORIZED_USER_FILE) as fh:
34 AUTHORIZED_USER_FILE_DATA = json.load(fh)
35
Thea Flowersa8d93482018-05-31 14:52:06 -070036AUTHORIZED_USER_CLOUD_SDK_FILE = os.path.join(
Bu Sun Kim9eec0912019-10-21 17:04:21 -070037 DATA_DIR, "authorized_user_cloud_sdk.json"
38)
Thea Flowersa8d93482018-05-31 14:52:06 -070039
Bu Sun Kim9eec0912019-10-21 17:04:21 -070040SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, "service_account.json")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070041
42with open(SERVICE_ACCOUNT_FILE) as fh:
43 SERVICE_ACCOUNT_FILE_DATA = json.load(fh)
44
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070045LOAD_FILE_PATCH = mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -070046 "google.auth._default._load_credentials_from_file",
47 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
48 autospec=True,
49)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070050
51
weitaiting6e86c932017-08-12 03:26:59 +080052def test__load_credentials_from_missing_file():
53 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
Bu Sun Kim9eec0912019-10-21 17:04:21 -070054 _default._load_credentials_from_file("")
weitaiting6e86c932017-08-12 03:26:59 +080055
Bu Sun Kim9eec0912019-10-21 17:04:21 -070056 assert excinfo.match(r"not found")
weitaiting6e86c932017-08-12 03:26:59 +080057
58
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070059def test__load_credentials_from_file_invalid_json(tmpdir):
Bu Sun Kim9eec0912019-10-21 17:04:21 -070060 jsonfile = tmpdir.join("invalid.json")
61 jsonfile.write("{")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070062
63 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
64 _default._load_credentials_from_file(str(jsonfile))
65
Bu Sun Kim9eec0912019-10-21 17:04:21 -070066 assert excinfo.match(r"not a valid json file")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070067
68
69def test__load_credentials_from_file_invalid_type(tmpdir):
Bu Sun Kim9eec0912019-10-21 17:04:21 -070070 jsonfile = tmpdir.join("invalid.json")
71 jsonfile.write(json.dumps({"type": "not-a-real-type"}))
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070072
73 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
74 _default._load_credentials_from_file(str(jsonfile))
75
Bu Sun Kim9eec0912019-10-21 17:04:21 -070076 assert excinfo.match(r"does not have a valid type")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070077
78
79def test__load_credentials_from_file_authorized_user():
Bu Sun Kim9eec0912019-10-21 17:04:21 -070080 credentials, project_id = _default._load_credentials_from_file(AUTHORIZED_USER_FILE)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070081 assert isinstance(credentials, google.oauth2.credentials.Credentials)
82 assert project_id is None
83
84
85def test__load_credentials_from_file_authorized_user_bad_format(tmpdir):
Bu Sun Kim9eec0912019-10-21 17:04:21 -070086 filename = tmpdir.join("authorized_user_bad.json")
87 filename.write(json.dumps({"type": "authorized_user"}))
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070088
89 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
90 _default._load_credentials_from_file(str(filename))
91
Bu Sun Kim9eec0912019-10-21 17:04:21 -070092 assert excinfo.match(r"Failed to load authorized user")
93 assert excinfo.match(r"missing fields")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070094
95
Thea Flowersa8d93482018-05-31 14:52:06 -070096def test__load_credentials_from_file_authorized_user_cloud_sdk():
Bu Sun Kim9eec0912019-10-21 17:04:21 -070097 with pytest.warns(UserWarning, match="Cloud SDK"):
Thea Flowersa8d93482018-05-31 14:52:06 -070098 credentials, project_id = _default._load_credentials_from_file(
Bu Sun Kim9eec0912019-10-21 17:04:21 -070099 AUTHORIZED_USER_CLOUD_SDK_FILE
100 )
Thea Flowersa8d93482018-05-31 14:52:06 -0700101 assert isinstance(credentials, google.oauth2.credentials.Credentials)
102 assert project_id is None
103
104
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700105def test__load_credentials_from_file_service_account():
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700106 credentials, project_id = _default._load_credentials_from_file(SERVICE_ACCOUNT_FILE)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700107 assert isinstance(credentials, service_account.Credentials)
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700108 assert project_id == SERVICE_ACCOUNT_FILE_DATA["project_id"]
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700109
110
111def test__load_credentials_from_file_service_account_bad_format(tmpdir):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700112 filename = tmpdir.join("serivce_account_bad.json")
113 filename.write(json.dumps({"type": "service_account"}))
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700114
115 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
116 _default._load_credentials_from_file(str(filename))
117
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700118 assert excinfo.match(r"Failed to load service account")
119 assert excinfo.match(r"missing fields")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700120
121
122@mock.patch.dict(os.environ, {}, clear=True)
123def test__get_explicit_environ_credentials_no_env():
124 assert _default._get_explicit_environ_credentials() == (None, None)
125
126
127@LOAD_FILE_PATCH
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700128def test__get_explicit_environ_credentials(load, monkeypatch):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700129 monkeypatch.setenv(environment_vars.CREDENTIALS, "filename")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700130
131 credentials, project_id = _default._get_explicit_environ_credentials()
132
133 assert credentials is mock.sentinel.credentials
134 assert project_id is mock.sentinel.project_id
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700135 load.assert_called_with("filename")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700136
137
138@LOAD_FILE_PATCH
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700139def test__get_explicit_environ_credentials_no_project_id(load, monkeypatch):
140 load.return_value = mock.sentinel.credentials, None
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700141 monkeypatch.setenv(environment_vars.CREDENTIALS, "filename")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700142
143 credentials, project_id = _default._get_explicit_environ_credentials()
144
145 assert credentials is mock.sentinel.credentials
146 assert project_id is None
147
148
149@LOAD_FILE_PATCH
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800150@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700151 "google.auth._cloud_sdk.get_application_default_credentials_path", autospec=True
152)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700153def test__get_gcloud_sdk_credentials(get_adc_path, load):
154 get_adc_path.return_value = SERVICE_ACCOUNT_FILE
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700155
156 credentials, project_id = _default._get_gcloud_sdk_credentials()
157
158 assert credentials is mock.sentinel.credentials
159 assert project_id is mock.sentinel.project_id
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700160 load.assert_called_with(SERVICE_ACCOUNT_FILE)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700161
162
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800163@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700164 "google.auth._cloud_sdk.get_application_default_credentials_path", autospec=True
165)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700166def test__get_gcloud_sdk_credentials_non_existent(get_adc_path, tmpdir):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700167 non_existent = tmpdir.join("non-existent")
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700168 get_adc_path.return_value = str(non_existent)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700169
170 credentials, project_id = _default._get_gcloud_sdk_credentials()
171
172 assert credentials is None
173 assert project_id is None
174
175
176@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700177 "google.auth._cloud_sdk.get_project_id",
178 return_value=mock.sentinel.project_id,
179 autospec=True,
180)
181@mock.patch("os.path.isfile", return_value=True, autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700182@LOAD_FILE_PATCH
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700183def test__get_gcloud_sdk_credentials_project_id(load, unused_isfile, get_project_id):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700184 # Don't return a project ID from load file, make the function check
185 # the Cloud SDK project.
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700186 load.return_value = mock.sentinel.credentials, None
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700187
188 credentials, project_id = _default._get_gcloud_sdk_credentials()
189
190 assert credentials == mock.sentinel.credentials
191 assert project_id == mock.sentinel.project_id
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700192 assert get_project_id.called
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700193
194
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700195@mock.patch("google.auth._cloud_sdk.get_project_id", return_value=None, autospec=True)
196@mock.patch("os.path.isfile", return_value=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700197@LOAD_FILE_PATCH
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700198def test__get_gcloud_sdk_credentials_no_project_id(load, unused_isfile, get_project_id):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700199 # Don't return a project ID from load file, make the function check
200 # the Cloud SDK project.
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700201 load.return_value = mock.sentinel.credentials, None
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700202
203 credentials, project_id = _default._get_gcloud_sdk_credentials()
204
205 assert credentials == mock.sentinel.credentials
206 assert project_id is None
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700207 assert get_project_id.called
208
209
210class _AppIdentityModule(object):
211 """The interface of the App Idenity app engine module.
212 See https://cloud.google.com/appengine/docs/standard/python/refdocs\
213 /google.appengine.api.app_identity.app_identity
214 """
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700215
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700216 def get_application_id(self):
217 raise NotImplementedError()
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700218
219
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700220@pytest.fixture
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700221def app_identity(monkeypatch):
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700222 """Mocks the app_identity module for google.auth.app_engine."""
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700223 app_identity_module = mock.create_autospec(_AppIdentityModule, instance=True)
224 monkeypatch.setattr(app_engine, "app_identity", app_identity_module)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700225 yield app_identity_module
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700226
227
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700228def test__get_gae_credentials(app_identity):
229 app_identity.get_application_id.return_value = mock.sentinel.project
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700230
231 credentials, project_id = _default._get_gae_credentials()
232
233 assert isinstance(credentials, app_engine.Credentials)
234 assert project_id == mock.sentinel.project
235
236
James Wilson6e0781b2018-12-20 20:38:52 -0500237def test__get_gae_credentials_no_app_engine():
238 import sys
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700239
240 with mock.patch.dict("sys.modules"):
241 sys.modules["google.auth.app_engine"] = None
James Wilson6e0781b2018-12-20 20:38:52 -0500242 credentials, project_id = _default._get_gae_credentials()
243 assert credentials is None
244 assert project_id is None
245
246
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700247def test__get_gae_credentials_no_apis():
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700248 assert _default._get_gae_credentials() == (None, None)
249
250
251@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700252 "google.auth.compute_engine._metadata.ping", return_value=True, autospec=True
253)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700254@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700255 "google.auth.compute_engine._metadata.get_project_id",
256 return_value="example-project",
257 autospec=True,
258)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700259def test__get_gce_credentials(unused_get, unused_ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700260 credentials, project_id = _default._get_gce_credentials()
261
262 assert isinstance(credentials, compute_engine.Credentials)
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700263 assert project_id == "example-project"
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700264
265
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800266@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700267 "google.auth.compute_engine._metadata.ping", return_value=False, autospec=True
268)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700269def test__get_gce_credentials_no_ping(unused_ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700270 credentials, project_id = _default._get_gce_credentials()
271
272 assert credentials is None
273 assert project_id is None
274
275
276@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700277 "google.auth.compute_engine._metadata.ping", return_value=True, autospec=True
278)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700279@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700280 "google.auth.compute_engine._metadata.get_project_id",
281 side_effect=exceptions.TransportError(),
282 autospec=True,
283)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700284def test__get_gce_credentials_no_project_id(unused_get, unused_ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700285 credentials, project_id = _default._get_gce_credentials()
286
287 assert isinstance(credentials, compute_engine.Credentials)
288 assert project_id is None
289
290
James Wilson6e0781b2018-12-20 20:38:52 -0500291def test__get_gce_credentials_no_compute_engine():
292 import sys
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700293
294 with mock.patch.dict("sys.modules"):
295 sys.modules["google.auth.compute_engine"] = None
James Wilson6e0781b2018-12-20 20:38:52 -0500296 credentials, project_id = _default._get_gce_credentials()
297 assert credentials is None
298 assert project_id is None
299
300
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800301@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700302 "google.auth.compute_engine._metadata.ping", return_value=False, autospec=True
303)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700304def test__get_gce_credentials_explicit_request(ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700305 _default._get_gce_credentials(mock.sentinel.request)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700306 ping.assert_called_with(request=mock.sentinel.request)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700307
308
309@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700310 "google.auth._default._get_explicit_environ_credentials",
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800311 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700312 autospec=True,
313)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700314def test_default_early_out(unused_get):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700315 assert _default.default() == (mock.sentinel.credentials, mock.sentinel.project_id)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700316
317
318@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700319 "google.auth._default._get_explicit_environ_credentials",
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800320 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700321 autospec=True,
322)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700323def test_default_explict_project_id(unused_get, monkeypatch):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700324 monkeypatch.setenv(environment_vars.PROJECT, "explicit-env")
325 assert _default.default() == (mock.sentinel.credentials, "explicit-env")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700326
327
328@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700329 "google.auth._default._get_explicit_environ_credentials",
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800330 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700331 autospec=True,
332)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700333def test_default_explict_legacy_project_id(unused_get, monkeypatch):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700334 monkeypatch.setenv(environment_vars.LEGACY_PROJECT, "explicit-env")
335 assert _default.default() == (mock.sentinel.credentials, "explicit-env")
Jon Wayne Parrottce37cba2016-11-07 16:41:42 -0800336
337
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700338@mock.patch("logging.Logger.warning", autospec=True)
Jon Wayne Parrottce37cba2016-11-07 16:41:42 -0800339@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700340 "google.auth._default._get_explicit_environ_credentials",
341 return_value=(mock.sentinel.credentials, None),
342 autospec=True,
343)
Jacob Hayes15af07b2017-12-13 14:09:47 -0600344@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700345 "google.auth._default._get_gcloud_sdk_credentials",
346 return_value=(mock.sentinel.credentials, None),
347 autospec=True,
348)
Jacob Hayes15af07b2017-12-13 14:09:47 -0600349@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700350 "google.auth._default._get_gae_credentials",
351 return_value=(mock.sentinel.credentials, None),
352 autospec=True,
353)
Jacob Hayes15af07b2017-12-13 14:09:47 -0600354@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700355 "google.auth._default._get_gce_credentials",
356 return_value=(mock.sentinel.credentials, None),
357 autospec=True,
358)
Jacob Hayes15af07b2017-12-13 14:09:47 -0600359def test_default_without_project_id(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700360 unused_gce, unused_gae, unused_sdk, unused_explicit, logger_warning
361):
362 assert _default.default() == (mock.sentinel.credentials, None)
Jacob Hayes15af07b2017-12-13 14:09:47 -0600363 logger_warning.assert_called_with(mock.ANY, mock.ANY, mock.ANY)
364
365
366@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700367 "google.auth._default._get_explicit_environ_credentials",
368 return_value=(None, None),
369 autospec=True,
370)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700371@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700372 "google.auth._default._get_gcloud_sdk_credentials",
373 return_value=(None, None),
374 autospec=True,
375)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700376@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700377 "google.auth._default._get_gae_credentials",
378 return_value=(None, None),
379 autospec=True,
380)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700381@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700382 "google.auth._default._get_gce_credentials",
383 return_value=(None, None),
384 autospec=True,
385)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700386def test_default_fail(unused_gce, unused_gae, unused_sdk, unused_explicit):
387 with pytest.raises(exceptions.DefaultCredentialsError):
388 assert _default.default()
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800389
390
391@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700392 "google.auth._default._get_explicit_environ_credentials",
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800393 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700394 autospec=True,
395)
396@mock.patch("google.auth.credentials.with_scopes_if_required", autospec=True)
Jacob Hayes15af07b2017-12-13 14:09:47 -0600397def test_default_scoped(with_scopes, unused_get):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700398 scopes = ["one", "two"]
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800399
400 credentials, project_id = _default.default(scopes=scopes)
401
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700402 assert credentials == with_scopes.return_value
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800403 assert project_id == mock.sentinel.project_id
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700404 with_scopes.assert_called_once_with(mock.sentinel.credentials, scopes)
James Wilson6e0781b2018-12-20 20:38:52 -0500405
406
407@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700408 "google.auth._default._get_explicit_environ_credentials",
James Wilson6e0781b2018-12-20 20:38:52 -0500409 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700410 autospec=True,
411)
James Wilson6e0781b2018-12-20 20:38:52 -0500412def test_default_no_app_engine_compute_engine_module(unused_get):
413 """
414 google.auth.compute_engine and google.auth.app_engine are both optional
415 to allow not including them when using this package. This verifies
416 that default fails gracefully if these modules are absent
417 """
418 import sys
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700419
420 with mock.patch.dict("sys.modules"):
421 sys.modules["google.auth.compute_engine"] = None
422 sys.modules["google.auth.app_engine"] = None
James Wilson6e0781b2018-12-20 20:38:52 -0500423 assert _default.default() == (
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700424 mock.sentinel.credentials,
425 mock.sentinel.project_id,
426 )