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 | |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame^] | 43 | @pytest.mark.parametrize('data, expected_project_id', [ |
| 44 | (CLOUD_SDK_CONFIG_FILE_DATA, 'example-project'), |
| 45 | (b'I am some bad json', None), |
| 46 | (b'{}', None) |
| 47 | ]) |
| 48 | def test_get_project_id(data, expected_project_id): |
| 49 | check_output_patch = mock.patch( |
| 50 | 'subprocess.check_output', autospec=True, return_value=data) |
| 51 | |
| 52 | with check_output_patch as check_output: |
| 53 | project_id = _cloud_sdk.get_project_id() |
| 54 | |
| 55 | assert project_id == expected_project_id |
| 56 | assert check_output.called |
Jon Wayne Parrott | 0c09c73 | 2017-03-24 12:10:44 -0700 | [diff] [blame] | 57 | |
| 58 | |
| 59 | @mock.patch( |
| 60 | 'subprocess.check_output', autospec=True, |
| 61 | side_effect=subprocess.CalledProcessError(-1, None)) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame^] | 62 | def test_get_project_id_call_error(check_output): |
Jon Wayne Parrott | 0c09c73 | 2017-03-24 12:10:44 -0700 | [diff] [blame] | 63 | project_id = _cloud_sdk.get_project_id() |
| 64 | assert project_id is None |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame^] | 65 | assert check_output.called |
Jon Wayne Parrott | 0c09c73 | 2017-03-24 12:10:44 -0700 | [diff] [blame] | 66 | |
| 67 | |
| 68 | @mock.patch( |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 69 | 'google.auth._cloud_sdk.get_config_path', autospec=True) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame^] | 70 | def test_get_application_default_credentials_path(get_config_dir): |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 71 | config_path = 'config_path' |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame^] | 72 | get_config_dir.return_value = config_path |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 73 | credentials_path = _cloud_sdk.get_application_default_credentials_path() |
| 74 | assert credentials_path == os.path.join( |
| 75 | config_path, _cloud_sdk._CREDENTIALS_FILENAME) |
| 76 | |
| 77 | |
| 78 | def test_get_config_path_env_var(monkeypatch): |
| 79 | config_path_sentinel = 'config_path' |
| 80 | monkeypatch.setenv( |
| 81 | environment_vars.CLOUD_SDK_CONFIG_DIR, config_path_sentinel) |
| 82 | config_path = _cloud_sdk.get_config_path() |
| 83 | assert config_path == config_path_sentinel |
| 84 | |
| 85 | |
| 86 | @mock.patch('os.path.expanduser') |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame^] | 87 | def test_get_config_path_unix(expanduser): |
| 88 | expanduser.side_effect = lambda path: path |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 89 | |
| 90 | config_path = _cloud_sdk.get_config_path() |
| 91 | |
| 92 | assert os.path.split(config_path) == ( |
| 93 | '~/.config', _cloud_sdk._CONFIG_DIRECTORY) |
| 94 | |
| 95 | |
| 96 | @mock.patch('os.name', new='nt') |
| 97 | def test_get_config_path_windows(monkeypatch): |
| 98 | appdata = 'appdata' |
| 99 | monkeypatch.setenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, appdata) |
| 100 | |
| 101 | config_path = _cloud_sdk.get_config_path() |
| 102 | |
| 103 | assert os.path.split(config_path) == ( |
| 104 | appdata, _cloud_sdk._CONFIG_DIRECTORY) |
| 105 | |
| 106 | |
| 107 | @mock.patch('os.name', new='nt') |
| 108 | def test_get_config_path_no_appdata(monkeypatch): |
| 109 | monkeypatch.delenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, raising=False) |
| 110 | monkeypatch.setenv('SystemDrive', 'G:') |
| 111 | |
| 112 | config_path = _cloud_sdk.get_config_path() |
| 113 | |
| 114 | assert os.path.split(config_path) == ( |
| 115 | 'G:/\\', _cloud_sdk._CONFIG_DIRECTORY) |
| 116 | |
| 117 | |
| 118 | def test_load_authorized_user_credentials(): |
| 119 | credentials = _cloud_sdk.load_authorized_user_credentials( |
| 120 | AUTHORIZED_USER_FILE_DATA) |
| 121 | |
| 122 | assert isinstance(credentials, google.oauth2.credentials.Credentials) |
| 123 | |
| 124 | assert credentials.token is None |
| 125 | assert (credentials._refresh_token == |
| 126 | AUTHORIZED_USER_FILE_DATA['refresh_token']) |
| 127 | assert credentials._client_id == AUTHORIZED_USER_FILE_DATA['client_id'] |
| 128 | assert (credentials._client_secret == |
| 129 | AUTHORIZED_USER_FILE_DATA['client_secret']) |
| 130 | assert credentials._token_uri == _cloud_sdk._GOOGLE_OAUTH2_TOKEN_ENDPOINT |
| 131 | |
| 132 | |
| 133 | def test_load_authorized_user_credentials_bad_format(): |
| 134 | with pytest.raises(ValueError) as excinfo: |
| 135 | _cloud_sdk.load_authorized_user_credentials({}) |
| 136 | |
| 137 | assert excinfo.match(r'missing fields') |