blob: 58c7270b6b9d4efdf8e47f49c8699c8579877205 [file] [log] [blame]
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -07001# 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 Parrott0c09c732017-03-24 12:10:44 -070015import io
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070016import json
17import os
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070018import subprocess
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070019
20import mock
21import pytest
22
23from google.auth import _cloud_sdk
24from google.auth import environment_vars
25import google.oauth2.credentials
26
27
28DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
29AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, 'authorized_user.json')
30
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070031with io.open(AUTHORIZED_USER_FILE) as fh:
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070032 AUTHORIZED_USER_FILE_DATA = json.load(fh)
33
34SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, 'service_account.json')
35
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070036with io.open(SERVICE_ACCOUNT_FILE) as fh:
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070037 SERVICE_ACCOUNT_FILE_DATA = json.load(fh)
38
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070039with io.open(os.path.join(DATA_DIR, 'cloud_sdk_config.json'), 'rb') as fh:
40 CLOUD_SDK_CONFIG_FILE_DATA = fh.read()
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070041
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070042
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070043@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])
48def 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 Parrott0c09c732017-03-24 12:10:44 -070057
58
59@mock.patch(
60 'subprocess.check_output', autospec=True,
61 side_effect=subprocess.CalledProcessError(-1, None))
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070062def test_get_project_id_call_error(check_output):
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070063 project_id = _cloud_sdk.get_project_id()
64 assert project_id is None
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070065 assert check_output.called
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070066
67
PicardParis4921d442017-08-01 19:05:41 +020068@mock.patch('os.name', new='nt')
69def test_get_project_id_windows():
70 check_output_patch = mock.patch(
71 'subprocess.check_output', autospec=True,
72 return_value=CLOUD_SDK_CONFIG_FILE_DATA)
73
74 with check_output_patch as check_output:
75 project_id = _cloud_sdk.get_project_id()
76
77 assert project_id == 'example-project'
78 assert check_output.called
79 # Make sure the executable is `gcloud.cmd`.
80 args = check_output.call_args[0]
81 command = args[0]
82 executable = command[0]
83 assert executable == 'gcloud.cmd'
84
85
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070086@mock.patch(
Jon Wayne Parrott8784b232016-11-10 12:53:55 -080087 'google.auth._cloud_sdk.get_config_path', autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070088def test_get_application_default_credentials_path(get_config_dir):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070089 config_path = 'config_path'
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070090 get_config_dir.return_value = config_path
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070091 credentials_path = _cloud_sdk.get_application_default_credentials_path()
92 assert credentials_path == os.path.join(
93 config_path, _cloud_sdk._CREDENTIALS_FILENAME)
94
95
96def test_get_config_path_env_var(monkeypatch):
97 config_path_sentinel = 'config_path'
98 monkeypatch.setenv(
99 environment_vars.CLOUD_SDK_CONFIG_DIR, config_path_sentinel)
100 config_path = _cloud_sdk.get_config_path()
101 assert config_path == config_path_sentinel
102
103
104@mock.patch('os.path.expanduser')
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700105def test_get_config_path_unix(expanduser):
106 expanduser.side_effect = lambda path: path
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700107
108 config_path = _cloud_sdk.get_config_path()
109
110 assert os.path.split(config_path) == (
111 '~/.config', _cloud_sdk._CONFIG_DIRECTORY)
112
113
114@mock.patch('os.name', new='nt')
115def test_get_config_path_windows(monkeypatch):
116 appdata = 'appdata'
117 monkeypatch.setenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, appdata)
118
119 config_path = _cloud_sdk.get_config_path()
120
121 assert os.path.split(config_path) == (
122 appdata, _cloud_sdk._CONFIG_DIRECTORY)
123
124
125@mock.patch('os.name', new='nt')
126def test_get_config_path_no_appdata(monkeypatch):
127 monkeypatch.delenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, raising=False)
128 monkeypatch.setenv('SystemDrive', 'G:')
129
130 config_path = _cloud_sdk.get_config_path()
131
132 assert os.path.split(config_path) == (
133 'G:/\\', _cloud_sdk._CONFIG_DIRECTORY)
134
135
136def test_load_authorized_user_credentials():
137 credentials = _cloud_sdk.load_authorized_user_credentials(
138 AUTHORIZED_USER_FILE_DATA)
139
140 assert isinstance(credentials, google.oauth2.credentials.Credentials)
141
142 assert credentials.token is None
143 assert (credentials._refresh_token ==
144 AUTHORIZED_USER_FILE_DATA['refresh_token'])
145 assert credentials._client_id == AUTHORIZED_USER_FILE_DATA['client_id']
146 assert (credentials._client_secret ==
147 AUTHORIZED_USER_FILE_DATA['client_secret'])
Hiranya Jayathilaka23c88f72017-12-05 09:29:59 -0800148 assert (credentials._token_uri ==
149 google.oauth2.credentials._GOOGLE_OAUTH2_TOKEN_ENDPOINT)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -0700150
151
152def test_load_authorized_user_credentials_bad_format():
153 with pytest.raises(ValueError) as excinfo:
154 _cloud_sdk.load_authorized_user_credentials({})
155
156 assert excinfo.match(r'missing fields')