C.J. Collier | 37141e4 | 2020-02-13 13:49:49 -0800 | [diff] [blame] | 1 | # Copyright 2016 Google LLC |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 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 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 28 | DATA_DIR = os.path.join(os.path.dirname(__file__), "data") |
| 29 | AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, "authorized_user.json") |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 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 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 34 | SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, "service_account.json") |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 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 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 39 | with io.open(os.path.join(DATA_DIR, "cloud_sdk_config.json"), "rb") as fh: |
Jon Wayne Parrott | 0c09c73 | 2017-03-24 12:10:44 -0700 | [diff] [blame] | 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 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 43 | @pytest.mark.parametrize( |
| 44 | "data, expected_project_id", |
| 45 | [ |
| 46 | (CLOUD_SDK_CONFIG_FILE_DATA, "example-project"), |
| 47 | (b"I am some bad json", None), |
| 48 | (b"{}", None), |
| 49 | ], |
| 50 | ) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 51 | def test_get_project_id(data, expected_project_id): |
| 52 | check_output_patch = mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 53 | "subprocess.check_output", autospec=True, return_value=data |
| 54 | ) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 55 | |
| 56 | with check_output_patch as check_output: |
| 57 | project_id = _cloud_sdk.get_project_id() |
| 58 | |
| 59 | assert project_id == expected_project_id |
| 60 | assert check_output.called |
Jon Wayne Parrott | 0c09c73 | 2017-03-24 12:10:44 -0700 | [diff] [blame] | 61 | |
| 62 | |
| 63 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 64 | "subprocess.check_output", |
| 65 | autospec=True, |
| 66 | side_effect=subprocess.CalledProcessError(-1, None), |
| 67 | ) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 68 | def test_get_project_id_call_error(check_output): |
Jon Wayne Parrott | 0c09c73 | 2017-03-24 12:10:44 -0700 | [diff] [blame] | 69 | project_id = _cloud_sdk.get_project_id() |
| 70 | assert project_id is None |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 71 | assert check_output.called |
Jon Wayne Parrott | 0c09c73 | 2017-03-24 12:10:44 -0700 | [diff] [blame] | 72 | |
| 73 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 74 | @mock.patch("os.name", new="nt") |
PicardParis | 4921d44 | 2017-08-01 19:05:41 +0200 | [diff] [blame] | 75 | def test_get_project_id_windows(): |
| 76 | check_output_patch = mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 77 | "subprocess.check_output", |
| 78 | autospec=True, |
| 79 | return_value=CLOUD_SDK_CONFIG_FILE_DATA, |
| 80 | ) |
PicardParis | 4921d44 | 2017-08-01 19:05:41 +0200 | [diff] [blame] | 81 | |
| 82 | with check_output_patch as check_output: |
| 83 | project_id = _cloud_sdk.get_project_id() |
| 84 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 85 | assert project_id == "example-project" |
PicardParis | 4921d44 | 2017-08-01 19:05:41 +0200 | [diff] [blame] | 86 | assert check_output.called |
| 87 | # Make sure the executable is `gcloud.cmd`. |
| 88 | args = check_output.call_args[0] |
| 89 | command = args[0] |
| 90 | executable = command[0] |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 91 | assert executable == "gcloud.cmd" |
PicardParis | 4921d44 | 2017-08-01 19:05:41 +0200 | [diff] [blame] | 92 | |
| 93 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 94 | @mock.patch("google.auth._cloud_sdk.get_config_path", autospec=True) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 95 | def test_get_application_default_credentials_path(get_config_dir): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 96 | config_path = "config_path" |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 97 | get_config_dir.return_value = config_path |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 98 | credentials_path = _cloud_sdk.get_application_default_credentials_path() |
| 99 | assert credentials_path == os.path.join( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 100 | config_path, _cloud_sdk._CREDENTIALS_FILENAME |
| 101 | ) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 102 | |
| 103 | |
| 104 | def test_get_config_path_env_var(monkeypatch): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 105 | config_path_sentinel = "config_path" |
| 106 | monkeypatch.setenv(environment_vars.CLOUD_SDK_CONFIG_DIR, config_path_sentinel) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 107 | config_path = _cloud_sdk.get_config_path() |
| 108 | assert config_path == config_path_sentinel |
| 109 | |
| 110 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 111 | @mock.patch("os.path.expanduser") |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 112 | def test_get_config_path_unix(expanduser): |
| 113 | expanduser.side_effect = lambda path: path |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 114 | |
| 115 | config_path = _cloud_sdk.get_config_path() |
| 116 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 117 | assert os.path.split(config_path) == ("~/.config", _cloud_sdk._CONFIG_DIRECTORY) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 118 | |
| 119 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 120 | @mock.patch("os.name", new="nt") |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 121 | def test_get_config_path_windows(monkeypatch): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 122 | appdata = "appdata" |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 123 | monkeypatch.setenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, appdata) |
| 124 | |
| 125 | config_path = _cloud_sdk.get_config_path() |
| 126 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 127 | assert os.path.split(config_path) == (appdata, _cloud_sdk._CONFIG_DIRECTORY) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 128 | |
| 129 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 130 | @mock.patch("os.name", new="nt") |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 131 | def test_get_config_path_no_appdata(monkeypatch): |
| 132 | monkeypatch.delenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, raising=False) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 133 | monkeypatch.setenv("SystemDrive", "G:") |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 134 | |
| 135 | config_path = _cloud_sdk.get_config_path() |
| 136 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 137 | assert os.path.split(config_path) == ("G:/\\", _cloud_sdk._CONFIG_DIRECTORY) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 138 | |
| 139 | |
| 140 | def test_load_authorized_user_credentials(): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 141 | credentials = _cloud_sdk.load_authorized_user_credentials(AUTHORIZED_USER_FILE_DATA) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 142 | |
| 143 | assert isinstance(credentials, google.oauth2.credentials.Credentials) |
| 144 | |
| 145 | assert credentials.token is None |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 146 | assert credentials._refresh_token == AUTHORIZED_USER_FILE_DATA["refresh_token"] |
| 147 | assert credentials._client_id == AUTHORIZED_USER_FILE_DATA["client_id"] |
| 148 | assert credentials._client_secret == AUTHORIZED_USER_FILE_DATA["client_secret"] |
| 149 | assert ( |
| 150 | credentials._token_uri |
| 151 | == google.oauth2.credentials._GOOGLE_OAUTH2_TOKEN_ENDPOINT |
| 152 | ) |
Jon Wayne Parrott | aadb3de | 2016-10-19 09:34:05 -0700 | [diff] [blame] | 153 | |
| 154 | |
| 155 | def test_load_authorized_user_credentials_bad_format(): |
| 156 | with pytest.raises(ValueError) as excinfo: |
| 157 | _cloud_sdk.load_authorized_user_credentials({}) |
| 158 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 159 | assert excinfo.match(r"missing fields") |