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 | |
Jon Wayne Parrott | 0c09c73 | 2017-03-24 12:10:44 -0700 | [diff] [blame^] | 15 | import io |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 16 | import json |
| 17 | import os |
Jon Wayne Parrott | 0c09c73 | 2017-03-24 12:10:44 -0700 | [diff] [blame^] | 18 | import subprocess |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 19 | |
| 20 | import mock |
| 21 | import pytest |
| 22 | |
| 23 | from google.auth import _cloud_sdk |
| 24 | from google.auth import environment_vars |
| 25 | import google.oauth2.credentials |
| 26 | |
| 27 | |
| 28 | DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') |
| 29 | AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, 'authorized_user.json') |
| 30 | |
Jon Wayne Parrott | 0c09c73 | 2017-03-24 12:10:44 -0700 | [diff] [blame^] | 31 | with io.open(AUTHORIZED_USER_FILE) as fh: |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 32 | AUTHORIZED_USER_FILE_DATA = json.load(fh) |
| 33 | |
| 34 | SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, 'service_account.json') |
| 35 | |
Jon Wayne Parrott | 0c09c73 | 2017-03-24 12:10:44 -0700 | [diff] [blame^] | 36 | with io.open(SERVICE_ACCOUNT_FILE) as fh: |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 37 | SERVICE_ACCOUNT_FILE_DATA = json.load(fh) |
| 38 | |
Jon Wayne Parrott | 0c09c73 | 2017-03-24 12:10:44 -0700 | [diff] [blame^] | 39 | with io.open(os.path.join(DATA_DIR, 'cloud_sdk_config.json'), 'rb') as fh: |
| 40 | CLOUD_SDK_CONFIG_FILE_DATA = fh.read() |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 41 | |
Jon Wayne Parrott | 0c09c73 | 2017-03-24 12:10:44 -0700 | [diff] [blame^] | 42 | |
| 43 | @mock.patch( |
| 44 | 'subprocess.check_output', autospec=True, |
| 45 | return_value=CLOUD_SDK_CONFIG_FILE_DATA) |
| 46 | def test_get_project_id(check_output_mock): |
| 47 | project_id = _cloud_sdk.get_project_id() |
| 48 | assert project_id == 'example-project' |
| 49 | |
| 50 | |
| 51 | @mock.patch( |
| 52 | 'subprocess.check_output', autospec=True, |
| 53 | side_effect=subprocess.CalledProcessError(-1, None)) |
| 54 | def test_get_project_id_call_error(check_output_mock): |
| 55 | project_id = _cloud_sdk.get_project_id() |
| 56 | assert project_id is None |
| 57 | |
| 58 | |
| 59 | @mock.patch( |
| 60 | 'subprocess.check_output', autospec=True, |
| 61 | return_value=b'I am some bad json') |
| 62 | def test_get_project_id_bad_json(check_output_mock): |
| 63 | project_id = _cloud_sdk.get_project_id() |
| 64 | assert project_id is None |
| 65 | |
| 66 | |
| 67 | @mock.patch( |
| 68 | 'subprocess.check_output', autospec=True, |
| 69 | return_value=b'{}') |
| 70 | def test_get_project_id_missing_value(check_output_mock): |
| 71 | project_id = _cloud_sdk.get_project_id() |
| 72 | assert project_id is None |
| 73 | |
| 74 | |
| 75 | @mock.patch( |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 76 | 'google.auth._cloud_sdk.get_config_path', autospec=True) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 77 | def test_get_application_default_credentials_path(mock_get_config_dir): |
| 78 | config_path = 'config_path' |
| 79 | mock_get_config_dir.return_value = config_path |
| 80 | credentials_path = _cloud_sdk.get_application_default_credentials_path() |
| 81 | assert credentials_path == os.path.join( |
| 82 | config_path, _cloud_sdk._CREDENTIALS_FILENAME) |
| 83 | |
| 84 | |
| 85 | def test_get_config_path_env_var(monkeypatch): |
| 86 | config_path_sentinel = 'config_path' |
| 87 | monkeypatch.setenv( |
| 88 | environment_vars.CLOUD_SDK_CONFIG_DIR, config_path_sentinel) |
| 89 | config_path = _cloud_sdk.get_config_path() |
| 90 | assert config_path == config_path_sentinel |
| 91 | |
| 92 | |
| 93 | @mock.patch('os.path.expanduser') |
| 94 | def test_get_config_path_unix(mock_expanduser): |
| 95 | mock_expanduser.side_effect = lambda path: path |
| 96 | |
| 97 | config_path = _cloud_sdk.get_config_path() |
| 98 | |
| 99 | assert os.path.split(config_path) == ( |
| 100 | '~/.config', _cloud_sdk._CONFIG_DIRECTORY) |
| 101 | |
| 102 | |
| 103 | @mock.patch('os.name', new='nt') |
| 104 | def test_get_config_path_windows(monkeypatch): |
| 105 | appdata = 'appdata' |
| 106 | monkeypatch.setenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, appdata) |
| 107 | |
| 108 | config_path = _cloud_sdk.get_config_path() |
| 109 | |
| 110 | assert os.path.split(config_path) == ( |
| 111 | appdata, _cloud_sdk._CONFIG_DIRECTORY) |
| 112 | |
| 113 | |
| 114 | @mock.patch('os.name', new='nt') |
| 115 | def test_get_config_path_no_appdata(monkeypatch): |
| 116 | monkeypatch.delenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, raising=False) |
| 117 | monkeypatch.setenv('SystemDrive', 'G:') |
| 118 | |
| 119 | config_path = _cloud_sdk.get_config_path() |
| 120 | |
| 121 | assert os.path.split(config_path) == ( |
| 122 | 'G:/\\', _cloud_sdk._CONFIG_DIRECTORY) |
| 123 | |
| 124 | |
| 125 | def test_load_authorized_user_credentials(): |
| 126 | credentials = _cloud_sdk.load_authorized_user_credentials( |
| 127 | AUTHORIZED_USER_FILE_DATA) |
| 128 | |
| 129 | assert isinstance(credentials, google.oauth2.credentials.Credentials) |
| 130 | |
| 131 | assert credentials.token is None |
| 132 | assert (credentials._refresh_token == |
| 133 | AUTHORIZED_USER_FILE_DATA['refresh_token']) |
| 134 | assert credentials._client_id == AUTHORIZED_USER_FILE_DATA['client_id'] |
| 135 | assert (credentials._client_secret == |
| 136 | AUTHORIZED_USER_FILE_DATA['client_secret']) |
| 137 | assert credentials._token_uri == _cloud_sdk._GOOGLE_OAUTH2_TOKEN_ENDPOINT |
| 138 | |
| 139 | |
| 140 | def test_load_authorized_user_credentials_bad_format(): |
| 141 | with pytest.raises(ValueError) as excinfo: |
| 142 | _cloud_sdk.load_authorized_user_credentials({}) |
| 143 | |
| 144 | assert excinfo.match(r'missing fields') |