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