blob: 3c87b35ebeefe3e732be0398466aaab4e7b2bdbb [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
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
arithmetic1728f30b45a2020-06-17 23:36:04 -070040AUTHORIZED_USER_CLOUD_SDK_WITH_QUOTA_PROJECT_ID_FILE = os.path.join(
41 DATA_DIR, "authorized_user_cloud_sdk_with_quota_project_id.json"
42)
43
Bu Sun Kim9eec0912019-10-21 17:04:21 -070044SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, "service_account.json")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070045
Bu Sun Kim15d5fa92020-06-18 14:05:40 -070046CLIENT_SECRETS_FILE = os.path.join(DATA_DIR, "client_secrets.json")
47
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070048with open(SERVICE_ACCOUNT_FILE) as fh:
49 SERVICE_ACCOUNT_FILE_DATA = json.load(fh)
50
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070051LOAD_FILE_PATCH = mock.patch(
Bu Sun Kim15d5fa92020-06-18 14:05:40 -070052 "google.auth._default.load_credentials_from_file",
Bu Sun Kim9eec0912019-10-21 17:04:21 -070053 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
54 autospec=True,
55)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070056
57
Bu Sun Kim15d5fa92020-06-18 14:05:40 -070058def test_load_credentials_from_missing_file():
weitaiting6e86c932017-08-12 03:26:59 +080059 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
Bu Sun Kim15d5fa92020-06-18 14:05:40 -070060 _default.load_credentials_from_file("")
weitaiting6e86c932017-08-12 03:26:59 +080061
Bu Sun Kim9eec0912019-10-21 17:04:21 -070062 assert excinfo.match(r"not found")
weitaiting6e86c932017-08-12 03:26:59 +080063
64
Bu Sun Kim15d5fa92020-06-18 14:05:40 -070065def test_load_credentials_from_file_invalid_json(tmpdir):
Bu Sun Kim9eec0912019-10-21 17:04:21 -070066 jsonfile = tmpdir.join("invalid.json")
67 jsonfile.write("{")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070068
69 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
Bu Sun Kim15d5fa92020-06-18 14:05:40 -070070 _default.load_credentials_from_file(str(jsonfile))
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070071
Bu Sun Kim9eec0912019-10-21 17:04:21 -070072 assert excinfo.match(r"not a valid json file")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070073
74
Bu Sun Kim15d5fa92020-06-18 14:05:40 -070075def test_load_credentials_from_file_invalid_type(tmpdir):
Bu Sun Kim9eec0912019-10-21 17:04:21 -070076 jsonfile = tmpdir.join("invalid.json")
77 jsonfile.write(json.dumps({"type": "not-a-real-type"}))
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070078
79 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
Bu Sun Kim15d5fa92020-06-18 14:05:40 -070080 _default.load_credentials_from_file(str(jsonfile))
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070081
Bu Sun Kim9eec0912019-10-21 17:04:21 -070082 assert excinfo.match(r"does not have a valid type")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070083
84
Bu Sun Kim15d5fa92020-06-18 14:05:40 -070085def test_load_credentials_from_file_authorized_user():
86 credentials, project_id = _default.load_credentials_from_file(AUTHORIZED_USER_FILE)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070087 assert isinstance(credentials, google.oauth2.credentials.Credentials)
88 assert project_id is None
89
90
Bu Sun Kim15d5fa92020-06-18 14:05:40 -070091def test_load_credentials_from_file_no_type(tmpdir):
92 # use the client_secrets.json, which is valid json but not a
93 # loadable credentials type
94 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
95 _default.load_credentials_from_file(CLIENT_SECRETS_FILE)
96
97 assert excinfo.match(r"does not have a valid type")
98 assert excinfo.match(r"Type is None")
99
100
101def test_load_credentials_from_file_authorized_user_bad_format(tmpdir):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700102 filename = tmpdir.join("authorized_user_bad.json")
103 filename.write(json.dumps({"type": "authorized_user"}))
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700104
105 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
Bu Sun Kim15d5fa92020-06-18 14:05:40 -0700106 _default.load_credentials_from_file(str(filename))
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700107
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700108 assert excinfo.match(r"Failed to load authorized user")
109 assert excinfo.match(r"missing fields")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700110
111
Bu Sun Kim15d5fa92020-06-18 14:05:40 -0700112def test_load_credentials_from_file_authorized_user_cloud_sdk():
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700113 with pytest.warns(UserWarning, match="Cloud SDK"):
Bu Sun Kim15d5fa92020-06-18 14:05:40 -0700114 credentials, project_id = _default.load_credentials_from_file(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700115 AUTHORIZED_USER_CLOUD_SDK_FILE
116 )
Thea Flowersa8d93482018-05-31 14:52:06 -0700117 assert isinstance(credentials, google.oauth2.credentials.Credentials)
118 assert project_id is None
119
arithmetic1728f30b45a2020-06-17 23:36:04 -0700120 # No warning if the json file has quota project id.
Bu Sun Kim15d5fa92020-06-18 14:05:40 -0700121 credentials, project_id = _default.load_credentials_from_file(
arithmetic1728f30b45a2020-06-17 23:36:04 -0700122 AUTHORIZED_USER_CLOUD_SDK_WITH_QUOTA_PROJECT_ID_FILE
123 )
124 assert isinstance(credentials, google.oauth2.credentials.Credentials)
125 assert project_id is None
126
Thea Flowersa8d93482018-05-31 14:52:06 -0700127
Bu Sun Kim15d5fa92020-06-18 14:05:40 -0700128def test_load_credentials_from_file_authorized_user_cloud_sdk_with_scopes():
129 with pytest.warns(UserWarning, match="Cloud SDK"):
130 credentials, project_id = _default.load_credentials_from_file(
131 AUTHORIZED_USER_CLOUD_SDK_FILE,
132 scopes=["https://www.google.com/calendar/feeds"],
133 )
134 assert isinstance(credentials, google.oauth2.credentials.Credentials)
135 assert project_id is None
136 assert credentials.scopes == ["https://www.google.com/calendar/feeds"]
137
138
139def test_load_credentials_from_file_service_account():
140 credentials, project_id = _default.load_credentials_from_file(SERVICE_ACCOUNT_FILE)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700141 assert isinstance(credentials, service_account.Credentials)
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700142 assert project_id == SERVICE_ACCOUNT_FILE_DATA["project_id"]
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700143
144
Bu Sun Kim15d5fa92020-06-18 14:05:40 -0700145def test_load_credentials_from_file_service_account_with_scopes():
146 credentials, project_id = _default.load_credentials_from_file(
147 SERVICE_ACCOUNT_FILE, scopes=["https://www.google.com/calendar/feeds"]
148 )
149 assert isinstance(credentials, service_account.Credentials)
150 assert project_id == SERVICE_ACCOUNT_FILE_DATA["project_id"]
151 assert credentials.scopes == ["https://www.google.com/calendar/feeds"]
152
153
154def test_load_credentials_from_file_service_account_bad_format(tmpdir):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700155 filename = tmpdir.join("serivce_account_bad.json")
156 filename.write(json.dumps({"type": "service_account"}))
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700157
158 with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
Bu Sun Kim15d5fa92020-06-18 14:05:40 -0700159 _default.load_credentials_from_file(str(filename))
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700160
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700161 assert excinfo.match(r"Failed to load service account")
162 assert excinfo.match(r"missing fields")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700163
164
165@mock.patch.dict(os.environ, {}, clear=True)
166def test__get_explicit_environ_credentials_no_env():
167 assert _default._get_explicit_environ_credentials() == (None, None)
168
169
170@LOAD_FILE_PATCH
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700171def test__get_explicit_environ_credentials(load, monkeypatch):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700172 monkeypatch.setenv(environment_vars.CREDENTIALS, "filename")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700173
174 credentials, project_id = _default._get_explicit_environ_credentials()
175
176 assert credentials is mock.sentinel.credentials
177 assert project_id is mock.sentinel.project_id
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700178 load.assert_called_with("filename")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700179
180
181@LOAD_FILE_PATCH
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700182def test__get_explicit_environ_credentials_no_project_id(load, monkeypatch):
183 load.return_value = mock.sentinel.credentials, None
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700184 monkeypatch.setenv(environment_vars.CREDENTIALS, "filename")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700185
186 credentials, project_id = _default._get_explicit_environ_credentials()
187
188 assert credentials is mock.sentinel.credentials
189 assert project_id is None
190
191
192@LOAD_FILE_PATCH
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800193@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700194 "google.auth._cloud_sdk.get_application_default_credentials_path", autospec=True
195)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700196def test__get_gcloud_sdk_credentials(get_adc_path, load):
197 get_adc_path.return_value = SERVICE_ACCOUNT_FILE
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700198
199 credentials, project_id = _default._get_gcloud_sdk_credentials()
200
201 assert credentials is mock.sentinel.credentials
202 assert project_id is mock.sentinel.project_id
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700203 load.assert_called_with(SERVICE_ACCOUNT_FILE)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700204
205
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800206@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700207 "google.auth._cloud_sdk.get_application_default_credentials_path", autospec=True
208)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700209def test__get_gcloud_sdk_credentials_non_existent(get_adc_path, tmpdir):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700210 non_existent = tmpdir.join("non-existent")
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700211 get_adc_path.return_value = str(non_existent)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700212
213 credentials, project_id = _default._get_gcloud_sdk_credentials()
214
215 assert credentials is None
216 assert project_id is None
217
218
219@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700220 "google.auth._cloud_sdk.get_project_id",
221 return_value=mock.sentinel.project_id,
222 autospec=True,
223)
224@mock.patch("os.path.isfile", return_value=True, autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700225@LOAD_FILE_PATCH
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700226def test__get_gcloud_sdk_credentials_project_id(load, unused_isfile, get_project_id):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700227 # Don't return a project ID from load file, make the function check
228 # the Cloud SDK project.
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700229 load.return_value = mock.sentinel.credentials, None
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700230
231 credentials, project_id = _default._get_gcloud_sdk_credentials()
232
233 assert credentials == mock.sentinel.credentials
234 assert project_id == mock.sentinel.project_id
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700235 assert get_project_id.called
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700236
237
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700238@mock.patch("google.auth._cloud_sdk.get_project_id", return_value=None, autospec=True)
239@mock.patch("os.path.isfile", return_value=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700240@LOAD_FILE_PATCH
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700241def test__get_gcloud_sdk_credentials_no_project_id(load, unused_isfile, get_project_id):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700242 # Don't return a project ID from load file, make the function check
243 # the Cloud SDK project.
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700244 load.return_value = mock.sentinel.credentials, None
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700245
246 credentials, project_id = _default._get_gcloud_sdk_credentials()
247
248 assert credentials == mock.sentinel.credentials
249 assert project_id is None
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700250 assert get_project_id.called
251
252
253class _AppIdentityModule(object):
254 """The interface of the App Idenity app engine module.
255 See https://cloud.google.com/appengine/docs/standard/python/refdocs\
256 /google.appengine.api.app_identity.app_identity
257 """
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700258
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700259 def get_application_id(self):
260 raise NotImplementedError()
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700261
262
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700263@pytest.fixture
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700264def app_identity(monkeypatch):
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700265 """Mocks the app_identity module for google.auth.app_engine."""
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700266 app_identity_module = mock.create_autospec(_AppIdentityModule, instance=True)
267 monkeypatch.setattr(app_engine, "app_identity", app_identity_module)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700268 yield app_identity_module
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700269
270
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700271def test__get_gae_credentials(app_identity):
272 app_identity.get_application_id.return_value = mock.sentinel.project
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700273
274 credentials, project_id = _default._get_gae_credentials()
275
276 assert isinstance(credentials, app_engine.Credentials)
277 assert project_id == mock.sentinel.project
278
279
James Wilson6e0781b2018-12-20 20:38:52 -0500280def test__get_gae_credentials_no_app_engine():
281 import sys
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700282
283 with mock.patch.dict("sys.modules"):
284 sys.modules["google.auth.app_engine"] = None
James Wilson6e0781b2018-12-20 20:38:52 -0500285 credentials, project_id = _default._get_gae_credentials()
286 assert credentials is None
287 assert project_id is None
288
289
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -0700290def test__get_gae_credentials_no_apis():
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700291 assert _default._get_gae_credentials() == (None, None)
292
293
294@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700295 "google.auth.compute_engine._metadata.ping", return_value=True, autospec=True
296)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700297@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700298 "google.auth.compute_engine._metadata.get_project_id",
299 return_value="example-project",
300 autospec=True,
301)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700302def test__get_gce_credentials(unused_get, unused_ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700303 credentials, project_id = _default._get_gce_credentials()
304
305 assert isinstance(credentials, compute_engine.Credentials)
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700306 assert project_id == "example-project"
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700307
308
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800309@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700310 "google.auth.compute_engine._metadata.ping", return_value=False, autospec=True
311)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700312def test__get_gce_credentials_no_ping(unused_ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700313 credentials, project_id = _default._get_gce_credentials()
314
315 assert credentials is None
316 assert project_id is None
317
318
319@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700320 "google.auth.compute_engine._metadata.ping", return_value=True, autospec=True
321)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700322@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700323 "google.auth.compute_engine._metadata.get_project_id",
324 side_effect=exceptions.TransportError(),
325 autospec=True,
326)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700327def test__get_gce_credentials_no_project_id(unused_get, unused_ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700328 credentials, project_id = _default._get_gce_credentials()
329
330 assert isinstance(credentials, compute_engine.Credentials)
331 assert project_id is None
332
333
James Wilson6e0781b2018-12-20 20:38:52 -0500334def test__get_gce_credentials_no_compute_engine():
335 import sys
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700336
337 with mock.patch.dict("sys.modules"):
338 sys.modules["google.auth.compute_engine"] = None
James Wilson6e0781b2018-12-20 20:38:52 -0500339 credentials, project_id = _default._get_gce_credentials()
340 assert credentials is None
341 assert project_id is None
342
343
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800344@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700345 "google.auth.compute_engine._metadata.ping", return_value=False, autospec=True
346)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700347def test__get_gce_credentials_explicit_request(ping):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700348 _default._get_gce_credentials(mock.sentinel.request)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700349 ping.assert_called_with(request=mock.sentinel.request)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700350
351
352@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700353 "google.auth._default._get_explicit_environ_credentials",
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800354 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700355 autospec=True,
356)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700357def test_default_early_out(unused_get):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700358 assert _default.default() == (mock.sentinel.credentials, mock.sentinel.project_id)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700359
360
361@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700362 "google.auth._default._get_explicit_environ_credentials",
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800363 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700364 autospec=True,
365)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700366def test_default_explict_project_id(unused_get, monkeypatch):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700367 monkeypatch.setenv(environment_vars.PROJECT, "explicit-env")
368 assert _default.default() == (mock.sentinel.credentials, "explicit-env")
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700369
370
371@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700372 "google.auth._default._get_explicit_environ_credentials",
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800373 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700374 autospec=True,
375)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700376def test_default_explict_legacy_project_id(unused_get, monkeypatch):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700377 monkeypatch.setenv(environment_vars.LEGACY_PROJECT, "explicit-env")
378 assert _default.default() == (mock.sentinel.credentials, "explicit-env")
Jon Wayne Parrottce37cba2016-11-07 16:41:42 -0800379
380
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700381@mock.patch("logging.Logger.warning", autospec=True)
Jon Wayne Parrottce37cba2016-11-07 16:41:42 -0800382@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700383 "google.auth._default._get_explicit_environ_credentials",
384 return_value=(mock.sentinel.credentials, None),
385 autospec=True,
386)
Jacob Hayes15af07b2017-12-13 14:09:47 -0600387@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700388 "google.auth._default._get_gcloud_sdk_credentials",
389 return_value=(mock.sentinel.credentials, None),
390 autospec=True,
391)
Jacob Hayes15af07b2017-12-13 14:09:47 -0600392@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700393 "google.auth._default._get_gae_credentials",
394 return_value=(mock.sentinel.credentials, None),
395 autospec=True,
396)
Jacob Hayes15af07b2017-12-13 14:09:47 -0600397@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700398 "google.auth._default._get_gce_credentials",
399 return_value=(mock.sentinel.credentials, None),
400 autospec=True,
401)
Jacob Hayes15af07b2017-12-13 14:09:47 -0600402def test_default_without_project_id(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700403 unused_gce, unused_gae, unused_sdk, unused_explicit, logger_warning
404):
405 assert _default.default() == (mock.sentinel.credentials, None)
Jacob Hayes15af07b2017-12-13 14:09:47 -0600406 logger_warning.assert_called_with(mock.ANY, mock.ANY, mock.ANY)
407
408
409@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700410 "google.auth._default._get_explicit_environ_credentials",
411 return_value=(None, None),
412 autospec=True,
413)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700414@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700415 "google.auth._default._get_gcloud_sdk_credentials",
416 return_value=(None, None),
417 autospec=True,
418)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700419@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700420 "google.auth._default._get_gae_credentials",
421 return_value=(None, None),
422 autospec=True,
423)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700424@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700425 "google.auth._default._get_gce_credentials",
426 return_value=(None, None),
427 autospec=True,
428)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700429def test_default_fail(unused_gce, unused_gae, unused_sdk, unused_explicit):
430 with pytest.raises(exceptions.DefaultCredentialsError):
431 assert _default.default()
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800432
433
434@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700435 "google.auth._default._get_explicit_environ_credentials",
Jon Wayne Parrott8784b232016-11-10 12:53:55 -0800436 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700437 autospec=True,
438)
439@mock.patch("google.auth.credentials.with_scopes_if_required", autospec=True)
Jacob Hayes15af07b2017-12-13 14:09:47 -0600440def test_default_scoped(with_scopes, unused_get):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700441 scopes = ["one", "two"]
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800442
443 credentials, project_id = _default.default(scopes=scopes)
444
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700445 assert credentials == with_scopes.return_value
Jon Wayne Parrott8a7e5062016-11-07 16:45:17 -0800446 assert project_id == mock.sentinel.project_id
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700447 with_scopes.assert_called_once_with(mock.sentinel.credentials, scopes)
James Wilson6e0781b2018-12-20 20:38:52 -0500448
449
450@mock.patch(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700451 "google.auth._default._get_explicit_environ_credentials",
James Wilson6e0781b2018-12-20 20:38:52 -0500452 return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700453 autospec=True,
454)
James Wilson6e0781b2018-12-20 20:38:52 -0500455def test_default_no_app_engine_compute_engine_module(unused_get):
456 """
457 google.auth.compute_engine and google.auth.app_engine are both optional
458 to allow not including them when using this package. This verifies
459 that default fails gracefully if these modules are absent
460 """
461 import sys
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700462
463 with mock.patch.dict("sys.modules"):
464 sys.modules["google.auth.compute_engine"] = None
465 sys.modules["google.auth.app_engine"] = None
James Wilson6e0781b2018-12-20 20:38:52 -0500466 assert _default.default() == (
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700467 mock.sentinel.credentials,
468 mock.sentinel.project_id,
469 )