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