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