Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 1 | # 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 | |
| 15 | import json |
| 16 | import os |
| 17 | |
| 18 | import mock |
| 19 | import pytest |
| 20 | |
| 21 | from google.auth import _default |
Jon Wayne Parrott | 2148fde | 2016-10-24 13:44:25 -0700 | [diff] [blame] | 22 | from google.auth import app_engine |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 23 | from google.auth import compute_engine |
| 24 | from google.auth import environment_vars |
| 25 | from google.auth import exceptions |
| 26 | from google.oauth2 import service_account |
| 27 | import google.oauth2.credentials |
| 28 | |
| 29 | |
| 30 | DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') |
| 31 | AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, 'authorized_user.json') |
| 32 | |
| 33 | with open(AUTHORIZED_USER_FILE) as fh: |
| 34 | AUTHORIZED_USER_FILE_DATA = json.load(fh) |
| 35 | |
Thea Flowers | a8d9348 | 2018-05-31 14:52:06 -0700 | [diff] [blame] | 36 | AUTHORIZED_USER_CLOUD_SDK_FILE = os.path.join( |
| 37 | DATA_DIR, 'authorized_user_cloud_sdk.json') |
| 38 | |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 39 | SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, 'service_account.json') |
| 40 | |
| 41 | with open(SERVICE_ACCOUNT_FILE) as fh: |
| 42 | SERVICE_ACCOUNT_FILE_DATA = json.load(fh) |
| 43 | |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 44 | LOAD_FILE_PATCH = mock.patch( |
| 45 | 'google.auth._default._load_credentials_from_file', return_value=( |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 46 | mock.sentinel.credentials, mock.sentinel.project_id), autospec=True) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 47 | |
| 48 | |
weitaiting | 6e86c93 | 2017-08-12 03:26:59 +0800 | [diff] [blame] | 49 | def test__load_credentials_from_missing_file(): |
| 50 | with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: |
| 51 | _default._load_credentials_from_file('') |
| 52 | |
| 53 | assert excinfo.match(r'not found') |
| 54 | |
| 55 | |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 56 | def test__load_credentials_from_file_invalid_json(tmpdir): |
| 57 | jsonfile = tmpdir.join('invalid.json') |
| 58 | jsonfile.write('{') |
| 59 | |
| 60 | with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: |
| 61 | _default._load_credentials_from_file(str(jsonfile)) |
| 62 | |
| 63 | assert excinfo.match(r'not a valid json file') |
| 64 | |
| 65 | |
| 66 | def test__load_credentials_from_file_invalid_type(tmpdir): |
| 67 | jsonfile = tmpdir.join('invalid.json') |
| 68 | jsonfile.write(json.dumps({'type': 'not-a-real-type'})) |
| 69 | |
| 70 | with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: |
| 71 | _default._load_credentials_from_file(str(jsonfile)) |
| 72 | |
| 73 | assert excinfo.match(r'does not have a valid type') |
| 74 | |
| 75 | |
| 76 | def test__load_credentials_from_file_authorized_user(): |
| 77 | credentials, project_id = _default._load_credentials_from_file( |
| 78 | AUTHORIZED_USER_FILE) |
| 79 | assert isinstance(credentials, google.oauth2.credentials.Credentials) |
| 80 | assert project_id is None |
| 81 | |
| 82 | |
| 83 | def test__load_credentials_from_file_authorized_user_bad_format(tmpdir): |
| 84 | filename = tmpdir.join('authorized_user_bad.json') |
| 85 | filename.write(json.dumps({'type': 'authorized_user'})) |
| 86 | |
| 87 | with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: |
| 88 | _default._load_credentials_from_file(str(filename)) |
| 89 | |
| 90 | assert excinfo.match(r'Failed to load authorized user') |
| 91 | assert excinfo.match(r'missing fields') |
| 92 | |
| 93 | |
Thea Flowers | a8d9348 | 2018-05-31 14:52:06 -0700 | [diff] [blame] | 94 | def test__load_credentials_from_file_authorized_user_cloud_sdk(): |
| 95 | with pytest.warns(UserWarning, matches='Cloud SDK'): |
| 96 | credentials, project_id = _default._load_credentials_from_file( |
| 97 | AUTHORIZED_USER_CLOUD_SDK_FILE) |
| 98 | assert isinstance(credentials, google.oauth2.credentials.Credentials) |
| 99 | assert project_id is None |
| 100 | |
| 101 | |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 102 | def test__load_credentials_from_file_service_account(): |
| 103 | credentials, project_id = _default._load_credentials_from_file( |
| 104 | SERVICE_ACCOUNT_FILE) |
| 105 | assert isinstance(credentials, service_account.Credentials) |
| 106 | assert project_id == SERVICE_ACCOUNT_FILE_DATA['project_id'] |
| 107 | |
| 108 | |
| 109 | def test__load_credentials_from_file_service_account_bad_format(tmpdir): |
| 110 | filename = tmpdir.join('serivce_account_bad.json') |
| 111 | filename.write(json.dumps({'type': 'service_account'})) |
| 112 | |
| 113 | with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: |
| 114 | _default._load_credentials_from_file(str(filename)) |
| 115 | |
| 116 | assert excinfo.match(r'Failed to load service account') |
| 117 | assert excinfo.match(r'missing fields') |
| 118 | |
| 119 | |
| 120 | @mock.patch.dict(os.environ, {}, clear=True) |
| 121 | def test__get_explicit_environ_credentials_no_env(): |
| 122 | assert _default._get_explicit_environ_credentials() == (None, None) |
| 123 | |
| 124 | |
| 125 | @LOAD_FILE_PATCH |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 126 | def test__get_explicit_environ_credentials(load, monkeypatch): |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 127 | monkeypatch.setenv(environment_vars.CREDENTIALS, 'filename') |
| 128 | |
| 129 | credentials, project_id = _default._get_explicit_environ_credentials() |
| 130 | |
| 131 | assert credentials is mock.sentinel.credentials |
| 132 | assert project_id is mock.sentinel.project_id |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 133 | load.assert_called_with('filename') |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 134 | |
| 135 | |
| 136 | @LOAD_FILE_PATCH |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 137 | def test__get_explicit_environ_credentials_no_project_id(load, monkeypatch): |
| 138 | load.return_value = mock.sentinel.credentials, None |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 139 | monkeypatch.setenv(environment_vars.CREDENTIALS, 'filename') |
| 140 | |
| 141 | credentials, project_id = _default._get_explicit_environ_credentials() |
| 142 | |
| 143 | assert credentials is mock.sentinel.credentials |
| 144 | assert project_id is None |
| 145 | |
| 146 | |
| 147 | @LOAD_FILE_PATCH |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 148 | @mock.patch( |
| 149 | 'google.auth._cloud_sdk.get_application_default_credentials_path', |
| 150 | autospec=True) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 151 | def test__get_gcloud_sdk_credentials(get_adc_path, load): |
| 152 | get_adc_path.return_value = SERVICE_ACCOUNT_FILE |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 153 | |
| 154 | credentials, project_id = _default._get_gcloud_sdk_credentials() |
| 155 | |
| 156 | assert credentials is mock.sentinel.credentials |
| 157 | assert project_id is mock.sentinel.project_id |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 158 | load.assert_called_with(SERVICE_ACCOUNT_FILE) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 159 | |
| 160 | |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 161 | @mock.patch( |
| 162 | 'google.auth._cloud_sdk.get_application_default_credentials_path', |
| 163 | autospec=True) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 164 | def test__get_gcloud_sdk_credentials_non_existent(get_adc_path, tmpdir): |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 165 | non_existent = tmpdir.join('non-existent') |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 166 | get_adc_path.return_value = str(non_existent) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 167 | |
| 168 | credentials, project_id = _default._get_gcloud_sdk_credentials() |
| 169 | |
| 170 | assert credentials is None |
| 171 | assert project_id is None |
| 172 | |
| 173 | |
| 174 | @mock.patch( |
| 175 | 'google.auth._cloud_sdk.get_project_id', |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 176 | return_value=mock.sentinel.project_id, autospec=True) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 177 | @mock.patch('os.path.isfile', return_value=True, autospec=True) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 178 | @LOAD_FILE_PATCH |
| 179 | def test__get_gcloud_sdk_credentials_project_id( |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 180 | load, unused_isfile, get_project_id): |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 181 | # Don't return a project ID from load file, make the function check |
| 182 | # the Cloud SDK project. |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 183 | load.return_value = mock.sentinel.credentials, None |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 184 | |
| 185 | credentials, project_id = _default._get_gcloud_sdk_credentials() |
| 186 | |
| 187 | assert credentials == mock.sentinel.credentials |
| 188 | assert project_id == mock.sentinel.project_id |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 189 | assert get_project_id.called |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 190 | |
| 191 | |
| 192 | @mock.patch( |
| 193 | 'google.auth._cloud_sdk.get_project_id', |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 194 | return_value=None, autospec=True) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 195 | @mock.patch('os.path.isfile', return_value=True) |
| 196 | @LOAD_FILE_PATCH |
| 197 | def test__get_gcloud_sdk_credentials_no_project_id( |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 198 | load, unused_isfile, get_project_id): |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 199 | # Don't return a project ID from load file, make the function check |
| 200 | # the Cloud SDK project. |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 201 | load.return_value = mock.sentinel.credentials, None |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 202 | |
| 203 | credentials, project_id = _default._get_gcloud_sdk_credentials() |
| 204 | |
| 205 | assert credentials == mock.sentinel.credentials |
| 206 | assert project_id is None |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 207 | assert get_project_id.called |
| 208 | |
| 209 | |
| 210 | class _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 | """ |
| 215 | def get_application_id(self): |
| 216 | raise NotImplementedError() |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 217 | |
| 218 | |
Jon Wayne Parrott | 2148fde | 2016-10-24 13:44:25 -0700 | [diff] [blame] | 219 | @pytest.fixture |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 220 | def app_identity(monkeypatch): |
Jon Wayne Parrott | 2148fde | 2016-10-24 13:44:25 -0700 | [diff] [blame] | 221 | """Mocks the app_identity module for google.auth.app_engine.""" |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 222 | app_identity_module = mock.create_autospec( |
| 223 | _AppIdentityModule, instance=True) |
Jon Wayne Parrott | 2148fde | 2016-10-24 13:44:25 -0700 | [diff] [blame] | 224 | monkeypatch.setattr( |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 225 | app_engine, 'app_identity', app_identity_module) |
| 226 | yield app_identity_module |
Jon Wayne Parrott | 2148fde | 2016-10-24 13:44:25 -0700 | [diff] [blame] | 227 | |
| 228 | |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 229 | def test__get_gae_credentials(app_identity): |
| 230 | app_identity.get_application_id.return_value = mock.sentinel.project |
Jon Wayne Parrott | 2148fde | 2016-10-24 13:44:25 -0700 | [diff] [blame] | 231 | |
| 232 | credentials, project_id = _default._get_gae_credentials() |
| 233 | |
| 234 | assert isinstance(credentials, app_engine.Credentials) |
| 235 | assert project_id == mock.sentinel.project |
| 236 | |
| 237 | |
| 238 | def test__get_gae_credentials_no_apis(): |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 239 | assert _default._get_gae_credentials() == (None, None) |
| 240 | |
| 241 | |
| 242 | @mock.patch( |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 243 | 'google.auth.compute_engine._metadata.ping', return_value=True, |
| 244 | autospec=True) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 245 | @mock.patch( |
Jon Wayne Parrott | 5b03ba1 | 2016-10-24 13:51:26 -0700 | [diff] [blame] | 246 | 'google.auth.compute_engine._metadata.get_project_id', |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 247 | return_value='example-project', autospec=True) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 248 | def test__get_gce_credentials(unused_get, unused_ping): |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 249 | credentials, project_id = _default._get_gce_credentials() |
| 250 | |
| 251 | assert isinstance(credentials, compute_engine.Credentials) |
| 252 | assert project_id == 'example-project' |
| 253 | |
| 254 | |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 255 | @mock.patch( |
| 256 | 'google.auth.compute_engine._metadata.ping', return_value=False, |
| 257 | autospec=True) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 258 | def test__get_gce_credentials_no_ping(unused_ping): |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 259 | credentials, project_id = _default._get_gce_credentials() |
| 260 | |
| 261 | assert credentials is None |
| 262 | assert project_id is None |
| 263 | |
| 264 | |
| 265 | @mock.patch( |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 266 | 'google.auth.compute_engine._metadata.ping', return_value=True, |
| 267 | autospec=True) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 268 | @mock.patch( |
Jon Wayne Parrott | 5b03ba1 | 2016-10-24 13:51:26 -0700 | [diff] [blame] | 269 | 'google.auth.compute_engine._metadata.get_project_id', |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 270 | side_effect=exceptions.TransportError(), autospec=True) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 271 | def test__get_gce_credentials_no_project_id(unused_get, unused_ping): |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 272 | credentials, project_id = _default._get_gce_credentials() |
| 273 | |
| 274 | assert isinstance(credentials, compute_engine.Credentials) |
| 275 | assert project_id is None |
| 276 | |
| 277 | |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 278 | @mock.patch( |
| 279 | 'google.auth.compute_engine._metadata.ping', return_value=False, |
| 280 | autospec=True) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 281 | def test__get_gce_credentials_explicit_request(ping): |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 282 | _default._get_gce_credentials(mock.sentinel.request) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 283 | ping.assert_called_with(request=mock.sentinel.request) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 284 | |
| 285 | |
| 286 | @mock.patch( |
| 287 | 'google.auth._default._get_explicit_environ_credentials', |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 288 | return_value=(mock.sentinel.credentials, mock.sentinel.project_id), |
| 289 | autospec=True) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 290 | def test_default_early_out(unused_get): |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 291 | assert _default.default() == ( |
| 292 | mock.sentinel.credentials, mock.sentinel.project_id) |
| 293 | |
| 294 | |
| 295 | @mock.patch( |
| 296 | 'google.auth._default._get_explicit_environ_credentials', |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 297 | return_value=(mock.sentinel.credentials, mock.sentinel.project_id), |
| 298 | autospec=True) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 299 | def test_default_explict_project_id(unused_get, monkeypatch): |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 300 | monkeypatch.setenv(environment_vars.PROJECT, 'explicit-env') |
| 301 | assert _default.default() == ( |
| 302 | mock.sentinel.credentials, 'explicit-env') |
| 303 | |
| 304 | |
| 305 | @mock.patch( |
| 306 | 'google.auth._default._get_explicit_environ_credentials', |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 307 | return_value=(mock.sentinel.credentials, mock.sentinel.project_id), |
| 308 | autospec=True) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 309 | def test_default_explict_legacy_project_id(unused_get, monkeypatch): |
Jon Wayne Parrott | ce37cba | 2016-11-07 16:41:42 -0800 | [diff] [blame] | 310 | monkeypatch.setenv(environment_vars.LEGACY_PROJECT, 'explicit-env') |
| 311 | assert _default.default() == ( |
| 312 | mock.sentinel.credentials, 'explicit-env') |
| 313 | |
| 314 | |
| 315 | @mock.patch( |
Jacob Hayes | 15af07b | 2017-12-13 14:09:47 -0600 | [diff] [blame] | 316 | 'logging.Logger.warning', |
| 317 | autospec=True) |
| 318 | @mock.patch( |
| 319 | 'google.auth._default._get_explicit_environ_credentials', |
| 320 | return_value=(mock.sentinel.credentials, None), autospec=True) |
| 321 | @mock.patch( |
| 322 | 'google.auth._default._get_gcloud_sdk_credentials', |
| 323 | return_value=(mock.sentinel.credentials, None), autospec=True) |
| 324 | @mock.patch( |
| 325 | 'google.auth._default._get_gae_credentials', |
| 326 | return_value=(mock.sentinel.credentials, None), autospec=True) |
| 327 | @mock.patch( |
| 328 | 'google.auth._default._get_gce_credentials', |
| 329 | return_value=(mock.sentinel.credentials, None), autospec=True) |
| 330 | def test_default_without_project_id( |
| 331 | unused_gce, unused_gae, unused_sdk, unused_explicit, logger_warning): |
| 332 | assert _default.default() == ( |
| 333 | mock.sentinel.credentials, None) |
| 334 | logger_warning.assert_called_with(mock.ANY, mock.ANY, mock.ANY) |
| 335 | |
| 336 | |
| 337 | @mock.patch( |
Jon Wayne Parrott | ce37cba | 2016-11-07 16:41:42 -0800 | [diff] [blame] | 338 | 'google.auth._default._get_explicit_environ_credentials', |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 339 | return_value=(None, None), autospec=True) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 340 | @mock.patch( |
| 341 | 'google.auth._default._get_gcloud_sdk_credentials', |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 342 | return_value=(None, None), autospec=True) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 343 | @mock.patch( |
| 344 | 'google.auth._default._get_gae_credentials', |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 345 | return_value=(None, None), autospec=True) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 346 | @mock.patch( |
| 347 | 'google.auth._default._get_gce_credentials', |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 348 | return_value=(None, None), autospec=True) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 349 | def test_default_fail(unused_gce, unused_gae, unused_sdk, unused_explicit): |
| 350 | with pytest.raises(exceptions.DefaultCredentialsError): |
| 351 | assert _default.default() |
Jon Wayne Parrott | 8a7e506 | 2016-11-07 16:45:17 -0800 | [diff] [blame] | 352 | |
| 353 | |
| 354 | @mock.patch( |
| 355 | 'google.auth._default._get_explicit_environ_credentials', |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 356 | return_value=(mock.sentinel.credentials, mock.sentinel.project_id), |
| 357 | autospec=True) |
Jon Wayne Parrott | 8a7e506 | 2016-11-07 16:45:17 -0800 | [diff] [blame] | 358 | @mock.patch( |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 359 | 'google.auth.credentials.with_scopes_if_required', autospec=True) |
Jacob Hayes | 15af07b | 2017-12-13 14:09:47 -0600 | [diff] [blame] | 360 | def test_default_scoped(with_scopes, unused_get): |
Jon Wayne Parrott | 8a7e506 | 2016-11-07 16:45:17 -0800 | [diff] [blame] | 361 | scopes = ['one', 'two'] |
| 362 | |
| 363 | credentials, project_id = _default.default(scopes=scopes) |
| 364 | |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 365 | assert credentials == with_scopes.return_value |
Jon Wayne Parrott | 8a7e506 | 2016-11-07 16:45:17 -0800 | [diff] [blame] | 366 | assert project_id == mock.sentinel.project_id |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 367 | with_scopes.assert_called_once_with( |
Jon Wayne Parrott | 8a7e506 | 2016-11-07 16:45:17 -0800 | [diff] [blame] | 368 | mock.sentinel.credentials, scopes) |